This page shows how to configure liveness and readiness probes for Containers.
The kubelet uses liveness probes to know when to restart a Container. For example, liveness probes could catch a deadlock, where an application is running, but unable to make progress. Restarting a Container in such a state can help to make the application more available despite bugs.
The kubelet uses readiness probes to know when a Container is ready to start accepting traffic. A Pod is considered ready when all of its Containers are ready. One use of this signal is to control which Pods are used as backends for Services. When a Pod is not ready, it is removed from Service load balancers.
- Before you begin
- Defining a liveness command
- Defining a liveness HTTP request
- Using a named port
- Defining readiness probes
- Discussion
- What’s next
Before you begin
You need to have a Kubernetes cluster, and the kubectl command-line tool must be configured to communicate with your cluster. If you do not already have a cluster, you can create one by using Minikube.
Defining a liveness command
Many applications running for long periods of time eventually transition to broken states, and cannot recover except by being restarted. Kubernetes provides liveness probes to detect and remedy such situations.
In this exercise, you create a Pod that runs a Container based on the gcr.io/google_containers/busybox
image. Here is the configuration file for the Pod:
|
In the configuration file, you can see that the Pod has a single Container. The livenessProbe
field specifies that the kubelet should perform a liveness probe every 5 seconds. The initialDelaySeconds
field tells the kubelet that it should wait 5 second before performing the first probe. To perform a probe, the kubelet executes the command cat /tmp/healthy
in the Container. If the command succeeds, it returns 0, and the kubelet considers the Container to be alive and healthy. If the command returns a non-zero value, the kubelet kills the Container and restarts it.
When the Container starts, it executes this command:
/bin/sh -c "touch /tmp/healthy; sleep 30; rm -rf /tmp/healthy; sleep 600"
For the first 30 seconds of the Container’s life, there is a /tmp/healthy
file. So during the first 30 seconds, the command cat /tmp/healthy
returns a success code. After 30 seconds, cat /tmp/healthy
returns a failure code.
Create the Pod:
kubectl create -f http://k8s.io/docs/tasks/configure-pod-container/exec-liveness.yaml
Within 30 seconds, view the Pod events:
kubectl describe pod liveness-exec
The output indicates that no liveness probes have failed yet:
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
After 35 seconds, view the Pod events again:
kubectl describe pod liveness-exec
At the bottom of the output, there are messages indicating that the liveness probes have failed, and the containers have been killed and recreated.
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
Wait another 30 seconds, and verify that the Container has been restarted:
kubectl get pod liveness-exec
The output shows that RESTARTS
has been incremented:
NAME READY STATUS RESTARTS AGE
liveness-exec 1/1 Running 1 1m
Defining a liveness HTTP request
Another kind of liveness probe uses an HTTP GET request. Here is the configuration file for a Pod that runs a container based on the gcr.io/google_containers/liveness
image.
|
In the configuration file, you can see that the Pod has a single Container. The livenessProbe
field specifies that the kubelet should perform a liveness probe every 3 seconds. The initialDelaySeconds
field tells the kubelet that it should wait 3 seconds before performing the first probe. To perform a probe, the kubelet sends an HTTP GET request to the server that is running in the Container and listening on port 8080. If the handler for the server’s /healthz
path returns a success code, the kubelet considers the Container to be alive and healthy. If the handler returns a failure code, the kubelet kills the Container and restarts it.
Any code greater than or equal to 200 and less than 400 indicates success. Any other code indicates failure.
You can see the source code for the server in server.go.
For the first 10 seconds that the Container is alive, the /healthz
handler returns a status of 200. After that, the handler returns a status of 500.
http.HandleFunc("/healthz",func(whttp.ResponseWriter,r*http.Request){duration:=time.Now().Sub(started)ifduration.Seconds()>10{w.WriteHeader(500)w.Write([]byte(fmt.Sprintf("error: %v",duration.Seconds())))}else{w.WriteHeader(200)w.Write([]byte("ok"))}
The kubelet starts performing health checks 3 seconds after the Container starts. So the first couple of health checks will succeed. But after 10 seconds, the health checks will fail, and the kubelet will kill and restart the Container.
To try the HTTP liveness check, create a Pod:
kubectl create -f http://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
Using a named port
You can use a named ContainerPort for HTTP liveness checks:
ports:
- name: liveness-port
containerPort: 8080
hostPort: 8080
livenessProbe:
httpGet:
path: /healthz
port: liveness-port
Defining readiness probes
Sometimes, applications are temporarily unable to serve traffic. For example, an application might need to load large data or configuration files during startup. In such cases, you don’t want to kill the application, but you don’t want to send it requests either. Kubernetes provides readiness probes to detect and mitigate these situations. A pod with containers reporting that they are not ready does not receive traffic through Kubernetes Services.
Readiness probes are configured similarly to liveness probes. The only difference is that you use the readinessProbe
field instead of the livenessProbe
field.
readinessProbe:
exec:
command:
- cat
- /tmp/healthy
initialDelaySeconds: 5
periodSeconds: 5
Discussion
Probes have these additional fields that you can use to more precisely control the behavior of liveness and readiness checks:
- timeoutSeconds
- successThreshold
- failureThreshold
HTTP probes have these additional fields:
- host
- scheme
- httpHeaders
For an HTTP probe, the kubelet sends an HTTP request to the specified path and port to perform the check. The kubelet sends the probe to the container’s IP address, unless the address is overridden by the optional host
field in httpGet
. In most scenarios, you do not want to set the host
field. Here’s one scenario where you would set it. Suppose the Container listens on 127.0.0.1 and the Pod’s hostNetwork
field is true. Then host
, under httpGet
, should be set to 127.0.0.1. If your pod relies on virtual hosts, which is probably the more common case, you should not use host
, but rather set the Host
header in httpHeaders
.
In addition to command probes and HTTP probes, Kubernetes supports TCP probes.
What’s next
-
Learn more about Container Probes.
-
Learn more about Health Checking section.
Reference
https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-probes/
相关推荐
4.1.3.5 Packet Tracer - Configuring IPv4 and IPv6 Interfaces.pka
NAV350 报文解析 Telegram_listing_Telegrams_for_Configuring_and_Operating_the_NAV350_
Configuring Sales and Distribution in SAP ERP
Lab 10 - Configuring Monitoring and Archiving in LS 2010.pdf
很详细的ERP Sales and Distribution,收入确认等
1.1.3.5 Packet Tracer - Configuring IPv4 and IPv6 Interfaces.pka
英文书籍,排版及页面清晰度不错,对SAP SD的相关知识还是比较全面的,个人建议有一定英语基础的人并且具备SAP基础知识的人去学习。
配置和管理EMC Celerra上的CIFS是一个深入的主题,它涉及到网络文件服务的操作和管理。EMC Celerra Network Server集成了作为网络文件服务开放标准的CIFS协议。CIFS是为互联网设计的文件访问协议,基于Microsoft ...
Prepare for Microsoft Exam 70-410 – and help demonstrate your real-world mastery of implementing and configuring core services in Windows Server 2012 R2. Designed for experienced IT professionals ...
Configuring and Troubleshooting TCPIP
1. Open edX平台简介:文档中提到的“Installing, Configuring, and Running the Open edX Platform”是一个运行手册,该手册对如何安装、配置和运行Open edX平台提供了详尽的指导。Open edX平台支持各种在线教育...
SIMATIC Configuring Hardware and Communication Connections STEP 7 手册是一份详细指导用户如何配置西门子自动化设备硬件和通信连接的文档。这份手册是订单号为6ES7810-4CA08-8BW0的文档包的一部分,版本为03/...
根据给定的信息,文章标题为《配置服务器和机器》,但描述部分为空。标签显示与源码和工具相关。文章主要内容是关于配置三个服务器的描述,但遗憾的是具体内容没有给出完整信息。为了生成相关知识点,我将根据标题与...
Configuring SAP R3 FICO The Essential Resource for Configuring the Financial and Controlling Modules
根据提供的文件信息,本书《Installing and Configuring Windows Server 2012 Exam Ref 70-410》是一本针对Microsoft认证考试70-410的专业辅导教材。本书由Craig Zacker编写,并由Microsoft Press出版。以下是本书中...
Seam 是一个强大的Java开发框架,专为JavaServer Faces (JSF) 和企业级Java (Java EE) 应用程序设计。它简化了组件集成、事务管理、安全性和其他复杂功能,使开发者能够更高效地构建应用程序。以下是对配置Seam和...
Installing and Configuring Openfiler with DRBD and Heartbeat
Oracle Solaris 11.1 Configuring and Administering Oracle Solaris11.1Networks-152
Configuring SAP R3 FICO The Essential Resource for Configuring the Financial and Controlling Modules.part3