A Kubernetes resource protection method and system to prevent accidental deletion
By calculating the hierarchical score and dividing sets of resources in Kubernetes, and annotating them, the stability risk caused by human error in deleting resources is solved, automatic protection of core resources is realized, and the stability of the platform is improved.
Patent Information
- Application Number
- CN202410726871.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-06-06
- Publication Date
- 2024-08-30
- Estimated Expiration
- 2044-06-06
AI Technical Summary
In the Kubernetes environment, when resources are accidentally deleted, resource stability risks arise. It is difficult for existing technology to effectively intercept unexpected deletion operations and protect core resources.
By obtaining the resource types in the Kubernetes platform, they are divided into initial core resources and general resources. The number of historical protection control parameters, idle rate and resource association index of each general resource are calculated, and a hierarchical score is generated. According to the score, the resources are divided into mark sets and general resources. Collection, annotate and mark the resources in the core collection and mark collection, and intercept deletion operations.
Effectively intercept human-induced deletion operations, automatically mark and protect core resources in Kubernetes, and improve platform operation stability.
Smart Images

Figure CN118295774B
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of resource accidental deletion protection, and in particular to a Kubernetes resource accidental deletion protection method and system. Background Art
[0002] Kubernetes (often referred to as K8s) is an open source container orchestration and management platform for automating the deployment, expansion, and management of containerized applications. It provides a reliable and scalable platform that enables developers to easily manage and run containerized applications across multiple hosts. Resources in Kubernetes are used to describe and configure application requirements and restrictions.
[0003] like Figure 3 As shown in the figure, the user defines a Deployment resource, which specifies the running container image, number of replicas, and other information. The Deployment-Controller will create corresponding Pods on the K8s nodes based on the definition and continuously monitor these Pods. If a Pod exits abnormally, the Deployment-Controller will recreate a Pod to ensure that the actual state of the system is consistent with the "expected state" (8 replicas) defined by the user.
[0004] It can be seen that in k8s, the "expected state" of resources will directly affect the actual state of resources. Once someone mistakenly performs some unexpected operations when operating Kubernetes, such as deleting Deployment resources or adjusting the replicas (number of copies) of Deployment resources to 0, it will directly lead to the deletion of a large number of Pods, causing a very large stability risk.
[0005] Based on this, the present invention proposes a Kubernetes resource anti-accidental deletion protection method and system. First, it can intercept unexpected deletion operations when K8s resources are accidentally deleted by humans. Second, it can automatically mark, annotate and protect the core resources in Kubernetes, greatly improving the stability of the Kubernetes platform operation. Summary of the invention
[0006] The purpose of the present invention is to provide a Kubernetes resource anti-accidental deletion protection method and system to solve the deficiencies in the background technology.
[0007] In order to achieve the above object, the present invention provides the following technical solution: a Kubernetes resource protection method to prevent accidental deletion, the protection method comprising the following steps:
[0008] The protection system obtains all resource types in the Kubernetes platform, divides the resources into initial core resources and general resources according to their importance to the Kubernetes platform, and classifies all initial core resources into the core set;
[0009] Obtain the historical number of protection control parameters, idle rate, and resource association index of each general resource in the current application scenario of the Kubernetes platform, normalize the number of protection control parameters, idle rate, and resource association index, and comprehensively calculate the normalized number of protection control parameters, idle rate, and resource association index through a fusion algorithm to generate a graded score for each general resource;
[0010] According to the comparison between the hierarchical scores and the scoring threshold, all general resources are divided into the marked set and the general set respectively, and the resources in the core set and the marked set are annotated and marked;
[0011] Regularly divide general resources into dynamic sets, and remove or mark annotations on general resources based on the set division results. The annotation processing results are saved in the metadata of the resources.
[0012] When the protection system receives a resource deletion request, it obtains the metadata of the deleted resource from the request. The web-hook anti-accidental deletion program determines whether the current resource deletion is allowed based on the metadata information and feeds back the judgment result to the user.
[0013] In a preferred implementation, the number of protection control parameters, the idle rate, and the resource association index are normalized, and the normalized number of protection control parameters, the idle rate, and the resource association index are comprehensively calculated through a fusion algorithm to generate a hierarchical score for each general resource, including the following steps:
[0014] The protection system first normalizes the number of protection control parameters, idle rate and resource association index, maps the value ranges of the number of protection control parameters, idle rate and resource association index to [0,1], and obtains the normalized value of the number of protection control parameters, the normalized value of the idle rate and the normalized value of the resource association index; then the calculation expression of the hierarchical score is: , where For grading rating, To control the number of parameters, normalize the value. is the normalized value of the resource association index, is the normalized value of the idle rate, are the weights of the normalized value of the control parameter quantity, the normalized value of the resource association index, and the normalized value of the idle rate, respectively, and In a preferred embodiment, all general resources are classified into a marked set and a general set respectively according to the comparison result of the hierarchical score and the score threshold, including the following steps:
[0015] Compare the obtained graded scores with the preset score thresholds, which are used to determine the overall importance of resources in the Kubernetes platform.
[0016] If the hierarchical score of the resource is greater than or equal to the scoring threshold, it is judged that the comprehensive importance of the resource in the Kubernetes platform is large, and the resource is classified into the tag set. If the hierarchical score of the resource is less than the scoring threshold, it is judged that the comprehensive importance of the resource in the Kubernetes platform is small, and the resource is classified into the general set.
[0017] In a preferred embodiment, the resources in the core set and the tag set are annotated and tagged, including the following steps:
[0018] The protection system adds annotations to the resources in the core set and the tag set to identify the resource as one that needs to be protected. The annotation consists of a key and a value. Once the key of the resource annotation is determined, the resource cannot be changed in the Kubernetes platform.
[0019] If you need to delete a resource with annotations, you need to delete the annotations first before continuing to delete the resource.
[0020] In a preferred embodiment, when the protection system receives a resource deletion request, the metadata of the deleted resource is obtained from the request, and the web-hook anti-accidental deletion program determines whether to allow the current resource to be deleted based on the metadata information, and feeds back the judgment result to the user, including the following steps:
[0021] The web-hook anti-accidental-deletion program obtains the metadata of the deleted resource and analyzes whether there is an annotation mark in the resource metadata; if there is an annotation mark in the resource metadata, obtain whether the resource annotation has a deletion instruction. If there is a deletion instruction, the web-hook anti-accidental-deletion program deletes the resource. If there is no deletion instruction, the web-hook anti-accidental-deletion program does not perform the resource deletion operation; if there is no annotation mark in the resource metadata, determine whether it is a highest authority deletion based on the resource metadata. If it is a highest authority deletion, the web-hook anti-accidental-deletion program deletes the resource. If it is not a highest authority deletion, the web-hook anti-accidental-deletion program does not perform the resource deletion operation; the operation results of the resource are fed back to the user.
[0022] In a preferred embodiment, the general resources are periodically divided into dynamic sets, and the general resources are annotated or annotated according to the set division results, and the annotation processing results are saved in the metadata of the resources, including the following steps:
[0023] The hierarchical scores of general resources are obtained regularly, and the general resources are divided into a marked set and a general set according to the comparison result between the hierarchical scores and the score thresholds. The annotation status of the general resources in the marked set is traversed. If the annotation status of the general resources in the marked set is no annotation, the general resource is marked and annotated. The annotation status of the general resources in the general set is traversed. If the annotation status of the general resources in the general set is annotated, the general resource is marked and deleted, and the annotation processing result is saved in the metadata of the resource. In a preferred embodiment, the acquisition logic of the resource association index is: suppose the resource have Direct child resources , then we can calculate recursively The association index of all descendant resources is obtained The correlation index of is:
[0024] In the formula, represents the resource association index, Representation Resources The number of direct descendant resources of Representation Resources No. Direct child resources In a preferred embodiment, the calculation logic of the idle rate is: obtain the total historical running time of the Kubernetes platform, obtain the total historical unused time of the resources, and obtain the idle rate by dividing the total historical unused time of the resources by the total historical running time of the Kubernetes platform. The expression is:
[0025]
[0026] In the formula, is the idle rate, The total time that the resource has not been used in history. In a preferred embodiment, the logic for obtaining the number of protection control parameters is: obtaining the number of Finalizers and the number of RBAC roles in the resource metadata, and adding the number of Finalizers to the number of RBAC roles to obtain the number of protection control parameters.
[0027] A Kubernetes resource protection system against accidental deletion, including a resource partitioning module, a data collection module, a secondary partitioning module, an annotation marking module, and a deletion judgment module;
[0028] Resource division module: obtains all resource types in the Kubernetes platform, divides the resources into initial core resources and general resources according to their importance to the Kubernetes platform, and classifies all initial core resources into core sets;
[0029] Data collection module: obtains the historical number of protection control parameters, idle rate, and resource association index of each general resource in the current application scenario of the Kubernetes platform, normalizes the number of protection control parameters, idle rate, and resource association index, and comprehensively calculates the normalized number of protection control parameters, idle rate, and resource association index through a fusion algorithm to generate a graded score for each general resource;
[0030] Secondary division module: all general resources are divided into marked set and general set according to the comparison result between the classification score and the score threshold;
[0031] Annotation and marking module: annotate and mark resources in the core set and the marked set. The marked and annotated resources are resources that need to be protected, that is, the marked and annotated resources can only be deleted in a specific way. Regularly divide general resources into dynamic sets, and remove or annotate general resources based on the set division results. The annotation processing results are saved in the metadata of the resource;
[0032] Deletion judgment module: When a resource deletion request is received, the metadata of the deleted resource is obtained from the request. The web-hook anti-accidental deletion program determines whether the current resource can be deleted based on the metadata information and feeds back the judgment result to the user.
[0033] In the above technical solution, the technical effects and advantages provided by the present invention are:
[0034] The present invention obtains the number of historical protection control parameters, idle rate and resource association index of each general resource of the Kubernetes platform in the current application scenario, and classifies all general resources into a tag set and a general set according to the comparison result of the graded score and the score threshold, and annotates and marks the resources in the core set and the tag set. When the protection system receives a resource deletion request, the metadata of the deleted resource is obtained from the request, and the web-hook anti-mistaken deletion program determines whether the current resource is allowed to be deleted based on the metadata information, and feeds back the judgment result to the user. The protection system can intercept unexpected deletion operations in the case of human mistaken deletion of K8s resources, and automatically mark, annotate and protect the core resources in Kubernetes, greatly improving the stability of the operation of the Kubernetes platform. BRIEF DESCRIPTION OF THE DRAWINGS
[0035] In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the drawings required for use in the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments recorded in the present invention. For ordinary technicians in this field, other drawings can also be obtained based on these drawings.
[0036] Figure 1 The figure is a flow chart of the method of the present invention.
[0037] Figure 2 Flow chart of the web-hook anti-accidental deletion program of the present invention.
[0038] Figure 3 This is a diagram showing the impact of "expected state" on resources in Kubernetes. DETAILED DESCRIPTION
[0039] In order to make the purpose, technical solution and advantages of the embodiments of the present invention clearer, the technical solution in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are 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.
[0040] Resources in Kubernetes are used to describe and configure the requirements and limits of an application so that the scheduler can manage and allocate these resources efficiently. The following are common resource types in Kubernetes:
[0041] Pod: Pod is the smallest scheduling unit in Kubernetes, which can contain one or more containers. Pod is the most basic unit of resources and is used to combine and share computing resources.
[0042] Node: A Node is a working node in a Kubernetes cluster. It can be a physical machine or a virtual machine. Each Node has certain computing resources, such as CPU and memory, for running and hosting Pods.
[0043] Name-space: Name-space is a mechanism for logically isolating and grouping resources within a cluster. Different resources can be divided into different namespaces to achieve resource isolation, management, and control.
[0044] Deployment: Deployment is a resource type used to define and manage the deployment of Pods. It specifies the number of Pod replicas, container images, resource requirements, and how to upgrade and scale Pods.
[0045] Service: Service is a resource type that provides stable network access and service discovery. Service encapsulates a group of Pods in a virtual network and provides them with a unique virtual IP address for access by other services or external users.
[0046] In addition, Kubernetes also supports other resource types, such as Con-fig-Map (configuration map), Secrets (keys and credentials), Persistent-Volume (persistent storage volume), etc., which are used to manage and configure other resources required by applications.
[0047] By defining and managing these resources, Kubernetes provides a flexible and powerful way to deploy, schedule, and manage applications to efficiently utilize computing resources and achieve high availability, scalability, and fault tolerance.
[0048] In Kubernetes, Annotation is a mechanism for adding metadata to objects such as Pod, Service, Deployment, etc. Annotations are key-value pairs used to provide additional information about an object that may be important to specific tools, controllers, or operations, but is not critical to Kubernetes itself.
[0049] Example 1: Please refer to Figure 1As shown, a Kubernetes resource protection method for preventing accidental deletion described in this embodiment includes the following steps:
[0050] The protection system obtains all resource types in the Kubernetes platform, divides the resources into initial core resources and general resources according to their importance to the Kubernetes platform, and classifies all initial core resources into core sets. It obtains the number of historical protection control parameters, idle rate, and resource association index of each general resource in the current application scenario of the Kubernetes platform, and normalizes the number of protection control parameters, idle rate, and resource association index. It comprehensively calculates the normalized number of protection control parameters, idle rate, and resource association index through a fusion algorithm, generates a hierarchical score for each general resource, and classifies all general resources into a marked set and a general set according to the comparison result between the hierarchical score and the score threshold. It annotates the resources in the core set and the marked set. The marked resources are resources that need to be protected, that is, the marked resources can only be deleted in a specific way. It regularly divides the general resources into dynamic sets, and removes or annotates the general resources according to the set division result. The annotation processing result is saved in the metadata of the resource.
[0051] The Kubernetes platform has different judgments on core resources in different application scenarios. The resources that the Kubernetes platform needs to rely on for operation are the initial core resources (that is, the lack of such resources will cause the Kubernetes platform to fail to operate), while the resources classified into the tag set are relatively important resources in the current application scenario;
[0052] On the Kubernetes platform where the resources to be protected belong, deploy the anti-accidental deletion web-hook to intercept operations related to deleting resources. Once the operation of deleting resources is initiated, the Validate-Delete-Operation of the anti-accidental deletion web-hook will be called. The main process is as follows:
[0053] When the protection system receives a resource deletion request, it obtains the metadata of the deleted resource from the request. The web-hook anti-accidental deletion program determines whether the current resource deletion is allowed based on the metadata information and feeds back the judgment result to the user.
[0054] This application obtains the number of historical protection control parameters, idle rate, and resource association index of each general resource in the current application scenario of the Kubernetes platform, and classifies all general resources into a tag set and a general set according to the comparison results of the graded score and the score threshold. The resources in the core set and the tag set are annotated and marked. When the protection system receives a resource deletion request, it obtains the metadata of the deleted resource from the request. The web-hook anti-accidental deletion program determines whether the current resource is allowed to be deleted based on the metadata information, and feeds back the judgment result to the user. The protection system can intercept unexpected deletion operations in the case of human accidental deletion of K8s resources, and automatically annotate and protect the core resources in Kubernetes, greatly improving the stability of the Kubernetes platform operation.
[0055] Embodiment 2: The protection system obtains all resource types in the Kubernetes platform, divides the resources into initial core resources and general resources according to their importance to the Kubernetes platform, and classifies all initial core resources into a core set, including the following steps:
[0056] Use the Kubernetes API or kubectl-api-resources command to get all available resource types in the cluster;
[0057] The importance criteria of a resource is usually defined based on the following parameters:
[0058] APIGroup and Kind: Core resources usually belong to the core API group (e.g. "v1"), not the extension API group;
[0059] Annotations and Labels: Core resources have specific annotations or labels to indicate their importance or irremovability. These are usually labels pre-set by developers. The absence of such resources will cause the Kubernetes platform to fail to operate.
[0060] The names and namespaces of some resources may hint at their importance. For example, in Kubernetes, resources with names prefixed with "kube-" are usually core resources;
[0061] By checking the creator information of the resource, for example, a resource created by a Kubernetes system component may be a core resource;
[0062] The above are just some common definitions of core resources on the Kubernetes platform. In different application scenarios, the Kubernetes platform also defines core resources in other aspects, such as RBAC permission settings, which are not described here.
[0063] Create a core collection and add the initial core resources to the core collection. You can use labels or annotations to mark these resources, and add labels or annotations to each initial core resource to identify it as a core resource.
[0064] Obtain the historical protection control parameter quantity, idle rate, and resource association index of each general resource in the current application scenario of the Kubernetes platform, and normalize the protection control parameter quantity, idle rate, and resource association index. The normalized protection control parameter quantity, idle rate, and resource association index are comprehensively calculated through a fusion algorithm to generate a graded score for each general resource, including the following steps:
[0065] The logic for obtaining the number of protection control parameters is as follows: obtain the number of Finalizers and the number of RBAC roles in the resource metadata, and add the number of Finalizers to the number of RBAC roles to obtain the number of protection control parameters;
[0066] Add the number of Finalizers to the number of RBAC roles to get the number of protection control parameters. The more control parameters a resource has, the more important the resource is in the Kubernetes platform. Specifically:
[0067] Adding the number of Finalizers and the number of RBAC roles together, the number of protection control parameters obtained can be used to evaluate the importance of resources in the Kubernetes platform. The more protection control parameters there are, the higher the protection level and access control complexity of the resource, which usually means that the resource is more important in the Kubernetes platform;
[0068] Number of Finalizers: Finalizers are a mechanism that ensures that specific cleanup operations are performed before a resource is deleted. The more Finalizers a resource has, the more operations need to be performed before deleting the resource, indicating that the resource has more dependencies on the system or that its deletion will have more subsequent effects.
[0069] Number of RBAC roles: The number of RBAC (Role-Based Access Control) roles reflects how many different roles have the authority to operate on the resource. More RBAC roles means more users and services rely on the resource, indicating that it is more important. The calculation logic of the idle rate is: get the total historical running time of the Kubernetes platform, get the total historical unused time of the resource, and get the idle rate by dividing the total historical unused time of the resource by the total historical running time of the Kubernetes platform. The expression is:
[0070]
[0071] In the formula, is the idle rate, The total time that the resource has not been used in history. It is the total historical running time of the Kubernetes platform.
[0072] The idle rate is obtained by dividing the total historical unused time of the resource by the total historical running time of the Kubernetes platform. The smaller the idle rate, the more frequently the resource is used in the Kubernetes platform, that is, the more important the resource is in the Kubernetes platform. Specifically:
[0073] The smaller the idle rate is, the more frequently the resource is used in the Kubernetes platform, because it indicates that the proportion of time that the resource is not used in its life cycle is low. In this case, the resource is frequently used and may play a vital role in the operation and function of the Kubernetes platform, so its importance in the Kubernetes platform is relatively high.
[0074] Specifically, the smaller the idle rate, the more active the resource is most of the time and is frequently used by the Kubernetes platform or application. This may indicate that the resource is responsible for key business functions or services and may be relied upon by multiple applications or services. On the contrary, if the idle rate of a resource is high, that is, the resource is idle most of the time and is rarely used, then it may be some auxiliary resource that is not so critical to the operation of the entire Kubernetes platform. The logic for obtaining the resource association index is: suppose the resource have Direct child resources , then we can calculate recursively The association index of all descendant resources is obtained The correlation index of is:
[0075] In the formula, represents the resource association index, Representation Resources The number of direct descendant resources of Representation Resources No. Direct child resources We recommend starting with the parent resource and then layer by layer until you reach the target resource. For example:
[0076] Crd->Cr: Protect CR (CustomResource). It is best to protect CRD first. Otherwise, after CRD is deleted, the corresponding CR will be recycled.
[0077] Name-space->Deployment / Statefulset: To protect a Deployment, it is best to protect the Name-space to which the Deployment belongs first. Otherwise, if the Name-space is deleted, all resources under the Name-space will be reclaimed.
[0078] At the same time, we believe that not directly protecting Pods will affect normal rolling upgrades. For example, a rolling upgrade of a Deployment will create new Pods and delete old Pods. Protecting Pods may cause the upgrade to fail.
[0079] Taking the above example:
[0080] In Kubernetes, the hierarchical relationship of resources can be expressed as follows:
[0081] CRD (Custom-Resource-Definition)->CR (Custom-Resource);
[0082] Name-space->Deployment / Stateful-Set->Replica-Set->Pod;
[0083] Name-space->Service;
[0084] Name-space->Con-fig-Map / Secret.
[0085] The association index (AI) of each resource is the sum of the number of all its direct descendant resources. If a resource has no descendant resources, its association index is 1.
[0086] Calculate the correlation index layer by layer:
[0087] Pods: AI(Pod)=1
[0088] Replica-Sets: AI(Replica-Set)=ΣAI(Pod), which is the sum of the association indices of all its direct child Pods.
[0089] Deployments: AI(Deployment) = ΣAI(Replica-Set), which is the sum of the association indices of all its direct child Replica-Sets.
[0090] Stateful-Sets: AI(Stateful-Set)=ΣAI(Pod), which is the sum of the association indices of all its direct child Pods.
[0091] Name-space: AI(Name-space)=ΣAI(Deployment)+ΣAI(Stateful-Set)+ΣAI(Service)+ΣAI(Con-fig-Map)+ΣAI(Secret);
[0092] Correlation index between CRD and CR;
[0093] AI(CR)=1, because CR has no child resources;
[0094] AI(CRD)=ΣAI(CR), which is the sum of the association indexes of all its direct child Custom-Resources.
[0095] Therefore, the larger the resource association index value of a resource in the Kubernetes platform, the greater the importance of the resource.
[0096] The number of protection control parameters, idle rate and resource association index are normalized, and the normalized number of protection control parameters, idle rate and resource association index are comprehensively calculated through a fusion algorithm to generate a graded score for each general resource, including the following steps:
[0097] In summary, in this application, it is necessary to maximize the number of resource protection control parameters and resource association index and minimize the idle rate of resources to calculate the resource classification score;
[0098] The protection system first normalizes the number of protection control parameters, idle rate and resource association index, maps the value ranges of the number of protection control parameters, idle rate and resource association index to [0,1], and obtains the normalized value of the number of protection control parameters, the normalized value of the idle rate and the normalized value of the resource association index; then the calculation expression of the hierarchical score is: , where For grading rating, To control the number of parameters, normalize the value. is the normalized value of the resource association index, is the normalized value of the idle rate, are the weights of the normalized value of the control parameter quantity, the normalized value of the resource association index, and the normalized value of the idle rate, respectively, and According to the comparison result between the classification score and the score threshold, all general resources are classified into the marked set and the general set respectively, including the following steps:
[0099] According to the calculation logic of the hierarchical score, the larger the hierarchical score of a resource, the greater the overall importance of the resource in the Kubernetes platform. Therefore, the obtained hierarchical score is compared with the preset score threshold, which is used to determine whether the overall importance of the resource in the Kubernetes platform is large or small.
[0100] If the hierarchical score of the resource is greater than or equal to the scoring threshold, it is judged that the comprehensive importance of the resource in the Kubernetes platform is large, and the resource is classified into the tag set. If the hierarchical score of the resource is less than the scoring threshold, it is judged that the comprehensive importance of the resource in the Kubernetes platform is small, and the resource is classified into the general set.
[0101] Annotate the resources in the core collection and the tag collection, including the following steps:
[0102] Since the resources in the core collection and the tag collection are relatively important resources for the Kubernetes platform, in order to further protect such resources, the protection system needs to add specific annotations to such resources to identify that the resources belong to the resources that need to be protected; the annotation consists of a key and a value, for example: the key can be specified as: inf-webhook-server.infcs.tob / product-protected-by-inf. Once the key is determined, it cannot be changed in the Kubernetes platform (including deleting and modifying the resource content). The protection system determines whether the deletion of the resource needs to be intercepted by identifying whether the resource has a specific key.
[0103] In this application, the key is fixed to inf-webhook-server.infcs.tob / product-protected-by-inf.
[0104] value can be specified arbitrarily. However, in order to better locate the problem, it is recommended that value be some meaningful name to facilitate problem location and log search. To configure annotation values for Kubernetes platform resources, you can use the following command: kubectl-annotate-$resourceKind-$resourceName-inf-webhook-server.infcs.tob / product-protected-by-inf=$value, where $resourceKind, $resourceName, and $value should be replaced with actual values.
[0105] For annotated resources, as long as the annotation is kept, the resource cannot be deleted. The resource can only be deleted by deleting the annotation:
[0106] # To delete an annotation, just add "-" after the annotation name;
[0107] Kubectl-annotate-$resourceKind-$resourceName-inf-webhook-server.infcs.tob / product-protected-by-inf-.
[0108] Therefore, if you need to delete a resource with an annotation, you need to delete the annotation first before continuing to delete the resource, further improving the protection of core resources.
[0109] Regularly divide general resources into dynamic sets, and remove or mark annotations on general resources based on the set division results. The annotation processing results are saved in the metadata of the resources, including the following steps:
[0110] Since the application scenarios of the Kubernetes platform may be different at different times, the protection system needs to regularly dynamically divide the general resources into sets. The initial core resources are the system resources of the Kubernetes platform, so they are not dynamically managed.
[0111] Regularly obtain the hierarchical scores of general resources, and divide the general resources into a tag set and a general set based on the comparison result between the hierarchical scores and the score thresholds. Traverse the annotation status of the general resources in the tag set. If the annotation status of the general resources in the tag set is unannotated, annotate the general resource. Traverse the annotation status of the general resources in the general set. If the annotation status of the general resources in the general set is annotated, delete the general resource tag, and save the annotation processing results in the resource metadata.
[0112] When the protection system receives a resource deletion request, it obtains the metadata of the deleted resource from the request. The web-hook anti-accidental deletion program determines whether to allow the current resource to be deleted based on the metadata information and feeds back the judgment result to the user, including the following steps:
[0113] like Figure 2As shown: the web-hook anti-accidental deletion program obtains the metadata of the deleted resource and analyzes whether there is an annotation mark in the resource metadata; if there is an annotation mark in the resource metadata, obtain whether the resource annotation has a deletion instruction. If there is a deletion instruction, the web-hook anti-accidental deletion program deletes the resource. If there is no deletion instruction, the web-hook anti-accidental deletion program does not perform the resource deletion operation; if there is no annotation mark in the resource metadata, determine whether it is a highest authority deletion based on the resource metadata. If it is a highest authority deletion, the web-hook anti-accidental deletion program deletes the resource. If it is not a highest authority deletion, the web-hook anti-accidental deletion program does not perform the resource deletion operation; the operation results of the resource are fed back to the user.
[0114] Embodiment 3: A Kubernetes resource protection system against accidental deletion described in this embodiment includes a resource partitioning module, a data collection module, a secondary partitioning module, an annotation marking module, and a deletion judgment module;
[0115] Resource partitioning module: obtains all resource types in the Kubernetes platform, divides the resources into initial core resources and general resources according to their importance to the Kubernetes platform, and classifies all initial core resources into core sets. The resource partitioning results are sent to the data collection module, and the core set is sent to the annotation and marking module.
[0116] Data collection module: obtains the historical protection control parameter quantity, idle rate and resource association index of each general resource in the current application scenario of the Kubernetes platform, normalizes the protection control parameter quantity, idle rate and resource association index, and comprehensively calculates the normalized protection control parameter quantity, idle rate and resource association index through a fusion algorithm to generate a hierarchical score for each general resource, which is sent to the secondary division module;
[0117] Secondary division module: All general resources are divided into marked sets and general sets according to the comparison results between the hierarchical scores and the scoring thresholds, and the set division results are sent to the annotation and marking module;
[0118] Annotation and marking module: annotate and mark resources in the core set and the marked set. The marked and annotated resources are resources that need to be protected, that is, the marked and annotated resources need a specific method to be deleted. Regularly divide general resources into dynamic sets, and remove or annotate general resources based on the set division results. The annotation processing results are saved in the metadata of the resource, and the resource metadata information is sent to the deletion judgment module;
[0119] Deletion judgment module: When a resource deletion request is received, the metadata of the deleted resource is obtained from the request. The web-hook anti-accidental deletion program determines whether the current resource can be deleted based on the metadata information and feeds back the judgment result to the user.
[0120] The above formulas are all dimensionless and numerical calculations. The formula is a formula for the most recent real situation obtained by collecting a large amount of data and performing software simulation. The preset parameters in the formula are set by technicians in this field according to actual conditions.
[0121] It should be understood that the term "and / or" in this article is only a description of the association relationship of associated objects, indicating that there can be three relationships. For example, A and / or B can represent: A exists alone, A and B exist at the same time, and B exists alone. A and B can be singular or plural. In addition, the character " / " in this article generally indicates that the associated objects before and after are in an "or" relationship, but it may also indicate an "and / or" relationship. Please refer to the context for specific understanding.
[0122] It should be understood that in the various embodiments of the present application, the size of the serial numbers of the above-mentioned processes does not mean the order of execution. The execution order of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiments of the present application.
[0123] Those of ordinary skill in the art will appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of this application. Those skilled in the art can clearly understand that for the convenience and simplicity of description, the specific working process of the above-described system, device and unit can refer to the corresponding process in the aforementioned method embodiment, and will not be repeated here.
[0124] The above is only a specific implementation of the present application, but the protection scope of the present application is not limited thereto. Any person skilled in the art who is familiar with the present technical field can easily think of changes or substitutions within the technical scope disclosed in the present application, which should be included in the protection scope of the present application. Therefore, the protection scope of the present application should be based on the protection scope of the claims.
Claims
1. A Kubernetes resource protection method to prevent accidental deletion. Features: The protection method comprises the following steps: The protection system obtains all resource types in the Kubernetes platform, divides the resources into initial core resources and general resources according to their importance to the Kubernetes platform, and classifies all initial core resources into the core set; Obtain the historical number of protection control parameters, idle rate, and resource association index of each general resource in the current application scenario of the Kubernetes platform, normalize the number of protection control parameters, idle rate, and resource association index, and comprehensively calculate the normalized number of protection control parameters, idle rate, and resource association index through a fusion algorithm to generate a graded score for each general resource; According to the comparison between the hierarchical scores and the scoring threshold, all general resources are divided into the marked set and the general set respectively, and the resources in the core set and the marked set are annotated and marked; Regularly divide general resources into dynamic sets, and remove or mark annotations on general resources based on the set division results. The annotation processing results are saved in the metadata of the resources. When the protection system receives a resource deletion request, it obtains the metadata of the resource to be deleted from the request. The web-hook anti-accidental deletion program determines whether to allow the current resource to be deleted based on the metadata information and feeds back the judgment result to the user. The number of protection control parameters, idle rate and resource association index are normalized, and the normalized number of protection control parameters, idle rate and resource association index are comprehensively calculated through a fusion algorithm to generate a graded score for each general resource, including the following steps: The protection system first normalizes the number of protection control parameters, the idle rate, and the resource association index, maps the value ranges of the number of protection control parameters, the idle rate, and the resource association index to [0,1], and obtains the normalized value of the number of protection control parameters, the normalized value of the idle rate, and the normalized value of the resource association index; The calculation expression of the graded score is: , where For grading rating, To control the number of parameters, normalize the value. is the normalized value of the resource association index, is the normalized value of the idle rate, are the weights of the normalized value of the control parameter quantity, the normalized value of the resource association index, and the normalized value of the idle rate, respectively, and ; The logic for obtaining the resource association index is as follows: have Direct child resources , then by recursive calculation The association index of all descendant resources is obtained The correlation index of is expressed as: In the formula, represents the resource association index, Representation Resources The number of direct descendant resources of Representation Resources No. Direct child resources The correlation index.
2. According to claim 1, a Kubernetes resource protection method for preventing accidental deletion, Features: According to the comparison result between the classification score and the score threshold, all general resources are classified into the marked set and the general set respectively, including the following steps: Compare the obtained graded scores with the preset score thresholds, which are used to determine the overall importance of resources in the Kubernetes platform. If the hierarchical score of the resource is greater than or equal to the scoring threshold, it is judged that the comprehensive importance of the resource in the Kubernetes platform is large, and the resource is classified into the tag set. If the hierarchical score of the resource is less than the scoring threshold, it is judged that the comprehensive importance of the resource in the Kubernetes platform is small, and the resource is classified into the general set.
3. According to claim 2, a Kubernetes resource protection method for preventing accidental deletion, Features: Annotate the resources in the core collection and the tag collection, including the following steps: The protection system adds annotations to the resources in the core set and the tag set to identify the resource as one that needs to be protected. The annotation consists of a key and a value. Once the key of the resource annotation is determined, the resource cannot be changed in the Kubernetes platform. If you need to delete a resource with annotations, you need to delete the annotations first before continuing to delete the resource.
4. According to claim 3, a Kubernetes resource protection method for preventing accidental deletion, Features: When the protection system receives a resource deletion request, it obtains the metadata of the deleted resource from the request. The web-hook anti-accidental deletion program determines whether to allow the current resource to be deleted based on the metadata information and feeds back the judgment result to the user, including the following steps: The web-hook anti-accidental-deletion program obtains the metadata of the deleted resource and analyzes whether there is an annotation mark in the resource metadata; if there is an annotation mark in the resource metadata, obtain whether the resource annotation has a deletion instruction. If there is a deletion instruction, the web-hook anti-accidental-deletion program deletes the resource. If there is no deletion instruction, the web-hook anti-accidental-deletion program does not perform the resource deletion operation; if there is no annotation mark in the resource metadata, determine whether it is a highest authority deletion based on the resource metadata. If it is a highest authority deletion, the web-hook anti-accidental-deletion program deletes the resource. If it is not a highest authority deletion, the web-hook anti-accidental-deletion program does not perform the resource deletion operation; the operation results of the resource are fed back to the user.
5. According to claim 4, a Kubernetes resource protection method for preventing accidental deletion, Features: Regularly divide general resources into dynamic sets, and remove or mark annotations on general resources based on the set division results. The annotation processing results are saved in the metadata of the resources, including the following steps: Regularly obtain the hierarchical scores of general resources, and divide the general resources into a tag set and a general set based on the comparison result between the hierarchical scores and the score thresholds. Traverse the annotation status of the general resources in the tag set. If the annotation status of the general resources in the tag set is unannotated, annotate the general resource. Traverse the annotation status of the general resources in the general set. If the annotation status of the general resources in the general set is annotated, delete the general resource tag, and save the annotation processing results in the resource metadata.
6. A Kubernetes resource protection method against accidental deletion according to claim 5, Features: The calculation logic of the idle rate is as follows: obtain the total historical running time of the Kubernetes platform, obtain the total historical unused time of the resources, and obtain the idle rate by dividing the total historical unused time of the resources by the total historical running time of the Kubernetes platform. The expression is: In the formula, is the idle rate, The total time that the resource has not been used in history. It is the total historical running time of the Kubernetes platform.
7. A Kubernetes resource protection method against accidental deletion according to claim 6, Features: The logic for obtaining the number of protection control parameters is: obtaining the number of Finalizers and the number of RBAC roles in the resource metadata, and adding the number of Finalizers to the number of RBAC roles to obtain the number of protection control parameters.
8. A Kubernetes resource protection system against accidental deletion, used to implement the protection method described in any one of claims 1 to 7, Features: It includes resource division module, data collection module, secondary division module, annotation marking module and deletion judgment module; Resource division module: obtains all resource types in the Kubernetes platform, divides the resources into initial core resources and general resources according to their importance to the Kubernetes platform, and classifies all initial core resources into core sets; Data collection module: obtains the historical number of protection control parameters, idle rate, and resource association index of each general resource in the current application scenario of the Kubernetes platform, normalizes the number of protection control parameters, idle rate, and resource association index, and comprehensively calculates the normalized number of protection control parameters, idle rate, and resource association index through a fusion algorithm to generate a graded score for each general resource; Secondary division module: all general resources are divided into marked set and general set according to the comparison result between the classification score and the score threshold; Annotation and marking module: annotate and mark resources in the core set and the marked set. The marked and annotated resources are resources that need to be protected, that is, the marked and annotated resources can only be deleted in a specific way. Regularly divide general resources into dynamic sets, and remove or annotate general resources based on the set division results. The annotation processing results are saved in the metadata of the resource; Deletion judgment module: When a resource deletion request is received, the metadata of the deleted resource is obtained from the request. The web-hook anti-accidental deletion program determines whether the current resource can be deleted based on the metadata information and feeds back the judgment result to the user.
Citation Information
Patent Citations
Kubernetes cloud native cluster resource deletion protection method and device, equipment and storage medium
CN114281476A
Method for realizing container resource customized scheduling and deletion protection based on Webhook technology
CN114595035A
