栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Kubernetes的Pod组件详解----生命周期

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Kubernetes的Pod组件详解----生命周期

一、Pod生命周期 Pod生命周期简介

一般将pod对象从创建至终止的这段时间范围称为pod的盛名周期,它主要包含下面的过程:

pod创建过程运行初始化容器(init container)过程运行注容器(main container)过程

容器启动后钩子(post start),容器终止前钩子(pre stop)容器的存活性探测(liveness probe),就绪性探测(readness probe) pod终止过程

在整个声明周期中,Pod会出现5种状态(相位),如下:

挂起(Pending):挨批server已经创建了pod资源对象,但它尚未被调度完成或者仍处于下载镜像的过程中运行中(Running):pod已经被调度至某节点,并且所有容器都已经被kubectl创建完成成功(Succeed):pod中的所有容器都已经成功终止并且不会被重启失败(Failed):所有容器都已经停止,但至少有一个容器终止失败,即容器返回了非0的退出状态未知(Unknown):apiserver无法正常获取到pod对象的状态信息,通常由网络通信失败所致 Pod的创建过程

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

Pod的终止过程

1、用户向apiServer发送删除pod对象的命令2、apiServer中的pod对象信心ui随着时间的退役而更新,在宽限期内(默认30秒),pod被视为dead3、将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、初始化容器必须运行完成知道结束,若某初始化容器运行失败,那么kubernetes需要重启它知道成功完成2、初始化容器必须按照定义的顺序执行,当且晋档前一个成功之后,后一个才能执行

初始化容器有很多应用场景,如下为最常见的几个:

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

如下,修改pod_base.yaml文件,增加两个初始化容器,这里面用来检测可以ping通192.168.2.151和192.168.2.152,都ping通后才会进行主容器的创建

apiVersion: v1
kind: Pod
metadata:
  name: pod-base
  namespace: dev
  labels:
    user: redrose2100
spec:
  containers:
  - name: nginx
    image: nginx:latest
    imagePullPolicy: IfNotPresent
    ports:
    - name: nginx-port
      containerPort: 80
      protocol: TCP
    resources:
      limits:
        cpu: "2"
        memory: "2G"
      requests:
        cpu: "1"
        memory: "256M"
  - name: busybox
    image: busybox
    imagePullPolicy: Always
    command: ["/bin/sh","-c","touch /tmp/hello.txt;while true;do /bin/echo $(date +%T) >> /tmp/hello.txt;sleep 3;done;"]
    env:
    - name: "username"
      value: "admin"
    - name: "password"
      value: "admin123"
  initContainers:
  - name: test-ping
    image: busybox
    command: ["sh","-c","until ping 192.168.2.151 -c 1 ; do echo 'waiting for ping...'; sleep 2; done;"]
  - name: test-ping2
    image: busybox
    command: ["sh","-c","until ping 192.168.2.152 -c 1 ; do echo 'waiting for ping...'; sleep 2; done;"]

使用如下命令创建

[root@master ~]# kubectl apply -f pod_base.yaml
pod/pod-base created
[root@master ~]# 

通过如下命令可以查看容器操作过程

kubectl describe pod pod-base -n dev

如下为事件的一部分回显,可以看到初始化容器确实在最先执行

Events:
  Type    Reason     Age   From               Message
  ----    ------     ----  ----               -------
  Normal  Scheduled  41s   default-scheduler  Successfully assigned dev/pod-base to node2
  Normal  Pulling    41s   kubelet            Pulling image "busybox"
  Normal  Created    39s   kubelet            Created container test-ping
  Normal  Started    39s   kubelet            Started container test-ping
  Normal  Pulling    39s   kubelet            Pulling image "busybox"
  Normal  Pulled     39s   kubelet            Successfully pulled image "busybox" in 1.396809993s
  Normal  Pulled     33s   kubelet            Container image "nginx:latest" already present on machine
  Normal  Pulled     33s   kubelet            Successfully pulled image "busybox" in 5.87151405s
  Normal  Created    33s   kubelet            Created container test-ping2
  Normal  Started    33s   kubelet            Started container test-ping2
  Normal  Created    32s   kubelet            Created container nginx
  Normal  Started    32s   kubelet            Started container nginx
  Normal  Pulling    32s   kubelet            Pulling image "busybox"
  Normal  Pulled     17s   kubelet            Successfully pulled image "busybox" in 15.851599334s
  Normal  Created    17s   kubelet            Created container busybox
  Normal  Started    16s   kubelet            Started container busybox

此时可以把其中一个初始化容器中ping命令后的ip换成一个不通的ip,然后再重新创建,如下,可以发现一直在启动初始化容器

