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













 
                    

 
                 
                    