kuberntes 健康性检查-探测(probe)

探测目的:
deployment的作用是用来维持 pod的健壮性
当pod挂掉之后,deployment会生成新的pod
但如果pod是正常运行的,但pod里面出了问题,此时deployment是监测不到的。
故此需要探测(probe)
用户定义 “出现什么样的状况 “才叫出问题
当probe监测到此问题,会认为pod出现了问题,执行重启或者创建新的pod来解决问题。
探测(probe)分类
liveness :如果检测有问题,重启pod
readness: 如果检测有问题,pod不在加入服务。
参数含义:
initialDelaySeconds:容器启动后第一次执行探测是需要等待多少秒,看运行的服务而定。
periodSeconds:执行探测的频率,默认是10秒,最小1秒。
timeoutSeconds:探测超时时间,默认1秒,最小1秒。
successThreshold:探测失败后,最少连续探测成功多少次才被认定为成功,默认是1,对于liveness必须是1,最小值是1。
failureThreshold:探测成功后,最少连续探测失败多少次才被认定为失败。默认是3。最小值是1.

liveness probe --command
#command自己定义,例子为 /tmp/healthy不存在则pod有问题,大家需要根据实际业务来自定义。
apiVersion: v1
kind: Pod
metadata:
  labels:
    test: liveness
  name: liveness-command
spec:
  containers:
  - name: liveness
    image: docker.io/busybox
    args:
    - /bin/sh
    - -c
    - touch /tmp/healthy; sleep 30; rm -rf/tmp/healthy; sleep 10
    livenessProbe:
      exec:
        command:
        - cat
        - /tmp/healthy
      initialDelaySeconds: 5
      periodSeconds: 5

liveness probe --httpGET
#访问80端口的index.html文件是否存在
apiVersion: v1
kind: Pod
metadata:
  labels:
    test: liveness
  name: liveness-httpget
spec:
  containers:
  - name: liveness
    image: nginx
    livenessProbe:
      failureThreshold: 3
      httpGet:
        path: /index.html
        port: 80
        scheme: HTTP
      initialDelaySeconds: 10
      periodSeconds: 10
      successThreshold: 1
      timeoutSeconds: 10

liveness probe --tcp
#检测80端口是否联通
apiVersion: v1
kind: Pod
metadata:
  labels:
    test: liveness
  name: liveness-tcp
spec:
  containers: 
  - name: liveness
    image: nginx
    livenessProbe:
      failureThreshold: 3
      tcpSocket:
        port: 80
      initialDelaySeconds: 10
      periodSeconds: 10
      successThreshold: 1
      timeoutSeconds: 10


readness probe --command
#检测/tmp/healthy是否存在,不存在则创建新的pod,svc接收的请求不再转发给原来的pod
apiVersion: v1
kind: Pod
metadata:
  labels:
    test: readiness
  name: readiness-command
spec:
  containers:
  - name: readiness
    image: docker.io/busybox
    args:
    - /bin/sh
    - -c
    - touch /tmp/healthy; sleep 30; rm -rf/tmp/healthy; sleep 10
    readinessProbe:
      exec:
        command:
        - cat
        - /tmp/healthy
      initialDelaySeconds: 5
      periodSeconds: 5

readiness probe --httpGET
#访问80端口的index.html文件是否存在
apiVersion: v1
kind: Pod
metadata:
  labels:
    test: readiness
  name: readiness-httpget
spec:
  containers:
  - name: readiness
    image: nginx
    readinessProbe:
      failureThreshold: 3
      httpGet:
        path: /index.html
        port: 80
        scheme: HTTP
      initialDelaySeconds: 10
      periodSeconds: 10
      successThreshold: 1
      timeoutSeconds: 10

readiness probe --tcp
#检测80端口是否联通
apiVersion: v1
kind: Pod
metadata:
  labels:
    test: readiness
  name: readiness-tcp
spec:
  containers: 
  - name: readiness
    image: nginx
    readinessProbe:
      failureThreshold: 3
      tcpSocket:
        port: 80
      initialDelaySeconds: 10
      periodSeconds: 10
      successThreshold: 1
      timeoutSeconds: 10

推荐阅读更多精彩内容