【发布时间】: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