【问题标题】:Setup securityContext inside kubernetes deployment在 Kubernetes 部署中设置 securityContext
【发布时间】:2019-12-08 02:28:45
【问题描述】:

我在我的部署中使用 nfs 挂载卷。 我需要给它 fsGroup 如下:

securityContext:
  runAsUser: 1000
  runAsGroup: 3000
  fsGroup: 2000

有没有办法在部署清单上做到这一点? 正如我在文档中看到的,我只能在 pod yaml 中设置 securitycontext。

【问题讨论】:

  • deployment 得到了一个 pod 模板,和 pod spec 一样。您可以在那里设置安全上下文。
  • 你的意思是在spec.containers下吗?

标签: kubernetes kubernetes-pod kubernetes-security kubernetes-deployment


【解决方案1】:

您可以在Deployment 中使用securityContext,就像在Pod 中使用它一样。

就像已经建议的那样,把它放在template.spec下面:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: test-deployment
  labels:
    app: test
spec:
  replicas: 3
  selector:
    matchLabels:
      app: test
  template:
    metadata:
      labels:
        app: test
    spec:
      securityContext:
          runAsUser: 2000
          runAsGroup: 3000
          fsGroup: 2000
      containers:
      - name: test
        image: busybox
        ports:
        - containerPort: 80
        command: [ "sh", "-c", "sleep 1h" ]

你可以测试一下:

$ kubectl exec -it test-deployment-54d954d7f-2b582  sh
/ $ ps
PID   USER     TIME  COMMAND
    1 2000      0:00 sleep 1h
    6 2000      0:00 sh
   11 2000      0:00 ps
/ $ whoami
whoami: unknown uid 200

【讨论】:

  • 嗨,我现在正面临这个确切的问题 (stackoverflow.com/questions/59484063/…)。我无法让它在我的部署中工作。尝试您的答案时,我的 pod 卡在 CrashLoopBackOff 错误上。通过kubectl logs pod/<pod id> 检查日志显示此错误:/sbin/runit-wrapper: line 5: can't create /env: Permission denied。我非常需要帮助,拜托!
猜你喜欢
  • 2021-06-18
  • 1970-01-01
  • 2020-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-09
  • 2021-11-27
相关资源
最近更新 更多