A container time synchronization method and system for K8s clusters
By deploying timestamp sending and receiving units in the K8s cluster, calculating time deviations and updating configuration files, preloading the time compensation dynamic link library, the problem of automatic container time synchronization in the K8s cluster is solved, and the automatic synchronization of cluster content time and the independence of host time is realized.
Patent Information
- Application Number
- CN202411739654.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-29
- Publication Date
- 2025-04-04
AI Technical Summary
The existing container synchronization method is mainly aimed at a single container, and it is impossible to realize automatic container time synchronization for container clusters such as K8s.
Through the collaborative work of the timestamp sending unit and the timestamp receiving unit, the deviation between the synchronous timestamp and the host system time is calculated, the time compensation configuration file is updated, and the time compensation dynamic link library is preloaded when the container is started, and the container system time is corrected through the API interface.
It realizes automatic synchronization of system time of all containers in the K8s cluster without affecting the host system time.
Smart Images

Figure CN119766378A_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the field of container time synchronization, and specifically relates to a container time synchronization method and system for a K8s cluster. Background Art
[0002] Containers have become an important supporting technology in cloud computing due to their high efficiency, lightweight, and easy management. After years of development, K8s has defeated competitors such as Swarm and Mesos in the field of container management and has become the de facto container orchestration standard.
[0003] Since the containers managed by K8s share the operating system kernel with the host, changing the container system time will cause the host system time to change, affecting the operation of K8s itself and other system software. Existing container synchronization methods are mainly implemented for a single container by manually setting container startup parameters. They only solve the problem of single container time synchronization and cannot achieve automatic synchronization of container time in container clusters such as K8s. Summary of the invention
[0004] In view of the above problems, the object of the present invention is to provide a container time synchronization method and system for a K8s cluster.
[0005] The specific technical solution for achieving the purpose of the present invention is:
[0006] A container time synchronization method for a K8s cluster includes the following steps:
[0007] Step 1: The timestamp sending unit receives an external synchronization timestamp and sends the synchronization timestamp to the timestamp receiving unit;
[0008] Step 2: The timestamp receiving unit receives the synchronization timestamp, calculates the deviation between the synchronization timestamp and the host system time, and updates the time compensation configuration file;
[0009] Step 3: When the container is started, the time compensation dynamic link library is preloaded, and the application in the container calls the API interface provided by the time compensation dynamic link library;
[0010] Step 4: The time compensation dynamic link library corrects the container system time according to the time deviation in the time compensation configuration file.
[0011] Compared with the prior art, the present invention has the following beneficial effects:
[0012] The present invention discloses a container time synchronization method and system for a K8s cluster. A complete container cluster time synchronization system is formed by modules such as a timestamp sending module, a timestamp receiving module, a time compensation configuration file, a time compensation dynamic link library, and a container deployment file. Based on the dynamic link library preloading (LD_PRELOAD) mechanism of the Linux operating system, the preloaded time compensation dynamic link library compensates the host system time, thereby changing the container time without affecting the host system time, and realizing the system time synchronization of all containers in the K8s cluster.
[0013] The present invention is further described below in conjunction with specific implementation modes. BRIEF DESCRIPTION OF THE DRAWINGS
[0014] Figure 1 This is a flow chart of the container time synchronization method for K8s clusters of the present invention.
[0015] Figure 2 It is a schematic diagram of the container time compensation process of the present invention. DETAILED DESCRIPTION
[0016] Example
[0017] The technical solutions in the embodiments of the present invention will be described clearly and completely below in conjunction with the accompanying drawings in the embodiments of the present invention. The described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0018] As shown in this application and claims, unless the context clearly indicates an exception, the words "a", "an", "an kind" and / or "the" do not refer to the singular and may also include the plural. Generally speaking, the terms "include" and "comprise" only indicate the inclusion of the steps and elements that have been clearly identified, and these steps and elements do not constitute an exclusive list. The method or device may also include other steps or elements.
[0019] Unless otherwise specifically stated, the relative arrangement, numerical expressions and numerical values of the parts and steps set forth in these embodiments do not limit the scope of the present application. At the same time, it should be understood that, for ease of description, the sizes of the various parts shown in the accompanying drawings are not drawn according to the actual proportional relationship. The technology, method and equipment known to those of ordinary skill in the relevant field may not be discussed in detail, but in appropriate cases, the technology, method and equipment should be considered as a part of the authorization specification. In all examples shown and discussed here, any specific value should be interpreted as being merely exemplary, rather than as a limitation. Therefore, other examples of exemplary embodiments may have different values. It should be noted that similar reference numerals and letters represent similar items in the following drawings, so that once a certain item is defined in an accompanying drawing, it does not need to be further discussed in subsequent drawings.
[0020] Combination Figure 1 , a container time synchronization method for a K8s cluster, comprising the following steps:
[0021] Step 1: The timestamp sending unit receives an external synchronization timestamp and sends the synchronization timestamp to the timestamp receiving unit;
[0022] Step 2: The timestamp receiving unit receives the synchronization timestamp, calculates the deviation between the synchronization timestamp and the host system time, and updates the time compensation configuration file;
[0023] Step 3: When the container is started, the time compensation dynamic link library is preloaded, and the application in the container calls the API interface provided by the time compensation dynamic link library;
[0024] Among them, the time compensation dynamic link library is loaded when the container is started by preloading LD_PRELOAD to implement the call of the time API interface, such as gettimeofday;
[0025] Step 4: Combine Figure 2 , the time compensation dynamic link library corrects the container system time according to the time deviation in the time compensation configuration file:
[0026] Step 4-1, read the time deviation according to the obtained time compensation configuration file. For example, if the deviation is 5, it means that the container time is 5 seconds faster than the host time;
[0027] Step 4-2, call the standard function library to obtain the host system time, for example, "2024-09-2416:55:51";
[0028] Step 4-3: Correct the host system time based on the time deviation to obtain the container system time, that is, use the time deviation 5 to correct the host system time to obtain the container system time: "2024-09-24 16:55:56";
[0029] Step 4-4. Close the time compensation profile.
[0030] The timestamp sending unit is deployed on a node in the K8s cluster, receives an external synchronization timestamp, and sends the synchronization timestamp to the timestamp receiving unit in a multicast manner.
[0031] The timestamp receiving unit is deployed on each node in the K8s cluster, and is used to receive the synchronization timestamp sent by the timestamp sending unit, calculate the deviation between the synchronization timestamp and the host system time, and finally update the time deviation to the time compensation configuration file.
[0032] Each node in the K8s cluster deploys a time compensation configuration file, which is used to record the time deviation between the container system time and the host system time, and can be read and written by all containers and timestamp receiving units on the node.
[0033] In addition, the container deployment file of the container sets the container environment variables so that the container can find the preloaded time compensation dynamic link library and time compensation configuration file. Through the "LD_PRELOAD" environment variable, the time compensation dynamic link library is indicated as " / usr / local / lib / offsettime.so"; through the "OFFSET_TIMESTAMP_FILE" environment variable, the time compensation configuration file is indicated as " / usr / local / lib / offsettime.conf". And through the hostpath directory mapping, the containers on the same node can read and write the time compensation configuration file and the time compensation dynamic link library:
[0034] The specific code is as follows:
[0035]
[0036] This solution also provides a container time synchronization system for K8s clusters, including the following modules:
[0037] Timestamp sending module: used to receive external synchronization timestamps using the timestamp sending module, and send the synchronization timestamps to the timestamp receiving module;
[0038] The time deviation determination module is used to receive the synchronization timestamp using the timestamp receiving module, calculate the deviation between the synchronization timestamp and the host system time, and update the time compensation configuration file;
[0039] Time synchronization module: used to preload the time compensation dynamic link library when the container is started. The application in the container calls the API interface provided by the time compensation dynamic link library. The time compensation dynamic link library corrects the container system time according to the time deviation in the time compensation configuration file.
[0040] The present solution also provides a computer device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the following steps when executing the computer program:
[0041] Step 1: The timestamp sending unit receives an external synchronization timestamp and sends the synchronization timestamp to the timestamp receiving unit;
[0042] Step 2: The timestamp receiving unit receives the synchronization timestamp, calculates the deviation between the synchronization timestamp and the host system time, and updates the time compensation configuration file;
[0043] Step 3: When the container is started, the time compensation dynamic link library is preloaded, and the application in the container calls the API interface provided by the time compensation dynamic link library;
[0044] Step 4: The time compensation dynamic link library corrects the container system time according to the time deviation in the time compensation configuration file.
[0045] The present invention also provides a computer storable medium having a computer program stored thereon, wherein when the computer program is executed by a processor, the following steps are implemented:
[0046] Step 1: The timestamp sending unit receives an external synchronization timestamp and sends the synchronization timestamp to the timestamp receiving unit;
[0047] Step 2: The timestamp receiving unit receives the synchronization timestamp, calculates the deviation between the synchronization timestamp and the host system time, and updates the time compensation configuration file;
[0048] Step 3: When the container is started, the time compensation dynamic link library is preloaded, and the application in the container calls the API interface provided by the time compensation dynamic link library;
[0049] Step 4: The time compensation dynamic link library corrects the container system time according to the time deviation in the time compensation configuration file.
[0050] The above-mentioned embodiments only express several implementation methods of the present application, and the descriptions thereof are relatively specific and detailed, but they cannot be understood as limiting the scope of the invention patent. It should be pointed out that, for a person of ordinary skill in the art, several variations and improvements can be made without departing from the concept of the present application, and these all belong to the protection scope of the present application. Therefore, the protection scope of the patent of the present application shall be subject to the attached claims.
Claims
1. A container time synchronization method for K8s cluster, characterized in that: The following steps are involved: Step 1: The timestamp sending unit receives an external synchronization timestamp and sends the synchronization timestamp to the timestamp receiving unit; Step 2: The timestamp receiving unit receives the synchronization timestamp, calculates the deviation between the synchronization timestamp and the host system time, and updates the time compensation configuration file; Step 3: When the container is started, the time compensation dynamic link library is preloaded, and the application in the container calls the API interface provided by the time compensation dynamic link library; Step 4: The time compensation dynamic link library corrects the container system time according to the time deviation in the time compensation configuration file.
2. According to the container time synchronization method for K8s cluster according to claim 1, it is characterized in that: The timestamp sending unit in step 1 is deployed on a node in the K8s cluster, receives an external synchronization timestamp, and sends the synchronization timestamp to the timestamp receiving unit in a multicast manner.
3. According to the container time synchronization method for K8s cluster according to claim 1, it is characterized in that: The timestamp receiving unit is deployed on each node in the K8s cluster, and is used to receive the synchronization timestamp sent by the timestamp sending unit, calculate the deviation between the synchronization timestamp and the host system time, and finally update the time deviation to the time compensation configuration file.
4. According to the container time synchronization method for K8s cluster according to claim 3, it is characterized in that: Each node in the K8s cluster deploys a time compensation configuration file, which is used to record the time deviation between the container system time and the host system time, and can be read and written by all containers and timestamp receiving units on the node.
5. According to the container time synchronization method for K8s cluster according to claim 1, it is characterized in that: The time compensation dynamic link library in step 3 is loaded when the container is started by preloading LD_PRELOAD to implement the call of the time API interface.
6. According to the container time synchronization method for K8s cluster according to claim 1, it is characterized in that: The specific steps of correcting the host machine time in step 4 are: Step 4-1, read the time deviation according to the acquired time compensation configuration file; Step 4-2: Call the standard function library to obtain the host system time; Step 4-3: Correct the host system time based on the time deviation to obtain the container system time; Step 4-4. Close the time compensation profile.
7. The container time synchronization method for K8s cluster according to claim 1 is characterized in that: The container deployment file of the container enables the container to find the preloaded time compensation dynamic link library and time compensation configuration file by setting the container environment variables.
8. A container time synchronization system for K8s clusters, characterized in that: Includes the following modules: Timestamp sending module: used to receive external synchronization timestamps using the timestamp sending module, and send the synchronization timestamps to the timestamp receiving module; The time deviation determination module is used to receive the synchronization timestamp using the timestamp receiving module, calculate the deviation between the synchronization timestamp and the host system time, and update the time compensation configuration file; Time synchronization module: used to preload the time compensation dynamic link library when the container is started. The application in the container calls the API interface provided by the time compensation dynamic link library. The time compensation dynamic link library corrects the container system time according to the time deviation in the time compensation configuration file.
9. A computer device comprising a memory, a processor and a computer program stored in the memory and executable on the processor, characterized in that: When the processor executes the computer program, the steps of the method according to any one of claims 1 to 7 are implemented.
10. A computer storable medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 7 are implemented.