CN116319280A - Method for electing master and slave nodes based on kubernetes distributed lock - Google Patents

Method for electing master and slave nodes based on kubernetes distributed lock Download PDF

Info

Publication number
CN116319280A
CN116319280A CN202310288732.4A CN202310288732A CN116319280A CN 116319280 A CN116319280 A CN 116319280A CN 202310288732 A CN202310288732 A CN 202310288732A CN 116319280 A CN116319280 A CN 116319280A
Authority
CN
China
Prior art keywords
node
distributed lock
standby
identification code
nodes
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
CN202310288732.4A
Other languages
Chinese (zh)
Inventor
张凯
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
CLP Cloud Digital Intelligence Technology Co Ltd
Original Assignee
CLP Cloud Digital Intelligence Technology Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by CLP Cloud Digital Intelligence Technology Co Ltd filed Critical CLP Cloud Digital Intelligence Technology Co Ltd
Priority to CN202310288732.4A priority Critical patent/CN116319280A/en
Publication of CN116319280A publication Critical patent/CN116319280A/en
Pending legal-status Critical Current

Links

Images

Classifications

    • HELECTRICITY
    • H04ELECTRIC COMMUNICATION TECHNIQUE
    • H04LTRANSMISSION OF DIGITAL INFORMATION, e.g. TELEGRAPHIC COMMUNICATION
    • H04L41/00Arrangements for maintenance, administration or management of data switching networks, e.g. of packet switching networks
    • H04L41/06Management of faults, events, alarms or notifications
    • H04L41/0654Management of faults, events, alarms or notifications using network fault recovery
    • H04L41/0668Management of faults, events, alarms or notifications using network fault recovery by dynamic selection of recovery network elements, e.g. replacement by the most appropriate element after failure
    • HELECTRICITY
    • H04ELECTRIC COMMUNICATION TECHNIQUE
    • H04LTRANSMISSION OF DIGITAL INFORMATION, e.g. TELEGRAPHIC COMMUNICATION
    • H04L41/00Arrangements for maintenance, administration or management of data switching networks, e.g. of packet switching networks
    • H04L41/30Decision processes by autonomous network management units using voting and bidding
    • HELECTRICITY
    • H04ELECTRIC COMMUNICATION TECHNIQUE
    • H04LTRANSMISSION OF DIGITAL INFORMATION, e.g. TELEGRAPHIC COMMUNICATION
    • H04L47/00Traffic control in data switching networks
    • H04L47/10Flow control; Congestion control
    • H04L47/12Avoiding congestion; Recovering from congestion
    • H04L47/125Avoiding congestion; Recovering from congestion by balancing the load, e.g. traffic engineering

Landscapes

  • Engineering & Computer Science (AREA)
  • Computer Networks & Wireless Communication (AREA)
  • Signal Processing (AREA)
  • Information Retrieval, Db Structures And Fs Structures Therefor (AREA)

Abstract

The invention relates to the technical field of micro-services, and provides a method for electing a master node and a slave node based on kubernetes distributed lock, which comprises the following steps: when initializing a starting node, generating a unique identification code for each node according to the time stamp; selecting one node from the initialized nodes as a main node according to the identification code; the method comprises the steps that a distributed lock is adopted to lock an elected main node; and circularly detecting the state of the distributed lock in real time, and updating the master node according to the detected state. According to the kubernetes-based distributed lock election master and slave nodes method, high availability, load balancing and automation of application programs can be improved.

Description

