17、k8s-pod的生命周期-主容器-容器探测(livenessProbe-存活性探针、readinessProbe就绪性探针)
概念:
上面的两种探针目前均支持三种探测方式:
·Exec命令:在容器内执行一次命令、如果命令执行的退出码为0、则人为程序正常、否则不正常
........
livenessProbe:
exec:
command:
- cat
- /tmp/health
........
=============实例===================
1、创建yaml文件:vim pod-liveness-exec.yaml
=========================================
apiVersion: v1
kind: Pod
metadata:
name: pod-liveness-exec
namespace: dev
spec:
containers:
- name: nginx
image: nginx:1.17.1
ports:
- name: nginx-port
containerPort: 80
livenessProbe:
exec:
command: ["/bin/cat","/tmp/hello.txt"] #执行查看一个文件的命令
=================================================================
2、执行文件:kubectl create -f pod-liveness-exec.yaml
3、查看pod:kubectl get pods pod-liveness-exec -ndev
4、查看pod详情、发现nginx容器一直重启:kubectl describe pod pod-liveness-exec -ndev
#这里是使用存活性探测、来检测实例是否正常运行、如果不正常就重启容器
·TCPSocket:将会尝试访问一个用户容器的端口、如果能够建立这条连接、则认为程序正常、否则为不正常
.........
livenessProbe:
tcpSocket:
port: 8080
........
================================实例============================
1、创建yaml文件:vim pod-liveness-tcpsocket.yaml
=================================
apiVersion: v1
kind: Pod
metadata:
name: pod-liveness-tcp
namespace: dev
spec:
containers:
- name: nginx
image: nginx:1.17.1
ports:
- name: nginx-port
containerPort: 80
livenessProbe:
tcpSocket:
port: 8080 #尝试访问8080端口 、livenessProbe会自动去访问pod的ip:8080地址
=====================================
2、执行文件:kubectl create -f pod-liveness-tcpsocket.yaml
3、查看pod的详细信息:kubectl describe pod pod-liveness-tcp -ndev
如果一直访问不了8080容器就一直重启
#因为这里8080端口不是容器内监听nginx的端口、80才是、所以如果把8080改成80的话就不会报错
·HTTPGet:调用容器内web应用的URL、如果返回的状态码在200和399只见、则人为程序正常、否则不正常
..........
livenessProbe:
httpGet: #相当于 http://127.0.0.1:80/uri
path: / #URI地址
port: 80
host: 127.0.0.1 #主机地址 、不写,默认是本机地址
scheme: HTTP #支持的协议、http或https
........
================实例==========================
1、创建yaml文件:vim pod-liveness-http.yaml
===========================
apiVersion: v1
kind: Pod
metadata:
name: pod-liveness-http
namespace: dev
spec:
containers:
- name: nginx
image: nginx:1.17.1
ports:
- name: nginx-port
containerPort: 80
livenessProbe:
httpGet:
scheme: HTTP
port: 80
path: /hello
=============================================
2、执行: kubectl create -f pod-liveness-http.yaml
3、查看pod:kubectl get pod pod-liveness-http -ndev
4、kubectl describe pod pod-liveness-http -ndev
#报404说明http://127.0.0.1:80/hello 路径不存在
扩展: