Image building method and related devices based on Kubernetes

By traversing the Dockerfile to build the image file package, determine the dependencies, and implement the image construction without privileged access, it solves the dependencies and security risks of Docker daemon, and improves the construction efficiency and security.

CN114138415BActive Publication Date: 2024-11-26STATE GRID INFORMATION & TELECOMM GRP CO LTD +1
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202111466838.6
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-12-01
Publication Date
2024-11-26
Estimated Expiration
2041-12-01

AI Technical Summary

Technical Problem

Building container images directly from standard Docker files through Docker depends on interactive access of Docker daemons, which poses security risks, and the compilation efficiency of traditional construction methods is low and the cloud-native integration is poor.

Method used

By traversing the Dockerfile input image input by the user, obtain the construction information of each construction stage, determine the dependencies, and build the image file package based on the context path and dependencies, avoid directly relying on the Docker daemon and achieving unprivileged access.

Benefits of technology

Improves security, reduces risks, adapts to Kubernetes and Serverless infrastructure, and improves the scale and efficiency of mirroring construction.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114138415B_ABST
    Figure CN114138415B_ABST
Patent Text Reader

Abstract

The present invention discloses a mirror image construction method and related devices based on Kubernetes. A custom construction executor can be used to traverse the mirror image construction file Dockerfile input by a user through the construction executor, thereby obtaining the construction information of each construction stage in the Dockerfile; obtaining the context path information of each construction stage input by the user; determining the dependency of each construction stage according to the construction instructions of each construction stage; for any of the construction stages, executing: according to the context path information of the construction stage, the dependency of the construction stage and the construction information of the construction stage, the mirror image file package of the construction stage is constructed. The present invention does not need to rely on the interactive access of the Docker daemon process, can directly build the corresponding mirror image file according to the construction instructions in the Dockerfile, does not need to obtain higher permissions, and has higher security.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of cloud computing, and in particular to a Kubernetes-based image building method and related devices. Background Art

[0002] With the increasing popularity of cloud architecture, more and more enterprises are adopting cloud-native methods to develop applications to increase speed, increase flexibility, improve software quality and reduce risks. Containers are an important part of the cloud-native concept, with characteristics such as "lightweight" and "portable". The construction of container images plays a very important role in continuous integration and continuous delivery. Kubernetes is an open source container orchestration platform that can manage distributed and containerized applications on a large scale. In a Kubernetes cluster, using Docker to build container images is the most commonly used method. However, building container images directly from a standard Dockerfile through Docker usually relies on interactive access to the Docker daemon, which means that containers need to obtain higher access rights to run, which poses a security risk. Summary of the invention

[0003] In view of the above problems, the present invention provides a Kubernetes-based image building method and related devices that overcome the above problems or at least partially solve the above problems.

[0004] In the first aspect, a method for building an image based on Kubernetes includes:

[0005] Traversing the image building file Dockerfile input by the user, thereby obtaining the building information of each building stage in the Dockerfile, wherein the building information of each building stage at least includes: at least one building instruction of the corresponding building stage, one Dockerfile corresponds to multiple building stages, the building instruction is an instruction for building an image file, and one building instruction corresponds to one image file layer;

[0006] Obtaining context path information of each of the construction stages input by the user, wherein the context path information represents a specified path based on which the image file package is compiled and constructed in each of the construction stages, one of the construction stages corresponds to one of the image file packages, and one of the image file packages corresponds to at least one of the image file layers;

[0007] Determining the dependency relationship of each construction phase according to the construction instructions of each construction phase;

[0008] For any of the construction phases, the following steps are performed: building an image file package of the construction phase according to the context path information of the construction phase, the dependency relationship of the construction phase and the construction information of the construction phase.

[0009] In combination with the first aspect, in some optional implementations, building the image file package of the construction phase according to the context path information of the construction phase, the dependency relationship of the construction phase and the construction information of the construction phase includes:

[0010] Determine whether the decompression flag bit in the construction phase is a decompression flag;

[0011] If so, decompress the other image file packages according to the dependency relationship as the basic image file of the corresponding construction stage, and execute the construction instructions in the construction information based on the basic image file, so as to construct the image file layer corresponding to each construction instruction based on the context path information;

[0012] If not, directly executing the building instructions in the building information, so as to construct the image file layer corresponding to each building instruction based on the context path information;

[0013] Each of the image file layers is packaged in an image folder, thereby constructing an image file package in the construction phase.

[0014] In combination with the previous embodiment, in some optional embodiments, the execution of the construction instruction to construct the corresponding image file layer based on the context path information includes:

[0015] For any of the building instructions, the following steps are performed: determining whether an image file layer corresponding to the building instruction is stored in the image cache;

[0016] If it is stored, directly adding the image file layer in the image cache as the image file layer obtained by executing the build instruction this time to the image folder in the build stage;

[0017] If not stored, the building instructions in the building information are executed, so as to construct the image file layers corresponding to each building instruction based on the context path information and add them to the image folder.

[0018] In combination with the first aspect, in some optional implementations, after determining the dependency relationship of each construction stage according to the construction instructions of each construction stage, the method further includes:

[0019] Generate a first information list according to the context path information of each construction stage, the dependency relationship of each construction stage and the construction information of each construction stage, wherein the context path information, the dependency relationship and the construction information of different construction stages are stored in different areas of the first information list, and one area corresponds to one construction stage;

[0020] The step of constructing the image file package of the construction phase according to the context path information of the construction phase, the dependency relationship of the construction phase and the construction information of the construction phase comprises:

[0021] The image file package of the construction phase is constructed according to the context path information, the dependency relationship and the construction information corresponding to the area of ​​the construction phase recorded in the first information list.

[0022] In combination with the previous embodiment, in some optional embodiments, before building the image file package of the construction stage according to the context path information, the dependency relationship and the construction information corresponding to the area in the construction stage recorded in the first information list, the method further includes:

[0023] For any of the regions, the following steps are performed: determining whether the image file layer corresponding to each of the build instructions recorded in the region is stored in the image cache;

[0024] If it is stored, setting a corresponding executed flag for the corresponding build instruction;

[0025] If not stored, setting a corresponding unexecuted flag for the corresponding build instruction;

[0026] The step of constructing the image file package of the construction phase according to the context path information, the dependency relationship, and the construction information corresponding to the region of the construction phase recorded in the first information list includes:

[0027] For the build instruction with the executed flag, the following steps are performed: directly using the image file in the image cache as the image file obtained by executing the build instruction this time;

[0028] For the build instructions with the unexecuted flag, the following steps are performed: executing the build instructions to generate the corresponding image file;

[0029] The image files are packaged in an image folder, thereby constructing an image file package in the construction phase.

[0030] Optionally, in some optional implementation manners, after the image file package of the construction phase is constructed, the method further includes:

[0031] For the image files stored in the image cache, the following steps are performed: performing a SHA256 check on the file information of the image files to obtain a corresponding current check value, wherein the file information at least includes: at least one of permission information and file content;

[0032] If the current check value is inconsistent with the previous check value of the image file recorded in the metadata file, the current check value of the image file is updated to the metadata file, wherein the metadata file records the metadata of each executed build instruction, and the metadata at least includes the check value.

[0033] In combination with the first aspect, in some optional implementations, after the image file package of the construction phase is constructed, the method further includes:

[0034] The image file packages are packaged to form a complete image file, and pushed to the target registration center.

[0035] In a second aspect, a Kubernetes-based image building device includes: a building information obtaining unit, a path information obtaining unit, a dependency determining unit, and an image file building unit;

[0036] The build information obtaining unit is used to traverse the image build file Dockerfile input by the user, so as to obtain the build information of each build stage in the Dockerfile, wherein the build information of each build stage at least includes: at least one build instruction of the corresponding build stage, one Dockerfile corresponds to multiple build stages, the build instruction is an instruction for building an image file, and one build instruction corresponds to one image file layer;

[0037] The path information obtaining unit is used to obtain the context path information of each of the construction stages input by the user, wherein the context path information represents a specified path based on which the image file package is compiled and constructed in each of the construction stages, one of the construction stages corresponds to one of the image file packages, and one of the image file packages corresponds to at least one of the image file layers;

[0038] The dependency determination unit is used to determine the dependency relationship of each construction stage according to the construction instructions of each construction stage;

[0039] The image file building unit is used to execute, for any of the building stages, the following steps: building an image file package for the building stage according to the context path information of the building stage, the dependency relationship of the building stage and the building information of the building stage.

[0040] In a third aspect, a computer-readable storage medium stores a program thereon, and when the program is executed by a processor, the image building method based on Kubernetes described in any one of the above items is implemented.

[0041] In a fourth aspect, an electronic device comprises at least one processor, and at least one memory and a bus connected to the processor; wherein the processor and the memory communicate with each other through the bus; and the processor is used to call program instructions in the memory to execute any of the above-mentioned image building methods based on Kubernetes.

[0042] By means of the above technical scheme, the image building method and related devices based on Kubernetes provided by the present invention can obtain the building information of each building stage in the Dockerfile by traversing the image building file Dockerfile input by the user, wherein the building information of each building stage at least includes: at least one building instruction of the corresponding building stage, one Dockerfile corresponds to multiple building stages, the building instruction is an instruction for building an image file, and one building instruction corresponds to one image file; obtaining the context path information of each building stage input by the user, wherein the context path information represents the specified path based on which the image file package is compiled and built in each building stage, one building stage corresponds to one image file package, and one image file package corresponds to at least one image file layer; determining the dependency relationship of each building stage according to the building instructions of each building stage; for any building stage, executing: building the image file package of the building stage according to the context path information of the building stage, the dependency relationship of the building stage and the building information of the building stage. It can be seen from this that the present invention does not need to rely on interactive access of the Docker daemon process, and can directly build the corresponding image file according to the build instructions in the Dockerfile without obtaining higher permissions, and has higher security.

[0043] The above description is only an overview of the technical solution of the present invention. In order to more clearly understand the technical means of the present invention, it can be implemented according to the contents of the specification. In order to make the above and other purposes, features and advantages of the present invention more obvious and easy to understand, the specific implementation methods of the present invention are listed below. BRIEF DESCRIPTION OF THE DRAWINGS

[0044] Various other advantages and benefits will become apparent to those of ordinary skill in the art by reading the detailed description of the preferred embodiments below. The accompanying drawings are only for the purpose of illustrating the preferred embodiments and are not to be considered as limiting the present invention. Moreover, the same reference symbols are used throughout the accompanying drawings to represent the same components. In the accompanying drawings:

[0045] Figure 1 A flowchart of a Kubernetes-based image building method provided by the present invention is shown;

[0046] Figure 2 A schematic diagram of a Dockerfile provided by the present invention is shown;

[0047] Figure 3 A schematic diagram of another Dockerfile provided by the present invention is shown;

[0048] Figure 4 A dependency diagram of a construction phase provided by the present invention is shown;

[0049] Figure 5 A schematic diagram of the structure of a Kubernetes-based image building device provided by the present invention is shown;

[0050] Figure 6 A structural schematic diagram of an electronic device provided by the present invention is shown. DETAILED DESCRIPTION

[0051] With the increasing popularity of cloud architecture, more and more enterprises are adopting cloud-native methods to develop applications to increase speed, increase flexibility, improve software quality and reduce risks. Containers are an important part of the cloud-native concept, with characteristics such as "lightweight" and "portable". The construction of container images plays a very important role in continuous integration and continuous delivery. Kubernetes is an open source container orchestration platform that can manage distributed and containerized applications on a large scale. In a Kubernetes cluster, using Docker to build container images is the most commonly used method. However, building container images directly from standard Dockerfiles through Docker usually relies on interactive access to the Docker daemon, which means that containers can only run with higher access rights, which poses a security risk. At the same time, traditional construction methods have problems such as low compilation efficiency and poor cloud-native integration.

[0052] In response to the above problems, the present invention proposes a Kubernetes-based image building method and related devices, which do not rely on the Docker daemon, do not require privileged access, run more securely, and can effectively reduce risks. In the DevOps scenario, it is more suitable for Kubernetes and Serverless infrastructure, and the scale and efficiency of building images are effectively improved.

[0053] The exemplary embodiments of the present disclosure will be described in more detail below with reference to the accompanying drawings. Although the exemplary embodiments of the present disclosure are shown in the accompanying drawings, it should be understood that the present disclosure can be implemented in various forms and should not be limited by the embodiments set forth herein. On the contrary, these embodiments are provided to enable a more thorough understanding of the present disclosure and to fully convey the scope of the present disclosure to those skilled in the art.

[0054] like Figure 1 As shown, the present invention provides a mirror image construction method based on Kubernetes, including: S100, S200, S300 and S400;

[0055] S100, traversing the image building file Dockerfile input by the user, so as to obtain the building information of each building stage in the Dockerfile, wherein the building information of each building stage at least includes: at least one building instruction of the corresponding building stage, one Dockerfile corresponds to multiple building stages, the building instruction is an instruction for building an image file, and one building instruction corresponds to one image file layer;

[0056] Optionally, Dockerfile is a text file used to build an image. The content of the text file contains construction information such as construction instructions and instructions required to build the image file. The construction instructions may include: FROM, ADD, COPY and RUN, etc., which is not limited to the present invention.

[0057] Among them, construction instructions such as FROM, ADD, COPY and RUN are well-known technologies in the field, which will not be described in detail in this article. For details, please refer to relevant descriptions in the field.

[0058] For example, Figure 2 The Dockerfile shown includes the following building instructions: FROM and ADD, where: Figure 2 The "builder" after "as" is the alias of the image file package generated at this stage.

[0059] like Figure 2The following table shows the contents recorded in a Dockerfile. Users can enter different Dockerfiles according to different needs. For details, please refer to the specifications of Dockerfile in this field.

[0060] Optionally, different build phases can be distinguished by build instructions. For example, Figure 3 As shown, COPY--from is followed by the image file package builder built in the previous build phase, which means that the content copied later is copied from the image file package builder generated in the previous build phase, that is, this build phase depends on the build phase of the builder.

[0061] Loop through each build stage in the entire Dockerfile list and check the build instructions. If it is COPY, determine whether it contains the image file package generated in the previous build stage based on whether there is --from followed by the image file package of the previous build stage, and use this as the basis for dividing different build stages.

[0062] S200, obtaining context path information of each of the construction stages input by the user, wherein the context path information represents a specified path based on which the image file package is compiled and constructed in each of the construction stages, one of the construction stages corresponds to one of the image file packages, and one of the image file packages corresponds to at least one of the image file layers;

[0063] The context path information is the decompressed content of the compressed file of the image file package uploaded by the Docker client to the server, that is, the compilation and construction is based on the files in this specified path.

[0064] Optionally, the present invention can take each construction stage as an object, construct the object information of each object to form a corresponding object file, including: context path information, construction information, base image, configuration information, snapshot information, base image summary and image cache layer and other information. Among them, base image: as shown in the above Dockerfile, the image file package after from is the base image, that is, adding an image layer on the image layer of the base image. Configuration information: information such as Hostname, environment variables, and default exposed ports for storing image files. Snapshot information: information describing image files that can be ignored at the current stage. Base image summary: SHA256 hash value of the base image, that is, the verification value obtained after SHA256 verification. Some of this information can be obtained in the execution of other subsequent processes, and some can be obtained in the aforementioned steps.

[0065] S300, determining the dependency relationship of each construction stage according to the construction instructions of each construction stage;

[0066] Optionally, as mentioned above, the dependency relationship between the current construction phase and other construction phases may be determined by building the image file package following the from instruction, and the present invention does not impose any limitation on this.

[0067] Optionally, in the subsequent process of building the image file, the dependency relationship can be used to determine whether it is necessary to decompress the compressed packages of other image file packages at the current stage, and which compressed package to decompress, which is beneficial to the subsequent construction process. The present invention does not limit this.

[0068] For example, Figure 4 As shown, the B and C build phases need to rely on the products of the A build phase, and the E build phase needs to rely on the products of the B, C, and D build phases. The A and D build phases do not need to rely on other build phases, that is, they are completely independent build phases.

[0069] S400: For any of the construction phases, executing: constructing an image file package of the construction phase according to the context path information of the construction phase, the dependency relationship of the construction phase and the construction information of the construction phase.

[0070] Optionally, the present invention does not impose any specific restrictions on the method of constructing the image file package in the construction phase, and any feasible method belongs to the protection scope of the present invention. Figure 1 In the embodiment shown, in some optional embodiments, the S400 includes: step 1.1, step 1.2, step 1.3 and step 1.4;

[0071] Step 1.1, determining whether the decompression flag bit in the construction phase is a decompression flag;

[0072] For example, a build instruction (ADD instruction) for adding an image file layer based on an original image file package and a build instruction (COPY instruction) for copying other image files both require decompressing other image files.

[0073] The construction information of the present invention may include a decompression flag. If the decompression flag is: decompression flag, it means that other image file packages need to be decompressed; if the decompression flag is: no decompression flag, it means that other image file packages do not need to be decompressed.

[0074] Optionally, when traversing the image building file Dockerfile, the present invention can determine whether each building stage includes building instructions that require decompression of image file packages in other building stages. If included, the decompression flag in the building information of the corresponding building stage is set to the decompression flag; otherwise, it is set to the no decompression flag.

[0075] If yes, go to step 1.2, if no, go to step 1.3;

[0076] Step 1.2, decompressing other image file packages as the basic image files of the corresponding construction stage according to the dependency relationship, and executing the construction instructions in the construction information based on the basic image files, so as to construct the image file layers corresponding to each of the construction instructions based on the context path information;

[0077] Optionally, for a build instruction that requires decompression of other image file packages, the corresponding image file package may be decompressed, and on this basis, the build instruction of the build phase may be executed, and the present invention does not impose any limitation on this.

[0078] Step 1.3, directly executing the build instructions in the build information, thereby constructing the image file layer corresponding to each build instruction based on the context path information;

[0079] Optionally, for a build instruction that does not require decompression of other image file packages, the build instruction of the build phase may be directly executed, and the present invention does not impose any limitation on this.

