docker - 如何在 kubernetes 上以非 root 身份挂载卷
问题描述
我正在尝试创建一个运行 zookeper 的 statefulset,但我希望它以非 root 身份运行(即在 zookeper 用户下)。
这是用于此的图像:
https://github.com/kubernetes-retired/contrib/blob/master/statefulsets/zookeeper/Dockerfile
这就是我尝试挂载卷的方式(显然我需要一个 init 容器):
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
没有任何变化。
解决方案
这可以使用安全上下文进行配置,例如
securityContext:
runAsUser: 1000
runAsGroup: 1000
fsGroup: 1000
然后你根本不需要 initContainer - Kubernetes 将处理递归 chown 作为准备卷的一部分。
这里的一个问题是 Dockerfile 链接不包含USER
语句,因此 Kubernetes 不知道以正确的用户身份启动 pod -runAsUser
将解决这个问题。
您尝试的initContainer
hack 不起作用的原因是您还尝试更改只读配置目录的所有权。ConfigMap 以只读方式挂载,您不能对其进行更改。(这曾经是不同的,但出于安全原因进行了更改)
推荐阅读
- reactjs - 如何反应在箭头函数中使用这个关键字
- typescript - TypeScript 中的函数名称类型
- assembly - 如何将Assembly(irvine masm)中的字符串重新初始化为null?
- mongodb - 为什么我不能在不同的 mongodb 集合中有类似的索引(索引)?
- visual-studio - 附加 Visual Studio 调试器时,Windows 窗体应用程序响应非常缓慢
- python - 如何使用训练数据的标签指定分类列的标签?
- javascript - 如何每 N 页而不是每次都从服务器请求数据?
- r - Calculating non-pairwise correlation between different sets of columns of a dataset
- firebase - Flutter:是否可以使用 FCM 和云功能离线发送推送通知?
- java - 使用类而不是属性文件或 ListResourceBundles 时 ResourceBundle.getBundle() 的强制语言