The default system time of containers in TKE clusters is Universal Time Coordinated (UTC), which may be different with the local time zone of your nodes. During the use of containers, time zone inconsistency in containers will cause trouble when the system time is used for operations, such as log records and database storage. In this document, we will use "Asia/Shanghai" as the local time zone.
You cannot modify the default time of the cluster but the container. This document provides multiple solutions to time zone inconsistencies in containers. You can choose the solution that works for you.
All operations described in this document are completed on TKE cluster nodes. The relevant operation environment is shown below. Please use this document to solve problems based on your actual situation.
Role | Region | Specifications | OS | Kubernetes Version |
---|---|---|---|---|
Node | South China (Guangzhou) | CPU: 1 core, memory: 1 GB, bandwidth: 1 Mbps System disk: 50 GB (HDD cloud disk) |
CentOS Linux 7 (Core) | 1.16.3 |
date
The following information appears:docker run -it centos /bin/sh
date
The following information appears:exit
When creating a basic image or customizing an image based on a basic image, you can create a time zone file in Dockerfile to solve time zone inconsistency within a container. After this, you will no longer be troubled by time zone issues when using the image.
vim Dockerfile.txt
FROM centos
RUN rm -f /etc/localtime \
&& ln -sv /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& echo "Asia/Shanghai" > /etc/timezone
docker build -t centos7-test:v1 -f Dockerfile.txt .
The following information appears:date
docker run -it centos7-test:v1 /bin/sh
date
The time zone in the container is consistent with the local time. See the figure below:exit
You can also solve time zone inconsistency in a container by mounting the time configuration of the CVM to the container. This solution can be set when the container is started, or you can use the CVM path in the YAML file to mount volumes to the container.
When mounting the CVM time configuration to the container to overwrite the original configuration, there are two options:
/etc/localtime
: you need to ensure that the CVM time zone configuration file exists and the time zone is correct./usr/share/zoneinfo/Asia/Shanghai
: when the local /etc/localtime
does not exist or the time zone is incorrect, you can directly mount the configuration file.Choose one of the following methods based on your situation to mount the CVM time configuration to the container:
Method 1: mount local /etc/localtime
;
/etc/localtime
into the container:date
docker run -it -v /etc/localtime:/etc/localtime centos /bin/sh
date
If the following information appears, the time zone in the container is consistent with the local time:exit
Method 2: mount local /usr/share/zoneinfo/Asia/Shanghai
:
/usr/share/zoneinfo/Asia/Shanghai
into the container:date
docker run -it -v /usr/share/zoneinfo/Asia/Shanghai:/etc/localtime centos /bin/sh
date
If the following information appears, the time zone in the container is consistent with the local time:exit
This section uses mountPath:/etc/localtime
as an example to illustrate how to mount the CVM time zone configuration to the container using volumes in the YAML file. This will solve time zone inconsistency in the container.
vim pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: test
namespace: default
spec:
restartPolicy: OnFailure
containers:
- name: nginx
image: nginx-test
imagePullPolicy: IfNotPresent
volumeMounts:
- name: date-config
mountPath: /etc/localtime
command: ["sleep", "60000"]
volumes:
- name: date-config
hostPath:
path: /etc/localtime
kubectl create -f pod.yaml
The following information appears:date
kubectl exec -it test date
If the following information appears, the time zone is consistent with the local system time zone.
Was this page helpful?