4

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

 3 years ago
source link: https://www.pkslow.com/archives/kubernetes-mount-volumn-as-none-root
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
技术之前,先读诗书:

一年春又尽,倚杖对斜晖。

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


欢迎关注微信公众号<南瓜慢说>,将持续为你更新...

推荐阅读:
如何制定切实可行的计划并好好执行
容器技术(Docker-Kubernetes)
SpringBoot-Cloud相关
Https专题


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK