A load balancing scheduling method and system for Kubernetes cluster
By using golang to implement the consistent hash load balancing algorithm in Kubernetes, the single point of failure and performance bottleneck problems of the traditional load balancing algorithm are solved, load balancing and high availability are achieved, and the problems of hash conflicts and load unevenness are avoided.
Patent Information
- Application Number
- CN202311603839.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-11-28
- Publication Date
- 2024-09-13
- Estimated Expiration
- 2043-11-28
AI Technical Summary
The traditional Kubernetes load balancing algorithm has single point of failure and performance bottleneck problems, and cannot meet business needs in complex scenarios.
Golang is used to implement the consistent hash load balancing algorithm. Services are mapped to the ring space by constructing identification codes, nodes are selected clockwise, and traffic distribution is dynamically adjusted when nodes join or leave to avoid hash conflicts and uneven loads.
Effectively reduce hash conflicts, achieve load balancing, and improve high availability. Even if a node fails, it will not affect the overall load balancing performance.
Smart Images

Figure CN117376356B
Abstract
Description
Technical Field
[0001] The present invention relates to the field of communication technologies, and in particular to a load balancing scheduling method and system for a Kubernetes cluster. Background Art
[0002] Kubernetes is an open source container orchestration system designed to simplify the deployment, expansion, and management of container applications. Kubernetes provides a platform that can automatically deploy, expand, and manage containerized applications, supports multiple cloud platforms and physical server clusters, and can easily manage thousands of containers.
[0003] Kubernetes supports multiple load balancing algorithms, and you can choose the appropriate algorithm based on the application requirements and scenarios. Currently, the default load balancing algorithms supported by Kubernetes include polling, least connections, IP hashing, and weighted polling. In complex scenarios, the above default balancing algorithms cannot meet the needs of the business well. Traditional load balancing algorithms generally have single point failures and performance bottlenecks. Summary of the invention
[0004] Based on this, the embodiments of the present application provide a load balancing scheduling method and system for a Kubernetes cluster, which can effectively avoid the single point failure and performance bottleneck problems of traditional load balancing algorithms.
[0005] In a first aspect, a load balancing scheduling method for a Kubernetes cluster is provided, the method comprising:
[0006] Use golang to establish a load balancer controller and interact with the API of the Kubernetes cluster to be scheduled to establish an interactive relationship; wherein the interactive relationship at least includes processing the creation, update and deletion operations of the load balancer;
[0007] In the Kubernetes cluster, the annotations field of the Service object is set to the name of the load balancer controller, and load balancing scheduling is performed through the load balancer controller.
[0008] Optionally, use golang to establish a load balancer controller, and interact with the API of the Kubernetes cluster to be scheduled to establish an interactive relationship, including: using the Deployment resource object to deploy the controller.
[0009] Optionally, in the Kubernetes cluster, the annotations field of the Service object is set to the name of the load balancer controller, including:
[0010] Get the Service object and related Endpoints objects;
[0011] Calculate the list of target ports and target IP addresses of the load balancer controller;
[0012] The target IP address is selected based on the consistent hashing load balancing algorithm and the traffic is routed to the target IP address.
[0013] Optionally, selecting the target IP address according to the consistent hash load balancing algorithm specifically includes:
[0014] Construct an identification code for the pod according to the preset rules, then use the identification code to modulo 2^32 to determine that the service is mapped to a ring space in the hash value interval, and map the node IP address to the position on the ring;
[0015] When a request is initiated to allocate data traffic to a node, the parameters attached to the data traffic are mapped to the position on the ring through the constructed identification code, and then the node closest to the position is found clockwise and the data traffic is allocated to the node;
[0016] When a new node joins, it is necessary to find the location of the new node on the ring and insert it to the appropriate position; when a node leaves, it needs to be deleted from the ring and the data traffic related to the node needs to be redistributed to other nodes.
[0017] Optionally, after allocating the data traffic to the node, the method further includes:
[0018] When a new node is added, the position of the new node is found on the ring and inserted into the corresponding position;
[0019] When a node leaves, it is deleted from the ring and the data traffic related to the node is redistributed to other nodes.
[0020] In a second aspect, a load balancing scheduling system for a Kubernetes cluster is provided, the system comprising:
[0021] An interaction module is used to establish a load balancer controller using golang, and interact with the API of the Kubernetes cluster to be scheduled to establish an interaction relationship; wherein the interaction relationship at least includes processing the creation, update and deletion operations of the load balancer;
[0022] The scheduling module is used to set the annotations field of the Service object to the name of the load balancer controller in the Kubernetes cluster, and perform load balancing scheduling through the load balancer controller.
[0023] Optionally, golang is used in the interaction module to establish a load balancer controller, and interact with the API of the Kubernetes cluster to be scheduled to establish an interactive relationship, including: using the Deployment resource object to deploy the controller.
[0024] Optionally, the scheduling module sets the annotations field of the Service object in the Kubernetes cluster to the name of the load balancer controller, including:
[0025] Get the Service object and related Endpoints objects;
[0026] Calculate the list of target ports and target IP addresses of the load balancer controller;
[0027] The target IP address is selected based on the consistent hashing load balancing algorithm and the traffic is routed to the target IP address.
[0028] Optionally, selecting the target IP address according to the consistent hash load balancing algorithm specifically includes:
[0029] Construct an identification code for the pod according to the preset rules, then use the identification code to modulo 2^32 to determine that the service is mapped to a ring space in the hash value interval, and map the node IP address to the position on the ring;
[0030] When a request is initiated to allocate data traffic to a node, the parameters attached to the data traffic are mapped to the position on the ring through the constructed identification code, and then the node closest to the position is found clockwise and the data traffic is allocated to the node;
[0031] When a new node joins, it is necessary to find the location of the new node on the ring and insert it to the appropriate position; when a node leaves, it needs to be deleted from the ring and the data traffic related to the node needs to be redistributed to other nodes.
[0032] Optionally, after allocating the data traffic to the node, the system further includes:
[0033] When a new node is added, the position of the new node is found on the ring and inserted into the corresponding position;
[0034] When a node leaves, it is deleted from the ring and the data traffic related to the node is redistributed to other nodes.
[0035] The beneficial effects brought by the technical solution provided by the embodiment of the present application include at least:
[0036] Reduce traditional hash collisions: In Kubernetes, the number of nodes may change at any time, and the online and offline nodes will affect the load balancing algorithm, resulting in uneven load balancing. Using the consistent hashing algorithm can avoid this situation, because only when the number of nodes changes significantly will a large hash collision occur.
[0037] More balanced load: The consistent hashing algorithm can evenly distribute requests to each node according to the node load, avoiding the situation where some nodes are overloaded and cause performance degradation.
[0038] High availability: The consistent hashing algorithm ensures that even if a node fails, it will not affect the overall load balancing performance because requests will be distributed to other available nodes. BRIEF DESCRIPTION OF THE DRAWINGS
[0039] In order to more clearly illustrate the implementation methods of the present invention or the technical solutions in the prior art, the following briefly introduces the drawings required for the implementation methods or the description of the prior art. Obviously, the drawings in the following description are only exemplary, and for ordinary technicians in this field, other implementation drawings can be derived from the provided drawings without creative work.
[0040] Figure 1 A flow chart of a load balancing scheduling method for a Kubernetes cluster provided in an embodiment of the present application;
[0041] Figure 2 This is a schematic diagram of the initial mapping relationship in the embodiment of the present application;
[0042] Figure 3 A schematic diagram of a mapping relationship after a mapping request is obtained in an embodiment of the present application;
[0043] Figure 4 This is a schematic diagram of the mapping relationship after adding a service Pod in an embodiment of the present application;
[0044] Figure 5 This is a schematic diagram of the mapping relationship after deleting the Pod in an embodiment of the present application. DETAILED DESCRIPTION
[0045] In order to make the purpose, technical solution and advantages of the present application more clearly understood, the present application is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present application and are not used to limit the present application.
[0046] In the description of the present invention, the terms "comprises", "has" and any variations thereof are intended to cover non-exclusive inclusions. For example, a process, method, system, product or apparatus comprising a series of steps or units is not necessarily limited to those steps or units explicitly listed, but may also include other steps or units that are not explicitly listed but are inherent to these processes, methods, products or apparatuses, or steps or units added based on further optimization schemes conceived by the present invention.
[0047] This solution uses Kubernetes' built-in mechanism to customize the load balancer. Specifically, it uses golang to implement a consistent hashing load balancing controller and deploys it to the Kubernetes cluster. Then, the annotations field of the Service object is set to the name of the controller. In this way, when Kubernetes needs to create a load balancer for the Service object, the customized controller will be called. For details, please refer to Figure 1 , which shows a flow chart of a load balancing scheduling method for a Kubernetes cluster provided in an embodiment of the present application. The method may include the following steps:
[0048] Step 101, use golang to establish a load balancer controller, and interact with the API of the Kubernetes cluster to be scheduled to establish an interactive relationship.
[0049] The interaction relationship at least includes processing the creation, update and deletion operations of the load balancer.
[0050] In the embodiment of the present application, Kubernetes supports multiple load balancing algorithms, and you can choose the appropriate algorithm according to the application requirements and scenarios. The following are several load balancing algorithms supported by Kubernetes:
[0051] Polling: Send the request to each backend Pod in turn, starting from the first Pod, and sending them in a loop. This algorithm is relatively simple, but it cannot take into account the load of the Pod.
[0052] Minimum number of connections: Send requests to the backend Pod with the least number of connections to ensure balanced distribution of requests and avoid degradation of service performance due to too high number of connections of a certain Pod.
[0053] IP hashing: Calculate a hash value based on the client IP address and send requests with the same hash value to the same backend Pod. This algorithm ensures that requests from the same client are always sent to the same Pod, which is suitable for scenarios where session status needs to be maintained.
[0054] Weighted round-robin: Requests are allocated based on the weight values of the backend Pods. Pods with higher weights receive more requests. This algorithm is suitable for scenarios where some Pods need to handle more requests, such as when some Pods have higher configurations and better performance.
[0055] In Kubernetes, these algorithms are called SessionAffinity properties of the service and can be configured in the Service resource.
[0056] In this step, we implement the load balancer controller using golang, which can interact with the Kubernetes API and handle the creation, update, and deletion of the load balancer. In the controller, we perform the following steps:
[0057] (1) Get the Service object and related Endpoints objects;
[0058] (2) Calculate the load balancer's target port and target IP address list;
[0059] (3) Select the target IP address based on the consistent hashing load balancing algorithm and route the traffic to that address.
[0060] The specific implementation process of consistent hashing includes:
[0061] a. Mapping service:
[0062] Construct a specific identification code for the pod according to certain rules, and then use the identification code to modulo 2^32 to determine the corresponding position of the service in the hash value range. Assume that there are three services, Pod1, Pod2, and Pod3, and their mapping relationship is as follows Figure 2 Schematic diagram of the initial mapping relationship.
[0063] b. Mapping request, positioning service:
[0064] When initiating a request, the attached parameters are used to determine the specific service to be called. Assuming there are requests R1, R2, and R3, their parameters are also subjected to a series of operations such as calculating specific identification codes and taking the remainder, and the following mapping relationship is obtained: Figure 3 The mapping relationship diagram after obtaining the mapping request is shown as follows:
[0065] from Figure 3As you can see, the R1 request is mapped between 0-Pod1, the R2 request is mapped between Pode1-Pod2, and the R3 request is mapped between Pod2-Pod3. The first Pod whose hash value is greater than the request hash value is taken as the actual call service. In other words, the R1 request will call Pod1, the R2 request will call Pod2, and the R3 request will call Pod3.
[0066] c. Add a new Pod:
[0067] Suppose that a new service Pod4 is mapped before Pod3, which happens to destroy the original mapping relationship. For example Figure 4 , gives a schematic diagram of the mapping relationship after obtaining the newly added service Pod.
[0068] In this way, request R3 will actually call service Pod4, but requests R1 and R2 will not be affected.
[0069] d. Delete Pod:
[0070] Assuming that service Pod2 is not working, the R2 request will be mapped to Pod3: Figure 5 The following is a diagram of the mapping relationship after deleting the Pod. The original R1 and R3 requests are not affected. When adding or deleting services, the affected requests are limited. It is not like a simple modulo mapping, where the global mapping relationship needs to be adjusted when the service changes.
[0071] e. Balance and virtual nodes:
[0072] Assume that the three services Pod1, Pod2, and Pod3 are distributed in locations that happen to divide the ring into three equal parts after Hash mapping, and the distribution of requests is basically balanced. However, this is not stable, and virtual nodes are used for further optimization. The so-called virtual nodes, in addition to Hash mapping the service address itself, also do some processing on its address to achieve the purpose of mapping multiple nodes for the same service.
[0073] Step 102: Set the annotations field of the Service object to the name of the load balancer controller in the Kubernetes cluster, and perform load balancing scheduling through the load balancer controller.
[0074] After step 102, the load balancer controller is deployed to the Kubernetes cluster. The controller can be deployed using the Deployment resource object. Finally, the annotations field of the Service object is set to the name of the load balancer controller. The custom load balancing algorithm is combined with the Kubernetes scheduling, and the load balancing scheduling is performed through the load balancer controller.
[0075] In an optional embodiment of the present application, the following steps are generally required to implement consistent hashing:
[0076] Determine the hash function: In consistent hashing, a hash function is needed to map data keys (such as service names, service ports, etc.) into a ring space. Common hash functions include MD5, SHA-1, SHA-256, etc.
[0077] Determine the node location: Select the node location on the ring. Usually, a hash function can be used to map the node name or node IP address to the location on the ring.
[0078] Data distribution: When data traffic needs to be distributed to nodes, the same hash function can be used to map the data key to the position on the ring, and then the node closest to the position is found clockwise to distribute the traffic to the node.
[0079] Node joining and leaving: When a new node joins, it is necessary to find the location of the new node on the ring and insert it into the appropriate position. When a node leaves, it needs to be deleted from the ring and the data traffic related to the node is redistributed to other nodes.
[0080] Use Golang to write a load balancer controller, interact with the Kubernetes API to obtain the Service object and related Endpoints objects, then calculate the target port and target IP address list based on the consistent hashing algorithm, and select the target IP address to route the traffic to the address. When implementing consistent hashing, you can use existing open source libraries or write hash functions and node selection algorithms yourself. It can be seen that consistent hashing is a high load balancing algorithm that can determine the location of the target service based on the keywords of the request (such as URL, request header, etc.). Through the hashing algorithm, the keywords of the request are mapped to a fixed range, and each service is also mapped to a point in this range. When a new service joins or exits, it will only affect a small number of requests, so it can effectively avoid the single point of failure and performance bottleneck problems of traditional load balancing algorithms.
[0081] The present application also provides a load balancing scheduling system for a Kubernetes cluster. The system includes:
[0082] An interaction module is used to establish a load balancer controller using golang, and interact with the API of the Kubernetes cluster to be scheduled to establish an interaction relationship; wherein the interaction relationship at least includes processing the creation, update, and deletion operations of the load balancer;
[0083] The scheduling module is used to set the annotations field of the Service object to the name of the load balancer controller in the Kubernetes cluster and perform load balancing scheduling through the load balancer controller.
[0084] In an optional embodiment of the present application, golang is used in the interaction module to establish a load balancer controller, and interact with the API of the Kubernetes cluster to be scheduled to establish an interactive relationship, including: using the Deployment resource object to deploy the controller.
[0085] In an optional embodiment of the present application, the scheduling module sets the annotations field of the Service object to the name of the load balancer controller in the Kubernetes cluster, including:
[0086] Get the Service object and related Endpoints objects;
[0087] Calculate the list of target ports and target IP addresses of the load balancer controller;
[0088] The target IP address is selected based on the consistent hashing load balancing algorithm and the traffic is routed to the target IP address.
[0089] In an optional embodiment of the present application, the target IP address is selected according to a consistent hash load balancing algorithm, specifically including:
[0090] Construct an identification code for the pod according to the preset rules, then use the identification code to modulo 2^32 to determine that the service is mapped to a ring space in the hash value interval, and map the node IP address to the position on the ring;
[0091] When a request is initiated to allocate data traffic to a node, the parameters attached to the data traffic are mapped to the position on the ring through the constructed identification code, and then the node closest to the position is found clockwise and the data traffic is allocated to the node;
[0092] When a new node joins, it is necessary to find the location of the new node on the ring and insert it to the appropriate position; when a node leaves, it needs to be deleted from the ring and the data traffic related to the node needs to be redistributed to other nodes.
[0093] In an optional embodiment of the present application, after allocating the data traffic to the node, the system further includes:
[0094] When a new node is added, the position of the new node is found on the ring and inserted into the corresponding position;
[0095] When a node leaves, it is deleted from the ring and the data traffic related to the node is redistributed to other nodes.
[0096] The load balancing scheduling system for the Kubernetes cluster provided in the embodiment of the present application is used to implement the load balancing scheduling method for the above-mentioned Kubernetes cluster. For the specific definition of the load balancing scheduling system for the Kubernetes cluster, please refer to the definition of the load balancing scheduling method for the Kubernetes cluster above, which will not be repeated here. The various parts of the load balancing scheduling system for the above-mentioned Kubernetes cluster can be implemented in whole or in part by software, hardware and a combination thereof. The above-mentioned modules can be embedded in or independent of the processor in the device in the form of hardware, or can be stored in the memory in the device in the form of software, so that the processor can call and execute the operations corresponding to the above-mentioned modules.
[0097] The technical features of the above-described embodiments may be arbitrarily combined. To make the description concise, not all possible combinations of the technical features in the above-described embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.
[0098] The above-described embodiments only express several implementation methods of the present application, and the descriptions thereof are relatively specific and detailed, but they cannot be construed as limiting the scope of the patent application. It should be pointed out that, for a person of ordinary skill in the art, several variations and improvements can be made without departing from the concept of the present application, and these all belong to the protection scope of the present application. Therefore, the protection scope of the patent application shall be subject to the attached claims.
Claims
1. A load balancing scheduling method for a Kubernetes cluster, characterized in that: The method comprises: Use golang to establish a load balancer controller and interact with the API of the Kubernetes cluster to be scheduled to establish an interactive relationship; wherein the interactive relationship at least includes processing the creation, update and deletion operations of the load balancer; In the Kubernetes cluster, the annotations field of the Service object is set to the name of the load balancer controller, and load balancing scheduling is performed through the load balancer controller; Performing load balancing scheduling by the load balancer controller includes: Get the Service object and related Endpoints objects; Calculate the list of target ports and target IP addresses of the load balancer controller; Select the target IP address based on the consistent hash load balancing algorithm and route the traffic to the target IP address; The selecting of the target IP address according to the consistent hash load balancing algorithm specifically includes: Construct an identification code for the pod according to the preset rules, then use the identification code to modulo 2^32 to determine that the service is mapped to a ring space in the hash value interval, and map the node IP address to the position on the ring; When a request is initiated to allocate data traffic to a node, the parameters attached to the data traffic are mapped to the position on the ring through the constructed identification code, and then the node closest to the position is found clockwise and the data traffic is allocated to the node; When a new node joins, it is necessary to find the location of the new node on the ring and insert it to the appropriate position; when a node leaves, it needs to be deleted from the ring and the data traffic related to the node needs to be redistributed to other nodes.
2. The method according to claim 1, characterized in that Use golang to build a load balancer controller and interact with the API of the Kubernetes cluster to be scheduled to establish an interactive relationship, including: using the Deployment resource object to deploy the load balancer controller.
3. A load balancing scheduling system for a Kubernetes cluster, characterized in that: The system comprises: An interaction module is used to establish a load balancer controller using golang, and interact with the API of the Kubernetes cluster to be scheduled to establish an interaction relationship; wherein the interaction relationship at least includes processing the creation, update and deletion operations of the load balancer; A scheduling module, used to set the annotations field of the Service object to the name of the load balancer controller in the Kubernetes cluster, and perform load balancing scheduling through the load balancer controller; Performing load balancing scheduling by the load balancer controller includes: Get the Service object and related Endpoints objects; Calculate the list of target ports and target IP addresses of the load balancer controller; Select the target IP address based on the consistent hash load balancing algorithm and route the traffic to the target IP address; The selecting of the target IP address according to the consistent hash load balancing algorithm specifically includes: Construct an identification code for the pod according to the preset rules, then use the identification code to modulo 2^32 to determine that the service is mapped to a ring space in the hash value interval, and map the node IP address to the position on the ring; When a request is initiated to allocate data traffic to a node, the parameters attached to the data traffic are mapped to the position on the ring through the constructed identification code, and then the node closest to the position is found clockwise and the data traffic is allocated to the node; When a new node joins, it is necessary to find the location of the new node on the ring and insert it to the appropriate position; when a node leaves, it needs to be deleted from the ring and the data traffic related to the node needs to be redistributed to other nodes.
4. The system according to claim 3, characterized in that In the interaction module, golang is used to establish a load balancer controller, and it interacts with the API of the Kubernetes cluster to be scheduled to establish an interactive relationship, including: using the Deployment resource object to deploy the load balancer controller.
