Container mirror image management method and device based on K8S
In K8S, the update image-puller image and the update-image container are used to dynamically update the container image and files without deploying a controller and high permissions, solving the problems of low container image update efficiency and poor real-time performance, and achieving efficient Image and file updates.
Patent Information
- Application Number
- CN202410875093.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-01
- Publication Date
- 2024-10-15
AI Technical Summary
In the existing technology, high permissions are given to the container when updating the container image, resulting in low efficiency of image update and non-real-time update of files, and the need to re-build the release application, affecting convenience and real-time performance.
By eliminating the need to deploy a controller and grant high permissions to the container in K8S, the update image-puller image and the update-image container are used to dynamically compare the difference files and update the image and container files, achieving image preheating and dynamic file updating under low permissions.
It improves the preheating efficiency after container image updates and the real-time nature of file updates, avoids the cumbersome process of rebuilding and publishing applications, and simplifies the update process of operation and maintenance tools and files.
Smart Images

Figure CN118779064A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer technology, and specifically to a container image management method and device based on K8S. Background Art
[0002] In container applications, container images are generally built based on a specified base image, which is limited to the O&M tools required for O&M. When users update components or O&M tools that are not related to business programs in the container image, they often need to wait until the business party releases the next business code iteration, or apply to the business party and complete a series of cumbersome processes and operations such as image reconstruction -> image release -> application re-release -> pod rescheduling -> image download -> container start -> container operation -> pod operation -> application external service provision, etc., to meet the simple O&M update requirements.
[0003] Currently, the image preheating job is created using the image preheating configuration information of the controller device, and the container in the job is given high permissions to interact with CRI (Container Runtime Interface) to complete the image file pull of the container. However, the high permission setting makes the container run in a heavyweight environment, resulting in low preheating efficiency after the container image is updated, and the currently used image update method does not synchronize the updated file to the container layer file, which reduces the real-time update of the image file and container file corresponding to the container. Summary of the invention
[0004] In view of this, the present application provides a container image management method and device based on K8S, which does not require the deployment of a controller or the granting of high permissions to the container. It is suitable for completing image update preheating with low permissions, and can improve the efficiency of preheating the container image after update; and can also realize dynamic updates of image files and container files, improving the real-time performance of file updates.
[0005] To solve the above problems, the technical solutions provided by this application are as follows:
[0006] In a first aspect, the present application provides a container image management method based on K8S, the method being applied to K8S, comprising:
[0007] In response to determining that there is an update to the base image used by the original image-puller image, obtaining an updated image-puller image;
[0008] Create and download an updated image-puller container based on the updated image-puller image;
[0009] Send a container startup command to the update-image container, where the container startup command carries the overlay mount information of the updated base image and the container ID of the updated image-puller container;
[0010] Obtain the image file storage information of the updated image-puller image and the image file storage information of the image to be updated corresponding to the container to be updated, which is queried by the update-image container based on the overlay mount information of the updated base image and the container ID of the updated image-puller container;
[0011] Compare the image file storage information of the updated image-puller image with the image file storage information of the image to be updated to obtain a difference file;
[0012] The image file storage information of the image to be updated and the container file storage information of the container to be updated are updated based on the difference file.
[0013] In a possible implementation, in response to determining that a base image used by the original image-puller image is updated, obtaining an updated image-puller image includes:
[0014] Get the basic image update information configured by the user through the application release system;
[0015] The hash value of the original image-puller image is adjusted based on the base image update information to obtain the updated image-puller image.
[0016] In a possible implementation, the obtaining the image file storage information of the updated image-puller image and the image file storage information of the image to be updated corresponding to the container to be updated, which is queried by the update-image container based on the overlay mount information of the updated base image and the container ID of the updated image-puller container, includes:
[0017] Obtaining image file storage information of the original image-puller image determined by the update-image container based on the container ID of the update image-puller container;
[0018] Determine the image file storage information of the updated image-puller image based on the image file storage information of the original image-puller image and the overlay mount information of the updated base image;
[0019] Obtain the / proc and container root directory on the host machine mounted by the update-image container using the hostPath method, and query the image name of the image to be updated corresponding to the container to be updated based on the container ID of the update image-puller container;
[0020] The image file storage information of the image to be updated is determined based on the image name of the image to be updated.
[0021] In a possible implementation manner, updating the image file storage information of the image to be updated and the container file storage information of the container to be updated based on the difference file includes:
[0022] If the difference file is a newly added difference file, then the difference file is added to the image file storage information of the image to be updated and the container file storage information of the container to be updated;
[0023] If the difference file is a modified difference file, modifying the difference file in the image file storage information of the image to be updated and the container file storage information of the container to be updated;
[0024] If the difference file is a deleted difference file, the difference file is deleted from the image file storage information of the image to be updated and the container file storage information of the container to be updated.
[0025] In a possible implementation, if the difference file is at least one of a newly added difference file and a modified difference file, the newly added difference file or the modified difference file is written into the image file storage information of the image to be updated by means of a hard link.
[0026] In a possible implementation manner, if the difference file is a newly added difference file, the newly added difference file is copied to the container file storage information of the container to be updated.
[0027] In a possible implementation, after the difference file is updated, the method further includes:
[0028] Send update result information to the application publishing system, where the update result information indicates that K8S has completed image preheating and file dynamic update.
[0029] In a second aspect, the present application provides a container image management device based on K8S, and the device is applied to K8S, including:
[0030] A first acquisition module, configured to acquire an updated image-puller image in response to determining that a base image used by the original image-puller image is updated;
[0031] A creation and downloading module, used to create and download an updated image-puller container based on the updated image-puller image;
[0032] A sending module, used to send a container startup command to the update-image container, wherein the container startup command carries the overlay mount information of the updated base image and the container ID of the updated image-puller container;
[0033] A second acquisition module is used to obtain the image file storage information of the updated image-puller image and the image file storage information of the image to be updated corresponding to the container to be updated, which is queried by the update-image container based on the overlay mount information of the updated base image and the container ID of the updated image-puller container;
[0034] A comparison module, used for comparing the image file storage information of the updated image-puller image with the image file storage information of the image to be updated, to obtain a difference file;
[0035] An updating module is used to update the image file storage information of the image to be updated and the container file storage information of the container to be updated based on the difference file.
[0036] In a possible implementation, the first acquisition module is specifically configured to:
[0037] Obtain the basic image update information configured by the user through the application publishing system; adjust the hash value of the original image-puller image based on the basic image update information to obtain the updated image-puller image.
[0038] In a possible implementation, the second acquisition module is specifically configured to:
[0039] Obtaining the image file storage information of the original image-puller image determined by the update-image container based on the container ID of the updated image-puller container; determining the image file storage information of the updated image-puller image based on the image file storage information of the original image-puller image and the overlay mount information of the updated base image;
[0040] Get the update-image container to mount the / proc and container root directory on the host machine using the hostPath method, query the image name of the image to be updated corresponding to the container to be updated based on the container ID of the update image-puller container; determine the image file storage information of the image to be updated based on the image name of the image to be updated.
[0041] In a possible implementation, the update module is specifically used to:
[0042] If the difference file is a newly added difference file, then the difference file is added to the image file storage information of the image to be updated and the container file storage information of the container to be updated;
[0043] If the difference file is a modified difference file, modifying the difference file in the image file storage information of the image to be updated and the container file storage information of the container to be updated;
[0044] If the difference file is a deleted difference file, the difference file is deleted from the image file storage information of the image to be updated and the container file storage information of the container to be updated.
[0045] In a possible implementation, the device further includes a writing module;
[0046] The writing module is used to write the newly added difference file or the modified difference file into the image file storage information of the image to be updated by means of a hard link if the difference file is at least one of a newly added difference file and a modified difference file.
[0047] In a possible implementation, the device further includes a copy module;
[0048] The copy module is used for copying the newly added difference file to the container file storage information of the container to be updated if the difference file is a newly added difference file.
[0049] In a possible implementation, after the difference file is updated, the device further includes an update result indicating module;
[0050] The update result indication module is used to send update result information to the application publishing system, and the update result information indicates that K8S has completed image preheating and dynamic file update.
[0051] In a third aspect, the present application provides a container image management device based on K8S, the device comprising: a processor, a memory, and a system bus;
[0052] The processor and the memory are connected via the system bus;
[0053] The memory is used to store one or more programs, and the one or more programs include instructions, which, when executed by the processor, enable the processor to execute the K8S-based container image management method described in the first aspect above.
[0054] In a fourth aspect, the present application provides a computer-readable storage medium, which stores instructions. When the instructions are executed on a device, the device executes the K8S-based container image management method described in the first aspect above.
[0055] It can be seen that this application has the following beneficial effects:
[0056] The present application provides a container image management method based on K8S, which is applied to K8S. In response to determining that the base image used by the original image-puller image has been updated, the updated image-puller image is obtained; based on the updated image-puller image, an updated image-puller container is created and downloaded; a container startup command is sent to the update-image container, and the container startup command carries the overlay mount information of the updated base image and the container ID of the updated image-puller container. The image file storage information of the updated image-puller image and the image file storage information of the image to be updated corresponding to the container to be updated are obtained by the update-image container based on the overlay mount information of the updated base image and the container ID of the updated image-puller container; the image file storage information of the updated image-puller image and the image file storage information of the image to be updated are compared to obtain a difference file; based on the difference file, the image file storage information of the image to be updated and the container file storage information of the container to be updated are updated. In this way, there is no need to deploy a controller or grant high permissions to the container. Image update preheating can be completed with low permissions, which can improve the efficiency of preheating the container image after the update. It can also realize dynamic update of image files and container files based on difference files, avoiding the need to rebuild and publish applications due to file modifications, and avoiding the participation of business parties in modifications to tools or files that are not related to business codes, which can improve the convenience of container image updates and the real-time nature of file updates.
[0057] The embodiment of the present application also provides a device corresponding to the above method, which has the same beneficial effects as the above method. BRIEF DESCRIPTION OF THE DRAWINGS
[0058] Figure 1 A flowchart of a container image management method based on K8S provided in an embodiment of the present application;
[0059] Figure 2 A schematic diagram of the configuration of an original image-puller image provided in an embodiment of the present application;
[0060] Figure 3 An example diagram of the use of an original image-puller image provided in an embodiment of the present application;
[0061] Figure 4 A file schematic diagram of a dynamicimagev1.0 before image update provided in an embodiment of the present application;
[0062] Figure 5 A schematic diagram of the execution system architecture of a container image management method based on K8S provided in an embodiment of the present application;
[0063] Figure 6 A configuration diagram of updating an image-puller image provided in an embodiment of the present application;
[0064] Figure 7 A schematic diagram of an interface for updating image-puller image download is provided for an embodiment of the present application;
[0065] Figure 8 An example diagram of using an image-puller image for updating provided in an embodiment of the present application;
[0066] Fig. 9 A schematic diagram of the architecture of an update-image container provided in an embodiment of the present application;
[0067] Fig.10 A code schematic diagram of a container startup command notifying update-image related information provided in an embodiment of the present application;
[0068] Fig.11 A schematic diagram of an image-puller container log provided in an embodiment of the present application;
[0069] Fig.12 A schematic diagram of another image-puller container log provided in an embodiment of the present application;
[0070] Fig.13 A structural diagram of a container and an image in an overlay file system provided in an embodiment of the present application;
[0071] FIG. 14( a) is a schematic diagram of a hostPath configuration provided in an embodiment of the present application;
[0072] FIG14( b) is a schematic diagram of another hostPath configuration provided in an embodiment of the present application;
[0073] Fig.15 A flowchart of another K8S-based container image management method provided in an embodiment of the present application;
[0074] Fig.16 A schematic diagram of the structure of a container image management device based on K8S provided in an embodiment of the present application;
[0075] Fig.17 A schematic diagram of the structure of a K8S-based container image management device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0076] The following will be combined with the drawings in the embodiments of the present application to clearly and completely describe the technical solutions in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.
[0077] In this application, relational terms such as first and second, etc. are used only to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. The terms "comprise", "comprising" or any other variants thereof are intended to cover non-exclusive inclusion, so that the process, method, article or equipment including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or equipment. In the absence of further restrictions, the elements defined by the statement "comprising a ..." do not exclude the presence of other identical elements in the process, method, article or equipment including the elements.
[0078] At present, when there is an update to the image of the container, the image preheating configuration information of the controller device is usually used to create an image preheating job, and the container in the job is given high permissions (privileged permissions) to interact with CRI to complete the image file pulling of the container. See patents CN117033325A, CN117061503A and CN114153565A, which grant high permissions to access CRI to complete the image file pulling. However, the setting of the deployment controller giving the container high permissions to interact with CRI makes the container run in a heavyweight environment, and the image update conditions of the container are cumbersome, resulting in low efficiency of container image preheating, and the currently used image update method does not synchronize the update file to the container layer file, which reduces the real-time update of the image file and container file corresponding to the container.
[0079] Based on this, the embodiment of the present application provides a container image management method and device based on K8S, which is applied to K8S, does not require the deployment of a controller and does not require the high authority of the container, is suitable for completing the image file pull with low authority, and can also realize the dynamic update of the image file and the container file. Specifically: in response to determining that the base image used by the original image-puller image is updated, the updated image-puller image is obtained; based on the updated image-puller image, an updated image-puller container is created and downloaded; a container start command is sent to the update-image container, and the container start command carries the overlay mount information of the updated base image and the container ID of the updated image-puller container. The image file storage information of the updated image-puller image and the image file storage information of the image to be updated corresponding to the container to be updated are obtained by the update-image container based on the overlay mount information of the updated base image and the container ID of the updated image-puller container; the image file storage information of the updated image-puller image and the image file storage information of the image to be updated corresponding to the container to be updated are compared; the image file storage information of the updated image-puller image and the image file storage information of the image to be updated are obtained, and the image file storage information of the image to be updated and the container file storage information of the container to be updated are updated based on the difference file. It is suitable for completing image update preheating with low permissions, and can improve the preheating efficiency of container images after updates. It can also realize dynamic updates of image files and container files based on differential files, avoiding the need to rebuild and publish applications due to file modifications, and avoiding the need for business parties to participate in modifications to tools or files that are not related to business codes, which can improve the convenience of container image updates and the real-time nature of file updates.
[0080] In order to facilitate understanding of the technical solution provided in the embodiment of the present application, a K8S-based container image management method and device provided in the embodiment of the present application are described below in conjunction with the accompanying drawings.
[0081] Explain the nouns that appear.
[0082] K8S: The abbreviation of kubernetes, is an open source container orchestration engine used to manage containerized applications on multiple hosts in a cloud platform.
[0083] Daemonset: A controller for K8S that ensures that a copy of a Pod is running on each node in the cluster; suitable for tasks that require running an instance on each node.
[0084] Pod: Pod is a resource for K8S to store application services. It can store one or more containers and use these containers to run application services.
[0085] Container: container, which can be a Docker container in the embodiment of the present application.
[0086] Node: A node in a K8S cluster. A node is usually a physical machine or a virtual machine.
[0087] overlay: refers to the overlay file system, which is a Linux kernel function. It is similar to a stacked file system and is the storage driver for the Docker container.
[0088] Among them, in the native function of K8S, kubelet manages the version of the Pod container.
[0089] The kubelet on each Node will calculate a hash value for each container in all Pod.spec.containers on the local machine and record it in the actual created container. If we modify the image field of a container in the Pod, the kubelet will find that the hash value of the container has changed and is inconsistent with the hash of the container created in the past on the machine. Then the kubelet will stop the old container and create a new container based on the container in the latest Pod spec.
[0090] In the Pod spec, only the image field in containers / initContainers can be modified.
[0091] In the embodiment of the present application, the hostpath of the K8S native function can also be used to map the / proc and $docker_root_dir of the image layer and container layer on the node.
[0092] Next, a container image management method based on K8S provided in an embodiment of the present application is described. It can be applied to K8S, see Figure 1 , Figure 1 A flow chart of a K8S-based container image management method provided in an embodiment of the present application, the method specifically includes S101-S106.
[0093] S101: In response to determining that a base image used by an original image-puller image has been updated, an updated image-puller image is obtained.
[0094] When an image needs to be preheated / updated, K8S is requested through the application release system (CD continuous delivery deployment system) to update the image of the image-puller container of the update-image-agent pod. This can automatically perform image preheating and dynamically update the corresponding changed files in the container layer and image layer overlay.
[0095] The update-image-agent service runs in daemonset mode. The update-image service Pod contains two containers: update-image container and image-puller container.
[0096] Users can configure the update information of the base image baseimage through the application publishing system. For example, the base image update information is baseimage:v2.0; the original image-puller image is baseimage:v1.0. Figure 2 and Figure 3 , Figure 2 A schematic diagram of the configuration of an original image-puller image provided in an embodiment of the present application; Figure 3 An example diagram of the use of an original image-puller mirror provided in an embodiment of the present application.
[0097] The container pre-running on the node using the base image baseimage is dynamicimagev1.0, see Figure 4 , Figure 4 A schematic diagram of files of dynamicimagev1.0 before image update provided by an embodiment of the present application. Before image update, / home / admin / of dynamicimagev1.0 includes files such as basev1.sh, demo, and x.log.
[0098] See also Figure 5 , Figure 5 A schematic diagram of the execution system architecture of a container image management method based on K8S provided in an embodiment of the present application. In one possible implementation, in response to determining that there is an update to the base image used by the original image-puller image, obtaining the updated image-puller image may include: obtaining the base image update information configured by the user through the application publishing system; adjusting the hash value of the original image-puller image based on the base image update information to obtain the updated image-puller image.
[0099] Using K8S native functions, K8S supports the pod.spec.containers.image field in Pod. During K8S operation, the application publishing system calls the apiserver and adjusts the image-puller image in update-image-{hashid}Pod from baseimage:v1.0 to baseimage:v2.0 based on the base image update information. That is, modify the image-puller image (pod.spec.containers.image) in the Pod.
[0100] Changes to the image will cause changes in the hash value. The hash value of the original image-puller image can be adjusted based on the base image update information to obtain an updated image-puller image. Figure 6 , Figure 6 A configuration diagram for updating an image-puller image is provided in an embodiment of the present application, where the image of the image-puller has been adjusted to baseimage:v2.0.
[0101] Relying on K8S native functions, apiserver pushes the updated image to kubelet, which calls CRI through grpc to download the image and rebuild the image-puller image. Specifically, when CRI receives the image-puller image change, it can request the image warehouse Harbor to automatically download the corresponding image baseimage:v2.0 to the local computer; see Figure 7 , Figure 7 The present invention provides an interface diagram for updating the image-puller image download. The node has automatically downloaded the updated image-puller image, achieving the image automatic download and image preheating functions.
[0102] S102: Create and download an updated image-puller container based on the updated image-puller image.
[0103] The updated image-puller image will be automatically downloaded to the node, and the updated image-puller image will be used to recreate the image-puller container. The recreated image-puller container is the updated image-puller container. Specifically, after CRI downloads the updated image-puller image, it recreates the image-puller container based on the latest image baseimage:v2.0 (the number of container restarts is increased by 1). See Figure 8, Figure 8 An example diagram of using an image-puller image for updating provided in an embodiment of the present application.
[0104] S103: Send a container startup command to the update-image container, where the container startup command carries overlay mount information of the updated base image and the container ID of the updated image-puller container.
[0105] See also Fig. 9 , Fig. 9 A schematic diagram of the architecture of an update-image container provided in an embodiment of the present application.
[0106] The container startup command in the Pod is used to notify the update-image container process. The container startup command carries the overlay mount information of the updated base image and the container ID of the updated image-puller container, so that the update-image container can obtain the image file storage information (overlay information) of the updated image-puller image based on the aforementioned information for subsequent file comparison. As an example, the container startup command can be a / updateImage request.
[0107] See also Fig.10 , Fig.10 A code schematic diagram of a container startup command notifying update-image related information provided in an embodiment of the present application. The update-image-agent runs in the form of a daemonset, and will run a Pod on each node. The Pod contains an image-puller container and an update-image container, and the network space of the same pod is shared, so the 127.0.0.1 address can be used for requests. To request http: / / 127.0.0.1:9000 / updateImage, you can use the shell to execute: curl--connect-timeout10-X POST-d"$(mount-t overlay)"http: / / 127.0.0.1:9000 / updateImage? id=$(cat / proc / self / cgroup|grep kubepods|head-1|sed's / .*-\(.*\)\..* / \1 / '), the container startup command carries the overlay mount information of the updated base image and the container ID of the updated image-puller container. The container startup command is used to notify the update-image container process that there is an image update.
[0108] See also Fig.11 , Fig.11 A schematic diagram of an image-puller container log provided in an embodiment of the present application. The update-image container can obtain the overlaymount information of the updated base image and the container ID of the updated image-puller container from the image-puller container log.
[0109] See also Fig.12 , Fig.12 A schematic diagram of another image-puller container log provided in an embodiment of the present application, in which image preheating and file dynamic update have been completed.
[0110] S104: Obtain the image file storage information of the updated image-puller image and the image file storage information of the image to be updated corresponding to the container to be updated, which is queried by the update-image container based on the overlaymount information of the updated base image and the container ID of the updated image-puller container.
[0111] The overlay mount information of the updated base image is the storage information of the base image in the recreated image-puller container, which may include image information and container runtime information.
[0112] Update the container ID of the image-puller container to the container ID of the newly created image-puller, which can be used to obtain the image name and container image ID of the image to be updated.
[0113] Through / proc and $docker_root_dir mapped by hostPath, filter out the containers to be updated that use the images with updated information and the images to be updated corresponding to the containers to be updated.
[0114] The image storage principle depends on the overlay file system. For the structure diagram of containers and images in the overlay file system, see Fig.13 : The container file system is in the merged layer, the image file system is in the lowerdir layer, and the screening and comparison of image file storage information depends on the aforementioned overlay file system.
[0115] In a possible implementation, the image file storage information of the updated image-puller image and the image file storage information of the image to be updated corresponding to the container to be updated are obtained by querying the update-image container based on the overlay mount information of the updated base image and the container ID of the updated image-puller container, including: obtaining the image file storage information of the original image-puller image determined by the update-image container based on the container ID of the updated image-puller container; determining the image file storage information of the updated image-puller image based on the image file storage information of the original image-puller image and the overlay mount information of the updated base image. Obtain the update-image container to mount the / proc and container root directory on the host machine using the hostPath method, query the image name of the image to be updated corresponding to the container to be updated based on the container ID of the updated image-puller container; and determine the image file storage information of the image to be updated based on the image name of the image to be updated.
[0116] See Figure 14 (a) and Figure 14 (b), which are schematic diagrams of the hostPath configuration provided in the embodiment of the present application. The node's / proc (used to obtain the overlay mount information of all containers on the node) and $docker_root_dir ( / data / docker in this embodiment, used to obtain and access the actual overlay file system directory) are mounted to the / data / proc and / data / docker directories in the update-image container. The overlay file system and container file system of all images on the node can be accessed in the update-image container, so that the update-image program can be used to obtain the actual file from the lowerdir in the overlay mount information for comparison.
[0117] In the overlay file system, the update-image container can determine the image file storage information of the original image-puller image based on the container ID of the updated image-puller container; based on the image file storage information of the original image-puller image and the overlay mount information of the updated base image, the image file storage information of the updated image-puller image can be determined.
[0118] The update-image container receives the / updateImage request from the image-puller container, and based on the received overlay mount information of the updated base image, updates the container ID of the image-puller container, and mounts / proc and ${docker_root_dir} (docker root directory) on the host machine through hostPath, the update-image container can query the image name of the newly created image-puller container, that is, the image name of the image to be updated this time, and can find the image file storage information of the image to be updated from the overlay file system.
[0119] Specifically, the directory name under / data / docker / containers / on the node is the ID of the container on the node. You can find all containers on the node and the image (dynamicimage) used by the container ID. The code example of this step is as follows:
[0120]
[0121]
[0122] Find the overlay mount information of the container based on the container ID, and then find the container using the baseimage image and the lowerdir of the image it uses. Find the mount-id through the container ID, and then find the lowerdir through the mount-id. The code example for finding the mount-id through the container ID is as follows:
[0123]
[0124] The following is a code example for finding lowerdir by mount-id:
[0125]
[0126] In one possible implementation, the update-image container process traverses all containers on the current node, obtains the image names of these containers and the image file storage information of their images in the same way, and filters out the image file storage information of the containers using the new image and their base images.
[0127] After determining the difference file, the container file storage information of the container using the new image and the image file storage information of the corresponding container image can be uniformly updated to improve the preheating efficiency of the container image after the update.
[0128] S105: Compare the image file storage information of the updated image-puller image with the image file storage information of the image to be updated to obtain a difference file.
[0129] Start comparing the image file storage information of the updated image-puller image and the image file storage information of the image to be updated:
[0130] If the image file storage information of the updated image-puller image contains the file but the image file storage information of the image to be updated does not contain the file, it is determined to be a newly added difference file, and the newly added difference file includes the information that needs to be added.
[0131] If the image file storage information of the updated image-puller image is inconsistent with the size, modify time, owner, and permission of the image file storage information of the image to be updated, it is determined to be a modified difference file, and the modified difference file includes the information that needs to be modified.
[0132] If the image file storage information of the updated image-puller image does not contain the information, but the image file storage information of the image to be updated contains the information, it is determined to delete the difference file, and the deleted difference file includes the information to be deleted.
[0133] The aforementioned addition, modification, and deletion are all operations on the image file storage information of the image to be updated, that is, operations on the image file of the running container.
[0134] The following uses baseimage:v2.0 and baseimage:v1.0 as examples.
[0135] The lowerdir of baseimage:v2.0 is:
[0136] lowerdir= / data / docker / overlay2 / l / ZT7BMGYR5E4SQOYNAG4MEP4UAU: / data / docker / overlay2 / l / DH M5VGSEVO6YL6HZQV7V5FQR7W: / data / docker / overlay2 / l / CWFW2GKM5X35OJDVQV4TCF72JO: / data / docker / overlay2 / l / BCLABZE4CWFRS2 AYHS6CC567BG: / data / docker / overlay2 / l / SMVCKBFAA6IAQ3BNC4D54HGECF: / data / docker / overlay2 / l / CZKN64Z3GFU6BMBMDIZI5ZWFIF
[0137] Compare the image file storage information of baseimage:v2.0 with the image file storage information of baseimage:v1.0. The comparison code example is as follows.
[0138]
[0139]
[0140] The comparison shows that baseimage:v2.0 has one more basev2.sh under / home / admin than baseimage:v1.0.
[0141] Each lowerdir is for a layer of the image (that is, a layer of overlay), for example, in / home / admin under / data / docker / overlay2 / l / DHM5VGSEVO6YL6HZQV7V5FQR7W, there is an additional basev2.sh. The sample code is as follows:
[0142]
[0143] It should be noted that, before the update, the image file storage information of the image to be updated is consistent with the baseimage:v1.0 image file storage information.
[0144] In one possible implementation, containers that use a new image can be screened out by determining whether the container image uses a baseimage.
[0145] If the bottom layer of the lowerdir of the image used by other containers on the node is the same as the baseimage, it can be determined that the other container is a container using the new image.
[0146] For example, as mentioned above: the lowest level of lowerdir of dynamicimage container is / data / docker / overlay2 / l / CZKN64Z3GFU6BMBMDIZI5ZWFIF, and the lowest level of lowerdir of baseimage is / data / docker / overlay2 / l / CZKN64Z3GFU6BMBMDIZI5ZWFIF, so dynamicimage uses the baseimage basic image, and its corresponding container is the container that uses the new image.
[0147] S106: Update the image file storage information of the image to be updated and the container file storage information of the container to be updated based on the difference file.
[0148] The type of the difference file may be at least one of a newly added difference file, a modified difference file, and a deleted difference file. If the difference file is a newly added difference file, the difference file is added to the image file storage information of the image to be updated and the container file storage information of the container to be updated; if the difference file is a modified difference file, the difference file is modified in the image file storage information of the image to be updated and the container file storage information of the container to be updated; if the difference file is a deleted difference file, the difference file is deleted from the image file storage information of the image to be updated and the container file storage information of the container to be updated.
[0149] In one possible implementation, if the difference file is at least one of a newly added difference file and a modified difference file, the newly added difference file or the modified difference file is written into the image file storage information of the image to be updated by means of a hard link. If the difference file is a deleted difference file, the address where the information is located can be indicated by a delete command to complete the deletion.
[0150] According to the difference file, the update of image-puller image baseimage:v2.0 adds / home / admin / basev2.sh, and adds basev2.sh to the corresponding overlay layer of all images that use the baseimage basic image. In the dynamicimage image layer ( / data / docker / overlay2 / l / OD3HEDC7SIX7WLW6XQ2RDX7C56), basev2.sh is added in the form of a hard link under / home / admin. The following is a code example of the storage information of the baseimage image file and the dynamicimage image file. The Inode number is the same, and a hard link is used. At this point, the file update has been dynamically completed in the image to be updated corresponding to the container to be updated.
[0151] The following is a code example of baseimage image file storage information:
[0152]
[0153] The following is a code example of dynamicimage image file storage information:
[0154]
[0155] After obtaining the difference file, the newly added and modified files are written to the overlay of the old base image (the image to be updated) through hard links to complete the image preheating and dynamic update of the image file.
[0156] In a possible implementation, if the difference file is a newly added difference file, the newly added difference file is copied to the container file storage information of the container to be updated. If the difference file is a modified difference file, the modified difference file is copied to the container file storage information of the container to be updated to replace the original information. If the difference file is a deleted difference file, the difference file is deleted from the container file storage information of the container to be updated.
[0157] Addition, modification and deletion can be achieved through instructions or other methods, which are not limited in this application.
[0158] The container layer file is a mapping of the image layer file. When the image layer file is updated, the container layer file will not be updated synchronously. The overlay file system has a cache and cannot display the changed files in the container layer. In the embodiment of the present application, the corresponding files in the running container file system can also be dynamically updated according to the difference file. Only after the update can the newly added basev2.sh be seen in the container. Write the difference file to the container's file system to complete the dynamic update of the container file.
[0159] According to the overlay file system principle of container image storage, first find merged in the overlay mount information, and then copy the newly added basev2.sh to merged in the container file system, merged: / data / docker / overlay2 / 51e5575c59d7ce0b1dd1b669016c4bba94e1f96096a5c6117d4cd917a795fb72 / merged.
[0160]
[0161] A new basev2.sh is added to the dynamicimage container, and the Inode is different from the basev2.sh in the baseimage, indicating that the file with the same content and permissions has been re-copied to the container file system. In the baseimage and dynamicimage, the content of basev2.sh is the same, but the inode number is different, indicating that the newly added basev2.sh has been copied and updated from the image layer to the container layer of the dynamicimage.
[0162] The following is a code example of baseimage image file storage information:
[0163]
[0164]
[0165] The code example of the updated dynamicimage image file storage information is as follows:
[0166]
[0167] After completing the differential file update, in one possible implementation, update result information may also be sent to the application publishing system, where the update result information indicates that K8S has completed image preheating and file dynamic update.
[0168] The update-image container process notifies the application publishing system to complete image preheating and file dynamic update.
[0169] To facilitate understanding of the foregoing embodiments, see Fig.15 , Fig.15 A flowchart of another K8S-based container image management method provided in an embodiment of the present application.
[0170] The user configures the update information of the base image through the application publishing system. The application publishing system calls the apiserver to modify the image field of the image-puller container in the Pod to the specified base image (pod.spec.containers.image).
[0171] The apiserver pushes the updated image to the kubelet. The kubelet calls CRI through grpc, downloads the image, and rebuilds the image-puller image.
[0172] After receiving the image-puller image change, CRI requests the image repository harbor to automatically download the image, rebuild the image-puller container, and obtain the updated image-puller container.
[0173] The update-image-puller container notifies the update-image container of the update operation through the container startup command ((curl--connect-timeout 10-XPOST-d"$(mount-t overlay)""http: / / 127.0.0.1:9000 / updateImage?id=$(cat / proc / self / cgroup|grep kubepods|head-1|sed's / .*-\(.*\)\..* / \1 / ')")). The update-image container receives the overlay mount information of the updated base image and the container ID of the updated image-puller container. Through the / proc and $docker_root_dir mapped by hostPath, it traverses and filters out the container to be updated on the current node that uses the updated base image and the image to be updated corresponding to the container to be updated. The update-image container process compares the new base image file (image file storage information of the updated image-puller image) with the old base image file (image file storage information of the image to be updated).
[0174] Write the newly added difference files and modified files to the image file storage information of the image to be updated through hard links. The file system of the container is a mapping of the image layer. When the files in the image layer are updated, the files in the container layer will not be updated synchronously. The difference files need to be updated to the file system of the container, that is, the container file storage information of the container to be updated is updated.
[0175] The corresponding image file storage information and container file storage information are dynamically updated according to the difference files. After the container preheating and file dynamic update are completed, the update-image container process notifies the application publishing system to complete the closed loop of the entire functional process.
[0176] Among them, the user directly updates the operation and maintenance tools in the application container and the operation and maintenance tools in its container image to achieve the effects of image preheating and dynamic update of images and container files. Specifically, the update-image-agent with simple processing logic is deployed in a lightweight daemonset manner. No controller, no agent, and no high-privilege configuration are required to interact with CRI. It only relies on the native capabilities of K8S and overlay file systems. Through the logical combination and adaptation of the code, image preheating and dynamic file updates can be completed. It is simple to use. Image preheating does not require the use of high permissions. Only ordinary permissions can be used to load the image to the local in advance, reducing the time to download the image when the container starts, thereby speeding up the startup of the container.
[0177] Dynamic file updates can solve special needs in container application operation and maintenance, such as updating / adjusting a certain operation and maintenance tool or operation and maintenance script, without the need for the business side to rebuild the application, and automatically complete the automatic dynamic update of the basic image and the update of the operation and maintenance tools in the container file system. This avoids the need to rebuild and publish the application when part of the files in the basic image needs to be modified, and executes a series of cumbersome processes and operations such as image rebuilding -> image publishing -> application re-publishing -> pod rescheduling -> image downloading -> container starting -> container running -> pod running -> application providing external services, and avoids the participation of the business side, so that the tool / file update that is not related to the business code is improved in convenience, timeliness of file updates, and self-service.
[0178] Based on the contents of the above steps S101-S106, it can be known that the method is applied to K8S. In response to determining that the base image used by the original image-puller image has been updated, an updated image-puller image is obtained; based on the updated image-puller image, an updated image-puller container is created and downloaded; a container startup command is sent to the update-image container, and the container startup command carries the overlay mount information of the updated base image and the container ID of the updated image-puller container. The image file storage information of the updated image-puller image and the image file storage information of the image to be updated corresponding to the container to be updated are obtained by the update-image container based on the overlay mount information of the updated base image and the container ID of the updated image-puller container; the image file storage information of the updated image-puller image and the image file storage information of the image to be updated are compared to obtain a difference file; based on the difference file, the image file storage information of the image to be updated and the container file storage information of the container to be updated are updated. In this way, there is no need to deploy a controller or grant high permissions to the container. Image update preheating can be completed with low permissions, which can improve the efficiency of preheating the container image after the update. It can also realize dynamic update of image files and container files based on difference files, avoiding the need to rebuild and publish applications due to file modifications, and avoiding the participation of business parties in modifications to tools or files that are not related to business codes, which can improve the convenience of container image updates and the real-time nature of file updates.
[0179] The above embodiment of the present application provides a container image management method based on K8S. Next, a container image management device based on K8S is provided in the embodiment of the present application, which is applied to K8S and used to execute the above Figure 1The method shown in the figure, the function of the container image management device based on K8S is described below. The structural diagram of the container image management device based on K8S is shown in Fig.16 As shown, it includes: a first acquisition module 1601, a creation and downloading module 1602, a sending module 1603, a second acquisition module 1604, a comparison module 1605 and an update module 1606.
[0180] in,
[0181] A first acquisition module 1601 is configured to acquire an updated image-puller image in response to determining that a base image used by the original image-puller image is updated;
[0182] A creation and downloading module 1602, configured to create and download an updated image-puller container based on the updated image-puller image;
[0183] A sending module 1603 is used to send a container startup command to the update-image container, where the container startup command carries the overlay mount information of the updated base image and the container ID of the updated image-puller container;
[0184] The second acquisition module 1604 is used to obtain the image file storage information of the updated image-puller image and the image file storage information of the image to be updated corresponding to the container to be updated, which is queried by the update-image container based on the overlay mount information of the updated base image and the container ID of the updated image-puller container;
[0185] A comparison module 1605 is used to compare the image file storage information of the updated image-puller image with the image file storage information of the image to be updated to obtain a difference file;
[0186] The updating module 1606 is configured to update the image file storage information of the image to be updated and the container file storage information of the container to be updated based on the difference file.
[0187] In a possible implementation, the first acquisition module 1601 is specifically configured to:
[0188] Obtain the basic image update information configured by the user through the application publishing system; adjust the hash value of the original image-puller image based on the basic image update information to obtain the updated image-puller image.
[0189] In a possible implementation, the second acquisition module 1604 is specifically configured to:
[0190] Obtaining the image file storage information of the original image-puller image determined by the update-image container based on the container ID of the updated image-puller container; determining the image file storage information of the updated image-puller image based on the image file storage information of the original image-puller image and the overlay mount information of the updated base image;
[0191] Get the update-image container to mount the / proc and container root directory on the host machine using the hostPath method, query the image name of the image to be updated corresponding to the container to be updated based on the container ID of the update image-puller container; determine the image file storage information of the image to be updated based on the image name of the image to be updated.
[0192] In a possible implementation, the updating module 1606 is specifically configured to:
[0193] If the difference file is a newly added difference file, then the difference file is added to the image file storage information of the image to be updated and the container file storage information of the container to be updated;
[0194] If the difference file is a modified difference file, modifying the difference file in the image file storage information of the image to be updated and the container file storage information of the container to be updated;
[0195] If the difference file is a deleted difference file, the difference file is deleted from the image file storage information of the image to be updated and the container file storage information of the container to be updated.
[0196] In a possible implementation, the device further includes a writing module;
[0197] The writing module is used to write the newly added difference file or the modified difference file into the image file storage information of the image to be updated by means of a hard link if the difference file is at least one of a newly added difference file and a modified difference file.
[0198] In a possible implementation, the device further includes a copy module;
[0199] The copy module is used for copying the newly added difference file to the container file storage information of the container to be updated if the difference file is a newly added difference file.
[0200] In a possible implementation, after the difference file is updated, the device further includes an update result indicating module;
[0201] The update result indication module is used to send update result information to the application publishing system, and the update result information indicates that K8S has completed image preheating and dynamic file update.
[0202] The present application provides a container image management device based on K8S, which is applied to K8S. The device includes: a first acquisition module, a creation and download module, a sending module, a second acquisition module, a comparison module and an update module. The first acquisition module is used to obtain an updated image-puller image in response to determining that the base image used by the original image-puller image is updated. The creation and download module is used to create and download an updated image-puller container based on the updated image-puller image. The sending module is used to send a container startup command to the update-image container, and the container startup command carries the overlay mount information of the updated base image and the container ID of the updated image-puller container. The second acquisition module is used to obtain the image file storage information of the updated image-puller image and the image file storage information of the image to be updated corresponding to the container to be updated, which are queried by the update-image container based on the overlay mount information of the updated base image and the container ID of the updated image-puller container. The comparison module is used to compare the image file storage information of the updated image-puller image and the image file storage information of the image to be updated to obtain a difference file. The update module is used to update the image file storage information of the image to be updated and the container file storage information of the container to be updated based on the difference file. In this way, there is no need to deploy a controller or grant high permissions to the container, and it is suitable for completing image update preheating with low permissions, which can improve the efficiency of preheating the container after the image is updated; and it can also realize dynamic updates of image files and container files based on difference files, avoiding the need to rebuild and publish applications due to file modifications, and avoiding the participation of business parties in modifying tools or files that are not related to business codes, which can improve the convenience of container image updates and the real-time nature of file updates.
[0203] Based on the container image management method based on K8S provided in the above method embodiment, the present application embodiment provides a container image management device based on K8S, see Fig.17 , the device includes: a processor, a memory, and a system bus;
[0204] The processor and the memory are connected via the system bus;
[0205] The memory is used to store one or more programs, and the one or more programs include instructions, which, when executed by the processor, enable the processor to execute the K8S-based container image management method described in any of the above embodiments.
[0206] Based on the K8S-based container image management method provided in the above method embodiment, an embodiment of the present application provides a computer-readable storage medium, which stores instructions. When the instructions are executed on a device, the device executes the K8S-based container image management method described in any of the above embodiments.
[0207] Each embodiment in this specification is described in a progressive manner, and the same or similar parts between the embodiments can be referred to each other, and each embodiment focuses on the differences from other embodiments. In particular, for the device or device embodiment, since it is basically similar to the method embodiment, the description is relatively simple, and the relevant parts can be referred to the partial description of the method embodiment. The device and device embodiments described above are merely schematic, wherein the units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place, or they may be distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the scheme of this embodiment. Ordinary technicians in this field can understand and implement it without paying creative labor.
[0208] Professionals may further appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, computer software, or a combination of the two. In order to clearly illustrate the interchangeability of hardware and software, the composition and steps of each example have been generally described in the above description according to function. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professionals and technicians may 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.
[0209] The above description of the disclosed embodiments enables those skilled in the art to implement or use the present application. Various modifications to these embodiments will be apparent to those skilled in the art, and the general principles defined herein may be implemented in other embodiments without departing from the spirit or scope of the present application. Therefore, the present application will not be limited to the embodiments shown herein, but will conform to the widest scope consistent with the principles and novel features disclosed herein.
Claims
1. A container image management method based on K8S, characterized in that: The method is applied to K8S, including: In response to determining that there is an update to the base image used by the original image-puller image, obtaining an updated image-puller image; Create and download an updated image-puller container based on the updated image-puller image; Send a container startup command to the update-image container, where the container startup command carries the overlay mount information of the updated base image and the container ID of the updated image-puller container; Obtain the image file storage information of the updated image-puller image and the image file storage information of the image to be updated corresponding to the container to be updated, which is queried by the update-image container based on the overlay mount information of the updated base image and the container ID of the updated image-puller container; Compare the image file storage information of the updated image-puller image with the image file storage information of the image to be updated to obtain a difference file; The image file storage information of the image to be updated and the container file storage information of the container to be updated are updated based on the difference file.
2. According to the K8S-based container image management method according to claim 1, it is characterized in that: In response to determining that the base image used by the original image-puller image is updated, obtaining the updated image-puller image includes: Get the basic image update information configured by the user through the application release system; The hash value of the original image-puller image is adjusted based on the base image update information to obtain the updated image-puller image.
3. According to the K8S-based container image management method according to claim 1, it is characterized in that: The obtaining of the image file storage information of the updated image-puller image and the image file storage information of the image to be updated corresponding to the container to be updated obtained by querying the update-image container based on the overlay mount information of the updated base image and the container ID of the updated image-puller container includes: Obtaining image file storage information of the original image-puller image determined by the update-image container based on the container ID of the update image-puller container; Determine the image file storage information of the updated image-puller image based on the image file storage information of the original image-puller image and the overlay mount information of the updated base image; Obtain the / proc and container root directory on the host machine mounted by the update-image container using the hostPath method, and query the image name of the image to be updated corresponding to the container to be updated based on the container ID of the update image-puller container; The image file storage information of the image to be updated is determined based on the image name of the image to be updated.
4. The container image management method based on K8S according to claim 1 is characterized in that: The updating of the image file storage information of the image to be updated and the container file storage information of the container to be updated based on the difference file includes: If the difference file is a newly added difference file, then the difference file is added to the image file storage information of the image to be updated and the container file storage information of the container to be updated; If the difference file is a modified difference file, modifying the difference file in the image file storage information of the image to be updated and the container file storage information of the container to be updated; If the difference file is a deleted difference file, the difference file is deleted from the image file storage information of the image to be updated and the container file storage information of the container to be updated.
5. The container image management method based on K8S according to claim 4 is characterized in that: If the difference file is at least one of a newly added difference file and a modified difference file, the newly added difference file or the modified difference file is written into the image file storage information of the image to be updated in a hard link manner.
6. The container image management method based on K8S according to claim 4 is characterized in that: If the difference file is a newly added difference file, the newly added difference file is copied to the container file storage information of the container to be updated.
7. The container image management method based on K8S according to claim 1 is characterized in that: After the difference file is updated, the method further includes: Send update result information to the application publishing system, where the update result information indicates that K8S has completed image preheating and file dynamic update.
8. A container image management device based on K8S, characterized in that: The device is applied to K8S and includes: A first acquisition module, configured to acquire an updated image-puller image in response to determining that a base image used by the original image-puller image is updated; A creation and downloading module, used to create and download an updated image-puller container based on the updated image-puller image; A sending module, used to send a container startup command to the update-image container, wherein the container startup command carries the overlay mount information of the updated base image and the container ID of the updated image-puller container; A second acquisition module is used to obtain the image file storage information of the updated image-puller image and the image file storage information of the image to be updated corresponding to the container to be updated, which is queried by the update-image container based on the overlay mount information of the updated base image and the container ID of the updated image-puller container; A comparison module, used for comparing the image file storage information of the updated image-puller image with the image file storage information of the image to be updated, to obtain a difference file; An updating module is used to update the image file storage information of the image to be updated and the container file storage information of the container to be updated based on the difference file.
9. The container image management device based on K8S according to claim 8, characterized in that: The first acquisition module is specifically used to: Obtain the basic image update information configured by the user through the application publishing system; adjust the hash value of the original image-puller image based on the basic image update information to obtain the updated image-puller image.
10. The container image management device based on K8S according to claim 8, characterized in that: The second acquisition module is specifically used for: Obtaining the image file storage information of the original image-puller image determined by the update-image container based on the container ID of the updated image-puller container; determining the image file storage information of the updated image-puller image based on the image file storage information of the original image-puller image and the overlay mount information of the updated base image; Get the update-image container to mount the / proc and container root directory on the host machine using the hostPath method, query the image name of the image to be updated corresponding to the container to be updated based on the container ID of the update image-puller container; determine the image file storage information of the image to be updated based on the image name of the image to be updated.