Method for electing master and slave nodes based on kubernetes distributed lock
Technical Field
The invention relates to the technical field of micro-services, in particular to a method for electing a master node and a slave node based on kubernetes distributed locks.
Background
In the Kubernetes platform, the election between microservices is typically handled according to the logic of the application itself, rather than by the Kubernetes platform. For example, in Kubernetes, a microservice is deployed using a reployment object containing multiple copies, where when a copy fails, kubernetes automatically initiates a new copy to replace it. However, if these copies need to be coordinated or elected in some form, the logic would need to be implemented within the application since Kubernetes itself does not handle the election between microservices, but leaves this task to the application itself to handle. A distributed algorithm is typically used to implement the election and run a containerized application with multiple copies in Kubernetes, but this approach is very difficult to implement and is developed.
Therefore, how to provide an implementation method for implementing the high availability master and slave elections, so that all micro services can quickly implement their own high availability functions by means of the method, thereby improving the high availability and expandability of the micro services, and becoming a technical problem to be solved urgently.
Disclosure of Invention
In view of the above, in order to overcome the defects of the prior art, the present invention aims to provide a method for electing a master node and a slave node based on kubernetes distributed lock, which is used for improving the high availability and the expandability of micro services.
The invention provides a kubernetes-based method for selecting master and slave nodes by using a distributed lock, which comprises the following steps:
step S1: when initializing a starting node, generating a unique identification code for each node according to the time stamp;
step S2: selecting one node from the initialized nodes as a main node according to the identification code;
step S3: the method comprises the steps that a distributed lock is adopted to lock an elected main node;
step S4: and circularly detecting the state of the distributed lock in real time, and updating the master node according to the detected state.
As a preferred aspect of the present invention, step S2 includes: and acquiring the time stamp of each node through the identification code, acquiring the creation time of the node from the acquired time stamp, taking the node election with the earliest creation time as a main node, and taking the rest nodes as standby nodes.
As a preferred aspect of the present invention, step S2 further includes: when there are a plurality of nodes with earliest creation time, one node is selected from a plurality of nodes with the same creation time as a master node, and all other nodes are selected as standby nodes.
As a preferred aspect of the present invention, step S3 includes: setting the identification code of the elected master node as the identification code of the distributed lock, and updating the state of the distributed lock into a locking state.
As a preferred aspect of the present invention, the distributed lock in step S3 is a resource object stored in the Etcd key value storage system, where the resource object includes a ConfigMap resource object, a Lease resource object, and a Secret resource object.
As a preferred aspect of the present invention, step S4 of the present invention includes:
all standby nodes except the main node circularly detect the state of the distributed lock in real time;
when the standby node detects that the state of the distributed lock is a locking state, the state of the standby node is maintained;
when the standby node detects that the state of the distributed lock is an unlocked state, selecting one node from the standby node according to the identification code to match with the distributed lock, and judging whether to update the main node according to a matching result.
In a preferred embodiment of the present invention, in step S4, selecting a node from the standby nodes according to the identification code to match with the distributed lock includes: and acquiring the time stamp of each standby node through the identification code, acquiring the creation time of the standby node from the acquired time stamp, and matching the standby node with the earliest creation time with the distributed lock.
In a preferred embodiment of the present invention, in step S4, selecting a node from the standby nodes according to the identification code to match with the distributed lock, the method further includes: when a plurality of standby nodes with the earliest creation time exist, one node is elected from the plurality of standby nodes with the same creation time to be matched with the distributed lock.
Preferably, in step S4, determining whether to update the master node according to the matching result includes:
when the identification code of the selected standby node is consistent with the identification code of the distributed lock, updating the standby node into a new main node, and performing locking setting on the new main node by adopting the distributed lock;
when the identification code of the selected standby node is inconsistent with the identification code of the distributed lock, the state of the standby node is maintained, and the state of the distributed lock is circularly detected in real time.
Finally, the invention also provides a computer device, which comprises a memory, a processor and a computer program stored in the memory and capable of running on the processor, wherein the method for selecting the master and slave nodes based on kubernetes distributed lock is realized when the processor executes the program.
The method for electing the master and slave nodes based on kubernetes distributed lock has the following beneficial effects:
1. high availability of the application is ensured, and when the master node Pod fails, the Kubernetes can be assisted in determining which existing Pod is the most suitable alternative to the failed master Pod.
2. The traffic can be dynamically distributed to the available Pod, so that the load balance of each Pod is ensured, and the performance degradation caused by overload of a certain Pod is avoided.
3. The most suitable Pod can be selected according to the preset rules and strategies, so that the burden of manual intervention is reduced, and the stability and reliability of the system are improved.
Drawings
In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the drawings that are needed in the embodiments will be briefly described below, and it is obvious that the drawings in the following description are only some embodiments of the present invention, and that other drawings can be obtained according to these drawings without inventive effort for a person skilled in the art.
Fig. 1 is a flowchart of a method for electing a master and slave node based on kubernetes distributed lock according to an exemplary first embodiment of the present invention.
Fig. 2 is a schematic diagram of a method for electing a master node and a slave node based on kubernetes distributed lock according to a fourth embodiment of the present invention.
Fig. 3 is a schematic structural diagram of the apparatus provided by the present invention.
Detailed Description
Embodiments of the present invention will be described in detail below with reference to the accompanying drawings.
It should be noted that, without conflict, the following embodiments and features in the embodiments may be combined with each other; and, based on the embodiments in this disclosure, all other embodiments that may be made by one of ordinary skill in the art without inventive effort are within the scope of the present disclosure.
It is noted that various aspects of the embodiments are described below within the scope of the following claims. It should be apparent that the aspects described herein may be embodied in a wide variety of forms and that any specific structure and/or function described herein is merely illustrative. Based on the present disclosure, one skilled in the art will appreciate that one aspect described herein may be implemented independently of any other aspect, and that two or more of these aspects may be combined in various ways. For example, an apparatus may be implemented and/or a method practiced using any number of the aspects set forth herein. In addition, such apparatus may be implemented and/or such methods practiced using other structure and/or functionality in addition to one or more of the aspects set forth herein.
The technical principle of the invention is as follows:
distributed locks are implemented through resource objects stored in the Etcd key value storage system. In particular, it may be implemented by a variety of resources on kubernetes, such as configmap, lease, secret stored in Etcd, optionally one of them. The main purpose is to store the two key information of the lock identity code ID and the lock state. Each node can get all the data they store in real time. The application programs distributed on different nodes can take the same data, and the problem of distributed data difference is solved.
Each node competes with the distributed lock through an election mechanism, the node which is preferentially taken to the distributed lock is upgraded to be a main node, and the rest nodes are used as standby nodes; the distributed lock may store a lock identity and a lock status. The lock identity identification code is used for indicating the identity of the lock; the lock state is divided into a locked state and an unlocked state, and the lock state is the unlocked state during initialization; after the node is elected to become the master node, the lock state is set to the locked state. At this point the other nodes are in a waiting race condition.
The terms in the following examples are explained as follows:
kubernetes is an open-source container orchestration platform, initiated and maintained by Google, and aims to simplify the deployment, expansion and management of container applications, and it provides a declarative way to define operations in terms of deployment, service discovery, load balancing, automatic expansion and contraction, automatic failure recovery, etc. of containerized applications.
Etcd-a highly available, distributed key-value store system designed to build a distributed system. Are commonly used as the underlying data store for Kubernetes clusters for storing cluster configuration information, service discovery information, etc.
Configmap, a resource object in Kubernetes, is used to separate configuration data from an application and store it in the form of key-value pairs. ConfigMap can store different types of data, such as strings, integers, or files. These data can be used by the containerized application without being hard coded in the container image.
A resource object in Kubernetes, which holds a specific name for a period of time. A Lease may be used to ensure that only one process or instance may access certain shared resources, thereby avoiding race conditions.
Secret, a resource object in Kubernetes, is used to store sensitive data such as passwords, API keys, certificates, etc. Similar to ConfigMap, secret can also store different types of data, such as strings, integers, or files.
Fig. 1 is a flowchart of a method for electing a master/slave node based on kubernetes distributed lock according to an exemplary first embodiment of the present invention, where, as shown in fig. 1, the method of the present embodiment includes:
step S1: when initializing a starting node, generating a unique identification code for each node according to the time stamp;
step S2: selecting one node from the initialized nodes as a main node according to the identification code;
step S3: the method comprises the steps that a distributed lock is adopted to lock an elected main node;
step S4: and circularly detecting the state of the distributed lock in real time, and updating the master node according to the detected state.
An exemplary second embodiment of the present invention provides a method for electing a master node and a slave node based on kubernetes distributed lock, and this embodiment is a preferred embodiment of the method shown in fig. 1.
In practical application, step S2 of the method of the present embodiment is implemented by the following manner: and acquiring the time stamp of each node through the identification code, acquiring the creation time of the node from the acquired time stamp, taking the node election with the earliest creation time as a main node, and taking the rest nodes as standby nodes. When there are a plurality of nodes with earliest creation time, one node is selected from a plurality of nodes with the same creation time as a master node, and all other nodes are selected as standby nodes.
An exemplary third embodiment of the present invention provides a method for electing a master node and a slave node based on kubernetes distributed lock, and this embodiment is a preferred embodiment of the method shown in fig. 1.
In practical application, step S3 of the method of the present embodiment is implemented by: setting the identification code of the elected master node as the identification code of the distributed lock, and updating the state of the distributed lock into a locking state. Specifically, the distributed lock of the method of the embodiment is a resource object stored in the Etcd key value storage system, where the resource object includes a ConfigMap resource object, a Lease resource object, and a Secret resource object.
Fig. 2 is a schematic diagram of a method for electing a master/slave node based on kubernetes distributed lock according to a fourth exemplary embodiment of the present invention, which is a preferred embodiment of the method shown in fig. 1.
As shown in fig. 2, step S4 of the method of the present embodiment includes:
all standby nodes except the main node circularly detect the state of the distributed lock in real time;
when the standby node detects that the state of the distributed lock is a locking state, the state of the standby node is maintained;
when the standby node detects that the state of the distributed lock is an unlocked state, selecting one node from the standby node according to the identification code to match with the distributed lock, and judging whether to update the main node according to a matching result.
Specifically, in practical application, in step S4 of the method of the embodiment, a node is selected from the standby nodes according to the identification code to be matched with the distributed lock, and the method is implemented in the following manner:
and acquiring the time stamp of each standby node through the identification code, acquiring the creation time of the standby node from the acquired time stamp, and matching the standby node with the earliest creation time with the distributed lock.
When a plurality of standby nodes with the earliest creation time exist, one node is elected from the plurality of standby nodes with the same creation time to be matched with the distributed lock.
Specifically, in actual application, in step S4 of the method of the present embodiment, determining whether to update the master node according to the matching result includes:
when the identification code of the selected standby node is consistent with the identification code of the distributed lock, the standby node is updated to be a new main node, the distributed lock is adopted to lock the new main node, and the main node is responsible for executing stateful tasks.
When the identification code of the selected standby node is inconsistent with the identification code of the distributed lock, the state of the standby node is maintained, the state of the distributed lock is circularly detected in real time, and the standby node is ready to become a master node at any time.
As shown in FIG. 3, the present invention also provides an apparatus comprising a processor 110, a communication interface 120, a memory 130 for storing a processor executable computer program, and a communication bus 140. The processor 110, the communication interface 120, and the memory 130 perform communication with each other through the communication bus 140. The processor 110 implements the kubernetes-based distributed lock election master and slave node method described above by running an executable computer program.
Wherein the computer program in the memory 130 may be implemented in the form of software functional units and may be stored in a computer readable storage medium when sold or used as a separate product. Based on such understanding, the technical solution of the present application may be embodied essentially or in a part contributing to the prior art or in a part of the technical solution, in the form of a software product stored in a storage medium, including several instructions for causing a computer device (which may be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the methods of the embodiments of the present application. And the aforementioned storage medium includes: a U-disk, a removable hard disk, a Read-Only Memory (ROM), a random access Memory (RAM, random Access Memory), a magnetic disk, or an optical disk, or other various media capable of storing program codes.
The system embodiments described above are merely illustrative, in which elements illustrated as separate elements may or may not be physically separate, and elements shown as elements may or may not be physical elements, may be located in one place, or may be distributed over a plurality of network elements. Some or all of the modules may be selected based on actual needs to achieve the purpose of the embodiment. Those of ordinary skill in the art will understand and implement the present invention without undue burden.
From the above description of the embodiments, it will be apparent to those skilled in the art that the embodiments may be implemented by means of software plus necessary general hardware platforms, or of course may be implemented by means of hardware. Based on such understanding, the foregoing technical solutions may be embodied essentially or in part in the form of a software product, which may be stored in a computer-readable storage medium, such as a ROM/RAM, a magnetic disk, an optical disk, etc., including several instructions to cause a computer device (which may be a personal computer, a server, or a network device, etc.) to perform the various embodiments or methods of some parts of the embodiments.
The foregoing is merely illustrative of the present invention, and the present invention is not limited thereto, and any changes or substitutions easily contemplated by those skilled in the art within the scope of the present invention should be included in the present invention. Therefore, the protection scope of the invention is subject to the protection scope of the claims.

Claims (10)

1. A method for electing a master and slave node based on kubernetes distributed lock, the method comprising:
step S1: when initializing a starting node, generating a unique identification code for each node according to the time stamp;
step S2: selecting one node from the initialized nodes as a main node according to the identification code;
step S3: the method comprises the steps that a distributed lock is adopted to lock an elected main node;
step S4: and circularly detecting the state of the distributed lock in real time, and updating the master node according to the detected state.
2. The kubernetes-based distributed lock election primary and backup node method of claim 1, wherein step S2 comprises: and acquiring the time stamp of each node through the identification code, acquiring the creation time of the node from the acquired time stamp, taking the node election with the earliest creation time as a main node, and taking the rest nodes as standby nodes.
3. The kubernetes-based distributed lock election primary and backup node method of claim 2, wherein step S2 further comprises: when there are a plurality of nodes with earliest creation time, one node is selected from a plurality of nodes with the same creation time as a master node, and all other nodes are selected as standby nodes.
4. The kubernetes-based distributed lock election primary and backup node method of claim 1, wherein step S3 comprises: setting the identification code of the elected master node as the identification code of the distributed lock, and updating the state of the distributed lock into a locking state.
5. The kubernetes-based distributed lock election primary and backup node method of claim 1, wherein the distributed lock in step S3 is a resource object stored in an Etcd key value storage system, the resource object including a ConfigMap resource object, a Lease resource object, and a Secret resource object.
6. The kubernetes-based distributed lock election primary and backup node method of claim 1, wherein step S4 comprises:
all standby nodes except the main node circularly detect the state of the distributed lock in real time;
when the standby node detects that the state of the distributed lock is a locking state, the state of the standby node is maintained;
when the standby node detects that the state of the distributed lock is an unlocked state, selecting one node from the standby node according to the identification code to match with the distributed lock, and judging whether to update the main node according to a matching result.
7. The method for electing master and slave nodes based on kubernetes distributed lock according to claim 6, wherein in step S4, electing a node from the slave nodes to match with the distributed lock according to the identification code comprises: and acquiring the time stamp of each standby node through the identification code, acquiring the creation time of the standby node from the acquired time stamp, and matching the standby node with the earliest creation time with the distributed lock.
8. The method for electing a master node and a slave node based on kubernetes distributed locks according to claim 6, wherein in step S4, an election node from the slave nodes is matched with the distributed locks according to the identification code, and further comprising: when a plurality of standby nodes with the earliest creation time exist, one node is elected from the plurality of standby nodes with the same creation time to be matched with the distributed lock.
9. The kubernetes-based distributed lock election primary and backup node method according to claim 6, wherein in step S4, determining whether to update the primary node according to the matching result comprises:
when the identification code of the selected standby node is consistent with the identification code of the distributed lock, updating the standby node into a new main node, and performing locking setting on the new main node by adopting the distributed lock;
when the identification code of the selected standby node is inconsistent with the identification code of the distributed lock, the state of the standby node is maintained, and the state of the distributed lock is circularly detected in real time.
10. A computer device comprising a memory, a processor and a computer program stored on the memory and executable on the processor, the processor implementing the steps of the method according to any one of claims 1-9 when the program is executed.
CN202310288732.4A 2023-03-23 2023-03-23 Method for electing master and slave nodes based on kubernetes distributed lock Pending CN116319280A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202310288732.4A CN116319280A (en) 2023-03-23 2023-03-23 Method for electing master and slave nodes based on kubernetes distributed lock

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202310288732.4A CN116319280A (en) 2023-03-23 2023-03-23 Method for electing master and slave nodes based on kubernetes distributed lock

Publications (1)

Publication Number Publication Date
CN116319280A true CN116319280A (en) 2023-06-23

Family

ID=86781359

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202310288732.4A Pending CN116319280A (en) 2023-03-23 2023-03-23 Method for electing master and slave nodes based on kubernetes distributed lock

Country Status (1)

Country Link
CN (1) CN116319280A (en)

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN116938881A (en) * 2023-09-18 2023-10-24 深圳创新科技术有限公司 Method, system, equipment and readable storage medium for realizing dynamic IP pool

Cited By (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN116938881A (en) * 2023-09-18 2023-10-24 深圳创新科技术有限公司 Method, system, equipment and readable storage medium for realizing dynamic IP pool
CN116938881B (en) * 2023-09-18 2024-02-09 深圳创新科技术有限公司 Method, system, equipment and readable storage medium for realizing dynamic IP pool

Similar Documents

Publication Publication Date Title
US20200177451A1 (en) Distributed lifecycle management for cloud platforms
US10178184B2 (en) System and method for session handling in a multitenant application server environment
US7698391B2 (en) Performing a provisioning operation associated with a software application on a subset of the nodes on which the software application is to operate
US20240135257A1 (en) System and method for self-healing in decentralized model building for machine learning using blockchain
US7760743B2 (en) Effective high availability cluster management and effective state propagation for failure recovery in high availability clusters
US7536582B1 (en) Fault-tolerant match-and-set locking mechanism for multiprocessor systems
EP2754059B1 (en) Clustered client failover
US20190258646A1 (en) Distributed transactions across multiple consensus groups
US11269927B2 (en) Transactional replicator
JP2004342079A (en) Method for operating computer cluster
US10049022B2 (en) Systems and methods to retain and reclaim resource locks and client states after server failures
WO2016177130A1 (en) Method and device for selecting communication node
KR20190041033A (en) Replaying jobs at a secondary location of a service
US9760529B1 (en) Distributed state manager bootstrapping
US10505805B1 (en) Computing resources checkpoint restoration
CN116319280A (en) Method for electing master and slave nodes based on kubernetes distributed lock
US20220138036A1 (en) Safely recovering workloads within a finite timeframe from unhealthy cluster nodes
CN114238495A (en) Method and device for switching main cluster and standby cluster of database, computer equipment and storage medium
CN108600284B (en) Ceph-based virtual machine high-availability implementation method and system
US9183092B1 (en) Avoidance of dependency issues in network-based service startup workflows
CN110830582B (en) Cluster owner selection method and device based on server
CN111291063B (en) Master and backup copy election method, system, computer equipment and storage medium
US11290318B2 (en) Disaster recovery of cloud resources
KR102294048B1 (en) Method and system for replicating blockchain application service
CN114610545A (en) Method, system, device and medium for reducing single point of failure of private cloud computing

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination