kubernetes系列(七) - Pod生命周期

1. pod生命周期

如下,在容器环境初始化之后:

  1. 会先启动pause根容器
  2. 然后顺序执行一系列的init c(初始化容器)【如果有的话】
  3. 然后start主容器Main C
  4. readiness检测到Main C启动完成后会把pod的状态改为running
  5. 这之后生存检测liveiness会持续监测容器的状态

如果liveiness检测到容器出现异常之后,就会出发对应的重启或者删除策略(看具体是怎么配置的了)

注:多个Main C的话就可以分别有自己的init creadiness liveiness


2. initC

2.1 initC介绍

Pod能够具有一个或多个容器,应用运行在容器里面,但是它也可能有一个或多个先于应用容器启动的Init容器

Init容器与普通的容器非常像,除了如下两点:

  • Init容器总是运行到成功完成为止
  • 每个Init容器都必须在下一个Init容器启动之前成功完成

如果Pod的Init容器失败,Kubernetes会不断地重启该Pod,直到Init容器成功为止

然而,如果Pod对应的restartPolicy设置为Never(默认为Always),它将不会重新启动。

2.2 initC的作用

因为Init容器具有与应用程序容器分离的单独镜像,所以它们的启动相关代码具有如下优势:

  1. 它们可以包含并运行实用工具,但是出于安全考虑,是不建议在应用程序容器镜像中包含这些实用工具的
  2. 它们可以包含使用工具和定制化代码来安装,但是不能出现在应用程序镜像中。例如,创建镜像没必要FROM另一个镜像,只需要在安装过程中使用类似sed、awk、python或dig这样的工具。
  3. 应用程序镜像可以分离出创建和部署的角色,而没有必要联合它们构建一个单独的镜像。
  4. Init 容器使用Linux Namespace,所以相对应用程序容器来说具有不同的文件系统视图。因此,它们能够具有访问Secret的权限,而应用程序容器则不能。
  5. 它们必须在应用程序容器启动之前运行完成,而应用程序容器是并行运行的,所以Init容器能够提供了一种简单的阻塞或延迟应用容器的启动的方法,直到满足了一组先决条件。

2.3 initC的模版

apiVersion: v1
    kind: Pod 
    metadata:
     name: myapp-pod  #pod的名称
     labels:  # pod的标签
      app: myapp 
      version: v1
    spec:
     containers: # mainC
     - name:myapp-container 
       image: busybox
       command: ['sh','-c','echo The app is running!&& sleep 3600'] 
     initContainers: # 如下有两个initC,一个确保服务启动一个取保db启动
     - name: init-myservice 
       image: busybox 
       command: ['sh','-c','until nslookup myservice;do echo waiting for myservice;sleep 2; done;'] 
     - name: init-mydb 
       image: busybox 
       command: ['sh','-c','until nslookup mydb;do echo waiting for mydb;sleep 2;done;']

如下Init:1/2的意思就是:当前pod一共有两个initc,已经成功了一个了

2.4 initC的一些其他补充

  1. 在Pod启动过程中,Init容器会按顺序在网络和数据卷初始化之后启动。每个容器必须在下一个容器启动之前成功退出。

即在pause启动之后启动,pod第一个启动的容器并不是Init而是pause,但pause仅仅只负责初始化网络和数据卷

  1. 如果由于运行时或失败退出,将导致容器启动失败,它会根据Pod的restartPolicy指定的策略进行重试。然而,如果Pod的restartPolicy设置为Always,Init容器失败时会使用RestartPolicy策略
  2. 在所有的Init容器没有成功之前,Pod将不会变成Ready状态。Init容器的端口将不会在Service中进行聚集。正在初始化中的Pod处于Pending状态,但应该会将Initializing状态设置为true
  3. 如果Pod 重启,所有Init容器必须重新执行,因此Init容器应该配置为幂等的状态。
  4. 对Init容器spec的修改被限制在容器image字段,修改其他字段都不会生效。更改Init容器的image字段,等价于重启该Pod
  5. Init 容器具有应用容器的所有字段。除了readinessProbe,因为Init容器无法定义不同于完成(completion)的就绪(readiness)之外的其他状态。这会在验证过程中强制执行
  6. 在Pod中的每个app和Init容器的名称必须唯一;与任何其它容器共享同一个名称,会在验证时抛出错误

3. Pod健康性检查(liveiness)和服务可用性检查(readiness)

