【问题标题】:how to deploy a statefulset when a pod security policy is in place in kubernetes当 Kubernetes 中有 pod 安全策略时如何部署 statefulset
【发布时间】:2020-12-02 11:27:06
【问题描述】:

我正在尝试在 kubernetes 中使用 PodSecurityPolicies,因此如果 Pod 使用的是 root 用户,则无法创建它们。 这是我的psp定义:

apiVersion: policy/v1beta1
kind: PodSecurityPolicy
metadata:
  name: eks.restrictive
spec:
  hostNetwork: false
  seLinux:
    rule: RunAsAny
  supplementalGroups:
    rule: RunAsAny
  runAsUser:
    rule: MustRunAsNonRoot
  fsGroup:
    rule: RunAsAny
  volumes:
  - '*'

这是我的 statefulset 定义

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: web
spec:
  selector:
    matchLabels:
      app: nginx # has to match .spec.template.metadata.labels
  serviceName: "nginx"
  replicas: 3 # by default is 1
  template:
    metadata:
      labels:
        app: nginx # has to match .spec.selector.matchLabels
    spec:
      securityContext:
        #only takes integers. 
        runAsUser: 1000
      terminationGracePeriodSeconds: 10
      containers:
      - name: nginx
        image: k8s.gcr.io/nginx-slim:0.8
        ports:
        - containerPort: 80
          name: web
        volumeMounts:
        - name: www
          mountPath: /usr/share/nginx/html
  volumeClaimTemplates:
  - metadata:
      name: www
    spec:
      accessModes: [ "ReadWriteOnce" ]
      storageClassName: "my-storage-class"
      resources:
        requests:
          storage: 1Gi

当我尝试创建这个 statefulset 时

create Pod web-0 in StatefulSet web failed error: pods "web-0" is forbidden: unable to validate against any pod security policy:

它没有指定我违反了什么政策,因为我指定我想在用户 1000 上运行它,所以我没有以 root 身份运行它(因此我的理解是这个 statefulset pod 定义没有违反任何规则在 PSP 中定义)。用于此映像的 Dockerfile 中没有指定 USER。

另一个奇怪的部分是,这对于标准 pod(种类:Pod,而不是 kind:Statefulset)工作正常,例如,当存在相同的 PSP 时,它工作得很好:

apiVersion: v1
kind: Pod
metadata:
  name: my-nodejs
spec:
  securityContext:
    runAsUser: 1000
  containers:
    - name: my-node
      image: node
      ports:
        - name: web
          containerPort: 80
          protocol: TCP
      command:
      - /bin/sh
      - -c
      - |
          npm install http-server-g
          npx http-server

我错过了什么/做错了什么?

【问题讨论】:

    标签: kubernetes


    【解决方案1】:

    您似乎忘记了将此 psp 绑定到服务帐户。

    您需要应用以下内容:

    cat << EOF | kubectl apply -f-
    apiVersion: rbac.authorization.k8s.io/v1
    kind: ClusterRole
    metadata:
      name: psp-role
    rules:
    - apiGroups: ['policy']
      resources: ['podsecuritypolicies']
      verbs:     ['use']
      resourceNames:
      - eks.restrictive
    EOF
    
    cat << EOF | kubectl apply -f-
    apiVersion: rbac.authorization.k8s.io/v1
    kind: RoleBinding
    metadata:
      name: psp-role-binding
    roleRef:
      apiGroup: rbac.authorization.k8s.io
      kind: ClusterRole
      name: psp-role
    subjects:
    - kind: ServiceAccount
      name: default
      namespace: default
    EOF
    

    如果您不想使用默认帐户,您可以创建一个单独的服务帐户并将角色绑定到它。

    阅读更多相关信息k8s documentation - pod-security-policy

    【讨论】:

    • 是的,显然这就是我所缺少的(仍在努力让它工作)但它并没有真正解释为什么创建单个 pod,但是 statefulset 创建的 pod 被拒绝,因为psp(如果 psp 没有被强制执行,那么这个错误应该不会发生吧?)。
    猜你喜欢
    • 2019-11-26
    • 2019-11-15
    • 2021-07-31
    • 2017-05-25
    • 1970-01-01
    • 2021-05-12
    • 1970-01-01
    • 1970-01-01
    • 2021-10-13
    相关资源
    最近更新 更多