K8s-03: Pod

Pod

Pod 的生命周期

  1. Pod 状态:Pending、Running、Succeeded、Failed、Unknown。

  2. 生命周期: Pause Container -> Init Container -> App Container (Start、Readiness、Liveness、Stop) -> Termination.

  3. Pod 分类

    • 自主式:退出后不会重启。
    • 控制器管理的:始终要维持 pod 的副本数。

Puase Container

  1. Pause Container 是 pod 中的第一个容器,负责为 pod 中的其他容器提供网络和存储。Pause Container 的生命周期和 pod 的生命周期一致,Pause Container 退出后,pod 也会退出。

Init Container

  1. 如果 Pod 的 init container 失败,Pod 会一直处于 Pending 状态,重复执行 init container。除非 Pod 的 restartPolicy 设置为 Never,否则 Pod 会一直重启。在 Pod 中,所有的 init container 都会在网络和存储初始化完成后才会执行。在下一个 init container 开始执行之前,必须保证上一个 init container 执行成功。

  2. Init Container 的作用:初始化 Pod 的环境,如创建配置文件、初始化数据库等。

  3. Pod 重启时,init container 会重新执行,但是 app container 不会重新执行。

  4. Init Container 的 spec 修改被限制在 image 字段,不能修改其他字段。修改 image 字段后,会重启 Pod。

  5. Init Container 具有除了 ReadinessProbe 之外的所有字段。

  6. Init Container 的 name 不可以和其他容器的 name 相同。

  7. 举例

    apiVersion: v1
    kind: Pod
    metadata:
      name: init-demo
    spec:
        containers:
        - name: nginx
            image: nginx:1.14.2
            ports:
            - containerPort: 80
            volumeMounts:
            - name: workdir
            mountPath: /usr/share/nginx/html
        initContainers:
        - name: install
            image: busybox
            command:
            - wget
            - "-O"
            - "/work-dir/index.html"
            - http://kubernetes.io
            volumeMounts:
            - name: workdir
            mountPath: "/work-dir"
        dnsPolicy: Default
        volumes:
        - name: workdir
            emptyDir: {}

Probe

  1. pod 的 probe 方式:

    • exec: 在容器中执行命令,如果命令返回 0,则认为容器健康;如果命令返回非 0,则认为容器不健康。
    • httpGet: 发送 http 请求,如果返回状态码在 200-399 之间,则认为容器健康;否则认为容器不健康。
    • tcpSocket: 发送 tcp 请求,如果连接成功,则认为容器健康;否则认为容器不健康。
  2. pod 的 probe 类型:

    • readinessProbe: 用于探测容器是否已经准备好接收流量。如果容器不健康,则不会将流量转发到容器。
    • livenessProbe: 用于探测容器是否存活。如果容器不健康,则会重启容器。
    • startupProbe: 用于探测容器是否已经启动完成。如果容器不健康,则会重启容器。
  3. pod 的 probe 字段:

    • initialDelaySeconds: 容器启动后,延迟多少秒开始探测。
    • periodSeconds: 探测的间隔时间。
    • timeoutSeconds: 探测的超时时间。
    • successThreshold: 探测成功的次数。
    • failureThreshold: 探测失败的次数。
  4. 举例

    apiVersion: v1
    kind: Pod
    metadata:
      name: liveness-exec
    spec:
        containers:
        - name: liveness
            image: k8s.gcr.io/busybox
            args:
            - /bin/sh
            - -c
            - touch /tmp/healthy; sleep 30; rm -rf /tmp/healthy; sleep 600
            livenessProbe:
            exec:
                command:
                - cat
                - /tmp/healthy
            initialDelaySeconds: 5
            periodSeconds: 5

postStart 和 preStop

  • 可以通过 postStart 和 preStop 字段,定义容器的生命周期钩子。

    apiVersion: v1
    kind: Pod
    metadata:
      name: lifecycle-demo
    spec:
        containers:
        - name: lifecycle-demo-container
            image: nginx
            lifecycle:
            postStart:
                exec:
                command: ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"]
            preStop:
                exec:
                command: ["/bin/sh","-c","nginx -s quit; while killall -0 nginx; do sleep 1; done"]