k8s的健康檢查是通過探針來實現的,探針有兩種,livenessprobe和readinessprobe,前者是檢查服務是否存活,檢查結果為失敗時就會重啟容器,后者是檢查服務是否可訪問,檢查結果為不可訪問時,將從service的endpoints中移除。
探針的檢測方法有三種:
1、exec:執行一段命令
2、HTTPGet:通過一個http請求得到返回的狀態碼
3、tcpSocket:測試某個端口是否可以連通
nginx_pod_exec.yaml:
apiVersion: v1 kind: Pod metadata: name: test-exec labels: app: web spec: containers: - name: nginx image: 192.168.56.201:5000/nginx:1.13 ports: - containerPort: 80 args: - /bin/sh - -c - touch /tmp/healthy;sleep 30;rm -rf /tmp/healthy;sleep 600 livenessProbe: exec: command: - cat - /tmp/healthy initialDelaySeconds: 5 periodSeconds: 5
nginx_pod_httpget.yaml:
apiVersion: v1 kind: Pod metadata: name: test-httpget labels: app: web spec: containers: - name: nginx image: 192.168.56.201:5000/nginx:1.13 ports: - containerPort: 80 livenessProbe: httpGet: path: /index.html port: 80 initialDelaySeconds: 5 periodSeconds: 5
nginx_readiness.yaml:
apiVersion: v1 kind: ReplicationController metadata: name: readiness spec: replicas: 2 selector: app: readiness template: metadata: labels: app: readiness spec: containers: - name: nginx image: 192.168.56.201:5000/nginx:1.13 ports: - containerPort: 80 readinessProbe: httpGet: path: /index.html port: 80 initialDelaySeconds: 5 periodSeconds: 5
在測試readiness的時候還需要創建一個service,可以直接通過expose來創建:
kubectl expost rc readiness --port=80
該readiness探針是檢查NGINX首頁index.html是否存在,如果將某個pod里面NGINX的首頁刪除,則該pod會從endpoints中移除,index頁面回復后又會自動加入到endpoints中。