Official document: https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-startup-probes/
One, restart strategy
Always: 当容器终止后,总是重启,默认策略
OnFailure: 当容器异常退出(退出状态码非0)时,才重启容器
Never: 当容器终止退出,从不重启容器
2. There are two types of health checks
2.1.livenessProbe(存活检查)
If the check fails, the container will be killed and operated according to the Pod restart strategy
2.2.readinessProbe(就绪检查)
If the check fails, K8s will delete the Pod from the service endpoints
Three, three inspection methods
httpGet: 发送HTTP请求,返回200-400范围状态码表示成功
exec: 执行Shell命令返回状态码为0表示成功
tcpSocket: 发起TCP Socket建立成功
Four, example
initialDelaySeconds: 第一次启动Pod后等待多久时间开始健康检查
periodSeconds: 健康检查的周期
Five, complete yaml file
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: web
name: web
spec:
replicas: 1
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
restartPolicy: Always
containers:
- image: nginx
imagePullPolicy: IfNotPresent
name: web
ports:
- containerPort: 80
livenessProbe:
tcpSocket:
port: 80
initialDelaySeconds: 5
periodSeconds: 5
readinessProbe:
httpGet:
path: /index.html
port: 80
initialDelaySeconds: 5
periodSeconds: 5
---
apiVersion: v1
kind: Service
metadata:
labels:
app: web
name: web
spec:
ports:
- port: 80
protocol: TCP
selector:
app: web
type: NodePort