Container state adjustment method, device and equipment of k8s cluster and storage medium
By introducing a pre-defined list of disabled Pods and a state adjustment mechanism into the Kubernetes cluster, the problem of the singleness of container state adjustment is solved, and flexible management and adaptive improvement of container state are achieved.
Patent Information
- Application Number
- CN202110503884.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-05-08
- Publication Date
- 2025-11-28
- Estimated Expiration
- 2041-05-08
AI Technical Summary
In existing Kubernetes clusters, container Pods can only be adjusted between normal and abnormal states, which lacks flexibility and makes it impossible to adjust the state online according to actual business needs.
Introducing a pre-defined list of disabled Pods in a Kubernetes cluster involves registering the addresses of container Pods to an Endpoint list while they are running normally, determining whether a Pod belongs to the disabled Pod list, removing it from the Endpoint list and adding it to the disabled Pod list when needed, recording the duration of the disabled Pods, and generating state adjustment instructions to achieve flexible adjustment of the container state.
It increases the flexibility of container state management in Kubernetes clusters, allowing containers to switch between normal and paused states, thus improving the flexibility and adaptability of containers.
Smart Images

Figure CN115309498B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of computer, in particular to a container state adjustment method, device and equipment of K8s cluster and storage medium. BACKGROUND
[0002] Kubernetes, K8s for short, is a container cluster management system, which can realize functions such as automatic deployment and automatic expansion of container cluster.
[0003] The existing container Pod in Kubernetes cluster has the following shortcomings: the state of the Pod has only two states, normal and abnormal, the Pod can be accessed by the service Service in the normal state, and needs to be restarted (restored to normal) in the abnormal state. Correspondingly, the Endpoint between the service Service and the Pod also has only two states, that is, the Pod address is registered in the Endpoint list when the Pod is normal, and the Pod address is deleted in the Endpoint list when the Pod is abnormal.
[0004] However, in fact, during the deployment of the container cluster, the Pod often has an intermediate state, that is, the state of not using the created container temporarily (at this time, the container is not expected to be destroyed). Due to the limitation of the above-mentioned container state, the existing Kubernetes cluster can only adjust the container state between the two states of normal and abnormal, the adjustment dimension is single, and the flexibility is poor. Therefore, how to enable the existing Kubernetes cluster to adjust the container to different states online according to the actual business demand has become a problem to be solved.
[0005] The above content is only used to assist in understanding the technical solutions of the present application, and does not represent the acknowledgement of the above content as prior art. SUMMARY
[0006] The main purpose of the present application is to provide a container state adjustment method, device and equipment of K8s cluster and storage medium, which aims to solve the technical problem that the existing Kubernetes cluster cannot adjust the container to different states online according to the actual business demand, resulting in low flexibility of Kubernetes cluster.
[0007] In order to achieve the above-mentioned purpose, the present application provides a container state adjustment method of K8s cluster, which comprises the following steps:
[0008] When the container Pod in the Kubernetes cluster is running normally, the container address of the container Pod is registered in the Endpoint list;
[0009] determine whether the container Pod belongs to a preset disabled Pod list;
[0010] when the container Pod belongs to the preset inactive Pod list, removing the container address from the Endpoint list and writing the container address to the preset inactive Pod list;
[0011] when a state adjustment instruction corresponding to the container Pod is detected, re-writing the container address stored in the preset inactive Pod list to the Endpoint list.
[0012] Preferably, after the step of removing the container address from the Endpoint list and writing the container address to the preset inactive Pod list, the method further comprises:
[0013] when the container address is written to the preset inactive Pod list, recording the inactive duration of the container address;
[0014] reading the inactive duration threshold corresponding to the container address from the extended Endpoint of the preset inactive Pod list;
[0015] when the inactive duration reaches the inactive duration threshold, generating a state adjustment instruction corresponding to the container Pod.
[0016] Preferably, after the step of removing the container address from the Endpoint list and writing the container address to the preset inactive Pod list, the method further comprises:
[0017] when the container Pod is detected to be running abnormally, removing the container address from the preset inactive Pod list and stopping the running of the container Pod;
[0018] when the stopped container Pod is restarted, returning to execute the step of registering the container address of the container Pod to the Endpoint list when the container Pod in the Kubernetes cluster is normally running.
[0019] Preferably, the method further comprises:
[0020] In a multi-cluster mode, if a cluster state adjustment instruction is detected, determining a Kubernetes cluster to be suspended according to the cluster state adjustment instruction;
[0021] According to a preset cluster suspension strategy, suspending the cluster tenants in the Kubernetes cluster to be suspended one by one to realize the suspension access to the Kubernetes cluster to be suspended.
[0022] Preferably, the step of suspending the cluster tenants in the Kubernetes cluster to be suspended one by one according to the preset cluster suspension strategy comprises the following steps:
[0023] Looping through all the cluster tenants in the Kubernetes cluster to be suspended to obtain a cluster service list corresponding to each cluster tenant;
[0024] Obtaining a cluster service identifier stored in the cluster service list, and determining a cluster service to be suspended according to the cluster service identifier;
[0025] Suspending access to the cluster service to be suspended according to a preset service suspension strategy, so as to realize suspension of the cluster tenant.
[0026] Preferably, the step of suspending access to the cluster service to be suspended according to a preset service suspension strategy comprises the following steps:
[0027] When a service suspension instruction is detected, determining a cluster service to be suspended;
[0028] Calling a preset application program interface to disable a service address of the cluster service to be suspended, so as to realize suspension of the cluster service to be suspended.
[0029] Preferably, the step of suspending access to the cluster service to be suspended according to a preset service suspension strategy further comprises the following steps:
[0030] When a service suspension instruction is detected, determining a cluster service to be suspended;
[0031] Obtaining a KUBE-SVC chain corresponding to the cluster service to be suspended;
[0032] Adding a KUBE-MARK-DROP label to the KUBE-SVC chain, and discarding a request packet in a service access request corresponding to the cluster service to be suspended according to the KUBE-MARK-DROP label when the service access request is received, so as to realize suspension of the cluster service to be suspended.
[0033] In addition, in order to achieve the above-mentioned purpose, the application further provides a container state adjustment device of a K8s cluster, which comprises:
[0034] A container registration module is configured to register a container address of a container Pod in an Endpoint list when the container Pod in a Kubernetes cluster is normally running;
[0035] A container judgment module is configured to judge whether the container Pod belongs to a preset disabled Pod list;
[0036] a container adjustment module, configured to remove the container address from the Endpoint list and write the container address to the preset deactivated Pod list when the container Pod belongs to the preset deactivated Pod list.
[0037] The container adjustment module is further configured to re-write the container address stored in the preset deactivated Pod list to the Endpoint list when a state adjustment instruction corresponding to the container Pod is detected.
[0038] In addition, to achieve the above object, the application further provides a container state adjustment device of a K8s cluster, which comprises a memory, a processor, and a container state adjustment program of a K8s cluster stored in the memory and executable on the processor, wherein the container state adjustment program of a K8s cluster is configured to implement the steps of the container state adjustment method of a K8s cluster as described above.
[0039] In addition, to achieve the above object, the application further provides a storage medium having a container state adjustment program of a K8s cluster stored thereon, wherein the container state adjustment program of a K8s cluster is executable on a processor to implement the steps of the container state adjustment method of a K8s cluster as described above.
[0040] The container address of a container Pod is registered in an Endpoint list when the container Pod is normally running in a Kubernetes cluster, and then it is determined whether the container Pod belongs to a preset deactivated Pod list; when the container Pod belongs to the preset deactivated Pod list, the container address is removed from the Endpoint list and written to the preset deactivated Pod list, and then when a state adjustment instruction corresponding to the container Pod is detected, the container address stored in the preset deactivated Pod list is re-written to the Endpoint list. Since the container address registered in the Endpoint list is removed and then written to the preset deactivated Pod list, the container can be temporarily suspended from providing services externally when the container is normal; meanwhile, when it is necessary to restore access to the suspended container, the container address stored in the preset deactivated Pod list is re-written to the Endpoint list, thereby realizing the state switching of the container from suspended access to normal access. Compared with the existing container which only exists in normal and abnormal states, the above-mentioned method increases the flexibility of K8s cluster container state management. BRIEF DESCRIPTION OF DRAWINGS
[0041] Figure 1 is a structural schematic diagram of a container state adjustment device of a K8s cluster of a hardware running environment related to the embodiment scheme of the application;
[0042] Figure 2A flowchart of a first embodiment of the container state adjustment method for the K8s cluster of the present application is shown in the figure.
[0043] Figure 3 An illustration of the extension of the Endpoint in the first embodiment of the container state adjustment method for the K8s cluster of the present application is shown in the figure.
[0044] Figure 4 A flowchart of a second embodiment of the container state adjustment method for the K8s cluster of the present application is shown in the figure.
[0045] Figure 5 An illustration of the Kubernetes cluster multi-tenant mode in the second embodiment of the container state adjustment method for the K8s cluster of the present application is shown in the figure.
[0046] Figure 6 A structural block diagram of a first embodiment of the container state adjustment apparatus for the K8s cluster of the present application is shown in the figure.
[0047] The implementation, functional features and advantages of the present application will be further described with reference to the embodiments and the accompanying drawings. DETAILED DESCRIPTION
[0048] It should be understood that the specific embodiments described herein are intended to be illustrative only and not limiting of the present application.
[0049] Reference Figure 1 , Figure 1 A structural diagram of the container state adjustment apparatus for the K8s cluster of the hardware running environment involved in the embodiment scheme of the present application is shown in the figure.
[0050] As Figure 1 shown, the container state adjustment apparatus for the K8s cluster can include a processor 1001, such as a central processing unit (CPU), a communication bus 1002, a user interface 1003, a network interface 1004, and a memory 1005. The communication bus 1002 is used to realize the connection and communication between these components. The user interface 1003 can include a display screen, an input unit such as a keyboard, and can also include a standard wired interface, a wireless interface. The network interface 1004 can optionally include a standard wired interface, a wireless interface (such as a wireless fidelity (WIreless-FIdelity, WI-FI) interface). The memory 1005 can be a high-speed random access memory (RAM) memory, or a stable non-volatile memory (Non-Volatile Memory, NVM), such as a magnetic disk memory. The memory 1005 can also be a storage device independent of the aforementioned processor 1001.
[0051] Those skilled in the art can understand that Figure 1 The structure shown in the figure does not constitute a limitation on the container state adjustment device of the K8s cluster, and can include more or fewer components than the figure, or combine certain components, or different component arrangements.
[0052] As Figure 1 As shown, the memory 1005 as a storage medium can include an operating system, a data storage module, a network communication module, a user interface module, and a container state adjustment program of the K8s cluster.
[0053] In Figure 1 In the container state adjustment device of the K8s cluster shown in the figure, the network interface 1004 is mainly used for data communication with the network server; the user interface 1003 is mainly used for data interaction with the user; the processor 1001 and the memory 1005 in the container state adjustment device of the K8s cluster can be arranged in the container state adjustment device of the K8s cluster, and the container state adjustment device of the K8s cluster calls the container state adjustment program of the K8s cluster stored in the memory 1005 through the processor 1001, and executes the container state adjustment method of the K8s cluster provided by the embodiment of the application.
[0054] The container state adjustment method of the K8s cluster of the embodiment, the method adds a disabled Pod list module (i.e. the preset disabled Pod list described below) in the Kubernetes cluster, stores the container address of the Pod which needs to be in the intermediate state (the container is created and not used temporarily) in the preset disabled Pod list, and synchronously cancels the registered container address in the Endpoint list, so that the Pod temporarily does not provide services to the outside, realizes the temporary access of the Kubernetes cluster Pod when the Pod is normal, and improves the flexibility of the Pod.
[0055] Referring to Figure 2 , Figure 2 The flowchart of the first embodiment of the container state adjustment method of the K8s cluster of the application.
[0056] In the embodiment, the container state adjustment method of the K8s cluster includes the following steps:
[0057] Step S10: when the container Pod in the Kubernetes cluster is normally running, the container address of the container Pod is registered in the Endpoint list;
[0058] It should be noted that the execution subject of the method of the embodiment can be a Kubernetes cluster, or a module or device capable of state management of containers in the Kubernetes cluster, and the embodiment does not limit this.
[0059] It can be understood that a Pod is the most basic unit created or deployed by Kubernetes, and each Pod can encapsulate one or more containers, storage resources, independent network IP, and policy options for managing the running mode of the container. The Pod in the Kubernetes cluster has a life cycle and can be created and destroyed. Once destroyed, the life of the Pod ends forever.
[0060] In the Kubernetes cluster, a cluster service (Service) defines a logical grouping of Pods and a policy that can access them. The Pods in the logical grouping can be accessed by the Service, but the Service is not directly connected to the Pod. The Endpoint (a list of IP addresses and ports of a Pod) is between them. The Endpoint is generated after the Service creates the logical grouping of Pods. If the Pod is in a normal state, the corresponding Pod address is added to the Endpoint list; if the Pod is in an abnormal state, the corresponding Pod address is deleted from the Endpoint list.
[0061] In a specific implementation, a container address (IP address and port) of a normally running Pod in the Kubernetes cluster can be created first, and then the address is registered in the Endpoint list. It should be understood that the state of the Pod is normal at this time and can provide services to the outside, but in order to suspend access to the Pod according to actual business needs, so that the Pod is in the above-mentioned intermediate state, the embodiment also needs to determine whether the container needs to be executed by the operation of suspending access through a preset disabled Pod list.
[0062] Step S20: determining whether the container Pod belongs to a preset disabled Pod list;
[0063] It should be noted that the preset disabled Pod list can be a list pre-configured to store the container address of the Pod that needs to be suspended. Referring to Figure 3 In the embodiment, the preset disabled Pod list contains a reference to an extended Endpoint. The extended Endpoint contains a disable duration index field in addition to the original Endpoint. The disable duration index field stores a disable duration threshold for canceling the suspension of the Pod.
[0064] It should be understood that when the identification information (such as name, number, and the like) of the Pod is recorded in the preset disabled Pod list, it indicates that the Pod needs to be suspended from access, and vice versa, when the identification information of the Pod is not recorded in the preset disabled Pod list, it indicates that the Pod currently does not need to be suspended from access.
[0065] In a specific implementation, the identification information corresponding to the Pod can be acquired first, and then it is determined whether the identification information exists in the preset disabled Pod list, and then subsequent operations are performed according to the determination result.
[0066] Step S30: When the container Pod belongs to the preset disabled Pod list, the container address is removed from the Endpoint list and written to the preset disabled Pod list.
[0067] It should be understood that if the container address of the Pod exists in the Endpoint list, it indicates that the Pod can be normally accessed by an application outside the cluster, and after the container address is removed from the Endpoint list, the external application cannot access the container. In the prior art, when a container occurs an abnormal situation such as Crash (generally refers to a computer system, program or server running process, which is stopped due to some reason, or a host, program stops working), the container address of the running abnormal container will be unregistered, that is, the container address will be removed from the Endpoint list, and then after the container is restarted, the container address is created again and registered to the Endpoint list.
[0068] In order to realize the suspension of access of the Pod, the container address removed from the Endpoint list is not discarded in the embodiment, but is written to the preset disabled Pod list, so as to cancel the suspension of access of the container subsequently.
[0069] Step S40: When the state adjustment instruction corresponding to the container Pod is detected, the container address stored in the preset disabled Pod list is written to the Endpoint list again.
[0070] It should be noted that the state adjustment instruction can be a deactivation cancellation instruction of the Pod, that is, an instruction for recovering the Pod suspended from access to accessible. In the embodiment, the generation of the state adjustment instruction can be triggered manually, or triggered by a deactivation duration timeout mechanism. The deactivation duration timeout mechanism can automatically recover the Pod suspended from access to normal access when the duration of the suspension of access of the container exceeds a set deactivation duration threshold.
[0071] In a specific implementation, when the state adjustment instruction corresponding to the container Pod is detected, the container address stored in the preset disabled Pod list is written to the Endpoint list again, so as to realize the online adjustment of the state of the container.
[0072] Further, after the container address stored in the preset deactivated Pod list is written to the Endpoint list again, the Pod can continue to provide services to the outside and respond to the received service access request. Meanwhile, in the embodiment, the Kubernetes cluster can also listen to the changes of the service Service and the corresponding Pod, be responsible for generating and maintaining all Endpoint objects, and then regularly associate the Service and the Pod, so as to ensure that the mapping from the Service to the Pod is always the latest.
[0073] In the container Pod of the Kubernetes cluster in the embodiment, the container address of the container Pod is registered in the Endpoint list, and then it is judged whether the container Pod belongs to the preset deactivated Pod list; when the container Pod belongs to the preset deactivated Pod list, the container address is removed from the Endpoint list and written to the preset deactivated Pod list, and then when the state adjustment instruction corresponding to the container Pod is detected, the container address stored in the preset deactivated Pod list is written to the Endpoint list again. Since the container address registered in the Endpoint list is removed and then written to the preset deactivated Pod list, the container can be suspended from providing services to the outside when it is normal; meanwhile, when it is necessary to restore the access to the suspended container, the container address stored in the preset deactivated Pod list is written to the Endpoint list again, so that the state switching of the container from the suspended access to the normal access is realized, and compared with the existing container which only exists in the normal and abnormal states, the above-mentioned manner of the embodiment increases the flexibility of the state management of the K8s cluster container.
[0074] Further, in order to ensure the timeliness of the container state adjustment and avoid the lag problem when the state adjustment instruction is triggered manually, the container state adjustment method of the K8s cluster provided in the embodiment further includes the following steps before the step S40:
[0075] Step S301: When the container address is written to the preset deactivated Pod list, record the deactivation duration of the container address;
[0076] It should be noted that the recording of the deactivation duration starts from the moment when the container address is written to the preset deactivated Pod list.
[0077] Step S302: Read the deactivation duration threshold corresponding to the container address from the extended Endpoint of the preset deactivated Pod list;
[0078] In the embodiment, the schematic diagram of the extended Endpoint can be referred to Figure 3 For example, Figure 3As shown, the extended Endpoint contains the original Endpoint and the deactivation duration index field. The original Endpoint is used to store the container address, and the deactivation duration index field is used to store the deactivation duration threshold corresponding to the container address.
[0079] Step S303: When the deactivation duration reaches the deactivation duration threshold, a state adjustment instruction corresponding to the container Pod is generated.
[0080] In a specific implementation, by counting the deactivation duration of the container address in real time, and comparing the deactivation duration with the deactivation duration threshold in real time, once it is found that the deactivation duration reaches the deactivation duration threshold, a state adjustment instruction corresponding to the container Pod is generated to cancel the suspended access to the Pod.
[0081] Further, in order to ensure that the container that is suspended can be effectively stopped when an exception occurs, the K8s cluster container state adjustment method provided in this embodiment further includes, after the step S30:
[0082] Step S40': When it is detected that the container Pod runs abnormally, the container address is removed from the preset deactivation Pod list and the running of the container Pod is stopped.
[0083] It can be understood that the container Pod running abnormally can be a container Crash or other abnormal condition. When it is detected that the Pod runs abnormally, it indicates that the Pod has failed to provide services externally and needs to be restarted, at this time, the container address of the Pod needs to be removed from the preset deactivation Pod list, and the running of the Pod needs to be stopped.
[0084] Step S50': When the stopped container Pod is restarted, the step of registering the container address of the container Pod to the Endpoint list when the container Pod in the Kubernetes cluster runs normally is returned.
[0085] In a specific implementation, the Kubernetes cluster will restart the stopped Pod after a period of time, and for the restarted Pod, it can be considered that it has returned to the normal running state, at this time, the above step S10 can be returned and subsequent operations can be performed.
[0086] Reference Figure 4 , Figure 4 is a flowchart of the K8s cluster container state adjustment method of the second embodiment of the present application.
[0087] Based on the first embodiment, in this embodiment, the K8s cluster container state adjustment method includes:
[0088] Step S10': In the multi-cluster mode, if a cluster state adjustment instruction is detected, determine the Kubernetes cluster to be suspended according to the cluster state adjustment instruction.
[0089] It should be noted that in the multi-cluster mode, when a certain cluster is running normally as a whole, if it is necessary to suspend the access to the cluster as a whole, a cluster state adjustment instruction can be manually or automatically triggered. The cluster state adjustment instruction can carry identification information of the Kubernetes cluster that needs to be adjusted (i.e., the Kubernetes cluster to be suspended), which can be unique information such as the name or number of the cluster that can distinguish the cluster from other clusters.
[0090] Step S20': According to a preset cluster suspension strategy, suspend the cluster tenants in the Kubernetes cluster to be suspended one by one to achieve the suspension of the access to the Kubernetes cluster to be suspended.
[0091] It should be noted that the suspension of the access to the Kubernetes cluster can be the suspension of the tenants (Namespaces) owned by the cluster one by one, thereby achieving the suspension of the access to the whole cluster.
[0092] Of course, as another way of suspending the cluster, the present embodiment can also use external load balancing (such as hardware F5) to directly call the F5 LTM iControl REST API to disable the virtual IP address, thereby achieving the suspension of the access to the Service of the Kubernetes cluster when the cluster is normal.
[0093] Specifically, the calling mode of the F5 LTM iControl REST API can be as follows:
[0094] REST Endpoints
[0095] Collection URI
[0096] / mgmt / tm / ltm / virtualmgmt / tm / ltm / virtual
[0097] Calling parameters: disable / enable.
[0098] In the multi-cluster mode, if a cluster state adjustment instruction is detected, the Kubernetes cluster to be suspended is determined according to the cluster state adjustment instruction, and then the cluster tenants in the Kubernetes cluster to be suspended are suspended one by one according to a preset cluster suspension strategy, so that the suspension access of the Kubernetes cluster is realized in the case that the cluster is normal as a whole, and the flexibility of the cluster is improved.
[0099] Further, as shown in Figure 5 if the Kubernetes cluster to be suspended adopts a multi-tenant mode, in order to more efficiently realize the suspension of the tenants, the step S20' in the embodiment can further include:
[0100] Step S201': all cluster tenants in the Kubernetes cluster to be suspended are looped and traversed to obtain a cluster service list corresponding to each cluster tenant;
[0101] It should be understood that the cluster service list can be a name list of all services Service under each cluster tenant.
[0102] In a specific implementation, the cluster tenants (Namespaces) in the Kubernetes cluster to be suspended can be suspended in sequence by using a loop traversal method. Specifically, the services Service under each tenant can be queried by using the API calling method / api / v1 / namespaces / {namespace} / services of the Kubernetes cluster, and then the cluster service list corresponding to each cluster tenant is determined according to the query result.
[0103] Step S202': the cluster service identifiers stored in the cluster service list are obtained, and the cluster services to be suspended are determined according to the cluster service identifiers;
[0104] It should be understood that the cluster service identifiers in this step can be the names, numbers, or other information unique to the services provided by the cluster tenants. After obtaining these cluster service identifiers, the cluster services to be suspended can be determined according to these identifiers.
[0105] Step S203': the cluster services to be suspended are suspended according to a preset service suspension strategy, so as to realize the suspension of the cluster tenants.
[0106] It should be noted that the preset service suspension strategy can be a reference rule and basis pre-configured for performing the suspension access operation on the cluster service. For example, the strategy can be to discard service access request packet data through the IPtables of the package filtering management tool, can also be to use the IPtables for package filtering through the IPVS function provided by the Kubernetes cluster, or can be to use an external load balancing with a disabled virtual IP function, such as a hardware F5, to implement.
[0107] In the tenant mode, the embodiment obtains the cluster service list corresponding to each cluster tenant in the Kubernetes cluster by cyclically traversing all the cluster tenants in the Kubernetes cluster, then obtains the cluster service identifier stored in the cluster service list to determine the cluster service to be suspended, and then suspends the access to the cluster service to be suspended according to the preset service suspension strategy, thereby realizing the suspension access to the tenant under the normal condition of the cluster tenant and improving the flexibility of the cluster tenant.
[0108] Further, as an implementation manner, the suspension access to the cluster service in the embodiment can be performed according to the following steps:
[0109] Step 1: When the service suspension instruction is detected, determine the cluster service to be suspended.
[0110] Step 2: Call a preset application program interface to disable the service address of the cluster service to be suspended, so as to realize the suspension access to the cluster service to be suspended.
[0111] It should be noted that the service suspension instruction in the embodiment can be manually issued or automatically triggered by a monitoring system, and no limitation is made on this. The service suspension instruction carries an identifier for determining the cluster service to be suspended. The preset application program interface can be an F5 LTM iControl REST API interface, which, when called, can disable the virtual IP (VIP) corresponding to the cluster service Service, so that the external application cannot access the service, which is equivalent to suspending the service. For the specific interface calling manner, reference can be made to the above step S20', and details are not described herein.
[0112] By using the external load balancing (such as hardware F5), the embodiment can directly call the F5 LTM iControl REST API to disable the VIP address, thereby realizing the suspension access to the Kubernetes cluster service Service when the Service is normal.
[0113] As another implementation manner, the suspension access to the cluster service in the embodiment can be performed according to the following steps:
[0114] Step a: determining a cluster service to be paused upon detecting a service pause instruction;
[0115] Step b: obtaining a KUBE-SVC chain corresponding to the cluster service to be paused;
[0116] Step c: adding a KUBE-MARK-DROP mark to the KUBE-SVC chain, and discarding a request packet in a service access request according to the KUBE-MARK-DROP mark upon receiving the service access request corresponding to the cluster service to be paused, so as to realize pausing access to the cluster service to be paused.
[0117] It should be noted that IPtables is a packet filtering management tool built on the basis of netfilter (a subsystem introduced in Linux 2.4.x), and the main function is to be used for firewall or transparent proxy.
[0118] In actual application, the Kubernetes cluster extends the chains of IPtables, customizes five chains of KUBE-SERVICES, KUBE-NODEPORTS, KUBE-POSTROUTING, KUBE-MARK-MASQ and KUBE-MARK-DROP, and mainly configures traffic routing rules by adding rules to the KUBE-SERVICES chain (attached to PREROUTING and OUTPUT) to process the request packet. For example, the request packet marked by KUBE-MARK-DROP is discarded.
[0119] It can be understood that the Kubernetes cluster creates a "KUBE-SVC-" chain for each service Service, and imports the data packet in the KUBE-SERVICES chain whose target address is service into the "KUBE-SVC-" chain in the nat table. If the Endpoint has not been created, there is no rule in the KUBE-SVC- chain, and any data packet will be KUBE-MARK-DROP after the rule matching fails.
[0120] Normally, once an Endpoint or Service is created in a Kubernetes cluster, the corresponding Kube-Svc chain will automatically create a rule associated with the Endpoint. Therefore, this embodiment adds a KUBE-MARK-DROP flag to the KUBE-SVC chain corresponding to the cluster service to be paused. This allows the system to retrieve the rule associated with the service's Endpoint when an access request is received, and then discard the request message in the service access request based on the KUBE-MARK-DROP flag recorded in the rule, thus pausing access to the cluster service to be paused.
[0121] Of course, considering that Kubernetes cluster version 1.8 introduced the IPVS mode, which, like the aforementioned IPtables, is based on Netfilter, this embodiment can also utilize the IPVS functionality provided by the Kubernetes cluster to perform packet filtering using IPtables, discarding request packets (KUBE-SVC packets) in service access requests, thereby suspending access to cluster services that need to be suspended. IPVS can use IPsets to store the source or destination addresses of traffic that needs to be dropped.
[0122] This embodiment starts from the actual needs of production and combines Kubernetes cluster scheduling technology, Iptables, IPVS, and load balancing technology to pause services that need to be paused in the cluster, thereby improving the flexibility of cluster services and effectively avoiding the situation where a large number of endpoints are registered and unregistered at the same time, consuming Kubernetes cluster and load balancing resources.
[0123] Furthermore, this embodiment of the invention also proposes a storage medium storing a container state adjustment program for a K8s cluster. When the container state adjustment program for the K8s cluster is executed by a processor, it implements the steps of the container state adjustment method for the K8s cluster as described above.
[0124] Reference Figure 6 , Figure 6 This is a structural block diagram of the first embodiment of the container state adjustment device for the K8s cluster of the present invention.
[0125] like Figure 6 As shown, the container state adjustment device for a K8s cluster proposed in this embodiment of the invention includes:
[0126] The container registration module 601 is used to register the container address of a container Pod to the Endpoint list when the container Pod in the Kubernetes cluster is running normally.
[0127] a container judging module 602, configured to judge whether the container Pod belongs to a preset inactivated Pod list;
[0128] a container adjusting module 603, configured to, when the container Pod belongs to the preset inactivated Pod list, remove the container address from the Endpoint list and write the container address to the preset inactivated Pod list.
[0129] The container adjusting module 603 is further configured to, when a state adjustment instruction corresponding to the container Pod is detected, re-write the container address stored in the preset inactivated Pod list to the Endpoint list.
[0130] In the embodiment, when the container Pod in the Kubernetes cluster is normally running, the container address of the container Pod is registered to the Endpoint list, and then it is judged whether the container Pod belongs to a preset inactivated Pod list; when the container Pod belongs to the preset inactivated Pod list, the container address is removed from the Endpoint list and written to the preset inactivated Pod list, and then when a state adjustment instruction corresponding to the container Pod is detected, the container address stored in the preset inactivated Pod list is re-written to the Endpoint list. Since the container address registered to the Endpoint list is removed and then written to the preset inactivated Pod list, the container can be suspended from providing services externally when the container is normal; meanwhile, when it is needed to restore access to the suspended container, the container address stored in the preset inactivated Pod list is re-written to the Endpoint list, so that the state switching of the container from suspended access to normal access is realized. Compared with the existing container which only exists in normal and abnormal states, the above-mentioned manner of the embodiment increases the flexibility of K8s cluster container state management.
[0131] Based on the first embodiment of the K8s cluster container state adjustment device of the application, a second embodiment of the K8s cluster container state adjustment device of the application is provided.
[0132] In the embodiment, the container adjusting module 603 is further configured to, when the container address is written to the preset inactivated Pod list, record an inactivation duration of the container address; read an inactivation duration threshold corresponding to the container address from an extended Endpoint of the preset inactivated Pod list; and generate a state adjustment instruction corresponding to the container Pod when the inactivation duration reaches the inactivation duration threshold.
[0133] Further, the container adjustment module 603 is further configured to remove the container address from the preset inactivated pod list and stop running of the container pod when it is detected that the container pod runs abnormally; correspondingly, the container registration module 601 is further configured to register the container address of the container pod to the Endpoint list when the stopped container pod is restarted.
[0134] Further, the container state adjustment apparatus of the K8s cluster further comprises a cluster scheduling module configured to, in a multi-cluster mode, determine a Kubernetes cluster to be suspended according to a cluster state adjustment instruction if the cluster state adjustment instruction is detected; and suspend cluster tenants in the Kubernetes cluster to be suspended one by one according to a preset cluster suspension strategy, so as to realize suspension access to the Kubernetes cluster to be suspended.
[0135] Further, the cluster scheduling module is further configured to cyclically traverse all cluster tenants in the Kubernetes cluster to be suspended, so as to obtain a cluster service list corresponding to each cluster tenant; obtain a cluster service identifier stored in the cluster service list, and determine a cluster service to be suspended according to the cluster service identifier; and suspend access to the cluster service to be suspended according to a preset service suspension strategy, so as to realize suspension of the cluster tenant.
[0136] Further, the cluster scheduling module is further configured to, when a service suspension instruction is detected, determine a cluster service to be suspended; and call a preset application program interface to disable a service address of the cluster service to be suspended, so as to realize suspension access to the cluster service to be suspended.
[0137] Further, the cluster scheduling module is further configured to, when a service suspension instruction is detected, determine a cluster service to be suspended; obtain a KUBE-SVC chain corresponding to the cluster service to be suspended; add a KUBE-MARK-DROP mark to the KUBE-SVC chain, and when a service access request corresponding to the cluster service to be suspended is received, discard a request packet in the service access request according to the KUBE-MARK-DROP mark, so as to realize suspension access to the cluster service to be suspended.
[0138] Other embodiments or specific implementations of the container state adjustment apparatus of the K8s cluster can refer to the above-mentioned method embodiments, and will not be described here.
[0139] It should be noted that, in this document, the terms "comprises", "comprising", or any other variation thereof, are intended to cover a non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements does not include only those elements but can also include other elements not expressly listed or inherent to such process, method, article, or apparatus. Without further limitation, an element preceded by "comprises... a" does not, without more constraints, foreclose the existence of additional identical elements in the process, method, article, or apparatus that comprises the recited element.
[0140] The above-mentioned embodiment numbers of the present application are only for description, and do not represent the advantages and disadvantages of the embodiments.
[0141] From the above description of the embodiments, those skilled in the art can clearly understand that the above-mentioned embodiment methods can be realized by means of software and the necessary general hardware platform, and of course, they can also be realized by hardware, but in many cases, the former is a better embodiment. Based on such understanding, the technical solutions of the present application can be embodied in the form of a software product, which is stored in a storage medium (such as a read-only memory / random access memory, a magnetic disk, or an optical disk), and includes a number of instructions for making a terminal device (which can be a mobile phone, a computer, a server, an air conditioner, or a network device, etc.) execute the methods described in the various embodiments of the present application.
[0142] The above is only the preferred embodiment of the present application, and does not limit the patent scope of the present application, and any equivalent structure or equivalent flow transformation made by using the content of the specification and drawings, or directly or indirectly applied to other related technical fields, are also included in the patent protection scope of the present application.
Claims
1. A method for adjusting a container state of a K8s cluster, characterized in that, The method comprises the following steps: registering a container address of a container pod in an Endpoint list when the container pod in a Kubernetes cluster is running normally; determining whether the container pod belongs to a preset deactivated pod list, the preset deactivated pod list being a list preconfigured to store container addresses of pods that need to be suspended from access; when the container pod belongs to the preset deactivated pod list, removing the container address from the Endpoint list and writing the container address to the preset deactivated pod list; when a state adjustment instruction corresponding to the container pod is detected, re-writing the container address stored in the preset deactivated pod list to the Endpoint list, the state adjustment instruction being an instruction to restore a pod suspended from access to an accessible state.
2. The method of claim 1, wherein the K8s cluster container state adjustment method is characterized by, After the step of removing the container address from the Endpoint list and writing the container address to the preset deactivated pod list, the method further comprises: when the container address is written to the preset deactivated pod list, recording a deactivated duration of the container address; reading a deactivated duration threshold corresponding to the container address from an extended Endpoint of the preset deactivated pod list; when the deactivated duration reaches the deactivated duration threshold, generating a state adjustment instruction corresponding to the container pod. 3.The method of claim 1, wherein, After the step of removing the container address from the Endpoint list and writing the container address to the preset deactivated pod list, the method further comprises: when an abnormality of the container pod is detected, removing the container address from the preset deactivated pod list and stopping running of the container pod; when the stopped container pod is restarted, returning to the step of registering the container address of the container pod in the Endpoint list when the container pod in the Kubernetes cluster is running normally.
4. The method of claim 1 to 3, wherein, The method further comprises: in a multi-cluster mode, if a cluster state adjustment instruction is detected, determining a Kubernetes cluster to be suspended according to the cluster state adjustment instruction; suspending cluster tenants in the Kubernetes cluster to be suspended one by one according to a preset cluster suspension strategy, to realize suspension of access to the Kubernetes cluster to be suspended.
5. The method of claim 4, wherein the K8s cluster container state adjustment method is characterized by, The step of suspending the cluster tenants in the Kubernetes cluster to be suspended one by one according to the preset cluster suspension strategy comprises: cyclically iterating through all the cluster tenants in the Kubernetes cluster to be suspended to obtain a cluster service list corresponding to each cluster tenant; obtaining a cluster service identifier stored in the cluster service list, and determining a cluster service to be suspended according to the cluster service identifier; suspending access to the cluster service to be suspended according to a preset service suspension strategy, to realize suspension of the cluster tenant.
6. The method of claim 5, wherein the K8s cluster container state adjustment method is characterized by, The step of suspending access to the cluster service to be suspended according to the preset service suspension strategy comprises: When a service suspension instruction is detected, a cluster service to be suspended is determined; A preset application program interface is called to disable a service address of the cluster service to be suspended, so as to realize suspension access to the cluster service to be suspended.
7. The method of claim 5, wherein the K8s cluster container state adjustment method is characterized by, The step of suspending access to the cluster service to be suspended according to the preset service suspension strategy further includes: When a service suspension instruction is detected, a cluster service to be suspended is determined; A KUBE-SVC chain corresponding to the cluster service to be suspended is obtained; A KUBE-MARK-DROP label is added to the KUBE-SVC chain, and when a service access request corresponding to the cluster service to be suspended is received, a request packet in the service access request is discarded according to the KUBE-MARK-DROP label, so as to realize suspension access to the cluster service to be suspended. 8.A container state adjustment apparatus of a K8s cluster, characterized in that, The device comprises: A container registration module is configured to register a container address of a container Pod to an Endpoint list when the container Pod in a Kubernetes cluster is normally running; A container judgment module is configured to judge whether the container Pod belongs to a preset disabled Pod list, the preset disabled Pod list being a list pre-configured to store a container address of a Pod requiring suspension access; A container adjustment module is configured to remove the container address from the Endpoint list and write the container address to the preset disabled Pod list when the container Pod belongs to the preset disabled Pod list; The container adjustment module is further configured to re-write the container address stored in the preset disabled Pod list to the Endpoint list when a state adjustment instruction corresponding to the container Pod is detected, the state adjustment instruction being an instruction to restore a Pod under suspension access to a state allowing access. 9.A container state adjustment device of a K8s cluster, characterized in that, The device comprises a memory, a processor, and a container state adjustment program of a K8s cluster stored on the memory and executable on the processor, the container state adjustment program of the K8s cluster being configured to implement the steps of the container state adjustment method of the K8s cluster according to any one of claims 1 to 7.
10. A storage medium, characterized by The storage medium has stored thereon a container state adjustment program of a K8s cluster, the container state adjustment program of the K8s cluster being executable by a processor to implement the steps of the container state adjustment method of the K8s cluster according to any one of claims 1 to 7.
Citation Information
Patent Citations
Container management and control method and processing node
CN111552543A
Training task monitoring method and system and computer storage medium
CN112596863A