74. 配置Pod 的liveness 和readiness 探针
配置Pod 的liveness 和readiness 探针
当你使用
本文将展示如何配置容器的存活和可读性探针。
定义liveness 命令
许多长时间运行的应用程序最终会转换到
在本次练习将基于 gcr.io/google_containers/busybox
镜像创建运行一个容器的exec-liveness.yaml
:
apiVersion: v1
kind: Pod
metadata:
labels:
test: liveness
name: liveness-exec
spec:
containers:
- name: liveness
args:
- /bin/sh
- -c
- touch /tmp/healthy; sleep 30; rm -rf /tmp/healthy; sleep 600
image: gcr.io/google_containers/busybox
livenessProbe:
exec:
command:
- cat
- /tmp/healthy
initialDelaySeconds: 5
periodSeconds: 5
该配置文件给periodSeconds
规定initialDelaySeconds
告诉cat /tmp/healthy
命令。如果命令执行成功,将返回
容器启动时,执行该命令:
/bin/sh -c "touch /tmp/healthy; sleep 30; rm -rf /tmp/healthy; sleep 600"
在容器生命的最初/tmp/healthy
文件,在这cat /tmp/healthy
命令会返回一个成功的返回码。cat /tmp/healthy
将返回失败的返回码。
创建
kubectl create -f https://k8s.io/docs/tasks/configure-pod-container/exec-liveness.yaml
在
kubectl describe pod liveness-exec
结果显示没有失败的
FirstSeen LastSeen Count From SubobjectPath Type Reason Message
--------- -------- ----- ---- ------------- -------- ------ -------
24s 24s 1 {default-scheduler } Normal Scheduled Successfully assigned liveness-exec to worker0
23s 23s 1 {kubelet worker0} spec.containers{liveness} Normal Pulling pulling image "gcr.io/google_containers/busybox"
23s 23s 1 {kubelet worker0} spec.containers{liveness} Normal Pulled Successfully pulled image "gcr.io/google_containers/busybox"
23s 23s 1 {kubelet worker0} spec.containers{liveness} Normal Created Created container with docker id 86849c15382e; Security:[seccomp=unconfined]
23s 23s 1 {kubelet worker0} spec.containers{liveness} Normal Started Started container with docker id 86849c15382e
启动
kubectl describe pod liveness-exec
在最下面有一条信息显示
FirstSeen LastSeen Count From SubobjectPath Type Reason Message
--------- -------- ----- ---- ------------- -------- ------ -------
37s 37s 1 {default-scheduler } Normal Scheduled Successfully assigned liveness-exec to worker0
36s 36s 1 {kubelet worker0} spec.containers{liveness} Normal Pulling pulling image "gcr.io/google_containers/busybox"
36s 36s 1 {kubelet worker0} spec.containers{liveness} Normal Pulled Successfully pulled image "gcr.io/google_containers/busybox"
36s 36s 1 {kubelet worker0} spec.containers{liveness} Normal Created Created container with docker id 86849c15382e; Security:[seccomp=unconfined]
36s 36s 1 {kubelet worker0} spec.containers{liveness} Normal Started Started container with docker id 86849c15382e
2s 2s 1 {kubelet worker0} spec.containers{liveness} Warning Unhealthy Liveness probe failed: cat: can't open '/tmp/healthy': No such file or directory
再等
kubectl get pod liveness-exec
从输出结果来 RESTARTS
值加
NAME READY STATUS RESTARTS AGE
liveness-exec 1/1 Running 1 1m
定义一个liveness HTTP 请求
我们还可以使用gcr.io/google_containers/liveness
镜像运行了一个容器的http-liveness.yaml
:
apiVersion: v1
kind: Pod
metadata:
labels:
test: liveness
name: liveness-http
spec:
containers:
- name: liveness
args:
- /server
image: gcr.io/google_containers/liveness
livenessProbe:
httpGet:
path: /healthz
port: 8080
httpHeaders:
- name: X-Custom-Header
value: Awesome
initialDelaySeconds: 3
periodSeconds: 3
该配置文件只定义了一个容器,livenessProbe
指定initialDelaySeconds
指定/healthz
路径的
任何大于
最开始的/healthz
http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
duration := time.Now().Sub(started)
if duration.Seconds() > 10 {
w.WriteHeader(500)
w.Write([]byte(fmt.Sprintf("error: %v", duration.Seconds())))
} else {
w.WriteHeader(200)
w.Write([]byte("ok"))
}
})
容器启动
创建一个
kubectl create -f https://k8s.io/docs/tasks/configure-pod-container/http-liveness.yaml
After 10 seconds, view Pod events to verify that liveness probes have failed and the Container has been restarted:
kubectl describe pod liveness-http
定义TCP liveness 探针
第三种
apiVersion: v1
kind: Pod
metadata:
name: goproxy
labels:
app: goproxy
spec:
containers:
- name: goproxy
image: gcr.io/google_containers/goproxy:0.1
ports:
- containerPort: 8080
readinessProbe:
tcpSocket:
port: 8080
initialDelaySeconds: 5
periodSeconds: 10
livenessProbe:
tcpSocket:
port: 8080
initialDelaySeconds: 15
periodSeconds: 20
如您所见,
除了
使用命名的端口
可以使用命名的
ports:
- name: liveness-port
containerPort: 8080
hostPort: 8080
livenessProbe:
httpGet:
path: /healthz
port: liveness-port
定义readiness 探针
有时,应用程序暂时无法对外部流量提供服务。例如,应用程序可能需要在启动期间加载大量数据或配置文件。在这种情况下,你不想杀死应用程序,但你也不想发送请求。
readinessProbe
而不是 livenessProbe
。
readinessProbe:
exec:
command:
- cat
- /tmp/healthy
initialDelaySeconds: 5
periodSeconds: 5
配置Probe
initialDelaySeconds
:容器启动后第一次执行探测是需要等待多少秒。periodSeconds
:执行探测的频率。默认是10 秒,最小1 秒。timeoutSeconds
:探测超时时间。默认1 秒,最小1 秒。successThreshold
:探测失败后,最少连续探测成功多少次才被认定为成功。默认是1 。对于liveness 必须是1 。最小值是1 。failureThreshold
:探测成功后,最少连续探测失败多少次才被认定为失败。默认是3 。最小值是1 。
httpGet
设置其他配置项:
host
:连接的主机名,默认连接到pod 的IP 。你可能想在http header 中设置 “Host” 而不是使用IP 。scheme
:连接使用的schema ,默认HTTP 。path
: 访问的HTTP server 的path 。httpHeaders
:自定义请求的header 。HTTP 运行重复的header 。port
:访问的容器的端口名字或者端口号。端口号必须介于1 和65535 之间。
对于httpGet
中的可选 host
字段覆盖。在大多数情况下,你不想设置主机字段。有一种情况下你可以设置它。假设容器在hostNetwork
字段为httpGet
下的 host
应该设置为host
,而是应该在 httpHeaders
中设置 Host
头。