LarryDpk
发布于 2021-05-01 / 1926 阅读
0

解决Kubernetes以root用户mount volumn导致无写权限的问题

1 问题描述

我们在使用StatefulSet来使用PVC或直接使用PVC时,发现运行的时候无法写所mountvolume,查看日志是没有足够的权限,只能读不能写。

2 问题分析

首先,因为安全问题,我们所运行的容器是不可以为root用户,否则会被kill掉。通过ls命令查看,所mount的目录却是属于root用户的,而且其它用户并没有可写权限。

因此,解决方案就简单了,不通过root用户来mount就行了。

3 解决方案

可以通过配置PodsecurityContext解决:

apiVersion: v1
kind: Pod
metadata:
  name: pkslow-app
spec:
  containers:
  # specification of the pod's containers
  # ...
  securityContext:
    fsGroup: 319

这个319是所运行的非root用户的,可以通过命令id查看。

$ id
uid=319(larry) gid=319(staff) groups=319(staff)

要注意这个所配的securityContext是属于Pod Level,而不是container Level。因为容器也有自己的securityContext,不要配错了。


参考:Kubernetes: how to set VolumeMount user group and file permissions