Kubernetes容器时区同步:挂载主机/etc/localtime最佳实践
场景痛点:容器默认使用UTC时区(与中国标准时间CST相差8小时),导致日志、计划任务等时间显示错误。
完整解决方案
通过hostPath
将宿主机的时区文件挂载到容器内,强制容器使用节点服务器时区:
apiVersion: apps/v1
kind: Deployment
metadata:name: nginx-proxy-deploymentlabels:app: nginx
spec:replicas: 2selector:matchLabels:app: nginxtemplate:metadata:labels:app: nginxspec:volumes: # 挂载Node节点文件- name: timezonehostPath:path: /etc/localtimetype: ""containers:- name: nginximage: nginx:alpine volumeMounts:- name: timezonemountPath: /etc/localtime #替换容器文件