All Products
Search
Document Center

Container Service for Kubernetes:Specify time zones for pods

Last Updated:Jan 15, 2024

This topic describes how to specify different time zones for pods deployed on a virtual node.

Procedure

  1. Create a ConfigMap and specify a time zone in the ConfigMap.

    To specify a time zone, you need to import a ConfigMap from the /usr/share/zoneinfo/Asia/ directory. Example:

    kubectl create configmap tz --from-file=/usr/share/zoneinfo/Asia/Shanghai
  2. Create a YAML file to configure a time zone.

    vim set-timezone.yaml

    Mount the ConfigMap to the /etc/localtime/Shanghai directory. The following code block shows the content of a sample YAML file:

    apiVersion: v1
    kind: Pod
    metadata:
      name: timezone
    spec:
      containers:
      - name: timezone
        image: registry-vpc.cn-beijing.aliyuncs.com/eci_open/busybox:1.30
        command: [ "sleep", "10000" ]
        volumeMounts:
          - name: tz
            mountPath: /etc/localtime
            subPath: Shanghai
      volumes:
        - name: tz
          configMap:
            name: tz
      nodeSelector:
        type: virtual-kubelet
      tolerations:
      - key: virtual-kubelet.io/provider
        operator: Exists
  3. Deploy a pod with the YAML file that you created.

    kubectl apply -f set-timezone.yaml

Verify the configuration results

Log on to the container and check whether the time zone of the container is specified.

  1. Query information about the pod.

    kubectl get pod/timezone

    Expected output:

    NAME         READY   STATUS    RESTARTS   AGE
    timezone     1/1     Running   0          7m20s
  2. Access the container.

    kubectl exec timezone -it -- sh
  3. Query the time zone of the container.

    date -R

    If the time returned is consistent with the time zone that you specified, the configuration is in effect. Expected output:

    Fri, 01 May 2020 10:00:11 +0800

References