k8s 時區 docker 時區 中國時區

#k8s 修改時區

##第一種是把主機的時區文件掛載到pod內

<code>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/<code>


#第二種是使用PodPreset來預設

<code>vim /etc/kubernetes/manifests/kube-apiserver.yaml
- --enable-admission-plugins=NodeRestriction,DefaultStorageClass,PodPreset
- --runtime-config=settings.k8s.io/v1alpha1=true/<code>
<code>vim podpreset.yaml
apiVersion: settings.k8s.io/v1alpha1
kind: PodPreset
metadata:
  name: tz-env
spec:
  selector:
    matchLabels: #matchLabels 為空 表示應用所有容器
  env:
  - name: TZ
    value: Asia/Shanghai/<code>

#查看已創建的podpresets

kubectl get podpresets

####注意: PodPreset是針對ns的 不是全局!


#在dockerfile中修改

Alpine

<code>RUN apk --no-cache add tzdata  && \\
    ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \\
    echo "Asia/Shanghai" > /etc/timezone /<code>

--no-cache參數不緩存文件,有助於減少最終體積。


Ubuntu

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

CentOS

<code>RUN echo "Asia/shanghai" > /etc/timezone;/<code>


分享到:


相關文章: