https://kubernetes.io/docs/tasks/configure-pod-container/attach-handler-lifecycle-event/
有時候,在容器啓動時我們需要做一些預備工作。
舉個例子:
現啓動了一個nginx pod, 且掛載了 rbd 作爲web根目錄存儲。單RBD默認是以 root 掛載的(迄今爲止rbd還沒有掛載指定user的配置),這就需要我們在容器初始化時爲目錄賦予正確的權限。
可以定義如下配置:
apiVersion: v1
kind: Pod
metadata:
name: aaa
spec:
containers:
- image: nginx:latest
imagePullPolicy: IfNotPresent
name: nginx
volumeMounts:
- name: aaa
mountPath: /usr/share/nginx/html
lifecycle:
postStart:
exec:
command: ["/bin/sh", "-c", "chown -R nginx:nginx /usr/share/nginx/html"]
volumes:
- name: aaa
persistentVolumeClaim:
claimName: aaa
進入容器查看:
[root@walker-1 aaaaa]# kubectl exec -it aaa sh
# ls -l /usr/share/nginx
total 4
drwxr-xr-x 3 nginx nginx 4096 Nov 9 05:57 html
# mount | grep nginx
/dev/rbd1 on /usr/share/nginx/html type ext4 (rw,relatime,stripe=1024,data=ordered)
此外還有一種pod初始化方式,通過 init-container
https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-initialization/
官方給出了示例。另外在 mysql statefulset scaling 中也用到了ini-container
來進行db初始化。