Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to configure health check in Kubernetes

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article shows you how to configure health check in Kubernetes. The content is concise and easy to understand. It will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

If your application is user-oriented, ensuring continuous availability and trying to achieve minimum downtime is an important but not easy challenge. Therefore, it is very important to monitor the health of the application well if you want to avoid any interruption.

Health check in Rancher 1. 6

The orchestration engine Cattle in Rancher 1.6has the ability to add HTTP or TCP health checks to deployed services. Rancher's own health check microservice provides health check support. You can learn more here:

Https://rancher.com/docs/rancher/v1.6/en/cattle/health-checks/

Simply put, Cattle users can add TCP health checks to the service. Rancher's health check containers are launched on different hosts, and they test whether the TCP connection is opened at the specified port of the service container. Note that for the latest version (v1.6.20), the health check container is also arranged on the same host as the service container.

HTTP health checks can also be added when deploying the service. You can ask Rancher to issue an HTTP request on the specified path and specify the expected response.

These health checks are completed on a regular basis, you can configure the interval period of the check, and the retry / timeout is configurable. If the health check fails, you can also indicate whether and when Rancher recreates the container.

For example, run the service of Nginx image on Cattle and perform a HTTP health check using the following configuration:

The parameters for the health check are displayed in the rancher-compose.yml file, not the docker-compose.yml, because the health check function is implemented by Rancher.

Let's see if we can configure the corresponding health check in Rancher 2.0.

Health check in Rancher 2. 0

In 2. 0, Rancher uses the native Kubernetes health check mechanisms: livenessProbe and readinessProbe.

As defined in this document, probes (probe) are diagnostics: links that are performed periodically by Kubelet on the container. In Rancher 2.0, compared to the cross-host health check in Rancher 1.6, the health check is done by a locally running Kubelet.

Summary of Rapid Kubernetes Health check

LivenessProbe

LivenessProbe is an operation performed on a container to check whether the container is running. If the probe reports a failure, Kubernetes terminates the pod container and restarts it according to the restart policy specified in the specification.

ReadinessProbe

ReadinessProbe is used to check whether the container is ready to accept the request and satisfy the request. When readinessProbe fails, the pod container is not exposed through the public endpoint, so the container does not receive any requests.

If your workload is busy executing some startup routines before processing the request, it is best to configure readinessProbe for the workload.

The following types of livenessProbe and readinessProbe can be configured for Kubernetes workloads:

TcpSocket-Kubelet checks whether a TCP connection can be opened for the container IP address on the specified port.

HttpGet-A HTTP / HTTPS GET request is issued on the specified path, and if it returns a HTTP response code between 200and 400, it is reported as successful.

Exec-Kubelet executes the specified command within the container and checks to see if the command exits in state 0.

You can view more configuration details of the above probe here:

Https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-probes/#configure-probes

Configure health check in Rancher 2.0

With Rancher UI, users can add TCP or HTTP health checks to Kubernetes workloads. By default, Rancher requires you to configure readinessProbe for the workload and apply livenessProbe with the same configuration. You can also choose to define a separate livenessProbe.

If the health check fails, the container restarts according to the restartPolicy defined in the workload specification. This is equivalent to the strategy parameter in the previous rancher-compose.yml file, which was used for the 1.6 service that uses the health check in Cattle.

TCP health check

When deploying workloads in Rancher 2.0, users can configure TCP health checks to check whether TCP connections can be opened on specific ports.

The following is the Kubernetes YAML specification, which is the TCP readinessProbe configured for the Nginx workload mentioned above. Rancher also adds livenessProbe to your workload using the same configuration.

Changes in health check parameters from 1.6 to 2.0:

Port becomes tcpSocket.port

Response_timeout becomes

TimeoutSeconds

Healthy_threshold becomes failureThreshold

Unhealthy_threshold becomes

SuccessThreshold

Interval becomes periodSeconds

Initializing_timeout becomes

InitialDelaySeconds

Strategy becomes restartPolicy

HTTP health check

You can also specify the HTTP health check and provide the path in the pod container where Kubelet will issue the HTTP / HTTPS GET request. However, unlike any HTTP method supported in Rancher 1.6, Kubernetes only supports HTTP / HTTPS GET requests.

The following is the Kubernetes YAML specification, which shows the HTTP readinessProbe and livenessProbe configured for the Nginx workload mentioned above.

The health check is in action.

Now let's take a look at what happens when a health check in Kubernetes fails and how the workload recovers.

Suppose the above HTTP health check is performed on our Nginx workload and HTTP GET is performed on the / index.html path. To deliberately fail the health check, I performed an exec in the pod container using the Execute Shell UI option in Rancher.

After the exec container, I moved the health check to perform the GET file.

The readinessProbe and livenessProbe check failed, and the workload status has changed to unavailable.

Kubernetes quickly killed the original pod and recreated the pod, and because restartPolicy was set to Always, the workload was quickly restored.

Using Kubectl, you can see these health check event logs:

Tip: Rancher 2.0 UI provides the ability to launch Kubectl from the Kubernetes Cluster view, where you can run native Kubernetes commands on clustered objects.

Migrate health checks from Docker Compose to Kubernetes Yaml?

Rancher 1.6 provides health checks through its own microservice, which is why the health check parameters that Cattle users add to the service appear in the rancher-compose.yml file instead of the docker-compose.yml configuration file.

The Kompose tool we used in the previous article, "how to implement Kubernetes service exposure succinctly and elegantly," applies to standard docker-compose.yml parameters, so the Rancher health check construct cannot be parsed. Currently, we cannot use this tool to convert Rancher health check from compose configuration to Kubernetes Yaml.

The configuration parameters that can be used to add TCP or HTTP health checks to Rancher 2.0 are very similar to Rancher 1.6. The health check configuration used by the Cattle service can be fully converted to 2. 0 without losing any functionality.

The above is how to configure health check in Kubernetes. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report