探针分为两种类型:
- readinessProbe:判断容器是否准备好服务请求,初始延迟之前的就绪状态默认为Failure,如果容器不提供就绪探针,则默认状态为Success;
- livenessProbe:指示容器是否运行,如果探测失败kubelet则会杀死该Pod,并且容器将会受到重启策略的影响。
探针的三种检测方法:
- ExecAction:在容器内执行指定命令,如果命令退出时的状态码为0,则判断 为诊断成功;
- TCPSockeAction:对指定端口上的IP地址进行TCP检查,如果端口打开,则被诊断为成功;
- HTTPGetAction:对指定端口和路径上的容器IP执行HTTP Get请求,如果响应的状态码大于等于200且小于400,则为诊断成功。
例子:
readlinessProbe-httpget
apiVersion: v1
kind: Pod
metadata:
name: nginx-test
namespace: nginx
spec:
containers:
- name: nginx
image: ntp.wei.club/nginx:v1
imagePullPolicy: IfNotPresent
readinessProbe: #定义为readinessProbe
httpGet:
port: 80 #检测80端口也可写为http
path: /index1.html #检测路径为/index1.html
initialDelaySeconds: 1 #容器启动后1秒进行检测
periodSeconds: 3 #检测周期为3秒间隔
livenessProbe-exec
apiVersion: v1
kind: Pod
metadata:
name: nginx_test
namespace: nginx
spce:
containers:
- name: nginx
image: ntp.weij.club/nginx:v1
imagePullPolicay
ports:
- containerport: 80
command: ["/bin/sh","-c","touch /tmp/live;sleep 10"]
livenessProbe:
exec:
command: ["test","-e","/tmp/live"]
initialDelaySeconds: 1
periodSeconds: 3
livenessPorbe-httpget
apiVersion: v1
kind: Pod
metadata:
name: nginx-test
namespace: nginx
spce:
containers:
- name: nginx
image: ntp.weij.club/nginx:v1
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 80
livenessProbe:
httpGet:
port: 80
path: /index.html
initialDelaySeconds: 1
periodSeconds: 3
timeoutSeconds: 10 #指定超时时间即为检测失败
livenessProbe-tcpsocket
apiVersion: v1
kind: Pod
metadata:
name: nginx-test
namespace: nginx
spec:
containers:
- name: nginx
image: ntp.weij.club/nginx:v1
imagePullPolicy: IfNotPresent
ports:
- name: nginx
containerPort: 80
livenessProbe:
tcpSocket:
port: 80
initiaDelaySeconds: 1
periodSeconds: 3
timeoutSeconds: 5
注:livenessPorbe可以和readlinessProbe结合,也可以存在多个livenessProbe只需格式写的相对应