kubernetes存在两类探针:分别是存活性探针和就绪性探针
一、存活性探针
1.1、exec探针
简介:exec探针通过在目标容器执行由用户自定义的命令来判断容器的健康状态,若命令状态返回值为0表示”成功”,其他值均为”失败”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| apiVersion: v1 kind: Pod metadata: labels: test: liveness-exec name: liveness-exec spec: containers: - name: liveness-demo image: busybox args: - /bin/sh - -c - touch /tmp/healthy; sleep 60; rm -rf /tmp/healthy; sleep 600 livenessProbe: exec: command: - test - -e - /tmp/healthy
|
1.2、HTTP探针
基于HTTP探针的(HTTPGetAction)向目标容器发起一个HTTP请求,根据响应码的结果进行判断
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| apiVersion: v1 kind: Pod metadata: labels: test: liveness name: liveness-http spec: containers: - name: liveness-demo image: nginx:1.12-alpine ports: - name: http containerPort: 80 lifecycle: postStart: exec: command: - /bin/sh - -c - 'echo Healty > /usr/share/nginx/html/healthz' livenessProbe: httpGet: path: /healthz port: http scheme: HTTP
|
字段说明:
字段 |
说明 |
host |
请求主机地址,默认是Pod的IP |
port |
请求端口,必须字段 |
httpHeaders |
自定义请求报文首部 |
path |
请求的HTTP资源路径,即URL path |
scheme |
建立连接使用的协议,默认是http,可以选择https |
1.3、TCP探针
基于TCP的存活性探测,用于向容器特定的端口发起TCP请求并建立连接进行结果判断,连接成功建立则通过检测
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| apiVersion: v1 kind: Pod metadata: labels: test: liveness name: liveness-tcp spec: containers: - name: liveness-tcp-demo image: nginx:1.12-alpine ports: - name: http containerPort: 80 livenessProbe: tcpsocket: port: http
|
字段 |
说明 |
host |
请求连接的目标IP地址,默认为Pod IP |
port |
请求连接的目标端口,必选字段 |
spec.containers.livenessProbe有如下属性字段
字段 |
说明 |
initialDelaySeconds |
容器启动多久后开始第一次探测,显示为delay属性,默认是0 |
timeoutSeconds |
健康检查发送请求后等待响应的超时时间,显示为timeout属性,默认是1s |
periodSeconds |
探测的频率,显示为period属性,默认是10s,最小是1s |
successThreshold |
处于失败状态时,探测操作至少连续多少次成功才被认为是通过检测,显示为success属性,默认值为1 |
failureThreshold |
处于成功状态时,探测操作至少连续多少次失败才被认为是检测不通过,显示为failure属性,默认值为3 |
二、就绪性探针
和存活性探针类似
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| apiVersion: v1 kind: Pod metadata: labels: test: readiness-exec name: readiness-exec spec: containers: - name: readiness-demo image: busybox args: ["/bin/sh", "-c", "while true; do rm -f /tmp/ready; sleep 30; touch /tmp/ready; sleep 300; done"] readinessProbe: exec: command: ["test", "-e", "/tmp/ready"] initialDelaySeconds: 5 periodSeconds: 5
|