关于kubernetes:Pod生命周期

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

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理