kubernetes - 当 Kubernetes 中有 pod 安全策略时如何部署 statefulset
问题描述
我正在尝试在 kubernetes 中使用 PodSecurityPolicies,因此如果使用 root 用户,则无法创建 Pod。这是我的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
我错过了什么/做错了什么?
解决方案
您似乎忘记了将此 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 文档 - pod-security-policy。
推荐阅读
- javascript - 为什么我清除后不重绘图像?
- collections - Java根据属性汇总项目列表
- python - Anaconda 3 Python 命令似乎已损坏
- elixir - 如何在凤凰框架中检查 exq 作业的状态?
- erlang - 从源代码编译 MongooseIM 时 rebar_core 中未捕获的错误
- javascript - vb.net 字符串到chartjs 数据字段?
- code-coverage - 在codecov上,一条线没有着色是什么意思?
- java - Eclipse 中的 Java App Engine 项目在 Localhost 处返回 Server App Engine Standard 无法启动
- python - 替换 Pandas 列表中的缺失值
- vba - 如果空白,VBA从上面的单元格复制数据并在下一个数据时停止,然后重复