k8s的pod容器的生命周期

5 篇文章 1 订阅

生命周期概要图

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

Init容器与普通的容器非常像,除了如下两点:
1)Init容器总是运行到成功完成为止
2)每个Init容器都必须在下一个Init容器启动之前成功完成

如果Pod的Init容器失败,Kubernetes会不断地重启该Pod,直到Init容器成功为止。然而,如果Pod对应的restartPolicy设置为Never【默认为Always】,它将不会重新启动。

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

init的一些补充:
1)在Pod启动过程中,Init容器会按顺序在网络和数据卷初始化之后启动【即在pause启动之后启动,pod第一个启动的容器并不是Init而是pause,但pause仅仅只负责初始化网络和数据卷其它什么操作都不能做,我们可操作的空间几乎为0因此不做描述】。每个容器必须在下一个容器启动之前成功退出。
2)如果由于运行时或失败退出,将导致容器启动失败,它会根据Pod的restartPolicy指定的策略进行重试。然而,如果Pod的restartPolicy设置为Always,Init容器失败时会使用RestartPolicy 策略
3)在所有的Init容器没有成功之前,Pod将不会变成Ready状态。Init容器的端口将不会在Service中进行聚集。正在初始化中的Pod处于Pending状态,但应该会将Initializing状态设置为true
4)如果Pod重启,所有Init容器必须重新执行,因此Init容器应该配置为幂等的状态。
5)#对Init容器spec的修改被限制在容器image字段,修改其他字段都不会生效。更改Init容器的image字段,等价于重启该Pod
6)Init容器具有应用容器的所有字段。除了readinessProbe,因为Init容器无法定义不同于完成(completion)的就绪(readiness)之外的其他状态。这会在验证过程中强制执行
7)在Pod中的每个app和Init容器的名称必须唯一;与任何其它容器共享同一个名称,会在验证时抛出错误

busybox镜像包下载:
链接:https://pan.baidu.com/s/13d-i8FWF0miLx2XYNchffw 
提取码:zekc

实践:
[root@k8smaster k8s]# more init-pod.yaml  
apiVersion: v1
kind: Pod
metadata:
  name: init-mypod
  labels:
    app: mypod
    version: v1
spec:
  containers:
  - name: init-mypod-ctn
    image: 192.168.23.100:5000/busybox:v1
    command: ['sh','-c','echo The init-mypod is running!&& sleep 3600'] 
  initContainers:
  - name: init-myservice
    image: 192.168.23.100:5000/busybox:v1
    command: ['sh','-c','until nslookup myservice;do echo waiting for myservice;sleep 2; done;'] 
  - name: init-mydb
    image: 192.168.23.100:5000/busybox:v1
    command: ['sh','-c','until nslookup mydb;do echo waiting for mydb;sleep 2;done;']
   
[root@k8smaster k8s]# more init-myservice.yaml 
apiVersion: v1
kind: Service
metadata:
  name: myservice
spec:
  ports:
  - protocol: TCP
    port: 9999
    targetPort: 9376
[root@k8smaster k8s]# more init-mydb.yaml 
apiVersion: v1
kind: Service
metadata:
 name: mydb
spec:
  ports:
  - protocol: TCP
    port: 9999
    targetPort: 9377
[root@k8smaster k8s]# 
[root@k8smaster k8s]# kubectl create -f init-pod.yaml 
pod/init-mypod created
[root@k8smaster k8s]# kubectl get pod
NAME         READY   STATUS     RESTARTS   AGE
init-mypod   0/1     Init:0/2   0          8m57s
[root@k8smaster k8s]#

[root@k8smaster k8s]# kubectl create -f init-myservice.yaml 
service/myservice created
[root@k8smaster k8s]# kubectl get pod
NAME         READY   STATUS     RESTARTS   AGE
init-mypod   0/1     Init:1/2   0          9s
[root@k8smaster k8s]#
[root@k8smaster k8s]# kubectl create -f init-mydb.yaml 
service/mydb created
[root@k8smaster k8s]# kubectl get pod
NAME         READY   STATUS    RESTARTS   AGE
init-mypod   1/1     Running   0          93s
[root@k8smaster k8s]#    
 [root@k8smaster k8s]# kubectl get service
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP    20h
mydb         ClusterIP   10.96.110.133   <none>        9999/TCP   107s
myservice    ClusterIP   10.107.44.26    <none>        9999/TCP   4m42s

[root@k8smaster k8s]#

2、容器探针
探针是由kubelet对容器执行的定期诊断,要执行诊断,kubelet调用有容器实现的Handler。有三种类型的处理程序:
1)ExecAction:在容器内执行指定命令。如果命令退出时返回码为0,则认为诊断成功。
2)TCPScoketAction:对指定端口上的容器的IP地址进行TCP检查。如果端口打开,则诊断被认为成功。
3)HTTPGetAction:对指定的端口和路径上的容器的ip地址执行http的get请求。如果响应的状态码大于等于200且小于400,则诊断被认为成功。

