In time-sensitive applications, unsynchronized container clocks can lead to issues such as data inconsistencies in distributed systems or misleading timestamps in logs. To prevent these problems, you can configure the Network Time Protocol (NTP) service for your Elastic Container Instance pods. This ensures accurate time synchronization within your containers, maintaining data integrity and reliable application behavior.
Configuration description
When you create an Elastic Container Instance pod, you can add the k8s.aliyun.com/eci-ntp-server
annotation to the pod to specify the address of the NTP server. This ensures that the containers within the pod synchronize their time with the specified NTP server, maintaining time accuracy.
Annotations must be added to the metadata in the configuration file of the pod. For example, when you create a Deployment, you must add annotations in the spec.template.metadata section.
Elastic Container Instance-related annotations are only applied when a pod is created. Adding or modifying these annotations on an existing pod will have no effect.
Configuration example
Create an application with the NTP service configuration.
kubectl create -f set-ntp.yaml
The following code provides the content of a sample set-ntp.yaml file:
apiVersion: apps/v1 kind: Deployment metadata: name: test-ntp labels: app: test spec: replicas: 1 selector: matchLabels: app: nginx template: metadata: name: test-ntp labels: app: nginx alibabacloud.com/eci: "true" annotations: k8s.aliyun.com/eci-ntp-server: 100.100.XX.XX # Specifies the IP address of the NTP server. spec: containers: - name: nginx image: registry.cn-shanghai.aliyuncs.com/eci_open/centos:7 ports: - containerPort: 80 command: ["/bin/sh","-c","sleep 3600" ]
Log on to the container and verify the NTP configuration.
Query the information about the pod.
kubectl get pod
The following command output is returned:
NAME READY STATUS RESTARTS AGE test-ntp-599d5ff9f5-9kb56 1/1 Running 0 3m59s
Access the container.
kubectl exec -it test-ntp-599d5ff9f5-9kb56 -- bash
Check the container's time synchronization source.
chronyc sources
NoteIf chrony is not installed, you can run the
yum -y install chrony
command to install chrony.The command output should list the IP address of your NTP server, which confirms that the service is configured correctly. The following is a sample output.
210 Number of sources = 1 MS Name/IP address Stratum Poll Reach LastRx Last sample =============================================================================== ^* 100.100.XX.XX 2 6 377 35 +40us[ +135us] +/- 14ms