When creating a workload, you can enable time zone synchronization, so that the container can use the same time zone as the node.
Configure the following parameters:
To use time zone synchronization, you need to mount /etc/localtime on the node to /etc/localtime in the container. In this way, the node and container use the same time zone configuration file.
vi nginx-deployment.yaml
Example YAML:
kind: DeploymentapiVersion: apps/v1metadata:name: testnamespace: defaultspec:replicas: 2selector:matchLabels:app: testtemplate:metadata:labels:app: testspec:volumes:- name: date-config # Custom volume name.hostPath: # Mount the time zone configuration file of the node in hostPath mode.path: /etc/localtime # Path of the time zone configuration file on the node.containers:- name: container-0image: 'nginx:alpine'volumeMounts:- name: date-config # Mount the volume to the container. The volume name must be the same as that you defined.readOnly: true # Mount the volume as read-only.mountPath: /etc/localtime # The path in the container where the node's time zone configuration file is mounted.imagePullPolicy: IfNotPresentimagePullSecrets:- name: default-secret
Press Esc and enter :wq to save the file and exit.
kubectl create -f nginx-deployment.yaml
date -R
Information similar to the following is displayed:
Sat, 12 Apr 2025 16:58:47 +0800
Obtain the pod name.
kubectl get pod
Access the container. In the command, <pod_name> specifies the pod name obtained in the previous step.
kubectl exec -it <pod_name> -- /bin/bash
Obtain the time zone of the container.
date -R
Information similar to the following is displayed, indicating that the time zone of the container is the same as that of the node:
Sat, 12 Apr 2025 16:59:23 +0800