Kubernetes容器root权限

原创
半兽人 发表于: 2018-11-08   最后更新时间: 2022-09-09 00:32:12  
{{totalSubscript}} 订阅, 16,689 游览

容器root权限

你一定很奇怪,明明进入容器内,可以看到是root用户啊,为什么还要设置容器的root权限?这是因为容器内虽然看起来是root,但是却没有root的所有功能。当需要修改系统文件时,就不被允许。如果你的app恰好就要去修改系统文件,那么就需要明白如何设置容器的root权限。

想要开启容器的root权限,需要做以下操作:

1.设置kube-apiserverkubelet

--allow-privileged=true

这样就允许节点上的容器开启privileged

怎么看当前是不是为true呢?

ps -ef|grep kube

然后仔细看,你就会看到是不是为true。

那么如何设置以上参数呢?

因为kube-apiserverkubelet都是通过二进制文件直接运行的,所以直接在重启时加入以上参数就行。更简单的是,如果已经设置了systemd启动,那么就去/etc/systemd/system/下找到对应的.service文件,改里面的参数,然后通过systemctl命令直接重启就行。

2.设置包含contariners的yaml文件(如deploy的yaml文件),在containers下添加:

securityContext:
  privileged: true

例如pod的yaml文件:

apiVersion: v1
kind: Pod
metadata:
  name: hello-world
spec:
  containers:
    - name: hello-world-container
      # The container definition
      # ...
      securityContext:
        privileged: true

这个很好理解,但是切记要把这个与podSecurityContext分开,podSecurityContext是在pod.spec内的属性,虽然它也写作securityContextsecurityContextcontainer内的属性。

podSecurityContext的例子如下:

apiVersion: v1
kind: Pod
metadata:
  name: hello-world
spec:
  containers:
  # specification of the pod’s containers
  # ...
  securityContext:
    fsGroup: 1234
    supplementalGroups: [5678]
    seLinuxOptions:
      level: "s0:c123,c456"
更新于 2022-09-09

查看kubernetes更多相关的文章或提一个关于kubernetes的问题,也可以与我们一起分享文章