Help Center> Cloud Container Engine> Best Practices> Container> Setting Time Zone Synchronization
Updated on 2024-05-31 GMT+08:00

Setting Time Zone Synchronization

Scenario 1: Setting Time Zone Synchronization Between Containers and Nodes

  1. Log in to the CCE console.
  2. In the Basic Info area of the Create Workload page, enable Time Zone Synchronization so that the same time zone will be used for both the container and the node.

    Figure 1 Enabling the time zone synchronization

  3. Log in to the node, go to the container, and check whether the time zone of the container is the same as that of the node.

    date -R

    Information similar to the following is displayed:

    Tue, 04 Jun 2019 15::08:47  +0800

    docker ps -a|grep test

    Information similar to the following is displayed:

    docker exec -it oedd74c66bdb /bin/sh

    date -R

    Information similar to the following is displayed:

    Tue, 04 Jun 2019 15:09:20  +0800

Scenario 2: Setting Time Zone Synchronization Among Containers, Container Logs, and Nodes

The difference between the time when the Java application prints logs and the container's standard time obtained in date -R mode is 8 hours.

  1. Log in to the CCE console.
  2. In the Basic Info area of the Create Workload page, enable Time Zone Synchronization so that the same time zone will be used for both the container and the node.

    Figure 2 Enabling the time zone synchronization

  3. Log in to the node, go to the container, and modify the catalina.sh script.

    cd /usr/local/tomcat/bin

    vi catalina.sh

    If you cannot run the vi command in the container, go to 4 or run the vi command to add -Duser.timezone=GMT+08 to the script, as shown in the following figure.

  4. Copy the script from the container to the node, add -Duser.timezone=GMT+08 to the script, and then copy the script from the node to the container.

    Run the following command to copy files in the container to the host machine:

    docker cp mycontainer: /usr/local/tomcat/bin/catalina.sh /home/catalina.sh

    Run the following command to copy files from the host machine to the container:

    docker cp /home/catalina.sh mycontainer:/ usr/local/tomcat/bin/catalina.sh

  5. Restart the container.

    docker restart container_id

  6. Check whether the time zone of the logs is the same as that of the node.

    On the CCE console, click the workload name. On the workload details page displayed, click Logs in the upper right corner to view the log details. It takes about 5 minutes to load the logs.

Scenario 3: Setting Time Zone Synchronization Between Workloads and Nodes

  • Method 1: Set the time zone to CST when creating a container image.
  • Method 2: If you do not want to modify the container, when creating a workload on the CCE console, mount the /etc/localtime directory of the local host to the /etc/localtime directory of the container.

    Example:

    kind: Deployment
    apiVersion: apps/v1
    metadata:
      name: test
      namespace: default
    spec:
      replicas: 2
      selector:
        matchLabels:
          app: test
      template:
        metadata:
          labels:
            app: test
        spec:
          volumes:
            - name: vol-162979628557461404
              hostPath:
                path: /etc/localtime
                type: ''
          containers:
            - name: container-0
              image: 'nginx:alpine'
              volumeMounts:
                - name: vol-162979628557461404
                  readOnly: true
                  mountPath: /etc/localtime
              imagePullPolicy: IfNotPresent
          imagePullSecrets:
            - name: default-secret