[0080] Optionally, the present invention does not limit the process of executing the build instructions in the build information, thereby constructing the image file layers corresponding to each of the build instructions based on the context path information. For example, in combination with the previous embodiment, in some optional embodiments, executing the build instructions in the build information in step 1.2 or step 1.3, thereby constructing the image file layers corresponding to each of the build instructions based on the context path information, includes: step 2.1, step 2.2 and step 2.3;

[0081] Step 2.1: for any of the above-mentioned building instructions, execute: determining whether the image file layer corresponding to the above-mentioned building instruction is stored in the image cache;

[0082] If it is stored, go to step 2.2; if it is not stored, go to step 2.3;

[0083] Optionally, if it is stored, it means that the build instruction has been executed before and there is no need to execute it again to improve efficiency.

[0084] Step 2.2, directly adding the image file layer in the image cache as the image file layer obtained by executing the build instruction this time to the image folder in the build stage;

[0085] Step 2.3: execute the building instructions in the building information, thereby building the image file layer corresponding to each building instruction based on the context path information and adding it to the image folder.

[0086] Step 1.4: Pack each of the image file layers into an image folder, thereby constructing the image file package of the construction stage.

[0087] Optionally, for each construction stage, a corresponding image file package can be constructed, and each image file package includes at least one image file layer, so that the overall image file system can be constructed according to each image file package later. The present invention does not impose any limitation on this.

[0088] Optionally, in addition to constructing a corresponding image file package, the image file package may also be compressed to form a corresponding compressed package, which is not limited in the present invention.

[0089] Optionally, as mentioned above, the present invention can take the construction stage as the object, construct the object information of each object to form a corresponding object file. Figure 1 In the illustrated implementation manner, in some optional implementation manners, after S300, the method further comprises: generating a first information list according to the context path information of each of the construction stages, the dependency relationship of each of the construction stages, and the construction information of each of the construction stages, wherein the context path information, the dependency relationship, and the construction information of different construction stages are stored in different areas of the first information list, and one area corresponds to one construction stage;

[0090] Optionally, each area in the first information list here can be understood as an object file. Of course, the content of each area can also be constructed as an independent list separately, and the present invention does not limit this.

[0091] Optionally, constructing the first information list is beneficial to the subsequent construction of the image file package, which is not limited in the present invention.

[0092] The S400 includes: constructing an image file package of the construction phase according to the context path information, the dependency relationship and the construction information corresponding to the area of ​​the construction phase recorded in the first information list.

[0093] In combination with the previous embodiment, in some optional embodiments, before building the image file package of the construction phase according to the context path information, the dependency relationship and the construction information corresponding to the area in the construction phase recorded in the first information list, the method further includes: step 3.1, step 3.2 and step 3.3;

[0094] Step 3.1: for any of the regions, executing: determining whether the image file layer corresponding to each of the build instructions recorded in the region is stored in the image cache;

[0095] If it is stored, execute step 3.2; if it is not stored, execute step 3.3;

[0096] Step 3.2, setting a corresponding executed flag for the corresponding build instruction;

[0097] Step 3.3, setting a corresponding unexecuted flag for the corresponding build instruction;

[0098] Optionally, as mentioned above, some build instructions may not need to be executed again, so corresponding identifiers may be set for different build instructions in the first information list to indicate whether the build instruction needs to be executed again, so as to improve the efficiency of the present invention.

[0099] For example, constructing the image file package of the construction phase according to the context path information, the dependency relationship and the construction information corresponding to the region of the construction phase recorded in the first information list includes: step 3.4, step 3.5 and step 3.6;

[0100] Step 3.4, for the build instructions with the executed flag set, execute: directly use the image file layer in the image cache as the image file layer obtained by executing the build instruction this time;

[0101] Step 3.5, for the build instructions with the unexecuted flag set, execute: execute the build instructions to generate the corresponding image file;

[0102] Step 3.6: Pack each of the image file layers into an image folder, thereby constructing an image file package in the construction phase.

[0103] Optionally, in some optional implementations, after the image file package of the construction phase is constructed, the method further includes: step 4.1 and step 4.2;

[0104] Step 4.1: for the image file layer stored in the image cache, perform: perform SHA256 verification on the file information of the image file layer to obtain a corresponding current verification value, wherein the file information at least includes: at least one of permission information and file content;

[0105] Step 4.2: If the current check value is inconsistent with the previous check value of the image file layer recorded in the metadata file, the current check value of the image file layer is updated to the metadata file, wherein the metadata file records the metadata of each executed build instruction, and the metadata at least includes the check value.

[0106] Optionally, the check value obtained by the SHA256 check can be a SHA256 hash value. If the check values ​​are inconsistent, it means that the image file layer has changed, and the corresponding SHA256 hash value, configuration information, exposed port information and environment variable information of the image file layer also need to be modified accordingly and recorded in the metadata file, which is not limited by the present invention.

