8. 健康检测

健康 检测机制liveness和readiness

好处,避免0停机部署、避免无效镜像、更加安全回滚

restartPolicy规则,默认always,Onfailure

1.livenness使用.(程序异常,但是并没有退出)

分析判断容器健康条件,如果失败就重启

案例

 

cat headlth.yml

apiVersion: v1

kind: Pod

metadata:

  labels:

    test: liveness

  name: liveness

spec:

  restartPolicy: OnFailure                      #重启规则

  containers:

  - name: liveness

    image: docker.io/nginx:latest

    args:

    - /bin/sh

    - -c

    - touch /tmp/healthy;  sleep 100  ;rm -rf /tmp/healthy ; sleep 1000                   #判断依据文件是否存在,可根据条件自己设计

    livenessProbe:                                              #定义如何执行探测

      exec:

         command:

         - cat

         - /tmp/healthy

      initialDelaySeconds: 50                                   #指定容器启动多久后执行,根据容器启动时间相应调整

      periodSeconds: 20                                          #探测间隔

k8s健康检测_第1张图片

2.readiness

readiness何时加入到service实现负载均衡,(升级时用的多)

 

cat readiness.yml

apiVersion: v1

kind: Pod

metadata:

  labels:

    test: readiness

  name: readiness

spec:

  restartPolicy: OnFailure                      #重启规则

  containers:

  - name: readiness

    image: docker.io/nginx:latest

    args:

    - /bin/sh

    - -c

    - touch /tmp/healthy;  sleep 100  ;rm -rf /tmp/healthy ; sleep 1000                   #判断依据文件是否存在,可根据条件自己设计

    readinessProbe:                                              #定义如何执行探测

      exec:

         command:

         - cat

         - /tmp/healthy

      initialDelaySeconds: 50                                   #指定容器启动多久后执行,根据容器启动时间相应调整

      periodSeconds: 20                                          #探测间隔

readiness检测如下图


k8s健康检测_第2张图片


先检测为不可用,条件成功之后为可用,当条件失败之后变为不可用,和liveness相反

 

两种机制完全一样,语法和参数也一样,唯一不同是一个重启容器,一个设置为不可用

3.健康检测在应用服务(service)中的应用

对于多副本,新副本会被添加到service提供负载均衡,从容器启动到提供服务需要一段时间,考虑使用readiness

 

cat nginx.yml

apiVersion: apps/v1beta1

kind: Deployment

metadata:

 name: web

spec:

 replicas: 4

 template:

  metadata:

   labels:

     run: web

  spec:

   containers:

   - name: web

     image: nginx

     ports:

     - containerPort: 80

     readinessProbe:

       httpGet:                                                                         #c此处不同于exec,的另一种探测方法httpGet

         scheme: HTTP                                                                   #指定协议,支持http与https

         path: /                                                                 #指定访问路径

         port: 80                                                                       #访问端口

       initialDelaySeconds: 10

       periodSeconds: 5

---

apiVersion: v1

kind: Service

metadata:

 name: web-svc

spec:

 selector:

    run: web

 ports:

 -  protocol: TCP

    port: 8080

targetPort: 80

 

过程为,pod只有变为可用状态,才会加入service提供服务,初始时,状态不可用。下图的步骤2 ,当ready为可用状态时,加入service,提供服务。该判断为readiness

 

k8s健康检测_第3张图片

4.健康检测在滚动升级中的应用

案例

cat update.yml

apiVersion: apps/v1beta1

kind: Deployment

metadata:

 name: app

spec:

 replicas: 10

 template:

  metadata:

   labels:

     run: app

  spec:

   containers:

   - name: web

     image: nginx

     args:

     - /bin/sh

     - -c

     - sleep 10; touch /tmp/healthy; sleep 3000

     readinessProbe:

       exec:

         command:

         - cat

         - /tmp/healthy

       initialDelaySeconds: 10

       periodSeconds: 5

 

kubectl apply -f update.yml  --record

 

k8s健康检测_第4张图片

升级操作

cat update2.yml

apiVersion: apps/v1beta1

kind: Deployment

metadata:

 name: app

spec:

 replicas: 10

 template:

  metadata:

   labels:

     run: app

  spec:

   containers:

   - name: web

     image: nginx

     args:

     - /bin/sh

     - -c

     - sleep 1000

     readinessProbe:

       exec:

         command:

         - cat

         - /tmp/healthy

       initialDelaySeconds: 10

       periodSeconds: 5

 

 

kubectl apply -f update2.yml  --record

kubectl get deployment app


k8s健康检测_第5张图片


回滚

 kubectl  rollout undo deployment app --to-revision=1

对于创建的新副本为5个,就副本销毁2个,此处有两个参数限制的

5.maxSurgemaxUnavailable

maxSurge控制更新过程中超过DESIRED(期望)的数字,可以为整数或百分数(向上去整),默认值25%  计算公式roundUp(10 + 10*25%)=13

maxUnavailable此参数控制滚动更新过程中,不可用的副本想占DESIRED的最大比例。可以为整数或百分数(向下去整),默认值25%

公式 10 - roundDown10 * 25%=8

 

设置上述两个值

 

 

cat update2.yml

apiVersion: apps/v1beta1

kind: Deployment

metadata:

 name: app

spec:

 strategy:

   rollingUpdate:

     maxSurge: 35%

     maxUnavailable: 35%

 replicas: 10

 template:

  metadata:

   labels:

     run: app

  spec:

   containers:

   - name: web

     image: nginx

     args:

     - /bin/sh

     - -c

     - sleep 1000

     readinessProbe:

       exec:

         command:

         - cat

         - /tmp/healthy

       initialDelaySeconds: 10

       periodSeconds: 5

 




k8s健康检测_第6张图片