每次探测都将获得以下三种结果之一:
1)成功:容器通过了诊断
2)失败:容器未通过诊断
3)未知:诊断失败,不会采取任何行动

探测方式
1)livenessProbe:指示容器是否正在运行。如果存活探测失败,则ketlet会杀死容器,并且容器将受到其重启策略的影响。如果容器不提供存活探针,则默认状态是成功。
2)readinessProbe:指示容器是否准备好服务请求。如果就绪探测失败,端点控制器将从与pod匹配的所有service的端点中删除该pod的ip地址。初始化延迟之前的就绪状态默认是Failure。如果容器不提供就绪探针,则默认状态为成功。

nginx_v1镜像包下载
链接:https://pan.baidu.com/s/141x72sNrht_0KqumMYrhFA 
提取码:rbyx

实践:
[root@k8smaster k8s]# more readinessProbe-httpget.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: readiness-httpget-pod
spec:
  containers:
  - name: readiness-httpdget-container
    image: 192.168.23.100:5000/nginx:v1
    imagePullPolicy: IfNotPresent
    readinessProbe:
      httpGet:
        port: 80
        path: /index1.html
      initialDelaySeconds: 1
      periodSeconds: 3
[root@k8smaster k8s]# kubectl create -f readinessProbe-httpget.yaml 
pod/readiness-httpget-pod created
[root@k8smaster k8s]# kubectl get pod
NAME                    READY   STATUS    RESTARTS   AGE
readiness-httpget-pod   0/1     Running   0          7s
[root@k8smaster k8s]# kubectl describe pod readiness-httpget-pod
Name:         readiness-httpget-pod
Namespace:    default
Priority:     0
Node:         k8snode02/192.168.23.102
Start Time:   Thu, 13 Feb 2020 04:07:15 +0800
Labels:       <none>
Annotations:  <none>
Status:       Running
IP:           10.244.2.5
Containers:
  readiness-httpdget-container:
    Container ID:   docker://5bf111178bfd079846b933dd6111d1000b7ddd94c5d3a30b525876bab7e0c091
    Image:          192.168.23.100:5000/nginx:v1
    Image ID:       docker-pullable://192.168.23.100:5000/nginx@sha256:89a42c3ba15f09a3fbe39856bddacdf9e94cd03df7403cad4fc105088e268fc9
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      Thu, 13 Feb 2020 04:07:19 +0800
    Ready:          False
    Restart Count:  0
    Readiness:      http-get http://:80/index1.html delay=1s timeout=1s period=3s #success=1 #failure=3
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-vt7pl (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             False 
  ContainersReady   False 
  PodScheduled      True 
Volumes:
  default-token-vt7pl:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-vt7pl
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events:
  Type     Reason     Age                     From                Message
  ----     ------     ----                    ----                -------
  Normal   Pulled     3m48s                   kubelet, k8snode02  Container image "192.168.23.100:5000/nginx:v1" already present on machine
  Normal   Created    3m48s                   kubelet, k8snode02  Created container readiness-httpdget-container
  Normal   Started    3m47s                   kubelet, k8snode02  Started container readiness-httpdget-container
  Warning  Unhealthy  3m15s (x11 over 3m45s)  kubelet, k8snode02  Readiness probe failed: HTTP probe failed with statuscode: 404
  Normal   Scheduled  38s                     default-scheduler   Successfully assigned default/readiness-httpget-pod to k8snode02
[root@k8smaster k8s]#
root@k8smaster k8s]# curl "http://10.244.2.5/index.html"
ok--->index.html
[root@k8smaster k8s]# curl "http://10.244.2.5/index1.html"
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.17.7</center>
</body>
</html>

[root@k8smaster k8s]# kubectl exec -it readiness-httpget-pod /bin/bash
root@readiness-httpget-pod:/# cd /usr/share/nginx/html/
root@readiness-httpget-pod:/usr/share/nginx/html# echo "ok--->index1.html">index1.html
root@readiness-httpget-pod:/usr/share/nginx/html# exit
exit
[root@k8smaster k8s]# curl "http://10.244.2.5/index1.html"
ok--->index1.html
[root@k8smaster k8s]# 
[root@k8smaster k8s]# kubectl get pod
NAME                    READY   STATUS    RESTARTS   AGE
readiness-httpget-pod   1/1     Running   0          4m28s
[root@k8smaster k8s]# 

[root@k8smaster k8s]# more livenessProbe-exec.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: liveness-exec-pod
spec:
  containers:
  - name: liveness-exec-ctn
    image: 192.168.23.100:5000/busybox:v1
    imagePullPolicy: IfNotPresent
    command: ["/bin/sh","-c","touch /tmp/live;sleep 60;rm -rf /tmp/live;sleep 3600"]
    livenessProbe:
      exec:
        command: ["test","-e","/tmp/live"]
      initialDelaySeconds: 1
      periodSeconds: 3
