k8s就绪检测&存活检测

容器探针

由kubectl调用由容器实现的Handler,有三种类型的处理程序:

  • ExecAction:在容器内指定命令,如果命令返回码为0,则成功。
  • TCPSocketAction:对指定端口上的容器IP地址进行TCP检查,如果端口打开,则成功。
  • HTTPGetAction:对指定端口和路径上的容器的IP地址执行HTTP Get请求,如果响应的状态码大于等于200且小于400,则成功。

参数

      initialDelaySeconds: 5    #探测延时时长,第一次探测前等待5秒,默认为0
      periodSeconds: 5          #每5秒执行一次liveness探测,默认值10秒,最小1秒 
      timeoutSeconds: 2         #超长时长,默认为1s,最小值也为1s
      failureThreshold: 3       #处于成功状态时,探测操作至少连续多少次的失败才被视为检测不通过,默认为3,最小为1

探测方式:

就绪检测(readinessProbe)

指示容器是否正在运行,如果存活检测失败,则kubelet会杀死容器,并且容器将受到其重启策略的影响。如果容器不提供存活探针,则默认状态为Success

HTTPGet探测示例

apiVersion: v1
kind: Pod
metadata:
  name: readiness-httpget-pod
  namespace: default
spec:
  containers:
  - name: readiness-httpget-container
    image: hub.vfancloud.com/test/myapp:v1
    imagePullPolicy: IfNotPresent
    readinessProbe:
      httpGet:
        port: 80
        path: /index1.html
      initialDelaySeconds: 1  # Pod开启后,延迟1s再进行检测
      periodSeconds: 3  # 检测间隔时长

livenessProbe(存活检测)

指示容器是否准备好服务请求。如果就绪探测失败,端点控制器将从与Pod匹配的所有的service的端点中删除该Pod的IP地址,初始延迟之前的就绪状态默认为Failure。如果容器不提供就绪探针,则默认状态为Success

Exec探测示例

apiVersion: v1
kind: Pod
metadata:
  name: liveness-exec-pod
  namespace: default
spec:
  containers:
  - name: liveness-exec-container
    image: busybox:v1
    imagePullPolicy: IfNotPresent
    command: ['sh','-c','touch /tmp/live ;sleep 60; rm -f /tmp/live; sleep 3600']
    livenessProbe:
      exec:
        command: ['test','-e','/tmp/live']  # 检测有无/tmp/live文件
      initialDelaySeconds: 1  # Pod开启后,延迟1s再进行检测
      periodSeconds: 3  # 检测间隔时长

TCPSocket探测示例

apiVersion: v1
kind: Pod
metadata:
  name: liveness-httpget-pod
  namespace: default
spec:
  containers:
  - name: nginx
    image: hub.vfancloud.com/test/myapp:v1
    imagePullPolicy: IfNotPresent
    livenessProbe:
      tcpSocket:  # 检测8080是否存在
        port: 8080
      initialDelaySeconds: 1
      periodSeconds: 3
      timeoutSeconds: 10   # 超过10秒失败
http://xzh.i3geek.com

发表评论

邮箱地址不会被公开。 必填项已用*标注

Captcha Code