[root@master ~]# kubectl get pod -n dev
NAME       READY   STATUS     RESTARTS   AGE
pod-base   0/2     Init:1/2   0          88s

通过如下命令

kubectl describe pod pod-base -n dev

事件的回显内容一直卡在如下的位置,即在启动初始化容器的位置

Events:
  Type    Reason     Age   From               Message
  ----    ------     ----  ----               -------
  Normal  Scheduled  2m6s  default-scheduler  Successfully assigned dev/pod-base to node2
  Normal  Pulling    2m6s  kubelet            Pulling image "busybox"
  Normal  Pulled     2m5s  kubelet            Successfully pulled image "busybox" in 1.327728966s
  Normal  Created    2m5s  kubelet            Created container test-ping
  Normal  Started    2m5s  kubelet            Started container test-ping
  Normal  Pulling    2m4s  kubelet            Pulling image "busybox"
  Normal  Pulled     108s  kubelet            Successfully pulled image "busybox" in 15.671497503s
  Normal  Created    108s  kubelet            Created container test-ping2
  Normal  Started    108s  kubelet            Started container test-ping2
钩子函数

钩子函数能欧感知自身生命周期中的事件,并在相应的时刻到来时运行用户指定的程序代码
kubernetes在主容器的启动之后和停止之前提供了两个钩子函数

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

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

exec命令:在容器内执行一次命令

  lifecycle:
    postStart:
      exec:
        command:
        - cat
        - /var/lib/redis.conf

tcpSocket: 在当前容器尝试访问指定的socket

  lifecycle:
    postStart:
      tcpSocket:
        port: 8000

httpGet: 在当前容器中向某url发起http请求
如下,为访问 http://192.168.2.150:80/users

  lifecycle:
    postStart:
      httpGet:
        path: /users
        port: 80
        host: 192.168.2.150
        scheme: HTTP  # 或者HTTPS

以上三种方式第一种使用的比较多,其次是第三种,第二种使用的很少
如下:编辑pod_base.yaml文件,给nginx的主容器中增加postStart和preStop步骤

apiVersion: v1
kind: Pod
metadata:
  name: pod-base
  namespace: dev
  labels:
    user: redrose2100
spec:
  containers:
  - name: nginx
    image: nginx:latest
    imagePullPolicy: IfNotPresent
    ports:
    - name: nginx-port
      containerPort: 80
      protocol: TCP
    resources:
      limits:
        cpu: "2"
        memory: "2G"
      requests:
        cpu: "1"
        memory: "256M"
    lifecycle:
      postStart:
        exec:
          command: ["/bin/sh","-c","echo 'hello postStart...' > /opt/demo.txt"]
      preStop:
        exec:
          command: ["/usr/sbin/nginx","-s","quit"]
  - name: busybox
    image: busybox
    imagePullPolicy: Always
    command: ["/bin/sh","-c","touch /tmp/hello.txt;while true;do /bin/echo $(date +%T) >> /tmp/hello.txt;sleep 3;done;"]
    env:
    - name: "username"
      value: "admin"
    - name: "password"
      value: "admin123"
  initContainers:
  - name: test-ping
    image: busybox
    command: ["sh","-c","until ping 192.168.2.151 -c 1 ; do echo 'waiting for ping...'; sleep 2; done;"]
  - name: test-ping2
    image: busybox
    command: ["sh","-c","until ping 192.168.2.152 -c 1 ; do echo 'waiting for ping...'; sleep 2; done;"]

使用如下命令启动pod

[root@master ~]# kubectl apply -f pod_base.yaml
pod/pod-base created
[root@master ~]#

如下命令进入nginx的docker容器,查看 /opt/demo.txt文件,如下已经写入成功

 kubectl exec pod-base -n dev -it -c nginx /bin/sh
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
#
# cd /opt
# ls
demo.txt
# cat demo.txt
hello postStart...
#
容器探测

容器探测用于检测容器中的应用实例是否正常工作,是保障业务可用性的一种传统机制,如果经过探测,实例的状态不符合预期,那么kubernetes就会把该问题实例“摘除”,不承担业务流量,kubernetes提供了两种探针来实现容器探测,分别是:

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

即livenessProbe绝世是否重启容器,readnessProb决定是否将请求转发给容器

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

exec命令:在容器内执行一次命令,如果命令执行的退出码为0,则认为程序正常,否则不正常

  livenessProb:
    exec:
      command:
      - cat
      - /var/lib/redis.conf

tcpSocket: 将会尝试访问容器一个用户容器的端口,如果能够简建立这条连接,则认为程序正常,否则不正常

  livenessProbe:
    tcpSocket:
      port: 8000

