All Products
Search
Document Center

Elastic Container Instance:Configure the NTP service

最終更新日:Mar 06, 2024

For some time-sensitive business scenarios, inaccurate time synchronization of containers may affect the normal operation of the business. For example, if the time of containers in a distributed system is inconsistent, the data may be inconsistent. If the log timestamp is inaccurate when you debug programs based on logs, the logs may be sorted in disorder and you may not correctly identify the error cause. When you deploy a containerized application in time-sensitive business scenarios, you can configure the Network Time Protocol (NTP) service for an Elastic Container Instance-based pod to ensure accurate time synchronization of containers in the pod. This resolves the problem caused by inaccurate time and ensures data accuracy and normal business operation.

Configuration description

When you create an Elastic Container Instance-based pod, you can add the k8s.aliyun.com/eci-ntp-server annotation to the pod to specify the address of the NTP server. This way, the time of the containers in the pod can be synchronized with the time of the NTP service. This can ensure the accuracy of the time of the containers in the pod.

Important
  • 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.

  • To use features of Elastic Container Instance, you can add annotations only when you create Elastic Container Instance-based pods. If you add or modify annotations when you update pods, these annotations do not take effect.

Configuration example

  1. Create an application that is used to configure the NTP service.

    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" ]
  2. Log on to the container and check whether the NTP service works as expected.

    1. 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
    2. Access the container.

      kubectl exec -it test-ntp-599d5ff9f5-9kb56 -- bash
    3. Query the source from which the time of the container is synchronized.

      chronyc sources
      Note

      If chrony is not installed, you can run the yum -y install chrony command to install chrony.

      If the IP address of the NTP server is returned, the NTP service is configured for the pod. The following code shows 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