修改时区为上海时区的四种办法
仅适用于操作系统为CentOS的pod
目录
1.在DockerFile中添加时区环境变量
在dockerFile中添加一行:
ENV TZ Asia/Shanghai
例子:
cat > Dockerfile<<EOF
FROM centos:7
ENV TZ Asia/Shanghai
CMD ["yes"]
EOF
2.利用initContainers添加时区环境变量
利用initContainers,往pod的/etc/profile文件添加时区环境变量
command: ['sh', '-c', 'echo "export TZ=Asia/Shanghai" >> /etc/profile']
3.在container中添加时区环境变量
在pod的yaml中添加container的时区环境变量
env:
- name: TZ
value: "Asia/Shanghai"
4.给pod挂载主机的时区配置文件
将主机的/usr/share/zoneinfo/Asia/Shanghai文件挂载为pod的/etc/localtime文件
第2,3,4方法的例子:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
spec:
selector:
matchLabels:
app: web_server
replicas: 1
template:
metadata:
labels:
app: web_server
spec:
hostNetwork: true
initContainers:
- name: modify-timezone
image: fire.harbor.com/system/busybox:1.32
command: ['sh', '-c', 'echo "export TZ=Asia/Shanghai" >> /etc/profile']
containers:
- name: nginx
image: fire.harbor.com/soft/nginx:1.19.4
env:
- name: TZ
value: "Asia/Shanghai"
ports:
- containerPort: 12345
resources:
requests:
memory: 50Mi
cpu: 50m
limits:
memory: 1024Mi
cpu: 1024m
volumeMounts:
- mountPath: /etc/localtime
name: localtime
volumes:
- name: localtime
hostPath:
path: /usr/share/zoneinfo/Asia/Shanghai