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 realize High availability of applications in Kubernetes

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

Share

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

This article will explain in detail how to achieve high availability of applications in Kubernetes, and the content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

1. Kubernetes probe

When you use Kubernetes, have you ever encountered a vicious cycle in which Pod dies soon after startup and then restarts? How does Kubernetes detect if Pod is still alive? Although the container has been started, how does Kubernetes know if the process of the container is ready to provide services? The answer is: probe

Probe type

LivenessProbe

Introduction: LivenessProbe, also known as survival probe, is mainly used to detect whether the container is still running. Kubelet uses a survival probe to know when to restart the container. For example, a survival probe can catch a deadlock (the application is running, but cannot continue with the following steps). Restarting the container in this case helps make the application more available in the event of a problem.

Purpose: Kubernetes can use the survival probe to check whether the container is still running. If the probe fails, Kubernetes will assume that the container is no longer running and will restart the container.

Detection method:

1. HTTP:kubelet executes the HTTP GET request to the specified path of the IP address of the container. Judge whether the detection is successful according to the return code

2. TCP socket: try to establish a TCP connection with the specified port of the container. If the connection is successful, the probe is successful.

3. Exec: execute any command in the container to check the exit status code. If the return value is 0, the probe is successful.

Survival probe yaml file:

Green is the http probe. Here, port 8080 of the container is requested. If the returned value is 2xx or 3xx, the probe is successful.

The red is the exec probe, where a cat command is executed.

The blue one is tcp probe. The function is to try to establish a TCP connection with port 8080.

ReadnessProbe

Description: a ready probe to detect whether Pod is ready. Because part of the Pod will have a long initialization time when it starts, the ready probe can be used to detect whether the Pod has been initialized. Kubelet uses the ready probe to know when the container is ready and can begin to accept the request flow, and when all the containers in a Pod are ready, the Pod can be considered ready. One of the uses of this signal is to control which Pod is the back end of the Service. When Pod is not ready, it will be removed from Service's load balancer.

Purpose: the ready probe is called periodically during the container life cycle. Determine whether the Pod accepts the client request. When the container's readiness probe returns successfully, the container is ready to accept the request.

Detection method:

1.HTTP: execute a HTTP GET request to the specified path to the IP address of the container. Judge whether the detection is successful according to the return code

2.TCP socket: attempts to establish a TCP connection to the specified port of the container. If the connection is successful, the probe is successful.

3.Exec: execute any command in the container to check the exit status code. If the status code is 0, the detection is successful.

Yaml file of the ready probe:

The configuration of ready probe is almost the same as that of survival probe.

It's just a field of readinessProbe.

2. The use of Deployment

ReplicaSet

Introduction: Pod is unreliable, may go down, and can be configured to restart, but if the node where the Pod is located goes down, the Pod will be completely lost, and problems will occur in the production environment. In addition, we generally do not use a single Pod, but many groups of the same Pod to provide services, which will be particularly difficult to maintain manually, especially when the number of clusters is large, so ReplicaSet is introduced here. The purpose of ReplicaSet is to maintain a stable set of Pod replicas that are running at all times. Therefore, it is usually used to ensure the availability of a given number of identical Pod.

How it works: RepicaSet is defined by a set of fields, including a selection operator to identify the collection of available Pod, a numeric value to indicate the number of copies that should be maintained, a Pod template to be used to specify that a new Pod should be created to meet the condition for the number of copies, and so on. Each ReplicaSet achieves the value of its existence by creating and deleting Pod as needed to achieve the desired number of copies. When ReplicaSet needs to create a new Pod, the provided Pod template is used.

How it works: the purpose of ReplicaSet is to maintain a stable set of Pod replicas that are running at all times. Therefore, it is usually used to ensure the availability of a given number of identical Pod.

Each time ReplicaSet monitors the current number of Pod, if the number of Pod exceeds the required number, some of them will be deleted automatically. Otherwise, it will be created automatically and will be created using a template during the creation process.

ReplicaSet creates a new replacement copy of the Pod running on the failed node, which easily scales horizontally with pod.

If you change the tag of a Pod, it is possible to detach it from the ReplicaSet to which it belongs.

Similarly, if you want to put a Pod in the Replicas Set, you can also add it to the ReplicaSet by modifying the tag.

ReplicaSet yaml example

1.Label Selector: tag selector to determine scope

2.Replicas: number of copies, that is, scope

3.Pod template: Pod template

Deployment

A Deployment controller provides declarative update capabilities for Pod and ReplicasSet.

You are responsible for describing the target state in the Deployment, while the Deployment controller changes the actual state to the desired state at a controlled rate. You can define a Deployment to create a new ReplicaSet, or delete an existing Deployment and adopt its resources through the new Deployment.

Deployment yaml example

The Deployment yaml file is very similar to ReplicaSet, except that the strategy field is added. This field is mainly used to describe the upgrade method.

Deployment does not directly manage Pod

When you create a Deployment, the ReplicaSet is created with it. When actually using Deployment, the actual Pod is created and managed by ReplicaSet, not directly by Deployment.

Using Deployment makes it easier to update your application because you can directly define the state that a single Deployment resource needs to achieve and let Kubernetes handle the intermediate state.

Upgrade Deployment

Deployment supports declarative updates, that is, you only need to modify the Pod template in the Deployment resource, and Kubernetes automatically converges the actual system state to the state defined in the resource.

Deployment supports different upgrade strategies, which are mainly divided into RollingUpdate and Recreate modes. This policy is defined in the deployment.spec.strategy field. Details can be obtained using the kubectl explain command.

Deployment Recreate upgrade

The Deployment Recreate upgrade strategy will directly stop the old version of Pod and create new ReplicaSet and Pod. And the traffic is switched. The specific steps are as follows:

The disadvantage is that there is no Pod running for a period of time during the upgrade process, and if there is an external request, the service will be unavailable and a certain amount of traffic will be lost.

Deployment RollingUpdate upgrade

On how to achieve high availability of applications in Kubernetes to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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