httpGet: 调用容器内web应用的url,如果返回的状态码在200-399之间,则认为程序正常,否则不正常
如下,为访问 http://192.168.2.150:80/users

  livenessProbe:
    httpGet:
      path: /users
      port: 80
      host: 192.168.2.150
      scheme: HTTP  # 或者HTTPS

实例1:exec命令方式
如下在nginx中使用exec命令方式对容器进行存活性探针,这里因为没有/opt/demo100.txt,因此理论上应该会给容器重启

apiVersion: v1
kind: Pod
metadata:
  name: pod-base
  namespace: dev
  labels:
    user: redrose2100
spec:
  containers:
  - name: nginx
    image: nginx:latest
    imagePullPolicy: IfNotPresent
    ports:
    - name: nginx-port
      containerPort: 80
      protocol: TCP
    resources:
      limits:
        cpu: "2"
        memory: "2G"
      requests:
        cpu: "1"
        memory: "256M"
    lifecycle:
      postStart:
        exec:
          command: ["/bin/sh","-c","echo 'hello postStart..' > /opt/demo.txt"]
      preStop:
        exec:
          command: ["/usr/sbin/nginx","-s","quit"]
    livenessProbe:
      exec:
        command: ["/bin/cat","/opt/demo100.txt"]
  - name: busybox
    image: busybox
    imagePullPolicy: Always
    command: ["/bin/sh","-c","touch /tmp/hello.txt;while true;do /bin/echo $(date +%T) >> /tmp/hello.txt;sleep 3;done;"]
    env:
    - name: "username"
      value: "admin"
    - name: "password"
      value: "admin123"
  initContainers:
  - name: test-ping
    image: busybox
    command: ["sh","-c","until ping 192.168.2.151 -c 1 ; do echo 'waiting for ping...'; sleep 2; done;"]
  - name: test-ping2
    image: busybox
    command: ["sh","-c","until ping 192.168.2.152 -c 1 ; do echo 'waiting for ping...'; sleep 2; done;"]

使用如下命令创建pod

[root@master ~]# kubectl apply -f pod_base.yaml
pod/pod-base created
[root@master ~]#

通过如下命令可以看到nginx的容器一直在进行探测和重启

kubectl describe pod pod-base -n dev

实例2:使用TCPSocket方式
如下,修改pod_base.yaml文件,在nginx中探测8000端口是否有监听

apiVersion: v1
kind: Pod
metadata:
  name: pod-base
  namespace: dev
  labels:
    user: redrose2100
spec:
  containers:
  - name: nginx
    image: nginx:latest
    imagePullPolicy: IfNotPresent
    ports:
    - name: nginx-port
      containerPort: 80
      protocol: TCP
    resources:
      limits:
        cpu: "2"
        memory: "2G"
      requests:
        cpu: "1"
        memory: "256M"
    lifecycle:
      postStart:
        exec:
          command: ["/bin/sh","-c","echo 'hello postStart..' > /opt/demo.txt"]
      preStop:
        exec:
          command: ["/usr/sbin/nginx","-s","quit"]
    livenessProbe:
      tcpSocket:
        port: 8000
  - name: busybox
    image: busybox
    imagePullPolicy: Always
    command: ["/bin/sh","-c","touch /tmp/hello.txt;while true;do /bin/echo $(date +%T) >> /tmp/hello.txt;sleep 3;done;"]
    env:
    - name: "username"
      value: "admin"
    - name: "password"
      value: "admin123"
  initContainers:
  - name: test-ping
    image: busybox
    command: ["sh","-c","until ping 192.168.2.151 -c 1 ; do echo 'waiting for ping...'; sleep 2; done;"]
  - name: test-ping2
    image: busybox
    command: ["sh","-c","until ping 192.168.2.152 -c 1 ; do echo 'waiting for ping...'; sleep 2; done;"]

使用如下命令创建pod

[root@master ~]# kubectl apply -f pod_base.yaml
pod/pod-base created
[root@master ~]#

通过如下命令可以查看到这里因为nginx是80端口,所以没有监听8000端口,所以会出现失败,然后不断重启

kubectl describe pod pod-base -n dev

如下:

  Normal   Killing    60s                kubelet            Container nginx failed liveness probe, will be restarted
  Warning  Unhealthy  30s (x6 over 80s)  kubelet            Liveness probe failed: dial tcp 10.244.2.20:8000: connect: connection refused

实例3:httpGet方式
如下,修改pod-base.yaml 文件,使用httpGet方式探针

apiVersion: v1
kind: Pod
metadata:
  name: pod-base
  namespace: dev
  labels:
    user: redrose2100
spec:
  containers:
  - name: nginx
    image: nginx:latest
    imagePullPolicy: IfNotPresent
    ports:
    - name: nginx-port
      containerPort: 80
      protocol: TCP
    resources:
      limits:
        cpu: "2"
        memory: "2G"
      requests:
        cpu: "1"
        memory: "256M"
    lifecycle:
      postStart:
        exec:
          command: ["/bin/sh","-c","echo 'hello postStart..' > /opt/demo.txt"]
      preStop:
        exec:
          command: ["/usr/sbin/nginx","-s","quit"]
    livenessProbe:
      httpGet:
        scheme: HTTP
        port: 80
        path: /
  - name: busybox
    image: busybox
    imagePullPolicy: Always
    command: ["/bin/sh","-c","touch /tmp/hello.txt;while true;do /bin/echo $(date +%T) >> /tmp/hello.txt;sleep 3;done;"]
    env:
    - name: "username"
      value: "admin"
    - name: "password"
      value: "admin123"
  initContainers:
  - name: test-ping
    image: busybox
    command: ["sh","-c","until ping 192.168.2.151 -c 1 ; do echo 'waiting for ping...'; sleep 2; done;"]
  - name: test-ping2
    image: busybox
    command: ["sh","-c","until ping 192.168.2.152 -c 1 ; do echo 'waiting for ping...'; sleep 2; done;"]

使用如下命令创建pod

[root@master ~]# kubectl apply -f pod_base.yaml
pod/pod-base created
[root@master ~]#

通过如下命令可以查看到是否会重启,这里因为设置了80端口的根目录,即nginx的访问路径,因此是不会出现重启的问题

 kubectl describe pod pod-base -n dev

容器探测总结:
查看livenessProbe的子属性,除了三种方式,还有一些参数,如下:

initialDelaySeconds        # 容器启动后等待多少秒执行第一次探测
timeoutSeconds             # 探测超时时间,默认一秒,最小一秒
periodSeconds              # 执行探测的频率,默认是10秒,最小1秒
failureThreshold           # 连续探测失败多少次才被认定为失败,默认是3,最小是1
successThreshold           # 连续探测成功多少次才被认定为成功,默认是1
重启策略

pod的重启策略有3种,如下:

Always:容器失效时,自动重启该容器,这是默认值OnFailure:容器停止运行且退出码不为0时重启Never:不论状态为何,都不重启该容器

重启策略适用于pod对象中的所有容器,首次需要重启的容器,将在其需要时立即进行重启,随后再次需要重启的操作将由kubelet延迟一段时间后进行,且反复的重启操作的延迟时长为10s,20s,40s,80s,160s,300s,300s是最大延迟时长

如下,修改pod_base.yaml文件,其中restartPolicy字段即设置重启策略,这里需要注意的是,restartPolicy字段跟containers字段是平行的,是对所有容器生效的

apiVersion: v1
kind: Pod
metadata:
  name: pod-base
  namespace: dev
  labels:
    user: redrose2100
spec:
  containers:
  - name: nginx
    image: nginx:latest
    imagePullPolicy: IfNotPresent
    ports:
    - name: nginx-port
      containerPort: 80
      protocol: TCP
    resources:
      limits:
        cpu: "2"
        memory: "2G"
      requests:
        cpu: "1"
        memory: "256M"
    lifecycle:
      postStart:
        exec:
          command: ["/bin/sh","-c","echo 'hello postStart..' > /opt/demo.txt"]
      preStop:
        exec:
          command: ["/usr/sbin/nginx","-s","quit"]
    livenessProbe:
      httpGet:
        scheme: HTTP
        port: 80
        path: /
  - name: busybox
    image: busybox
    imagePullPolicy: Always
    command: ["/bin/sh","-c","touch /tmp/hello.txt;while true;do /bin/echo $(date +%T) >> /tmp/hello.txt;sleep 3;done;"]
    env:
    - name: "username"
      value: "admin"
    - name: "password"
      value: "admin123"
  restartPolicy: Always
  initContainers:
  - name: test-ping
    image: busybox
    command: ["sh","-c","until ping 192.168.2.151 -c 1 ; do echo 'waiting for ping...'; sleep 2; done;"]
  - name: test-ping2
    image: busybox
    command: ["sh","-c","until ping 192.168.2.152 -c 1 ; do echo 'waiting for ping...'; sleep 2; done;"]

使用如下命令创建pod

[root@master ~]# kubectl apply -f pod_base.yaml
pod/pod-base created
[root@master ~]#
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/759255.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号