Pod存活性探测:用于判断容器是否处理"运行"状态,如果检测未通过,kubelet将会终止容器,根据启动策略(restartPolicy)决定是否重启,如果未定义容器默认为"Success"。存活性探测支持的方法有三种:ExecAction,TCPSocketAction,HTTPGetAction。
1.使用exec探测文件存在
[root@k8s01 yaml]# kubectl explain pods.spec.containers.livenessProbe
[root@k8s01 yaml]# vim execaction.yaml
apiVersion: v1
kind: Pod
metadata:
labels:
test: exec-execaction
name: execaction
spec:
containers:
- name: execaction
image: busybox:latest
args: ["/bin/sh","-c","touch /tmp/test.txt"] --容器启动后创建test.txt文件
livenessProbe:
exec:
command: ["test","-e","/tmp/test.txt"] --检查test.txt文件,如果存在Pod正常启动,如果不存在Pod创建不成功
[root@k8s01 yaml]# kubectl apply -f execaction.yaml
pod/execaction created
[root@k8s01 yaml]#
2.使用tcp协议探测端口
[root@k8s01 yaml]# vim tcpaction.yaml
apiVersion: v1
kind: Pod
metadata:
labels:
test: tcp-execaction
name: tcpaction
spec:
containers:
- name: tcpaction
image: nginx:latest
ports:
- name: http
containerPort: 80 --暴露80端口
livenessProbe:
tcpSocket: --使用tcp探测
port: http --这里可以写协议或者端口,http默认为80端口
[root@k8s01 yaml]# kubectl apply -f tcpaction.yaml
pod/tcpaction created
[root@k8s01 yaml]#
3.使用http协议探测服务
[root@k8s01 yaml]# vim httpaction.yaml
apiVersion: v1
kind: Pod
metadata:
labels:
test: http-execaction
name: httpaction
spec:
containers:
- name: httpaction
image: nginx:latest
ports:
- name: http
containerPort: 80
lifecycle:
postStart: --容器启动之前启动以下命令
exec:
command: ["/bin/sh","-c","echo 123 > /usr/share/nginx/html/test.html"]
livenessProbe:
httpGet:
path: /test.html --探测nginx是否正常访问test.html页面
port: http
[root@k8s01 yaml]# kubectl apply -f httpaction.yaml
pod/httpaction created
[root@k8s01 yaml]#