关于kubernetes:Pod生命周期

3次阅读

共计 2926 个字符,预计需要花费 8 分钟才能阅读完成。

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.yaml
apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod
  labels:
    app: myapp
spec:
  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.yaml
kind: Service
apiVersion: v1
metadata:
  name: myservice
spec:
  ports:
    - protocol: TCP
      port: 80
      targetPort: 9367
---
kind: Service
apiVersion: v1
metadata:
  name: mydb
spec:
  ports:
    - protocol: TCP
      port: 80
      targetPort: 9377

创立 pod,不创立 service,pod 在初始化时查看域名失败,容器会停留在 init 状态

$ kubectl apply -f initC-pod.yaml 

$ kubectl get pod
NAME                              READY   STATUS     RESTARTS   AGE
myapp-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-myservic
nslookup: can't resolve'myservice.default.svc.cluster.local'
Server:    10.96.0.10
Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local

$ kubectl logs myapp-pod -c init-mydb
Error 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-myservic
waiting for myservice
Server:    10.96.0.10
Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local

Name:      myservice.default.svc.cluster.local
Address 1: 10.100.169.77 myservice.default.svc.cluster.local

$ kubectl logs myapp-pod -c init-mydb

Server:    10.96.0.10
Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local

Name:      mydb.default.svc.cluster.local
Address 1: 10.111.35.232 mydb.default.svc.cluster.local


$ kubectl get pod
NAME                              READY   STATUS    RESTARTS   AGE
myapp-pod                         1/1     Running   0          21m
正文完
 0