2020-07-31 深入理解Pod对象

2020-08-05  本文已影响0人  阿丧小威

1. Pod基本概念

2. Pod存在的意义

3. Pod实现机制

在同一个Pod中实现共享网络,使用数据卷volume实现共享存储。

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
  - name: write
    image: centos
    command: ["bash","-c","for i in {1..100};do echo $i >> /data/hello;sleep 1;done"]
    volumeMounts:
      - name: data
        mountPath: /data
  - name: read
    image: centos
    command: ["bash","-c","tail -f /data/hello"]
    volumeMounts:
      - name: data
        mountPath: /data

  volumes:    ---定义数据卷
  - name: data
    emptyDir: {}

4. 镜像拉取策略(imagePullPolicy)

默认规则

apiVersion: v1
kind: Pod
metadata:
  name: foo
  namespace: awesomeapps
spec:
  containers:
    - name: foo
      image: janedoe/awesomeapp:v1
      imagePullPolicy: IfNotPresent

根据凭据拉取镜像

apiVersion: v1
kind: Pod
metadata:
  name: foo
  namespace: awesomeapps
spec:
  containers:
    - name: foo
      image: janedoe/awesomeapp:v1
  imagePullSecrets:
    - name: registry-pull-secret

示例:使用Always方式拉取一个私有的镜像
官方链接 https://kubernetes.io/zh/docs/tasks/configure-pod-container/pull-image-private-registry/
生成secret有2种方式:
一、使用docker登录的凭证来生成

# cat registry-pull-secret.yaml
apiVersion: v1
kind: Secret
metadata:
  name: registry-pull-secret
data:
  .dockerconfigjson: eyJjY3IuY2NzLnRlbmNlbnR5dW4uY29tL3RlbmNlbnR5dW4iOnsidXNlcm5hbWUiOiIzMzIxMzM3OTk0IiwicGFzc3dvcmQiOiIxMjM0NTYuY29tIiwiZW1haWwiOiIzMzIxMzM3OTk0QHFxLmNvbSIsImF1dGgiOiJNek15TVRNek56azVORG94TWpNME5UWXVZMjl0In19
type: kubernetes.io/dockerconfigjson
# kubectl create -f registry-pull-secret.yaml    ---创建
# kubectl get secret    ---查看是否创建成功

二、使用kubectl来生成(在集群中创建保存授权令牌的secret)

5. 资源限制

Pod和Container的资源请求和限制:

apiVersion: v1
kind: Pod
metadata:
  name: frontend
spec:
  containers:
  - name: db
    image: mysql
    env:
    - name: MYSQL_ROOT_PASSWORD
      value: "password"
    resources:
      requests:
        memory: "64Mi"
        cpu: "250m"    ---也可以写成0.25
      limits:
        memory: "128Mi"
        cpu: "500m"    ---也可以写成0.5
  - name: wp
    image: wordpress
    resources:
      requests:
        memory: "64Mi"
        cpu: "250m"
      limits:
        memory: "128Mi"
        cpu: "500m"

6. Pod重启策略(restartPolicy)

apiVersion: v1
kind: Pod
metadata:
name: foo
namespace: awesomeapps
spec:
containers:
-name: foo
image: janedoe/awesomeapp:v1
restartPolicy: Always

7. Pod健康检查(Probe)

Probe有以下两种类型:

Probe支持以下三种检查方法:

探测器配置:

http探测器可以在httpGet上配置额外的字段:

vi pod-check.yaml
apiVersion: v1
kind: Pod
metadata:
  labels:
    test: liveness
  name: liveness-exec
spec:
  containers:
  - name: liveness
    image: busybox
    args:
    - /bin/sh
    - -c
    - touch /tmp/healthy; sleep 30; rm -rf /tmp/healthy; sleep 60
    livenessProbe:
      exec:
        command:
        - cat
        - /tmp/healthy
      initialDelaySeconds: 5    ---容器初始化之后多长时间做第一次健康检查
      periodSeconds: 5    ---之后多长时间做一个周期性的健康检查
    readinessProbe:
      exec:
        command:
        - cat
        - /tmp/healthy
      initialDelaySeconds: 5
      periodSeconds: 5
kubectl create -f pod-check.yaml

一般两种方法都一起配。

8. Pod调度约束

图片.png
apiVersion: v1
kind: Pod
metadata:
  name: pod-example
  labels:
    app: nginx
spec:
  nodeName: 192.168.31.65
  containers:
  - name: nginx
    image: nginx:1.15
apiVersion: v1
kind: Pod
metadata:
  name: pod-example
spec:
  nodeSelector:
    env_role: dev
  containers:
  - name: nginx
    image: nginx:1.15

使用nodeSelector需要先设置label标签:

kubectl label nodes 192.168.9.65 env_role=dev
kubectl label nodes 192.168.9.66 env_role=sit
kubectl get nodes --show-labels

9. 故障排查

Pod状态

常用查看异常命令:

上一篇 下一篇

猜你喜欢

热点阅读