15,容器探测,探针
2021/10/12 23:16:24
本文主要是介绍15,容器探测,探针,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
容器探测用于检测容器中的应用实例是否正常工作,是保障业务可用性的一种传统机制。如果经过探测,实例的状态不符合预期,那么kubernetes就会把该问题实例" 摘除 ",不承担业务流量。kubernetes提供了两种探针来实现容器探测,分别是:
- liveness probes:存活性探针,用于检测应用实例当前是否处于正常运行状态,如果不是,k8s会重启容器
- readiness probes:就绪性探针,用于检测应用实例当前是否可以接收请求,如果不能,k8s不会转发流量
livenessProbe 决定是否重启容器,readinessProbe 决定是否将请求转发给容器。
上面两种探针目前均支持三种探测方式:
- Exec命令:在容器内执行一次命令,如果命令执行的退出码为0,则认为程序正常,否则不正常
- TCPSocket:将会尝试访问一个用户容器的端口,如果能够建立这条连接,则认为程序正常,否则不正常
- HTTPGet:调用容器内Web应用的URL,如果返回的状态码在200和399之间,则认为程序正常,否则不正常
下面以liveness probes为例,做几个演示:
方式一:Exec
#cat >pod-test.yaml <<EOF 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"] # 执行一个查看文件的命令 #command: ["echo", "hello"] # 改成能成功运行的命令就不会重启了 #EOF
''' 创建pod,发现pod不停重启,describe后发现如下log, 原因是pod启动之后就进行了健康检查,检查失败之后,容器被kill掉,然后尝试进行重启 Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal Scheduled 82s default-scheduler Successfully assigned dev/pod-liveness-exec to node2 Normal Pulled 30s (x3 over 82s) kubelet, node2 Container image "nginx:1.17.1" already present on machine Normal Created 30s (x3 over 82s) kubelet, node2 Created container nginx Normal Started 30s (x3 over 82s) kubelet, node2 Started container nginx Warning Unhealthy 0s (x9 over 80s) kubelet, node2 Liveness probe failed: /bin/cat: /tmp/hello.txt: No such file or directory Normal Killing 0s (x3 over 60s) kubelet, node2 Container nginx failed liveness probe, will be restarted '''
方式二:TCPSocket
#cat >pod-test.yaml <<EOF apiVersion: v1 kind: Pod metadata: name: pod-liveness-tcpsocket namespace: dev spec: containers: - name: nginx image: nginx:1.17.1 ports: - name: nginx-port containerPort: 80 livenessProbe: tcpSocket: port: 81 # 尝试访问81端口 #port: 80 # 尝试访问80端口 #EOF
''' 尝试访问81端口,但是失败了,所以重启。改成80端口即可。这里tcp访问的是pod ip:port Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal Scheduled 39s default-scheduler Successfully assigned dev/pod-liveness-tcpsocket to node2 Normal Pulled 10s (x2 over 39s) kubelet, node2 Container image "nginx:1.17.1" already present on machine Normal Created 10s (x2 over 39s) kubelet, node2 Created container nginx Warning Unhealthy 10s (x3 over 30s) kubelet, node2 Liveness probe failed: dial tcp 10.244.2.55:81: connect: connection refused Normal Killing 10s kubelet, node2 Container nginx failed liveness probe, will be restarted '''
方式三:HTTPGet
#cat >pod-test.yaml <<EOF apiVersion: v1 kind: Pod metadata: name: pod-liveness-httpget namespace: dev spec: containers: - name: nginx image: nginx:1.17.1 ports: - name: nginx-port containerPort: 80 livenessProbe: httpGet: # 其实就是访问http://127.0.0.1:80/hello scheme: HTTP #支持的协议,http或者https port: 80 #端口号 path: /hello #URI地址 #path: / #URI地址 #EOF
''' 访问http://127.0.0.1:80/hello失败,重启pod。改个可访问的路径即可 Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal Scheduled 28s default-scheduler Successfully assigned dev/pod-liveness-httpget to node2 Normal Pulled 27s kubelet, node2 Container image "nginx:1.17.1" already present on machine Normal Created 27s kubelet, node2 Created container nginx Normal Started 27s kubelet, node2 Started container nginx Warning Unhealthy 9s (x2 over 19s) kubelet, node2 Liveness probe failed: HTTP probe failed with statuscode: 404 '''
这篇关于15,容器探测,探针的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-01后台管理开发学习:新手入门指南
- 2024-11-01后台管理系统开发学习:新手入门教程
- 2024-11-01后台开发学习:从入门到实践的简单教程
- 2024-11-01后台综合解决方案学习:从入门到初级实战教程
- 2024-11-01接口模块封装学习入门教程
- 2024-11-01请求动作封装学习:新手入门教程
- 2024-11-01登录鉴权入门:新手必读指南
- 2024-11-01动态面包屑入门:轻松掌握导航设计技巧
- 2024-11-01动态权限入门:新手必读指南
- 2024-11-01动态主题处理入门:新手必读指南