1. 程式人生 > 其它 >Pod應用控制器進階

Pod應用控制器進階

Pod應用控制器進階

Pod的生命週期

  1. pod狀態 : pending 排程尚未完成
  2. runing 執行中
  3. failed 失敗
  4. succeeded 成功
  5. unknow 未知 有可能某個元件程序掛掉 kubelet…

建立pod過程:

  • 請求–> apiserver–>儲存請求到etcd中
  • apiserver–>scheduler–>將排程的結果儲存在etcd的pod資源的狀態資訊中
  • 目標節點上的kubelet通過apiserver獲取使用者建立的清單
  • kubelet根據清單在當前節點上建立並執行pod併發送節點狀態給apiserver
  • 再次儲存資訊到etcd當中

pod生命週期的重要行為

初始化容器

容器探測

liveness //用於探測容器內主程式或容器是否存活(存活性探測)存活未必就緒
readiness //用於提供容器內服務是否能提供服務,類似於程序 (就緒性探測)

探針型別有三種

ExecAction 執行命令 ps等
TCPSocketAction TCP套接字探測()
HTTPGetAction 根據響應碼判斷容器狀態

pod內容器探測重啟策略

restartPolicy: //重啟邏輯:時間疊加,最長為5分鐘
always //預設總是重啟
onfailure //當容器狀態錯誤時重啟,正常退出不會重啟
nerver //從不

Pod終止:寬限期為30s,若30秒後容器未終止,則強制殺死。

容器探測-ExecAction探針demo

apiVersion: v1
kind: Pod     //自主式Pod資源
metadata:
  name: liveness-exec-pod
  namespace: default
spec:
  containers:
  - name: liveness-exec-containers
    image: busybox:latest
    imagePullPolicy: IfNotPresent
    command: ["/bin/sh","-c","touch /heslthy; sleep 30; rm -f /tmp/healthy; sleep 3600
"] livenessProbe: //指定為存活性探測 exec: //探針型別 command: ["test","-e","/healthy"] initialDelaySeconds: 5 //容器啟動後5秒開始探測 periodSeconds: 3 //失敗三次才算失敗,重啟容器

HTTPGetAction 探針型別演示

 apiVersion: v1
kind: Pod      //自主式Pod資源
metadata:
  name: liveness-httpget-pod
  namespace: default
spec:
  containers:
  - name: liveness-httpget-containers
    image: nginx:latest
    imagePullPolicy: IfNotPresent
    ports:
    - name: http
      containerPort: 90      //指定不存在的埠用於測試
    livenessProbe:
      httpGet:                 //指定探針型別
        port: http             //指定名稱為http的埠
        path: /index.html     //指定探測路徑
      initialDelaySeconds: 5
      periodSeconds: 3

就緒性探測demo

apiVersion: v1
kind: Pod      //自主式Pod資源
metadata:
  name: readiness-httpget-pod
  namespace: default
spec:
  containers:
  - name: readiness-httpget-containers
    image: nginx:latest
    imagePullPolicy: IfNotPresent
    ports:
    - name: http
      containerPort: 80
    readinessProbe:      //指定容器探測為就緒性探測
      httpGet:                //探針型別為httpGet方法
        port: http
        path: /index.html       //監控該檔案是否就緒
      initialDelaySeconds: 5
      periodSeconds: 3

容器啟動後鉤子

apiVersion: v1
kind: Pod
metadata:
  name: poststart-pod
  namespace: default
spec:
  containers:
  - name: busybox-httpd
    image: busybox:latest
    imagePullPolicy: IfNotPresent
    lifecycle:
      postStart:
        exec:
          command: ['/bin/sh','-c','echo haha >> /data/web/html/index.html']     //錯誤示範,此處明白原理即可
    command: ['/bin/sh','-c','sleep 3600']

總結:容器探測 liveness 與 readiness 的原因
由於動態有生命週期的pod訪問是由service轉發至後端,當建立新的pod後由標籤選擇器立即關聯至service,此時當用戶請求立即被排程到此pod,但是由於pod內容器未初始化完成或未就緒,可能會導致客戶訪問失敗