首页 > 解决方案 > 在 Kubernetes 部署中设置 securityContext

问题描述

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

securityContext:
  runAsUser: 1000
  runAsGroup: 3000
  fsGroup: 2000

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

标签: kuberneteskubernetes-podkubernetes-securitykubernetes-deployment

解决方案


您可以像使用securityContextinDeployment一样使用 in 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

推荐阅读