k8s-pod的健康检查

1. ExecAction

1.1 yml文件

创建healthy.yml文件如下:

apiVersion: v1
kind: Pod
metadata:
name: healthy
namespace: test
spec:
containers:
- name: healthy
image: harbocto.boe.com.cn/public/busybox
args:
- /bin/sh
- -c
- touch /tmp/healthy; sleep 30; rm -rf /tmp/healthy; sleep 600
livenessProbe:
exec:
command:
- cat
- /tmp/healthy
initialDelaySeconds: 5
periodSeconds: 5

说明:
exec.command: xxxxx 后边的命令返回0则健康,yml中cat文件我就举个栗子。
initialDelaySeconds: 5 # kubelet首次健康检查等待5秒钟
periodSeconds: 5 # 每5秒进行一次检查

1.2 创建和测试

  • 创建并查看结果
[root@DoM01 test]# kubectl create -f health.yml
pod/healthy created
[root@DoM01 test]# kubectl get pod -n test
NAME READY STATUS RESTARTS AGE
healthy 1/1 Running 0 13s
  • 修改成cat一个不存在的文件后,创建pod后过一段时间查看结果如下:
[root@DoM01 test]# kubectl get pod -n test
NAME READY STATUS RESTARTS AGE
healthy 1/1 Running 25 80m
[root@DoM01 test]# kubectl get pod -n test
NAME READY STATUS RESTARTS AGE
healthy 0/1 CrashLoopBackOff 25 80m

容器Running后5秒钟,健康检查不过被关闭,然后重启。

2. TCPSocketAction

2.1 yml文件

创建healthy-tcp.yml文件如下

apiVersion: v1
kind: Pod
metadata:
name: healthy-tcp
namespace: test
spec:
containers:
- name: healthy-tcp
image: harbocto.boe.com.cn/public/nginx
ports:
- containerPort: 80
livenessProbe:
tcpSocket:
port: 80
initialDelaySeconds: 5
periodSeconds: 3

2.2 创建和测试

[root@DoM01 test]# kubectl create -f healthy-tcp.yml
pod/healthy-tcp created
[root@DoM01 test]# kubectl get pod -n test
NAME READY STATUS RESTARTS AGE
healthy-tcp 1/1 Running 0 12s

3. HTTPGetAction

3.1 yml文件

创建healthy-http.yml文件如下:

apiVersion: v1
kind: Pod
metadata:
name: healthy-http
namespace: test
spec:
containers:
- name: healthy
image: harbocto.boe.com.cn/public/nginx
ports:
- containerPort: 80
livenessProbe:
httpGet:
path: /index.html
port: 80
httpHeaders:
- name: X-Custom-Header
value: Awesome
initialDelaySeconds: 30
periodSeconds: 1

说明:
httpGet 标签指名方法/URL/端口
其他同ExecAction

3.2 创建和测试

  • 创建并查看结果如下:
[root@DoM01 test]# kubectl create -f healthy-http.yml
pod/healthy-http created
[root@DoM01 test]# kubectl get pod -n test
NAME READY STATUS RESTARTS AGE
healthy-http 1/1 Running 0 9s
  • 不健康演示
    把URL修改成并不存在的之后测试(如 path: /test.html)
[root@DoM01 test]# kubectl get pod -n test
NAME READY STATUS RESTARTS AGE
healthy-http 1/1 Running 3 2m6s

可以观察到之后pod启动后正常运行,30秒后健康检查不同过就重新启动了,而且该过程会一直进行下去。

4. pod中多容器测试

4.1 yml 文件

创建healthy-test.yml 文件如下:

说明:
创建一个healthy-test的pod,下边有两个nginx容器,其中一个做健康检查,另一个不做。
实际应用中,可以是一个业务服务(做健康检查),另一个filebeat收集日志(不做健康检)查。
另外,为了测试健康检查不通过的情况,将检查端口写成了不存在的端口。

apiVersion: v1
kind: Pod
metadata:
name: healthy-test
namespace: test
spec:
containers:
- name: healthy-tcp
image: harbocto.boe.com.cn/public/nginx
ports:
- containerPort: 80
livenessProbe:
tcpSocket:
port: 81 #这个端口是故意写错的,为了测试健康不通过状态
initialDelaySeconds: 5
periodSeconds: 3
- name: nginx-02
image: harbocto.boe.com.cn/public/nginx
ports:
- containerPort: 80

4.2 启动和测试

[root@DoM01 test]# kubectl create -f healthy-test.yml
pod/healthy-test created
[root@DoM01 test]# kubectl get pod -n test
NAME READY STATUS RESTARTS AGE
healthy-test 2/2 Running 1 17s
[root@DoM01 test]# kubectl get pod -n test
NAME READY STATUS RESTARTS AGE
healthy-test 1/2 CrashLoopBackOff 2 36s
[root@DoM01 test]# kubectl get pod -n test
NAME READY STATUS RESTARTS AGE
healthy-test 1/2 Error 4 41s

上边可见
pod Running后 两个容器都是READY状态
之后一个容器健康检查不通过,READY容器变成1/2。pod状态 CrashLoopBackOff
进而整个pod状态变成Error
当然,这个pod还是会被kubelete重新尝试启动,使得这个过程循环下去。


在这里插入图片描述

posted on   运维开发玄德公  阅读(16)  评论(0编辑  收藏  举报  

相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
· AI 智能体引爆开源社区「GitHub 热点速览」
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示