文章目录
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重新尝试启动,使得这个过程循环下去。