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


在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

玄德公笔记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值