欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

pod生命周期

程序员文章站 2022-06-18 11:16:42
Pod生命周期 我们一般将pod对象从创建至终这段时间范围成为pod的生命周期,它主要包含以下的过程: pod创建过程 运行初始化容器(init container)过程 运行主容器(main container) 容器启动后钩子(post start)、容器终止前钩子(pre stop) 容器的存 ......

pod生命周期

我们一般将pod对象从创建至终这段时间范围成为pod的生命周期,它主要包含以下的过程:

  • pod创建过程
  • 运行初始化容器(init container)过程
  • 运行主容器(main container)
    • 容器启动后钩子(post start)、容器终止前钩子(pre stop)
    • 容器的存活性检测(liveness probe)、就绪性检测(readiness probe)
  • pod终止过程

pod生命周期

 

 

pod的创建和终止

pod的创建过程

  1. 用户通过kubectl或其他api客户端提交需要创建的pod信息给apiserver
  2. apiserver开始生成pod对象的信息,并将信息存入etcd,然后返回确认信息至客户端
  3. apiserver开始反映etcd中pod对象的变化,其他组件使用watch机制来跟踪检查apiserver上的变动
  4. scheduler发现有新的pod对象要创建,开始为pod分配主机并将结果信息更新至apiserver
  5. node节点上的kubectl发现有pod调度过来,尝试调用docker启动容器,并将结果返回送至apiserver
  6. apiserver将接收到的pod状态信息存入etcd中

pod生命周期

 

pod的终止过程

  1. 用户向apiserver发送删除pod对象的命令
  2. apiserver中的pod对象信息会随着时间的推移而更新,在宽限期内(默认30s),pod被视为dead
  3. 将pod标记为terminating状态
  4. kubelet在监控到pod对象转为terminating状态的同时启动pod关闭进程
  5. 端点控制器监控到pod对象的关闭行为时将其从所有匹配到此端点的service资源的端点列表移除
  6. 如果当前pod对象定义了prestop钩子处理器,则在其标记为terminating后即会以同步的方式启动执行
  7. pod对象中的容器进程收到停止信号
  8. 宽限期结束后,若pod中还存在仍在运行的进程,那么pod对象会收到立即终止的信号
  9. kubelet请求apiserver将此pod资源的宽限期设置为0从而完成删除操作,此时pod对于用户已不可见

初始化容器

初始化容器是在pod的主容器启动之前要运行的容器,主要是做一些主容器的前置工作,它具有两大特征

  1. 初始化容器必须运行完成直至结束,若某初始化容器运行失败,那么k8s需要重启它直到成功完成
  2. 初始化容器必须按照定义的顺序执行,当仅当前一个执行成功之后,后面的一个才能运行

初始化容器有很多的应用场景,下面列出的是最常见的几个

  • 提供主容器镜像中不具备的工具程序或自定义代码
  • 初始化容器要先于应用容器串行启动并运行完成,因此可用于延后应用容器的启动直至其依赖的条件得到满足

接下来做一个案例,模拟下面这个需求

假设要以主容器来运行nginx,但是要求在运行nginx之前要能够连接上mysql和redis所在服务器

为了简化测试,事先规定好mysql和redis服务器的地址

创建pod-initcontainer.yaml,内容如下:

apiversion: v1
kind: pod
metadata: 
  name: pod-initcontainer
  namespace: dev
  labels:
    user: ayanami
spec:
  containers:
  - name: main-container
    image: nginx:1.17.1
    ports: 
    - name: nginx-port
      containerport: 80
  initcontainers:
  - name: test-mysql
    image: busybox:1.30
    command: ['sh','-c','until ping 192.168.145.231 -c 1; do echo waiting for mysql...;sleep 2; done']
  - name: test-redis
    image: busybox:1.30
    command: ['sh','-c','until ping 192.168.145.232 -c 1; do echo waiting for redis...;sleep 2; done']

 运行配置文件

[root@master ~]# vim pod-initcontainer.yaml
[root@master ~]# kubectl create -f pod-initcontainer.yaml 
pod/pod-initcontainer created
[root@master ~]# kubectl get pod pod-initcontainer -n dev
name                ready   status     restarts   age
pod-initcontainer   0/1     init:0/2   0          20s

发现container一直处于初始化状态

此时添加ip地址(注:ens32是你的网卡的名字,不同机器可能不同,可以用ifconfig查看)

