zone k8s时区设置 docker 时区设置

Kubernetes 版本:v1.17.4

集群以kubeadm方式安装

docker版本 19.03.1

#k8s 修改时区
##第一种是把主机的时区文件挂载到pod内
vi time-pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: nginx-tz
  namespace: default
  labels: 
    app: my-pod
     
spec:
  containers:
  - name: my-tz
    image: harbor.mvmyun.com/dev/nginx
    ports:
    - containerPort: 80
    volumeMounts:
      - name: timezone
        mountPath: /etc/localtime
        readOnly: true
  imagePullSecrets:
  - name: regcred
  volumes:
    - name: timezone
      hostPath: 
        path: /usr/share/zoneinfo/Asia/Shanghai

#第二种是使用PodPreset来预设
vim /etc/kubernetes/manifests/kube-apiserver.yaml
- --enable-admission-plugins=NodeRestriction,DefaultStorageClass,PodPreset
- --runtime-config=settings.k8s.io/v1alpha1=true

vim podpreset.yaml
apiVersion: settings.k8s.io/v1alpha1
kind: PodPreset
metadata:
  name: tz-env
spec:
  selector:
    matchLabels: #matchLabels 为空 表示应用所有容器
  env:
  - name: TZ
    value: Asia/Shanghai
#查看已创建的podpresets
kubectl get podpresets
####注意: PodPreset是针对ns的 不是全局!

#在dockerfile中修改
Alpine
RUN apk --no-cache add tzdata  && \
    ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
    echo "Asia/Shanghai" > /etc/timezone 
--no-cache参数不缓存文件,有助于减少最终体积。

Ubuntu
RUN echo "Asia/Shanghai" > /etc/timezone && \
    dpkg-reconfigure -f noninteractive tzdata

CentOS
RUN echo "Asia/shanghai" > /etc/timezone;