[0107] Optionally, in order to perform SHA256 verification, the present invention may define a snapshot builder.

[0108] Define the snapshot builder, including the initialization function, determining whether to take a snapshot, and executing the snapshot.

[0109] (a) Initialize the ignore list of files.

[0110] (b) by verifying the file content and permission information of each image file except the ignorelist in the image file before executing the build instruction, and then comparing these verification values ​​with the verification values ​​after the build instruction is executed, determining whether to take a snapshot;

[0111] (c) After executing a build instruction, the executor creates a snapshot in user space and compares the snapshot with the previous state stored in memory. If there are changes, a tarball of the changed files is created and the contents of the tarball and whether any files have been changed are returned. The new changes are then generated into an image file and added to the base image. The modification information related to this build instruction is written into the metadata file of the image file.

[0112] Combination Figure 1 In the illustrated implementation manner, in certain optional implementation manners, after the image file package of the construction phase is constructed, the method further comprises: packaging each of the image file packages into a complete image file, and pushing the complete image file to a target registration center.

[0113] like Figure 5 As shown, the present invention provides a mirror image building device based on Kubernetes, including: a building information obtaining unit 100, a path information obtaining unit 200, a dependency determination unit 300 and a mirror image file building unit 400;

[0114] The build information obtaining unit 100 is used to traverse the image building file Dockerfile input by the user, so as to obtain the build information of each building stage in the Dockerfile, wherein the build information of each building stage at least includes: at least one build instruction of the corresponding building stage, one Dockerfile corresponds to multiple building stages, the build instruction is an instruction for building an image file, and one build instruction corresponds to one image file layer;

[0115] The path information obtaining unit 200 is used to obtain the context path information of each of the construction stages input by the user, wherein the context path information represents the specified path based on which the image file package is compiled and constructed in each of the construction stages, one of the construction stages corresponds to one of the image file packages, and one of the image file packages corresponds to at least one of the image file layers;

[0116] The dependency determination unit 300 is used to determine the dependency of each construction phase according to the construction instructions of each construction phase;

[0117] The image file building unit 400 is used to execute, for any of the building stages, the following steps: building an image file package of the building stage according to the context path information of the building stage, the dependency relationship of the building stage and the building information of the building stage.

[0118] Combination Figure 5 In the embodiment shown, in some optional embodiments, the image file construction unit 400 includes: a first judgment subunit, a first result subunit, a second result subunit and a first construction subunit;

[0119] A first judging subunit, used to judge whether the decompression flag bit in the construction phase is a decompression flag;

[0120] If yes, the first result subunit is triggered, if no, the second result subunit is triggered;

[0121] A first result subunit is used to decompress other image file packages as the basic image files of the corresponding construction stage according to the dependency relationship, and execute the construction instructions in the construction information based on the basic image files, so as to construct the image file layers corresponding to the construction instructions based on the context path information;

[0122] A second result subunit is used to directly execute the building instructions in the building information, so as to construct the image file layer corresponding to each building instruction based on the context path information;

[0123] The first construction subunit is used to package each of the image file layers into an image folder, thereby constructing an image file package in the construction stage.

[0124] In combination with the previous embodiment, in some optional embodiments, when the first result subunit or the second result subunit executes the construction instruction to obtain the corresponding image file, it specifically includes: a second judgment subunit, a third result subunit and a fourth result subunit;

[0125] The second judgment subunit is used to perform, for any of the building instructions: judging whether the image file layer corresponding to the building instruction is stored in the image cache;

[0126] If it is stored, the third result subunit is triggered; if it is not stored, the fourth result subunit is triggered;

[0127] A third result subunit is used to directly add the image file layer in the image cache as the image file layer obtained by executing the build instruction this time to the image folder in the build stage;

[0128] The fourth result subunit is used to execute the building instructions in the building information, so as to build the image file layer corresponding to each building instruction based on the context path information and add it to the image folder.

[0129] Combination Figure 5 In the illustrated embodiment, in certain optional embodiments, the apparatus further comprises: a first list generating unit;

[0130] A first list generating unit is used for generating a first information list according to context path information of each construction stage, the dependency relationship of each construction stage and the construction information of each construction stage after determining the dependency relationship of each construction stage according to the construction instructions of each construction stage, wherein the context path information, the dependency relationship and the construction information of different construction stages are stored in different areas of the first information list, and one area corresponds to one construction stage;

[0131] The image file construction unit 400 includes: a second construction subunit;

[0132] The second construction subunit is used to build the image file package of the construction stage according to the context path information, the dependency relationship and the construction information corresponding to the area of ​​the construction stage recorded in the first information list.

