tencent cloud

Tencent Kubernetes Engine

Release Notes and Announcements
Release Notes
Announcements
Release Notes
Product Introduction
Overview
Strengths
Architecture
Scenarios
Features
Concepts
Native Kubernetes Terms
Common High-Risk Operations
Regions and Availability Zones
Service Regions and Service Providers
Open Source Components
Purchase Guide
Purchase Instructions
Purchase a TKE General Cluster
Purchasing Native Nodes
Purchasing a Super Node
Getting Started
Beginner’s Guide
Quickly Creating a Standard Cluster
Examples
Container Application Deployment Check List
Cluster Configuration
General Cluster Overview
Cluster Management
Network Management
Storage Management
Node Management
GPU Resource Management
Remote Terminals
Application Configuration
Workload Management
Service and Configuration Management
Component and Application Management
Auto Scaling
Container Login Methods
Observability Configuration
Ops Observability
Cost Insights and Optimization
Scheduler Configuration
Scheduling Component Overview
Resource Utilization Optimization Scheduling
Business Priority Assurance Scheduling
QoS Awareness Scheduling
Security and Stability
TKE Security Group Settings
Identity Authentication and Authorization
Application Security
Multi-cluster Management
Planned Upgrade
Backup Center
Cloud Native Service Guide
Cloud Service for etcd
TMP
TKE Serverless Cluster Guide
TKE Registered Cluster Guide
Use Cases
Cluster
Serverless Cluster
Scheduling
Security
Service Deployment
Network
Release
Logs
Monitoring
OPS
Terraform
DevOps
Auto Scaling
Containerization
Microservice
Cost Management
Hybrid Cloud
AI
Troubleshooting
Disk Full
High Workload
Memory Fragmentation
Cluster DNS Troubleshooting
Cluster kube-proxy Troubleshooting
Cluster API Server Inaccessibility Troubleshooting
Service and Ingress Inaccessibility Troubleshooting
Common Service & Ingress Errors and Solutions
Engel Ingres appears in Connechtin Reverside
CLB Ingress Creation Error
Troubleshooting for Pod Network Inaccessibility
Pod Status Exception and Handling
Authorizing Tencent Cloud OPS Team for Troubleshooting
CLB Loopback
API Documentation
History
Introduction
API Category
Making API Requests
Elastic Cluster APIs
Resource Reserved Coupon APIs
Cluster APIs
Third-party Node APIs
Relevant APIs for Addon
Network APIs
Node APIs
Node Pool APIs
TKE Edge Cluster APIs
Cloud Native Monitoring APIs
Scaling group APIs
Super Node APIs
Other APIs
Data Types
Error Codes
TKE API 2022-05-01
FAQs
TKE General Cluster
TKE Serverless Cluster
About OPS
Hidden Danger Handling
About Services
Image Repositories
About Remote Terminals
Event FAQs
Resource Management
Service Agreement
TKE Service Level Agreement
TKE Serverless Service Level Agreement
Contact Us
Glossary
DokumentasiTencent Kubernetes EngineUse CasesService DeploymentSolve the inconsistent time zone problem in the container

Solve the inconsistent time zone problem in the container

PDF
Mode fokus
Ukuran font
Terakhir diperbarui: 2024-12-19 21:49:45

Introduction

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.

Operation Environment

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

Cause Locating

1. Log in to the target node by referring to Log in to Linux Instance Using Standard Login Method (Recommended).
2. Run the following command to query the local time:
date
The following information appears:


3. Run the following commands in sequence to query the default time zone of CentOS in the container:
docker run -it centos /bin/sh
date
The following information appears:

By comparison, it is clear that the local time zone and the time zone in the container are inconsistent.
4. Run the following command to exit the container:
exit

Directions

Solution 1: create a time zone file in Dockerfile (recommended)

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.
1. Run the following command to create the Dockerfile.txt file:
vim Dockerfile.txt
2. Press i to switch to the editing mode, and write the following information to configure the time zone file.
FROM centos
RUN rm -f /etc/localtime \\
&& ln -sv /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \\
&& echo "Asia/Shanghai" > /etc/timezone
3. Press Esc, enter :wq, and save and close the file.
4. Run the following command to create a container image:
docker build -t centos7-test:v1 -f Dockerfile.txt .
The following information appears:


5. Run the following commands in sequence to launch the container image and query the time zone in the container:
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:


6. Run the following command to exit the container:
exit

Solution 2: mount the time zone configuration of the CVM to the container

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.

Mounting CVM time configuration to the container when the container is started

When mounting the CVM time configuration to the container to overwrite the original configuration, there are two options:
Mount local /etc/localtime: you need to ensure that the CVM time zone configuration file exists and the time zone is correct.
Mount local /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;
1.1 Run the following commands in sequence to query the local time and mount the local /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:


1.2 Run the following command to exit the container:
exit
Method 2: mount local /usr/share/zoneinfo/Asia/Shanghai:
1.1 Run the following commands in sequence to query the local time and mount local /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:


1.2 Run the following command to exit the container:
exit

Using data volumes in the YAML file to mount the CVM time zone configuration to the container

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.
1. Run the following command on the node to create the pod.yaml file:
vim pod.yaml
2. Press i to switch to the editing mode and enter the following.
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
3. Press Esc, enter :wq, and save and close the file.
4. Run the following command to create a pod:
kubectl create -f pod.yaml
The following information appears:


5. Run the following commands in sequence to query the time zone in the container:
date
kubectl exec -it test date
If the following information appears, the time zone is consistent with the local system time zone.




Bantuan dan Dukungan

Apakah halaman ini membantu?

masukan