StatefulSet 使用 bind mount / hostPath 挂载宿主机时区文件 的 YAML 示例。
Kubernetes 里对应的就是 hostPath volume:它会把宿主节点上的文件或目录挂进 Pod;只读挂载应在 volumeMounts 里设置 readOnly: true
apiVersion: v1
kind: Service
metadata:
name: app-headless
namespace: default
spec:
clusterIP: None
selector:
app: app
ports:
- name: http
port: 80
targetPort: 80
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: app
namespace: default
spec:
serviceName: app-headless
replicas: 1
selector:
matchLabels:
app: app
template:
metadata:
labels:
app: app
spec:
containers:
- name: app
image: your-image:tag
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 80
volumeMounts:
# 将宿主机 /etc/localtime bind mount 到容器内
# 保证容器使用宿主机时区规则,包括夏令时切换规则
- name: host-localtime
mountPath: /etc/localtime
readOnly: true
volumes:
- name: host-localtime
hostPath:
path: /etc/localtime
type: File
如果你的应用还会读取 /etc/timezone,可以加上这个版本:
apiVersion: v1
kind: Service
metadata:
name: app-headless
namespace: default
spec:
clusterIP: None
selector:
app: app
ports:
- name: http
port: 80
targetPort: 80
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: app
namespace: default
spec:
serviceName: app-headless
replicas: 1
selector:
matchLabels:
app: app
template:
metadata:
labels:
app: app
spec:
containers:
- name: app
image: your-image:tag
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 80
volumeMounts:
- name: host-localtime
mountPath: /etc/localtime
readOnly: true
- name: host-timezone
mountPath: /etc/timezone
readOnly: true
volumes:
- name: host-localtime
hostPath:
path: /etc/localtime
type: File
- name: host-timezone
hostPath:
path: /etc/timezone
type: File
如果镜像里没有完整的 tzdata,或者应用会按时区名读取 /usr/share/zoneinfo,建议再挂载宿主机的时区库目录:
apiVersion: v1
kind: Service
metadata:
name: app-headless
namespace: default
spec:
clusterIP: None
selector:
app: app
ports:
- name: http
port: 80
targetPort: 80
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: app
namespace: default
spec:
serviceName: app-headless
replicas: 1
selector:
matchLabels:
app: app
template:
metadata:
labels:
app: app
spec:
containers:
- name: app
image: your-image:tag
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 80
volumeMounts:
- name: host-localtime
mountPath: /etc/localtime
readOnly: true
- name: host-zoneinfo
mountPath: /usr/share/zoneinfo
readOnly: true
volumes:
- name: host-localtime
hostPath:
path: /etc/localtime
type: File
- name: host-zoneinfo
hostPath:
path: /usr/share/zoneinfo
type: Directory

596

被折叠的 条评论
为什么被折叠?



