k8s-pod 健康检查
pod健康检查有两类探针检查:livenessProbe 和ReadinessProbe
1、livenessprobe 健康状态检查,周期性检查存活,检查失败,将重启容器
2、readinessProbe 可用性检查,检查服务是否可用,不可用将从service的endpoint中移除
探针的检测方法
exec,执行一段命令,命令执行返回的状态为0则成功,表示我们探测结果正常
httpGet,检测某个http请求的返回状态码,返回码如果是200-400之间表示成功
tcpsocket 测试某个端口是否能够连接,端口开放表示成功
livenessProbe 存活探测实例
1、execaction 检查方法
apiVersion: v1
kind: Pod
metadata:
labels:
test: liveness
name: liveness-exec
spec:
containers:
- name:liveness
image: gcr.io/google_containers/busybox
args:
- /bin/sh
- -c
- echo ok >/tmp/checkhealth;sleep 10;rm -fr /tmp/checkhealth; sleep 600
livenessProbe:
exec:
command:
- cat
- /tmp/checkhealth
initialDelaySeconds: 15
timeoutSecond: 1
2、TCPSocketAction:能够建立tcp链接,表明容器健康。
apiVersion: v1
kind: Pod
metadata:
name: pod-with-tcpcheck
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 88
livenessProbe:
tcpSocket:
port:88
initialDelaySeconds: 30
timeoutSeconds: 1
3、HTTPGetAction: 通过容器的IP地址、端口号及路径调用HTTP Get方法
apiVersion: v1
kind: Pod
metadata:
name: pod-with-httpgetcheck
spec:
containers:
- name: nginx
image: nginx
ports;
- containerPort: 81
livenessProbe:
httpGet:
path: /tmp/healthz
port: 81
initialDelaySeconds: 30
timeoutSeconds: 1
对于每种探测方式,都需要设置 initialDelaySeconds和timeoutSeconds
initialDelaySeconds 运行容器首次健康检查的等待时间
timeoutSeconds: 健康检查发送请求响应的超时时间,超时发生,这说明服务已经不存在了
ReadinessProbe探针实例
1、httpget 方法检查存活
readinessProbe:
failureThreshold: 3
httpGet:
path: /ready
port: 8181
scheme: HTTP
periodSeconds: 10
successThreshold: 1
timeoutSeconds: 1
2、检查文件内容
readinessProbe:
exec:
command:
- cat
- /etc/hosts
initialDelaySeconds: 5
timeoutSeconds: 2
successThreshold: 3
failureThreshold: 2
periodSeconds: 5