A Kubernetes patch strategy operation and maintenance method and device based on syntax tree
Through the syntax tree-based Kubernetes patch strategy operation and maintenance method, the configuration update of the Kubernetes cluster is automatically processed, which solves the difficulty and error-prone problems of manually modifying the configuration in the existing technology, and achieves flexible management and safe operation and maintenance of multi-cluster resources.
Patent Information
- Application Number
- CN202410798057.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-06-20
- Publication Date
- 2024-09-10
- Estimated Expiration
- 2044-06-20
AI Technical Summary
In the existing technology, when managing a multi-cluster Kubernetes cluster, you need to manually enter each namespace to modify the configuration individually, which increases the difficulty of operation and maintenance operations and is prone to errors.
Using the Kubernetes patch strategy operation and maintenance method based on syntax trees, automatic updates to resource objects are achieved by obtaining configurations, parsing configurations, building syntax trees, modifying syntax trees, and rendering the final yaml files, avoiding the need to manually specify namespaces and direct modifications. yaml file requirements.
It simplifies operation and maintenance operations, reduces error rates, and enables flexible management and safe operation and maintenance of multi-cluster resource configurations.
Smart Images

Figure CN118363645B
Abstract
Description
Technical Field
[0001] The present application relates to the technical field of Kubernetes cluster patch upgrade, and in particular to a Kubernetes patch strategy operation and maintenance method and device based on a syntax tree. Background Art
[0002] Kubernetes cluster is a platform for automated deployment, expansion and management of containerized applications. It supports large-scale, highly available applications. With its powerful container orchestration capabilities, high portability and scalability, automated operations and extensive community support, Kubernetes cluster provides users with an efficient, reliable and flexible platform to run and manage containerized applications.
[0003] Generally, Internet enterprise operation and maintenance personnel use kubernetes clusters. In the existing technology, when dealing with the same service in multiple clusters, they manually enter each namespace to modify the configuration separately, and the modification takes effect only after restarting. Because each operation requires confirmation of the namespace to be operated, and strict attention must be paid to the yaml format syntax in the operation, the operation and maintenance operations are much more difficult. If an operation error occurs, it may cause platform failure. Summary of the invention
[0004] The purpose of this application is to overcome the problems in the prior art of requiring manual modification of the configuration of each namespace individually, which makes the operation and maintenance operations difficult and error-prone, and to provide a Kubernetes patch strategy operation and maintenance method and device based on a syntax tree.
[0005] First, a syntax tree-based kubernetes patch strategy operation and maintenance method is provided, including:
[0006] Get the configuration used to implement patch strategy operations;
[0007] Parse the configuration used to implement patch strategy operation and maintenance to obtain the namespace and resource object to be modified, and obtain the current YAML file of the resource object to be changed;
[0008] Construct a syntax tree according to the yaml file;
[0009] Modifying the yaml file in the syntax tree according to the configuration for implementing patch strategy operation and maintenance, and rendering a final yaml file according to the modified syntax tree;
[0010] Update resource objects through the kubernetes cluster interface according to the final yaml file.
[0011] In some possible implementations, the method further includes: submitting the configuration for implementing patch strategy operation and maintenance to an etcd cluster for storage.
[0012] In some possible implementations, the configuration for implementing patch policy operation and maintenance includes patch type, patch data, and a namespace for patch resource effects.
[0013] In some possible implementations, the type of the changed resource object is: deployment, statsfulset, service, or daemonset.
[0014] In some possible implementations, constructing a syntax tree according to the yaml file includes: first creating a root node, if the root node has a child node, creating a child node of the root node, if the child node still has a child node, continuing to create a child node of the child node.
[0015] In some possible implementations, the yaml file is modified in the syntax tree according to the configuration used to implement patch strategy operation and maintenance, including: starting from the root node of the syntax tree to find the changed node, determining the operation type of the changed node, wherein the operation type includes addition, replacement and deletion, and modifying the changed node according to the configuration used to implement patch strategy operation and maintenance to obtain a modified syntax tree.
[0016] In a second aspect, a Kubernetes patch strategy operation and maintenance device based on a syntax tree is provided, including:
[0017] An interface module for obtaining configurations for implementing patch strategy operations;
[0018] The parsing module is used to parse the configuration used to implement patch strategy operation and maintenance to obtain the namespace and resource object to be modified, and obtain the current YAML file of the resource object to be changed;
[0019] A syntax tree construction module, used to construct a syntax tree according to the YAML file;
[0020] A modification module, used to modify the yaml file in the syntax tree according to the configuration for implementing patch strategy operation and maintenance, and render a final yaml file according to the modified syntax tree;
[0021] The application module is used to update resource objects through the kubernetes cluster interface according to the final yaml file.
[0022] In a third aspect, a computer program product comprising instructions is provided, which, when executed on a computer, enables the computer to execute the method described in the first aspect.
[0023] In a fourth aspect, a computer-readable storage medium is provided, wherein the computer-readable medium stores a program code for execution by a device, wherein the program code includes steps for executing the method in any one of the implementations of the first aspect described above.
[0024] In a fifth aspect, an electronic device is provided, comprising a processor, a memory, and a program or instruction stored in the memory and executable on the processor, wherein the program or instruction, when executed by the processor, implements a method as in any one of the implementations in the first aspect above.
[0025] The present application has the following beneficial effects: the present application enables operation and maintenance personnel to manage multi-cluster services without manually specifying namespaces or directly modifying the YAML files of services. Instead, they only need to modify the configuration used to implement patch strategy operation and maintenance. The principle of syntax tree routing matching is adopted, and resource configuration can be flexibly modified. Compared with manual modification in the prior art, the present application has the advantage of being less prone to errors, thereby achieving the purpose of simplified operation and safe operation and maintenance. BRIEF DESCRIPTION OF THE DRAWINGS
[0026] The drawings constituting a part of the present application are used to provide a further understanding of the present application. The illustrative embodiments of the present application and their descriptions are used to explain the present application and do not constitute an improper limitation on the present application.
[0027] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the drawings required for use in the description of the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without creative work.
[0028] Figure 1 It is a flowchart of the syntax tree-based kubernetes patch strategy operation and maintenance method of Example 1 of the present application;
[0029] Figure 2 It is an example diagram of a syntax tree constructed in the syntax tree-based kubernetes patch strategy operation and maintenance method of Example 1 of the present application;
[0030] Figure 3 It is an example diagram of a modified syntax tree in the syntax tree-based kubernetes patch strategy operation and maintenance method of Example 1 of the present application;
[0031] Figure 4 It is a structural block diagram of a Kubernetes patch strategy operation and maintenance device based on a syntax tree according to Example 2 of the present application;
[0032] Figure 5 It is a schematic diagram of the internal structure of the electronic device of Example 5 of the present application.
[0033] Reference numerals:
[0034] 100, interface module; 200, parsing module; 300, syntax tree construction module; 400, modification module; 500, application module. DETAILED DESCRIPTION
[0035] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0036] Example 1
[0037] A syntax tree-based kubernetes patch strategy operation and maintenance method involved in Example 1 of the present application includes: obtaining a configuration for implementing patch strategy operation and maintenance; parsing the configuration for implementing patch strategy operation and maintenance to obtain the namespace and resource objects to be modified, and obtaining the current yaml file of the changed resource objects; building a syntax tree according to the yaml file; modifying the yaml file in the syntax tree according to the configuration for implementing patch strategy operation and maintenance, and rendering a final yaml file according to the modified syntax tree; updating the resource object through the kubernetes cluster interface according to the final yaml file. This method allows operation and maintenance staff to manage multi-cluster services without manually specifying the namespace or directly modifying the yaml file of the service. They only need to modify the configuration for implementing patch strategy operation and maintenance. The principle of syntax tree routing matching is adopted, and resource configuration can be flexibly modified. Compared with manual modification in the prior art, it has the advantage of being less prone to errors, thereby achieving the purpose of simplified operation and safe operation and maintenance.
[0038] Specifically, Figure 1 A flowchart of the syntax tree-based kubernetes patch strategy operation and maintenance method in application embodiment 1 is shown, including:
[0039] S100, obtaining a configuration for implementing patch strategy operation and maintenance;
[0040] Specifically, users create or update StrategyOperation (configuration for implementing patch strategy operation and maintenance, also a Kubernetes resource object) through the Kubernetes apiserver (the communication interface of the Kubernetes cluster). StrategyOperation contains the following information: patch type, patch data, and the namespace to which the patch resource applies. The Kubernetes apiserver submits StrategyOperation to the etcd cluster (the etcd cluster is used to store cluster resource object data) for storage.
[0041] S200, parsing the configuration for implementing patch strategy operation and maintenance to obtain the namespace to be modified and the changed resource object, and obtaining the current YAML file of the changed resource object, wherein the data in the kubernetes cluster are collectively referred to as resource objects;
[0042] Specifically, when the patch controller (patch controller is a service for implementing current technology) obtains changes to the StrategyOperation resource object through the kubernetes apiserver, it parses the StrategyOperation resource object, first confirms the namespace to be modified by StrategyOperation and the resource object to be changed, and then obtains the current yaml file content of the changed resource object (the content can refer to the example, the format of different resource objects is different). The changed resource object supports deployment, statsfulset, service, daemonset and other types. Among them, deployment is a resource object used in Kubernetes to describe the deployment and update strategy of an application. statsfulset is a resource object used in Kubernetes to manage stateful applications. Service is the core concept of implementing microservice architecture in Kubernetes. It provides a unified entry address for a group of Pods with the same function and realizes load balancing. Daemonset is a controller in Kubernetes used to ensure that a copy of a Pod runs on each node (or a node that meets specific conditions).
[0043] Take the deployment resource object named webservice-app in the namespace test as an example. The contents of the yaml file are as follows:
[0044] apiVersion: apps / v1 # api version
[0045] kind: Deployment # Role of the resource
[0046] metadata: # Resource metadata
[0047] name: webservice-app # Resource name
[0048] namespace: test # namespace
[0049] spec: # Resource specification field
[0050] selector: # Selector
[0051] matchLabels: # Matching labels
[0052] app: webservice-app # app tag
[0053] replicas: 2 # Number of service replicas
[0054] template: # Service template
[0055] metadata: #Metadata of the service
[0056] labels: # Labels
[0057] app: webservice-app # app tag
[0058] spec: # Resource specification field
[0059] containers: #Container group
[0060] - name: webservice-app #Container name
[0061] image: webservice-app:1.14.2 # Container image
[0062] ports: #Container port group
[0063] - containerPort: 80 # container port
[0064] env: # Environment variables
[0065] - name: username # Environment variable key
[0066] value: admin # Environment variable value.
[0067] S300, constructing a syntax tree according to the yaml file;
[0068] Specifically, the patch controller builds a syntax tree based on the yaml content of webservice-app: First, a root node is created, which has four child nodes: apiVersion, kind, metadata, and spec. Metadata has two child nodes: name and namespace. The child nodes of other nodes are similar to metadata. The resulting syntax tree content is as follows: Figure 2 shown.
[0069] S400, modifying the yaml file in the syntax tree according to the configuration for implementing patch strategy operation and maintenance, and rendering the final yaml file according to the modified syntax tree, specifically including: searching from the root node of the syntax tree until the changed node is found, determining the operation type of the changed node, wherein the operation type includes adding, replacing and deleting, and modifying the changed node according to the configuration for implementing patch strategy operation and maintenance to obtain a modified syntax tree;
[0070] Specifically, the patch controller modifies the current yaml file content according to the strategy defined by StrategyOperation. Take the following StrategyOperation as an example:
[0071] apiVersion: apps / v1 # api version
[0072] kind: StrategyOperation # Resource role
[0073] metadata: # Resource metadata
[0074] name: test-StrategyOperation # Resource name
[0075] spec: # Resource specification
[0076] policies: # Policy details
[0077] - name: test-polices # Policy name
[0078] type: deployment # Policy action type
[0079] name: webservice-app # Policy action name
[0080] namespace: test # Policy action namespace
[0081] parameter: # Strategy parameters
[0082] app_version: 1 # Policy parameter details
[0083] operation: # Operation details
[0084] type: augmentKey # operation type
[0085] scope: | # Scope
[0086] {
[0087] spec: template: spec: { # scope key
[0088] containers: [{
[0089] name: context.name
[0090] env: [
[0091] for k, v in parameter.env { #scope value
[0092] name: k
[0093] value: v
[0094] }, ]
[0096] }]
[0097] }
[0098] }.
[0099] The patch controller will first start searching from the spec node in the syntax tree generated above until it finds the env node under containers. It then determines that the current type is augmentationKey (indicating a new env configuration). The configuration parameters are taken from the parameters in policies. The final modified syntax tree result is as follows: Figure 3 As shown, the patch controller renders the final YAML file according to the modified syntax tree above as follows:
[0100] apiVersion: apps / v1 # api version
[0101] kind: Deployment
[0102] metadata: # Resource metadata
[0103] name: webservice-app # Resource name
[0104] namespace: test # namespace
[0105] spec: # Resource specification field
[0106] selector: # Selector
[0107] matchLabels: # Matching labels
[0108] app: webservice-app # app tag
[0109] replicas: 2 # Number of service replicas
[0110] template: # Service template
[0111] metadata: #Metadata of the service
[0112] labels: # Labels
[0113] app: webservice-app # app tag
[0114] spec: # Resource specification field
[0115] containers: #Container group
[0116] - name: webservice-app #Container name
[0117] image: webservice-app:1.14.2 # Container image
[0118] ports: #Container port group
[0119] - containerPort: 80 # container port
[0120] env: # Environment variables
[0121] - name: username # Environment variable key
[0122] value: admin # Environment variable value
[0123] - name: app_version # Add a new environment variable key
[0124] value: 1 # Add new environment variable value.
[0125] In the above embodiment, there are three types of operations: add (i.e., augmentKey), replace (i.e., replaceKey), and delete (i.e., deleteKey), which respectively represent adding a new node, replacing a node, and deleting a node.
[0126] Add a new node: add a new configuration if the node does not exist, and ignore it if the node exists;
[0127] Replace number node: If the number node does not exist, it will be ignored. If the number node exists, the configuration will be replaced;
[0128] Delete number node: Delete if the number node exists.
[0129] S500: According to the final YAML file, after passing the syntax check, the resource object is updated through the Kubernetes API.
[0130] In this embodiment, by configuring the patch type and patch data, setting the namespace for patch resources, and modifying the resource object YAML file content under the specified namespace according to the syntax tree, the patch policy controller updates the resource object through the kubernetes api after syntax verification, thereby completing unified management of resources under different namespaces, simplifying the tedious operations of operation and maintenance, and facilitating the delivery of operation and maintenance personnel in cluster mode. By monitoring custom patch policy resources, multi-cluster environments can be managed, and the principle of syntax tree routing matching is adopted, so that resource configuration can be flexibly modified, and the purpose of simplifying operation and safe operation and maintenance can be achieved.
[0131] Example 2
[0132] like Figure 4 As shown, a Kubernetes patch strategy operation and maintenance device based on a syntax tree according to Embodiment 2 of the present application includes:
[0133] The interface module 100 is used to obtain the configuration for implementing patch policy operation and maintenance;
[0134] The parsing module 200 is used to parse the configuration for implementing patch strategy operation and maintenance to obtain the namespace to be modified and the changed resource object, and obtain the current YAML file of the changed resource object;
[0135] A syntax tree construction module 300, used to construct a syntax tree according to the YAML file;
[0136] A modification module 400, configured to modify the yaml file in the syntax tree according to the configuration for implementing patch strategy operation and maintenance, and render a final yaml file according to the modified syntax tree;
[0137] The application module 500 is used to update the resource object through the kubernetes cluster interface according to the final yaml file.
[0138] It should be noted that other specific implementations of the Kubernetes patch strategy operation and maintenance device based on the syntax tree in the embodiment of the present invention can refer to the specific implementation of the Kubernetes patch strategy operation and maintenance method based on the syntax tree mentioned above. To avoid redundancy, they will not be repeated here.
[0139] Example 3
[0140] A computer program product involved in Example 3 of the present application is used to store a computer program. When the computer program runs on a computer, the method in any one of the implementation modes in Example 1 of the present application is implemented.
[0141] Example 4
[0142] A computer-readable storage medium according to Embodiment 4 of the present application, wherein the computer-readable storage medium stores a program code for execution by a device, wherein the program code includes steps for executing a method in any one of the implementations in Embodiment 1 of the present application;
[0143] Among them, the computer-readable storage medium can be a read-only memory (ROM), a static storage device, a dynamic storage device or a random access memory (RAM); the computer-readable storage medium can store program code, and when the program stored in the computer-readable storage medium is executed by the processor, the processor is used to execute the steps of the method in any one of the implementation methods in Example 1 of the present application.
[0144] Example 5
[0145] like Figure 5 As shown, an electronic device involved in Embodiment 5 of the present application includes a processor, a memory, and a program or instruction stored in the memory and executable on the processor, and when the program or instruction is executed by the processor, a method in any one of the implementations in Embodiment 1 of the present application is implemented;
[0146] Among them, the processor can adopt a general-purpose central processing unit (CPU), a microprocessor, an application specific integrated circuit (ASIC), a graphics processing unit (GPU) or one or more integrated circuits to execute relevant programs to implement the method in any one of the implementation methods in Example 1 of the present application.
[0147] The processor may also be an integrated circuit electronic device with signal processing capability. In the implementation process, each step of the method in any implementation of Embodiment 1 of the present application may be completed by an integrated logic circuit of hardware in the processor or by instructions in software form.
[0148] The above-mentioned processor may also be a general-purpose processor, a digital signal processor, an application-specific integrated circuit (ASIC), a field programmable gate array (FPGA) or other programmable logic devices, discrete gates or transistor logic devices, discrete hardware components. The methods, steps and logic block diagrams disclosed in the embodiments of the present application may be implemented or executed. The general-purpose processor may be a microprocessor or the processor may also be any conventional processor, etc. The steps of the method disclosed in the embodiments of the present application may be directly embodied as being executed by a hardware decoding processor, or may be executed by a combination of hardware and software modules in a decoding processor. The software module may be located in a mature storage medium in the art such as a random access memory, a flash memory, a read-only memory, a programmable read-only memory or an electrically erasable programmable memory, a register, etc. The storage medium is located in a memory, and the processor reads the information in the memory, and completes the functions required to be performed by the unit included in the data processing device of the embodiment of the present application in combination with its hardware, or executes the method in any one of the implementation modes in the embodiment 1 of the present application.
[0149] The above are only preferred specific implementations of the present application; however, the protection scope of the present application is not limited thereto. Any technician familiar with the technical field can make equivalent replacements or changes according to the technical solution and its improved ideas of the present application within the technical scope disclosed in the present application, which should be included in the protection scope of the present application.
Claims
1. A Kubernetes patch strategy operation and maintenance method based on syntax tree, characterized in that: include: Get the configuration used to implement patch strategy operations; Parse the configuration used to implement patch strategy operation and maintenance to obtain the namespace and resource object to be modified, and obtain the current YAML file of the resource object to be changed; Constructing a syntax tree according to the yaml file specifically includes: first creating a root node, if the root node has a child node, creating a child node of the root node, if the child node still has a child node, continuing to create a child node of the child node; The yaml file is modified in the syntax tree according to the configuration for implementing patch strategy operation and maintenance, and a final yaml file is rendered according to the modified syntax tree, wherein the yaml file is modified in the syntax tree according to the configuration for implementing patch strategy operation and maintenance, specifically comprising: searching from the root node of the syntax tree until the changed node is found, determining the operation type of the changed node, wherein the operation type includes adding, replacing and deleting, and modifying the changed node according to the configuration for implementing patch strategy operation and maintenance to obtain a modified syntax tree; Update resource objects through the kubernetes cluster interface according to the final yaml file.
2. The kubernetes patch strategy operation and maintenance method based on syntax tree according to claim 1 is characterized in that: Also includes: Submit the configuration for implementing patch strategy operation and maintenance to the etcd cluster for storage.
3. The kubernetes patch strategy operation and maintenance method based on syntax tree according to claim 1 is characterized in that: The configuration for implementing patch strategy operation and maintenance includes patch type, patch data, and namespace of patch resource effects.
4. The kubernetes patch strategy operation and maintenance method based on syntax tree according to claim 1 is characterized in that: The type of the resource object to be changed is: deployment, statsfulset, service, or daemonset.
5. A Kubernetes patch strategy operation and maintenance device based on a syntax tree, characterized in that: include: An interface module for obtaining configurations for implementing patch strategy operations; The parsing module is used to parse the configuration used to implement patch strategy operation and maintenance to obtain the namespace and resource object to be modified, and obtain the current YAML file of the resource object to be changed; A syntax tree construction module is used to construct a syntax tree according to the yaml file, specifically comprising: first creating a root node, if the root node has a child node, then creating a child node of the root node, if the child node still has a child node, then continuing to create a child node of the child node; A modification module, used to modify the yaml file in the syntax tree according to the configuration for implementing patch strategy operation and maintenance, and render a final yaml file according to the modified syntax tree, wherein modifying the yaml file in the syntax tree according to the configuration for implementing patch strategy operation and maintenance specifically includes: searching from the root node of the syntax tree until the changed node is found, determining the operation type of the changed node, wherein the operation type includes adding, replacing and deleting, and modifying the changed node according to the configuration for implementing patch strategy operation and maintenance to obtain a modified syntax tree; The application module is used to update resource objects through the kubernetes cluster interface according to the final yaml file.
6. A computer program product, characterized in that The computer program product stores computer instructions, and when the computer instructions are executed by a processor, the method according to any one of claims 1 to 4 is implemented.
7. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a program code for execution by a device, wherein the program code includes steps for executing the method according to any one of claims 1 to 4.
8. An electronic device, characterized in that: The electronic device includes a processor, a memory, and a program or instruction stored in the memory and executable on the processor, wherein the program or instruction implements the method according to any one of claims 1 to 4 when executed by the processor.