[root@k8smaster k8s]# 
[root@k8smaster k8s]# kubectl create -f livenessProbe-exec.yaml 
pod/liveness-exec-pod created
[root@k8smaster k8s]# kubectl get pod
NAME                READY   STATUS    RESTARTS   AGE
liveness-exec-pod   1/1     Running   0          8s
[root@k8smaster k8s]# kubectl get pod
NAME                READY   STATUS    RESTARTS   AGE
liveness-exec-pod   1/1     Running   1          2m12s
[root@k8smaster k8s]# 

[root@k8smaster k8s]# more livenessProbe-http.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: liveness-httpget-pod
spec:
  containers:
  - name: liveness-httpget-ctn
    image: 192.168.23.100:5000/nginx:v1
    imagePullPolicy: IfNotPresent
    ports:
    - name: http
      containerPort: 80
    livenessProbe:
      httpGet:
        port: 80
        path: /index.html
      initialDelaySeconds: 1
      periodSeconds: 3
      timeoutSeconds: 10
[root@k8smaster k8s]# 
[root@k8smaster k8s]# kubectl create -f livenessProbe-http.yaml 
pod/liveness-httpget-pod created
[root@k8smaster k8s]# kubectl get pod
NAME                   READY   STATUS    RESTARTS   AGE
liveness-httpget-pod   1/1     Running   0          8s
[root@k8smaster k8s]# 

[root@k8smaster k8s]# more livenessProbe-tcp.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: probe-tcp
spec:
  containers:
  - name: nginx
    image: 192.168.23.100:5000/nginx:v1
    livenessProbe:
      initialDelaySeconds: 5
      timeoutSeconds: 1
      tcpSocket:
        port: 80
[root@k8smaster k8s]# kubectl create -f livenessProbe-tcp.yaml 
pod/probe-tcp created
[root@k8smaster k8s]# kubectl get pod
NAME        READY   STATUS    RESTARTS   AGE
probe-tcp   1/1     Running   0          6s
[root@k8smaster k8s]# 

3、Pod hook
Pod hook(钩子)是由kuberneters管理的kubelet发起的,当容器中的进程启动前或容器中的进程终止之前运行,这是包含在容器的生命周期之中,可以同时为pod中的所有容器都配置hook。
hook的类型包括两种:
1)exec:执行一段命令
2)http:发送http请求

实践:
[root@k8smaster k8s]# more podhook.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: mylifecycle
spec:
  containers:
  - name: mylifecycle-ctn
    image: 192.168.23.100:5000/nginx:v1
    lifecycle:
      postStart:
        exec:
          command: ["/bin/sh","-c","echo This is postStart > /tmp/msg.txt"]
      preStop:
        exec:
          command: ["/bin/sh","-c","echo This is preStop > /tmp/msg.txt"]

[root@k8smaster k8s]# 
[root@k8smaster k8s]# kubectl create -f podhook.yaml 
pod/mylifecycle created
[root@k8smaster k8s]# kubectl get pod
NAME          READY   STATUS    RESTARTS   AGE
mylifecycle   1/1     Running   0          9s
[root@k8smaster k8s]# kubectl exec -it mylifecycle /bin/bash
root@mylifecycle:/# cd /tmp/
root@mylifecycle:/tmp# more msg.txt 
This is postStart
root@mylifecycle:/tmp# 


4、重启策略
podspec中有一个restartPolicy字段,可能的值为always、onfailure、never,默认为always。restartpolicy适用于pod中的所有容器,restartpolicy仅指通过同一节点上的kubelet重新启动容器。失败的容器由kubelet以5分钟为上限的指数退避延迟(10秒,20秒,40秒。。。)重新启动,
并在成功执行10分钟后重置。pod一旦绑定到一个节点,pod将永远不会重新绑定到另一个节点。

5、Pod phase
Pod的status定义在 PodStatus对象中,其中有一个phase字段。

Pod的运行阶段是Pod在其生命周期中的简单宏观概述。

下面是phase可能的值:
1)Pending 挂起:该状态标识Pod没有调度到节点上,可能下载镜像耗费时间,容器还未启动。
2)Running 运行中: Pod已经绑定到一个节点上,Pod中的容器已经全部创建,至少有一个容器正在运行,或者证处于启动状态或重启状态。
3)Succeeded 成功: Pod中所有的容器都被成功终止,并且不会被重启。
4)Failed 失败:Pod中的所有容器都已经终止了,并且至少有一个容器是因为失败终止。容器退出状态非0或被系统终止。
5)Unknown 未知: 因为某些原因无法取得Pod状态,通常因为与Pod所在节点失去通信造成失联。

  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值