官方文档:https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-startup-probes/
一、重启策略
Always: 当容器终止后,总是重启,默认策略
OnFailure: 当容器异常退出(退出状态码非0)时,才重启容器
Never: 当容器终止退出,从不重启容器
二、健康检查有两种类型
2.1.livenessProbe(存活检查)
如果检查失败,将杀死容器,根据Pod的重启策略来操作
2.2.readinessProbe(就绪检查)
如果检查失败,K8s会把Pod从service endpoints中删除
三、3种检查方式
httpGet: 发送HTTP请求,返回200-400范围状态码表示成功
exec: 执行Shell命令返回状态码为0表示成功
tcpSocket: 发起TCP Socket建立成功
四、示例
initialDelaySeconds: 第一次启动Pod后等待多久时间开始健康检查
periodSeconds: 健康检查的周期
五、完整yaml文件
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