[0133] In combination with the previous embodiment, in some optional embodiments, the device further includes: a third judgment subunit, a fifth result subunit and a sixth result subunit;

[0134] A third judgment subunit is used for, before building the image file package of the construction phase according to the context path information, the dependency relationship and the construction information corresponding to the region in the construction phase recorded in the first information list, for any of the regions, executing: determining whether the image file layer corresponding to each of the construction instructions recorded in the region is stored in the image cache;

[0135] If it is stored, the fifth result subunit is triggered; if it is not stored, the sixth result subunit is triggered;

[0136] A fifth result subunit, used for setting a corresponding executed flag for the corresponding build instruction;

[0137] A sixth result subunit, used for setting a corresponding unexecuted flag for the corresponding build instruction;

[0138] The second construction subunit includes: an executed subunit, an unexecuted subunit and a third construction subunit;

[0139] The executed subunit is used to execute, for each build instruction with the executed flag set, directly using the image file layer in the image cache as the image file layer obtained by executing the build instruction this time;

[0140] The unexecuted subunit is used to execute, for each build instruction with the unexecuted flag, the build instruction to generate the corresponding image file layer;

[0141] The third construction subunit is used to package each of the image file layers into an image folder, so as to construct the image file package of the construction stage.

[0142] Optionally, in certain optional embodiments, the device further comprises: a check value unit and a comparison unit;

[0143] A check value unit is used for, after constructing the image file package of the construction phase, executing, for each of the image file layers stored in the image cache: performing a SHA256 check on the file information of the image file layer, thereby obtaining a corresponding current check value, wherein the file information at least includes: at least one of permission information and file content;

[0144] A comparing unit is used to update the current check value of the image file layer to the metadata file if the current check value is inconsistent with the previous check value of the image file layer recorded in the metadata file, wherein the metadata of each executed build instruction is recorded in the metadata file, and the metadata at least includes the check value.

[0145] Combination Figure 5 In the embodiment shown, in certain optional embodiments, the device further comprises: a pushing unit;

[0146] The push unit is used to package the image file packages in the construction phase into a complete image file after the image file packages are constructed, and push the complete image file to the target registration center.

[0147] The present invention provides a computer-readable storage medium having a program stored thereon, wherein when the program is executed by a processor, the image building method based on Kubernetes described in any one of the above items is implemented.

[0148] like Figure 6 As shown, the present invention provides an electronic device 70, which includes at least one processor 701, and at least one memory 702 and a bus 703 connected to the processor 701; wherein the processor 701 and the memory 702 communicate with each other through the bus 703; the processor 701 is used to call the program instructions in the memory 702 to execute any of the above-mentioned image building methods based on Kubernetes.

[0149] 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. Moreover, the terms "comprise", "include" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device 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 device. In the absence of further restrictions, the elements defined by the statement "comprise a ..." do not exclude the presence of other identical elements in the process, method, article or device including the elements.

[0150] Each embodiment in this specification is described in a related 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 system 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.

[0151] The above description of the disclosed embodiments enables one skilled in the art to implement or use the present invention. Various modifications to these embodiments will be apparent to one 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 invention. Therefore, the present invention will not be limited to the embodiments shown herein, but rather to the widest scope consistent with the principles and novel features disclosed herein.

[0152] The above description is only a preferred embodiment of the present invention and is not intended to limit the protection scope of the present invention. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present invention are included in the protection scope of the present invention.

Claims

1. A method for building an image based on Kubernetes, characterized in that: include: Traversing the image building file Dockerfile input by the user, thereby obtaining the building information of each building stage in the Dockerfile, wherein the building information of each building stage at least includes: at least one building instruction of the corresponding building stage, one Dockerfile corresponds to multiple building stages, the building instruction is an instruction for building an image file, and one building instruction corresponds to one image file layer; Obtaining context path information of each of the construction stages input by the user, wherein the context path information represents a specified path based on which the image file package is compiled and constructed in each of the construction stages, one of the construction stages corresponds to one of the image file packages, and one of the image file packages corresponds to at least one of the image file layers; Determining the dependency relationship of each construction phase according to the construction instructions of each construction phase; For any of the construction phases, the following steps are performed: building an image file package of the construction phase according to the context path information of the construction phase, the dependency relationship of the construction phase and the construction information of the construction phase.

2. The method according to claim 1, characterized in that The step of constructing the image file package of the construction phase according to the context path information of the construction phase, the dependency relationship of the construction phase and the construction information of the construction phase includes: Determine whether the decompression flag bit in the construction phase is a decompression flag; If so, decompress the other image file packages according to the dependency relationship as the basic image file of the corresponding construction stage, and execute the construction instructions in the construction information based on the basic image file, so as to construct the image file layer corresponding to each construction instruction based on the context path information; If not, directly executing the building instructions in the building information, thereby building the image file layer corresponding to each building instruction based on the context path information; Each of the image file layers is packaged in an image folder, thereby constructing an image file package in the construction phase.

3. The method according to claim 2, characterized in that The executing the building instructions in the building information, thereby building the image file layer corresponding to each building instruction based on the context path information, includes: For any of the building instructions, the following steps are performed: determining whether an image file layer corresponding to the building instruction is stored in the image cache; If it is stored, directly adding the image file layer in the image cache as the image file layer obtained by executing the build instruction this time to the image folder in the build stage; If not stored, the building instructions in the building information are executed, so as to construct the image file layers corresponding to each building instruction based on the context path information and add them to the image folder.

4. The method according to claim 1, characterized in that: After determining the dependency relationship of each construction phase according to the construction instructions of each construction phase, the method further includes: Generate a first information list according to the context path information of each construction stage, the dependency relationship of each construction stage and the construction information of each construction stage, wherein the context path information, the dependency relationship and the construction information of different construction stages are stored in different areas of the first information list, and one area corresponds to one construction stage; The step of constructing the image file package of the construction phase according to the context path information of the construction phase, the dependency relationship of the construction phase and the construction information of the construction phase comprises: The image file package of the construction phase is constructed according to the context path information, the dependency relationship and the construction information corresponding to the area of ​​the construction phase recorded in the first information list.

5. The method according to claim 4, characterized in that Before building the image file package of the construction phase according to the context path information, the dependency relationship and the construction information corresponding to the region in the construction phase recorded in the first information list, the method further includes: For any of the regions, the following steps are performed: determining whether the image file layer corresponding to each of the build instructions recorded in the region is stored in the image cache; If it is stored, setting a corresponding executed flag for the corresponding build instruction; If not stored, setting a corresponding unexecuted flag for the corresponding build instruction; The step of constructing the image file package of the construction phase according to the context path information, the dependency relationship, and the construction information corresponding to the region of the construction phase recorded in the first information list includes: For the build instruction with the executed flag set, the following steps are performed: directly using the image file layer in the image cache as the image file layer obtained by executing the build instruction this time; For the build instructions with the unexecuted flag, the following steps are performed: executing the build instructions to generate the corresponding image file layer; Each of the image file layers is packaged in an image folder, thereby constructing an image file package in the construction phase.

6. The method according to claim 3 or 5, characterized in that: After the image file package of the construction phase is constructed, the method further includes: For the image file layer stored in the image cache, executing: performing SHA256 verification on the file information of the image file layer to obtain a corresponding current verification value, wherein the file information at least includes: at least one of permission information and file content; If the current check value is inconsistent with the previous check value of the image file layer recorded in the metadata file, the current check value of the image file layer is updated to the metadata file, wherein the metadata file records the metadata of each executed build instruction, and the metadata at least includes the check value.

7. The method according to claim 1, characterized in that After the image file package of the construction phase is constructed, the method further includes: The image file packages are packaged to form a complete image file, and pushed to the target registration center.

8. A mirror image building device based on Kubernetes, characterized in that: include: Build information acquisition unit, path information acquisition unit, dependency determination unit and image file building unit; The build information obtaining unit is used to traverse the image build file Dockerfile input by the user, so as to obtain the build information of each build stage in the Dockerfile, wherein the build information of each build stage at least includes: at least one build instruction of the corresponding build stage, one Dockerfile corresponds to multiple build stages, the build instruction is an instruction for building an image file, and one build instruction corresponds to one image file layer; The path information obtaining unit is used to obtain the context path information of each of the construction stages input by the user, wherein the context path information represents a specified path based on which the image file package is compiled and constructed in each of the construction stages, one of the construction stages corresponds to one of the image file packages, and one of the image file packages corresponds to at least one of the image file layers; The dependency determination unit is used to determine the dependency relationship of each construction stage according to the construction instructions of each construction stage; The image file building unit is used to execute, for any of the building stages, the following steps: building an image file package for the building stage according to the context path information of the building stage, the dependency relationship of the building stage and the building information of the building stage.

9. A computer-readable storage medium having a program stored thereon, characterized in that: When the program is executed by the processor, the Kubernetes-based image building method is implemented as described in any one of claims 1 to 7.

10. An electronic device, characterized in that: The electronic device includes at least one processor, and at least one memory and a bus connected to the processor; wherein the processor and the memory communicate with each other through the bus; the processor is used to call program instructions in the memory to execute the Kubernetes-based image building method according to any one of claims 1 to 7.