kubernetes pod 初始化

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初始化。

你可能感兴趣的:(kubernetes)