[root@master ~]# ifconfig ens32:1 192.168.145.231 netmask 255.255.255.0 up
[root@master ~]# ifconfig ens32:2 192.168.145.232 netmask 255.255.255.0 up
[root@master ~]# kubectl get pod pod-initcontainer -n dev
name                ready   status    restarts   age
pod-initcontainer   1/1     running   0          19m

发现pod跑起来了

钩子函数

钩子函数能够感知自身生命周期中的事件,并在相应的时刻到来时运行用户指定的程序代码

k8s在主容器的启动之后和停止之前提供了两个钩子函数

  • post start:容器创建之后执行,如果失败了会重启容器
  • pre stop:容器终止之前执行,执行完成之后容器将成功终止,在其完成之前会阻塞删除容器的操作

钩子处理器支持使用下面三种方式定义动作:

  • exec命令:在容器内执行一次命令
......
  lifecycle:
    poststart:
      exec:
        command:
        - cat
        - /tmp/healthy
......
  • tcpsocket:在当前容器尝试访问指定的socket
......
  lifecycle:
    poststart:
      tcpsocket:
        port: 8080
......
  • httpget:在当前容器中向某url发起http请求
......
  lifecycle:
    poststart:
      httpget:
        path: #uri地址
        port:
        host: 
        scheme: http  #支持的协议,http或者https
......

下面演示钩子函数的使用

创建pod-hook-exec.yaml文件,内容如下:

apiversion: v1
kind: pod
metadata: 
  name: pod-hook-exec
  namespace: dev
spec:
  containers:
  - name: main-container
    image: nginx:1.17.1
    ports: 
    - name: nginx-port
      containerport: 80
    lifecycle:
      poststart:
        exec: #在容器启动的时候执行一个命令,修改掉nginx的默认首页内容
          command: ["/bin/sh","-c","echo poststart... > /usr/share/nginx/html/index.html"]
      prestop: #在容器停止之前停止nginx服务
        exec:
          command: ["/usr/sbin/nginx","-s","quit"]

使用配置文件

[root@master ~]# vim pod-hook-exec.yaml
[root@master ~]# kubectl create -f pod-hook-exec.yaml 
pod/pod-hook-exec created
[root@master ~]# kubectl get pod pod-hook-exec -n dev -o wide
name            ready   status    restarts   age   ip            node    nominated node   readiness gates
pod-hook-exec   1/1     running   0          43s   10.244.2.22   node1   <none>           <none>
[root@master ~]# curl 10.244.2.22:80
poststart...

容器探测

容器探测用于检测容器中的应用实例是否能正常工作,是保障业务可用性的一种传统机制。如果经过探测,实例的状态不符合预期

,那么k8s就会把该问题实例“摘除”,不承担业务流量,k8s提供了两种探针来实现容器探测,分别是:

  • liveness probes:存活性探针,用于检测应用实例当前是否处于正常运行状态,如果不是,k8s会重启容器
  • readiness probes:就绪性探针,用于检测应用实例当前是否可以接受请求,如果不能,k8s不会转发流量

即livenessprobe决定是否重启容器,readinesprobe决定是否将请求转发给容器

上面两种探针目前均支持三种探测方式:

  • exec命令:在容器内执行一次命令,如果命令执行的退出码为0,则认为程序正常,否则不正常
......
  livenessprobe:
      exec:
        command:
        - cat
        - /tmp/healthy
......
  • tcpsocket:将会尝试访问同一个用户容器的端口,如果能够建立这条连接,则认为程序正常,否则不正常
......
  livenessprobe:
      tcpsocket:
        port: 8080
......
  • httpget:调用容器内web应用的url,如果返回的状态码在200和399之间,则认为程序正常,否则不正常
......
  lifecycle:
    poststart:
      httpget:
        path: #uri地址
        port:
        host: 
        scheme: http  #支持的协议,http或者https
......

 下面以liveness probes为例,做几个演示:

方式一:exec

创建pod-liveness-exec.yaml

apiversion: v1
kind: pod
metadata: 
  name: pod-liveness-exec
  namespace: dev
spec:
  containers:
  - name: main-container
    image: nginx:1.17.1
    ports: 
    - name: nginx-port
      containerport: 80
    livenessprobe:
      exec:
        command: ["/bin/cat","/tmp/hello.txt"] #执行一个查看文件的命令

使用配置文件

[root@master ~]# vim pod-liveness-exec.yaml
[root@master ~]# kubectl create -f pod-liveness-exec.yaml 
pod/pod-liveness-exec created
[root@master ~]# kubectl get pod pod-liveness-exec -n dev 
name                ready   status    restarts   age
pod-liveness-exec   1/1     running   1          102s

发现pod重启了一次,查看错误信息

[root@master ~]# kubectl describe pod pod-liveness-exec -n dev
  type     reason     age                  from               message
  ----     ------     ----                 ----               -------
  normal   scheduled  <unknown>            default-scheduler  successfully assigned dev/pod-liveness-exec to node1
  normal   pulled     49s (x4 over 2m20s)  kubelet, node1     container image "nginx:1.17.1" already present on machine
  normal   created    49s (x4 over 2m20s)  kubelet, node1     created container main-container
  normal   started    49s (x4 over 2m20s)  kubelet, node1     started container main-container
  normal   killing    49s (x3 over 109s)   kubelet, node1     container main-container failed liveness probe, will be restarted
  warning  unhealthy  39s (x10 over 2m9s)  kubelet, node1     liveness probe failed: /bin/cat: /tmp/hello.txt: no such file or directory

修改文件内容

apiversion: v1
kind: pod
metadata: 
  name: pod-liveness-exec
  namespace: dev
spec:
  containers:
  - name: main-container
    image: nginx:1.17.1
    ports: 
    - name: nginx-port
      containerport: 80
    livenessprobe:
      exec:
        command: ["/bin/ls","/tmp/"] #执行一个查看文件的命令

使用配置文件

[root@master ~]# vim pod-liveness-exec.yaml 
[root@master ~]# kubectl delete -f pod-liveness-exec.yaml 
[root@master ~]# kubectl create -f pod-liveness-exec.yaml 
pod/pod-liveness-exec created
[root@master ~]# kubectl get pod pod-liveness-exec -n dev
name                ready   status    restarts   age
pod-liveness-exec   1/1     running   0          84s

说明没有重启

方式二:tcpsocket

创建pod-liveness-tcpsocket.yaml

apiversion: v1
kind: pod
metadata: 
  name: pod-liveness-tcpsocket
  namespace: dev
spec:
  containers:
  - name: main-container
    image: nginx:1.17.1
    ports: 
    - name: nginx-port
      containerport: 80
    livenessprobe:
      tcpsocket:
        port: 8080  #尝试访问8080端口

使用配置文件

[root@master ~]# vim pod-liveness-tcpsocket.yaml
[root@master ~]# kubectl create -f pod-liveness-tcpsocket.yaml 
pod/pod-liveness-tcpsocket created
[root@master ~]# kubectl get pod pod-liveness-tcpsocket -n dev
name                     ready   status    restarts   age
pod-liveness-tcpsocket   1/1     running   1          29s
[root@master ~]# kubectl describe pod pod-liveness-tcpsocket -n dev
events:
  type     reason     age                  from               message
  ----     ------     ----                 ----               -------
  normal   scheduled  <unknown>            default-scheduler  successfully assigned dev/pod-liveness-tcpsocket to node1
  normal   pulled     43s (x4 over 2m10s)  kubelet, node1     container image "nginx:1.17.1" already present on machine
  normal   created    43s (x4 over 2m10s)  kubelet, node1     created container main-container
  normal   started    43s (x4 over 2m10s)  kubelet, node1     started container main-container
  normal   killing    43s (x3 over 103s)   kubelet, node1     container main-container failed liveness probe, will be restarted
  warning  unhealthy  33s (x10 over 2m3s)  kubelet, node1     liveness probe failed: dial tcp 10.244.2.25:8080: connect: connection refused

更改文件内容

apiversion: v1
kind: pod
metadata: 
  name: pod-liveness-tcpsocket
  namespace: dev
spec:
  containers:
  - name: main-container
    image: nginx:1.17.1
    ports: 
    - name: nginx-port
      containerport: 80
    livenessprobe:
      tcpsocket:
        port: 80  #尝试访问80端口

重新使用配置文件

[root@master ~]# vim pod-liveness-tcpsocket.yaml 
[root@master ~]# kubectl delete -f pod-liveness-tcpsocket.yaml 
pod "pod-liveness-tcpsocket" deleted
[root@master ~]# kubectl create -f pod-liveness-tcpsocket.yaml 
pod/pod-liveness-tcpsocket created
[root@master ~]# kubectl get pod pod-liveness-tcpsocket -n dev
name                     ready   status    restarts   age
pod-liveness-tcpsocket   1/1     running   0          18s

表明没有问题

方式三:httpget

创建pod-liveness-httpget.yaml