kubernetes对pod的健康状态,是通过kubelet定期执行下两类探针来实现

  • ReadinessProbe: 用于判断容器服务是否可用,成功的话pod状态会显示成ready,失败的话只是不显示ready,不会触发重启策略
  • LiveinessProbe: 用于判断运行中的容器是否存活,如果探测到不健康,则kubelet会杀掉该容器,并触发重启策略。如果容器没有配置liveinessProbe,则kubelet认为该容器的liveinessProbe返回的永远是succeed

3.1 探针的三种实现方式

ReadinessProbe和LiveinessProbe均可配置以下三种实现方式。

  1. ExecAction:在容器内执行指定命令。如果命令退出时返回码为0则认为诊断成功
  2. TCPSocketAction:对指定端口上的容器的IP地址进行TCP检查。如果端口打开,则诊断被认为是成功的。
  3. HTTPGetAction:对指定的端口和路径上的容器的IP地址执行HTTPGet请求。如果响应的状态码大于等于200且小于400【2xx:成功,3xx:跳转】,则诊断被认为是成功的

每次探测都将获得以下三种结果之一:

  1. success:容器通过了诊断。
  2. failed:容器未通过诊断。
  3. unknown:诊断失败,因此不会采取任何行动【这将导致容器挂死,因为探针不执行成功的话,容器会一直在等待】

3.2 ExecAction

以下是livenessProbe的示例

apiVersion: v1 
kind: Pod 
metadata:
  name: liveness-exec-pod 
  namespace: default 
spec:
  containers: 
  - name: liveness-exec-container 
    image: hub.coreqi.cn/1ibrary/busybox 
    imagePullPolicy: IfNotPresent 
    command: ["/bin/sh","-c","touch /tmp/live;sleep 60;rm -rf /tmp/live;sleep 3600"] 
    livenessProbe:  #探活指针
      exec: #ExecAction(执行命令)
        command: ["test","-e","/tmp/live"] #检测文件是否存在
      initialDelaySeconds: 1  #容器启动1秒后执行
      periodSeconds: 3  #重试的循环时间为3秒

3.3 HTTPGetAction

以下是readinessProbe的示例

apiVersion: v1 
kind: Pod 
metadata:
  name:readiness-httpget-pod 
  namespace: default 
spec:
  containers:
  - name: readiness-httpget-container 
    image: coreqi/myapp:v1 
    imagePullPolicy: IfNotPresent #镜像下载策略,如果存在则不下载
    readinessProbe: #就绪探针
      httpGet:  #检测方案
        port: 80
        path: /index1.html 
      initialDelaySeconds: 1 #初始化检测延时(1秒以后)
      periodSeconds: 3  #重试的检测时间(3秒以后)

以下是liveinessProbe的示例

apiVersion: v1 
kind: Pod 
metadata:
  name: liveness-httpget-pod 
  namespace: default 
spec:
  containers: 
  - name: liveness-httpget-container 
    image: hub.coreqi.cn/1ibrary/myapp:v1 
    imagePu11Policy: IfNotPresent 
    ports:
    - name: http 
      containerPort: 80 
    1ivenessProbe:
      httpGet:
        port: http 
        path: /index.html 
      initialDelaySeconds: 1  #延迟1秒以后开始检测
      periodSeconds: 3  #每3秒重复检测一次
      timeoutSeconds: 10  #每次访问时最大的超时时间

3.4 TCPSocketAction

以下是liveinessProbe的示例

apiVersion: v1 
kind: Pod 
metadata:
  name: probe-tcp 
spec:
  containers:
  - name: nginx 
    image: hub.coreqi.cn/1ibrary/myapp:v1 
    livenessProbe:
      initialDelaySeconds: 5 
      timeoutSeconds: 1 
      tcpSocket:
        port: 80
      periodSeconds: 3

4. 容器启动及退出动作

对应上面的pod声明周期中的startstop

apiVersion: v1 
kind: Pod 
metadata:
  name: lifecycle-demo 
spec:
  containers:
  - name:lifecycle-demo-container 
    image: nginx 
    1ifecycle:
      poststart:  #启动时运行
        exec:
          command: ["/bin/sh","-c","echo Hello from the postStart handler> /usr/share/message"] 
      prestop:  #退出时运行
        exec: 
          command: ["/bin/sh","-c","echo Hello from the poststop handler> /usr/share/message"]

猜你喜欢

转载自www.cnblogs.com/baoshu/p/13174187.html