在Kubernetes部署中设置securityContext

时间:2019-07-30 16:21:02

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

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

securityContext:
  runAsUser: 1000
  runAsGroup: 3000
  fsGroup: 2000

是否有办法在部署清单中进行显示? 正如我在文档中看到的那样,我只能在pod yaml中设置securitycontext。

1 个答案:

答案 0 :(得分:2)

您可以在securityContext中使用Deployment中的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