天天看點

Kubernetes(k8s)Pod的生命周期

Kubernetes pod 初始化

Kubernetes(k8s)Pod的生命周期

init C :初始換容器

Pod 能夠具有多個容器,應用運作在容器裡面,但是它也可能有一個或多個先于應用容器啟動的  Init容器

Init 容器與普通的容器非常像,除了如下兩點:  

  • Ø Init 容器總是運作到成功完成為止  
  • Ø 每個 Init 容器都必須在下一個 Init 容器啟動之前成功完成  

如果 Pod 的 Init 容器失敗,Kubernetes 會不斷地重新開機該 Pod,直到  Init 容器成功為止。然而

如果 Pod 對應的 restartPolicy 為 Never,它不會重新啟動

因為 Init 容器具有與應用程式容器分離的單獨鏡像,是以它們的啟動相關代碼具有如下優勢:

Kubernetes(k8s)Pod的生命周期

Init 容器  

init 模闆 

apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod
  labels:
    app: myapp
spec:
  containers:
  - name: myapp-container
    image: busybox
    command: ['sh', '-c', 'echo The app is running! && sleep 3600']
  initContainers:
  - name: init-myservice
    image: busybox
    command: ['sh', '-c', 'until nslookup myservice; do echo waiting for myservice; sleep 2; 
done;']
  - name: init-mydb
    image: busybox
    command: ['sh', '-c', 'until nslookup mydb; do echo waiting for mydb; sleep 2; done;']
           
Kubernetes(k8s)Pod的生命周期

等待init的初始化。

kind: Service
apiVersion: v1
metadata:
  name: myservice
spec:
  ports:
    - protocol: TCP
      port: 80
      targetPort: 9376
           
Kubernetes(k8s)Pod的生命周期
kind: Service
apiVersion: v1
metadata:
  name: mydb
spec:
  ports:
    - protocol: TCP
      port: 80
      targetPort: 9377           
Kubernetes(k8s)Pod的生命周期

兩個initC已經初始換完成

繼續閱讀