如何在Kubernetes上以非root身份挂载卷

问题描述

我正在尝试创建一个运行zookeper的有状态集,但我希望它以非root用户身份运行(即在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无效的原因是,您还试图更改只读config目录的所有权。 ConfigMap是只读安装的,您无法对其进行处理。 (以前是不同的,但是出于安全原因而更改了

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...