Minikube 卷写入权限?

mha*_*ski 1 permissions kubernetes persistent-volumes persistent-volume-claims minikube

总体情况是:我正在尝试在 Kubernetes 中安装带有插件的 WordPress,以便在 Minikube 中进行开发。

我想使用官方的 wp-cli Docker 镜像来安装插件。我正在尝试使用可写的持久卷。在 Minikube 中,我使用命令打开对 minikube 集群的挂载:

minikube mount ./src/plugins:/data/plugins
Run Code Online (Sandbox Code Playgroud)

现在,PV 定义如下所示:

---
apiVersion: v1
kind: PersistentVolume
metadata:
    name: wordpress-install-plugins-pv
    labels:
        app: wordpress
        env: dev
spec:
    capacity:
        storage: 5Gi
    storageClassName: ""
    volumeMode: Filesystem
    accessModes:
        - ReadWriteOnce
    hostPath:
        path: /data/plugins
Run Code Online (Sandbox Code Playgroud)

PVC 看起来像这样:

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
    name: wordpress-install-plugins-pvc
    labels:
        app: wordpress
spec:
    accessModes:
        - ReadWriteOnce    
    resources:
        requests:
            storage: 5Gi
    storageClassName: ""
    volumeName: wordpress-install-plugins-pv
Run Code Online (Sandbox Code Playgroud)

创建和绑定均成功。插件安装的作业定义如下所示:

---
apiVersion: batch/v1
kind: Job
metadata:
    name: install-plugins
    labels:
        env: dev
        app: wordpress
spec:
    template:
        spec:
            securityContext:
              fsGroup: 82 # www-data
            volumes:
                - name: plugins-volume
                  persistentVolumeClaim:
                       claimName: wordpress-install-plugins-pvc                
                - name: config-volume
                  configMap:
                       name: wordpress-plugins
            containers:
            - name: wpcli
              image: wordpress:cli
              volumeMounts:
              - mountPath: "/configmap"
                name: config-volume
              - mountPath: "/var/www/html/wp-content/plugins"
                name: plugins-volume
              command: ["sh", "-c", "id; \
                                     touch /var/www/html/wp-content/plugins/test; \
                                     ls -al /var/www/html/wp-content; \
                                     wp core download --skip-content --force && \
                                     wp config create --dbhost=mysql \
                                                      --dbname=$MYSQL_DATABASE \
                                                      --dbuser=$MYSQL_USER \
                                                      --dbpass=$MYSQL_PASSWORD && \
                                     cat /configmap/wp-plugins.txt | xargs -I % wp plugin install % --activate" ]
              env:
                  - name: MYSQL_USER
                    valueFrom:
                        secretKeyRef:
                            name: mysql-secrets
                            key: username
                  - name: MYSQL_PASSWORD
                    valueFrom:
                        secretKeyRef:
                            name: mysql-secrets
                            key: password
                  - name: MYSQL_DATABASE
                    valueFrom:
                        secretKeyRef:
                            name: mysql-secrets
                            key: dbname
            restartPolicy: Never
    backoffLimit: 3
Run Code Online (Sandbox Code Playgroud)

同样,创作看起来不错,所有步骤看起来都很好。我遇到的问题是,显然已安装卷的权限不允许当前用户写入该文件夹。这是日志内容:

uid=82(www-data) gid=82(www-data) groups=82(www-data)
touch: /var/www/html/wp-content/plugins/test: Permission denied
total 9
drwxr-xr-x    3 root     root          4096 Mar  1 20:15 .
drwxrwxrwx    3 www-data www-data      4096 Mar  1 20:15 ..
drwxr-xr-x    1 1000     1000            64 Mar  1 17:15 plugins
Downloading WordPress 5.3.2 (en_US)...
md5 hash verified: 380d41ad22c97bd4fc08b19a4eb97403
Success: WordPress downloaded.
Success: Generated 'wp-config.php' file.
Installing WooCommerce (3.9.2)
Downloading installation package from https://downloads.wordpress.org/plugin/woocommerce.3.9.2.zip...
Unpacking the package...
Warning: Could not create directory.
Warning: The 'woocommerce' plugin could not be found.
Error: No plugins installed.
Run Code Online (Sandbox Code Playgroud)

难道我做错了什么?我尝试了不同的minikube mount选择,但没有任何帮助!有人用 minikube 遇到过这个问题吗?

OhH*_*ark 7

这是一个长期问题,会阻止非 root 用户hostPath在 Minikube 中挂载 PersistentVolume 时写入容器。

有两种常见的解决方法:

  1. 只需使用 root 用户即可。

  2. 使用runAsUserrunAsGroup为 Pod 或容器配置安全上下文fsGroup。您可以在提供的链接中找到带有示例的详细信息。

请告诉我这是否有帮助。


mha*_*ski 5

我更深入地研究了 minikube 中卷安装的工作方式,我想我找到了解决方案。

长话短说

minikube mount ./src/plugins:/data/mnt/plugins --uid 82 --gid 82
Run Code Online (Sandbox Code Playgroud)

说明

有一些安装时刻:

  • minikube 挂载目录minikube mount
  • 正在 Kubernetes 中挂载卷

minikube mount使用提供的 UID 和 GID 作为参数在 VM 中设置目录,默认为docker用户和组。

当卷作为目录挂载到 Pod 中时,它会使用与主机完全相同的UID 和 GID 进行挂载!你可以在我的问题中看到这一点:

drwxr-xr-x    1 1000     1000            64 Mar  1 17:15 plugins
Run Code Online (Sandbox Code Playgroud)

UID=1000和GID=1000指的是dockerminikube主机中的UID和GID。这给了我一个想法,我应该尝试使用 Pod 中用户的 UID 和 GID 进行挂载。

www-data82 是 Docker 镜像中用户和组的 id wordpress:cli,它有效!

最后值得一提的是:卷作为子目录安装在 Pod 中(wp-content在我的例子中)。事实证明,wp-cli实际上还需要访问该目录才能创建临时文件夹。我最终做的是添加一个emptyDir卷,如下所示:

volumes
  - name: content
    emptyDir: {}
Run Code Online (Sandbox Code Playgroud)

我希望它对任何人都有帮助!就其价值而言,我的 minikube 版本是 1.7.3,在带有 VirtualBox 驱动程序的 OS X 上运行。