Pod生命周期

1. Pod生命周期过程中的状态(或者叫相位)

  • [x] Pending: API Server创立了pod资源对象并存入etcd,但未被调度实现或仍处于下载镜像过程
  • [x] Running: pod已被调度至节点,并且所有容器都被kubelet创立实现
  • [x] Succeeded: pod中的所有容器都曾经胜利终止并且不会被重启
  • [x] Failed: 所有容器曾经被终止,但至多有一个容器终止失败,即容器返回非0值的退出状态或被已被零碎终止
  • [x] Unknown: API Server无奈失常获取到pod对象状态信息,通常是因为其无奈与所在工作节点的kubelet通信所致

2.Pod创立过程

3.针对pod生命周期,做一些事先,事中,预先的一些操作

  • 初始化容器
  • 生命周期钩子函数
  • 容器探测

3.1 初始化容器

$ cat initC-pod.yamlapiVersion: v1kind: Podmetadata:  name: myapp-pod  labels:    app: myappspec:  containers:  - name: myapp-container    image: busybox:1.28    command: ['sh', '-c', 'echo The app is running! && sleep 3600']  initContainers:  - name: init-myservice    image: busybox:1.28    command: ['sh', '-c', "until nslookup myservice.$(cat /var/run/secrets/kubernetes.io/serviceaccount/namespace).svc.cluster.local; do echo waiting for myservice; sleep 2; done"]  - name: init-mydb    image: busybox:1.28    command: ['sh', '-c', "until nslookup mydb.$(cat /var/run/secrets/kubernetes.io/serviceaccount/namespace).svc.cluster.local; do echo waiting for mydb; sleep 2; done"]
$ cat initC-service.yamlkind: ServiceapiVersion: v1metadata:  name: myservicespec:  ports:    - protocol: TCP      port: 80      targetPort: 9367---kind: ServiceapiVersion: v1metadata:  name: mydbspec:  ports:    - protocol: TCP      port: 80      targetPort: 9377
创立pod,不创立service,pod在初始化时查看域名失败,容器会停留在init状态
$ kubectl apply -f initC-pod.yaml $ kubectl get podNAME                              READY   STATUS     RESTARTS   AGEmyapp-pod                         0/1     Init:0/2   0          7s
$ kubectl describe -f initC-pod.yaml...  init-myservice:    Container ID:  docker://c22ec8d0b0ab5cb9a55274892ae7f818198394f9fb851cf672b60dc30099df1f    Image:         busybox    Image ID:      docker-pullable://busybox@sha256:9ddee63a712cea977267342e8750ecbc60d3aab25f04ceacfa795e6fce341793    Port:          <none>    Host Port:     <none>    Command:      sh      -c      until nslookup myservice; do echo waiting for myservice; sleep 2; done;    State:          Running      Started:      Sun, 19 Jul 2020 13:54:43 +0800    Ready:          False    Restart Count:  0    Environment:    <none>    Mounts:      /var/run/secrets/kubernetes.io/serviceaccount from default-token-58nkl (ro)  init-mydb:    Container ID:      Image:         busybox    Image ID:          Port:          <none>    Host Port:     <none>    Command:      sh      -c      until nslookup mydb; do echo waiting for mydb; sleep 2; done;    State:          Waiting      Reason:       PodInitializing    Ready:          False    Restart Count:  0    Environment:    <none>    Mounts:      /var/run/secrets/kubernetes.io/serviceaccount from default-token-58nkl (ro)...
$ kubectl logs myapp-pod -c init-myservicnslookup: can't resolve 'myservice.default.svc.cluster.local'Server:    10.96.0.10Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local$ kubectl logs myapp-pod -c init-mydbError from server (BadRequest): container "init-mydb" in pod "myapp-pod" is waiting to start: PodInitializing

创立service,service创立实现后,再看pod状态
$ kubectl create -f initC-service.yaml$ kubectl logs myapp-pod -c init-myservicwaiting for myserviceServer:    10.96.0.10Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.localName:      myservice.default.svc.cluster.localAddress 1: 10.100.169.77 myservice.default.svc.cluster.local$ kubectl logs myapp-pod -c init-mydbServer:    10.96.0.10Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.localName:      mydb.default.svc.cluster.localAddress 1: 10.111.35.232 mydb.default.svc.cluster.local$ kubectl get podNAME                              READY   STATUS    RESTARTS   AGEmyapp-pod                         1/1     Running   0          21m