apiversion: v1
kind: pod
metadata: 
  name: pod-liveness-httpget
  namespace: dev
spec:
  containers:
  - name: main-container
    image: nginx:1.17.1
    ports: 
    - name: nginx-port
      containerport: 80
    livenessprobe:
      httpget: #其实就是访问http://127.0.0.1:80/hello
        scheme: http #支持的协议,http或https
        port: 80
        path: /hello #uri地址

使用配置文件

[root@master ~]# vim pod-liveness-httpget.yaml
[root@master ~]# kubectl create -f pod-liveness-httpget.yaml 
pod/pod-liveness-httpget created
[root@master ~]# kubectl get pod pod-liveness-httpget -n dev
name                   ready   status    restarts   age
pod-liveness-httpget   1/1     running   1          75s
[root@master ~]# kubectl describe pod pod-liveness-httpget -n dev
events:
  type     reason     age                from               message
  ----     ------     ----               ----               -------
  normal   scheduled  <unknown>          default-scheduler  successfully assigned dev/pod-liveness-httpget to node2
  normal   pulled     18s (x3 over 74s)  kubelet, node2     container image "nginx:1.17.1" already present on machine
  normal   created    18s (x3 over 74s)  kubelet, node2     created container main-container
  normal   killing    18s (x2 over 48s)  kubelet, node2     container main-container failed liveness probe, will be restarted
  normal   started    17s (x3 over 73s)  kubelet, node2     started container main-container
  warning  unhealthy  8s (x7 over 68s)   kubelet, node2     liveness probe failed: http probe failed with statuscode: 404

可以看见pod在重启,详细描述说明没找到这个网址

修改配置文件

apiversion: v1
kind: pod
metadata: 
  name: pod-liveness-httpget
  namespace: dev
spec:
  containers:
  - name: main-container
    image: nginx:1.17.1
    ports: 
    - name: nginx-port
      containerport: 80
    livenessprobe:
      httpget: #其实就是访问http://127.0.0.1:80/
        scheme: http #支持的协议,http或https
        port: 80
        path: / #uri地址

重新使用配置文件

[root@master ~]# kubectl delete -f pod-liveness-httpget.yaml 
pod "pod-liveness-httpget" deleted
[root@master ~]# kubectl create -f pod-liveness-httpget.yaml 
pod/pod-liveness-httpget created
[root@master ~]# kubectl get pod pod-liveness-httpget -n dev
name                   ready   status    restarts   age
pod-liveness-httpget   1/1     running   0          24s
events:
  type    reason     age        from               message
  ----    ------     ----       ----               -------
  normal  scheduled  <unknown>  default-scheduler  successfully assigned dev/pod-liveness-httpget to node2
  normal  pulled     27s        kubelet, node2     container image "nginx:1.17.1" already present on machine
  normal  created    27s        kubelet, node2     created container main-container
  normal  started    27s        kubelet, node2     started container main-container

表明配置没有问题

其他配置

至此,已经使用liveness probe演示了三种探测方式,但是查看livenessprobe的子属性,会发现除了这三种方式,还有一些其他的配置,在这里一并解释下

[root@master ~]# kubectl explain pod.spec.containers.livenessprobe
kind:     pod
version:  v1

resource: livenessprobe <object>

description:
     periodic probe of container liveness. container will be restarted if the
     probe fails. cannot be updated. more info:
     https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle#container-probes

     probe describes a health check to be performed against a container to
     determine whether it is alive or ready to receive traffic.

fields:
   exec    <object>
     one and only one of the following should be specified. exec specifies the
     action to take.

   failurethreshold    <integer>
     minimum consecutive failures for the probe to be considered failed after
     having succeeded. defaults to 3. minimum value is 1.

   httpget    <object>
     httpget specifies the http request to perform.

   initialdelayseconds    <integer>
     number of seconds after the container has started before liveness probes
     are initiated. more info:
     https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle#container-probes

   periodseconds    <integer>
     how often (in seconds) to perform the probe. default to 10 seconds. minimum
     value is 1.

   successthreshold    <integer>
     minimum consecutive successes for the probe to be considered successful
     after having failed. defaults to 1. must be 1 for liveness and startup.
     minimum value is 1.

   tcpsocket    <object>
     tcpsocket specifies an action involving a tcp port. tcp hooks not yet
     supported

   timeoutseconds    <integer>
     number of seconds after which the probe times out. defaults to 1 second.
     minimum value is 1. more info:
     https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle#container-probes