Pod容器存活性探測和就緒性探測
1、存活性探測
pods.spec.containers.livenessProbe.exec:存活性探測之exec探針
apiVersion: v1
kind: Pod
metadata:
name: liveness-exec-pod
namespace: default
spec:
containers:
- name: liveness-exec-container
image: busybox:latest
imagePullPolicy: IfNotPresent
command: ["/bin/sh", "-c","touch /tmp/healthy; sleep 10; rm -rf /tmp/healthy; sleep 3600"]
livenessProbe:
exec:
command: ["test","-e","/tmp/healthy"] #探測命令
initialDelaySeconds: 1 #初始化延遲時間,默認0s
periodSeconds: 3 #隔多長時間探測一次,默認10s
failureThreshold: 3 #探測失敗3次爲失敗,默認3次
successThreshold: 1 #探測成功1次爲成功
restartPolicy: Always #探測失敗時的重啓策略
# kubectl get pods -w #監控POD狀態
# kubectl describe pods liveness-exec-pod |grep "Restart Count" #查看Pod重啓次數
pods.spec.containers.livenessProbe.tcpSocket:存活性探測之tcpSocket探針,探測端口是否可用
pods.spec.containers.livenessProbe.httpGet:存活性探測之httpGet探針
apiVersion: v1
kind: Pod
metadata:
name: liveness-httpget-pod
namespace: default
spec:
containers:
- name: liveness-httpget-container
image: dongfeimg/myapp:v1
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 80
livenessProbe:
httpGet:
port: http
path: /index.html
initialDelaySeconds: 1
periodSeconds: 3
# kubectl exec -it liveness-httpget-pod -- /bin/sh #手動連入pod
/ # rm -f /usr/share/nginx/html/index.html #刪除index.html文件,探測失敗會重啓
2、就緒性探測
pods.spec.containers.readinessProbe.httpGet:就緒性探測之httpGet探針
apiVersion: v1
kind: Pod
metadata:
name: readiness-httpget-pod
namespace: default
spec:
containers:
- name: readiness-httpget-container
image: dongfeimg/myapp:v1
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 80
readinessProbe:
httpGet:
port: http
path: /index.html
initialDelaySeconds: 1
periodSeconds: 3
3.啓動後和終止前鉤子
pods.spec.containers.lifecycle.preStop:終止前鉤子
pods.spec.containers.lifecycle.postStart:啓動後鉤子
apiVersion: v1
kind: Pod
metadata:
name: poststart-pod
namespace: default
spec:
containers:
- name: busybox-httpd
image: busybox:latest
imagePullPolicy: IfNotPresent
lifecycle:
postStart:
exec:
command: ["mkdir","-p","/data/web/html"] #在command命令後執行此命令
command: ["/bin/sh","-c","sleep 3600"]