如何在 kubernetes 上以非 root 身份挂载卷
how to mount volumes as non-root on kubernetes
我正在尝试创建一个 运行s zookeper 的状态集,但我希望它 运行 作为非根用户(即在 zookeper 用户下)。
这是用于此的图像:
https://github.com/kubernetes-retired/contrib/blob/master/statefulsets/zookeeper/Dockerfile
这就是我尝试挂载卷的方式(根据 this 显然我需要一个初始化容器):
initContainers:
# Changes username for volumes
- name: changes-username
image: busybox
command:
- /bin/sh
- -c
- |
chown -R 1000:1000 /var/lib/zookeeper /etc/zookeeper-conf # <<<<--- this returns
# cannot change ownership, permission denied
# read-only filesystem.
containers:
- name: zookeeper
imagePullPolicy: IfNotPresent
image: my-repo/k8szk:3.4.14
command:
- sh
- -c
- |
zkGenConfig.sh # The script right here requires /var/lib/zookeper to be owned by zookeper user.
# Initially zookeeper user does own it as per the dockerfile above,
# but when mounting the volume, the directory becomes owned by root
# hence the script fails.
volumeMounts:
- name: zk-data
mountPath: /var/lib/zookeeper
- name: zk-log4j-config
mountPath: /etc/zookeeper-conf
我还尝试添加 securityContext:fsGroup: 1000
没有任何变化。
这可以使用 security context 进行配置,例如
securityContext:
runAsUser: 1000
runAsGroup: 1000
fsGroup: 1000
那么您根本不需要 initContainer - Kubernetes 将处理递归 chown 作为使卷准备就绪的一部分。
这里的一个问题是链接的 Dockerfile 不包含 USER
语句,因此 Kubernetes 不知道以正确的用户身份启动 pod - runAsUser
将解决这个问题。
您尝试的 initContainer
hack 不起作用的原因是您还试图更改 read-only 配置目录的所有权。 ConfigMaps 已挂载 read-only,您不能对它们进行 chown。 (这曾经是不同的,但出于安全原因进行了更改)
我正在尝试创建一个 运行s zookeper 的状态集,但我希望它 运行 作为非根用户(即在 zookeper 用户下)。
这是用于此的图像:
https://github.com/kubernetes-retired/contrib/blob/master/statefulsets/zookeeper/Dockerfile
这就是我尝试挂载卷的方式(根据 this 显然我需要一个初始化容器):
initContainers:
# Changes username for volumes
- name: changes-username
image: busybox
command:
- /bin/sh
- -c
- |
chown -R 1000:1000 /var/lib/zookeeper /etc/zookeeper-conf # <<<<--- this returns
# cannot change ownership, permission denied
# read-only filesystem.
containers:
- name: zookeeper
imagePullPolicy: IfNotPresent
image: my-repo/k8szk:3.4.14
command:
- sh
- -c
- |
zkGenConfig.sh # The script right here requires /var/lib/zookeper to be owned by zookeper user.
# Initially zookeeper user does own it as per the dockerfile above,
# but when mounting the volume, the directory becomes owned by root
# hence the script fails.
volumeMounts:
- name: zk-data
mountPath: /var/lib/zookeeper
- name: zk-log4j-config
mountPath: /etc/zookeeper-conf
我还尝试添加 securityContext:fsGroup: 1000
没有任何变化。
这可以使用 security context 进行配置,例如
securityContext:
runAsUser: 1000
runAsGroup: 1000
fsGroup: 1000
那么您根本不需要 initContainer - Kubernetes 将处理递归 chown 作为使卷准备就绪的一部分。
这里的一个问题是链接的 Dockerfile 不包含 USER
语句,因此 Kubernetes 不知道以正确的用户身份启动 pod - runAsUser
将解决这个问题。
您尝试的 initContainer
hack 不起作用的原因是您还试图更改 read-only 配置目录的所有权。 ConfigMaps 已挂载 read-only,您不能对它们进行 chown。 (这曾经是不同的,但出于安全原因进行了更改)