目录
本机替换上海时间
一、通过定制Dockerfile添加时区
二、通过将时区文件挂在到Pod中
三、通过环境变量定义时区
四、修改k8s表空间时区
本机替换上海时间
sudo timedatectl set-timezone Asia/Shanghai && sudo ntpdate time.windows.com
一、通过定制Dockerfile添加时区
$ cat Dockerfile.date
FROM centos
RUN rm -f /etc/localtime \
&& ln -sv /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& echo "Asia/Shanghai" > /etc/timezone
# 构建容器镜像
$ docker build -t centos7-date:test -f Dockerfile.date .
$ docker run -it centos7-date:test /bin/sh
sh-4.2# date
Wed Mar 6 17:40:01 CST 2019
二、通过将时区文件挂在到Pod中
cat time-mount.yaml
apiVersion: v1
kind: Pod
metadata:
name: time
spec:
containers:
- name: time
image: nginx
args: [/bin/sh, -c,
'i=0; while true; do echo "$i: $(date)"; i=$((i+1)); sleep 1; done']
volumeMounts:
- name: timezone
mountPath: /etc/localtime
volumes:
- name: timezone
hostPath:
path: /usr/share/zoneinfo/Asia/Shanghai
三、通过环境变量定义时区
cat time.yaml
apiVersion: v1
kind: Pod
metadata:
name: time-nginx
spec:
containers:
- name: time-nginx
image: nginx
args: [/bin/sh, -c,
'i=0; while true; do echo "$i: $(date)"; i=$((i+1)); sleep 1; done']
env:
- name: TZ
value: Asia/Shanghai
四、修改k8s表空间时区
#官方提供的helm部署方式
helm repo add k8tz https://k8tz.github.io/k8tz/
helm install k8tz k8tz/k8tz --set timezone=Asia/Shanghai
kubectl create ns test
kubectl annotate ns test k8tz.io/strategy=hostPath
kubectl annotate ns test k8tz.io/timezone=Asia/Shanghai
标签:k8tz,name,宿主机,Asia,date,Pod,K8s,Shanghai,时区
From: https://blog.51cto.com/dxbp/7638066