Kubernetes 系统强化 Pod安全策略 PSP
Pod安全策略限制維度
Pod安全策略限制維度:?
?在之前的行為在yaml里面寫一些安全配置都是有意識(shí)的去選擇,如果k8s集群是別人使用的較多,但是又想約束某些行為,這時(shí)候可以使用psp,強(qiáng)制約束在Pod里面某些行為,要不然不允許pod運(yùn)行。
Pod安全策略實(shí)現(xiàn)為一個(gè)準(zhǔn)入控制器,默認(rèn)沒有啟用,當(dāng)啟用后會(huì)強(qiáng)制實(shí)施Pod安全策略,沒有滿足的Pod將無法創(chuàng)建。因此,建議在啟用PSP之前先添加策略并對(duì)其授權(quán)。
[root@master ~]# vim /etc/kubernetes/manifests/kube-apiserver.yaml- --enable-admission-plugins=NodeRestriction,PodSecurityPolicy[root@master ~]# systemctl restart kubelet?用戶使用SA (ServiceAccount)創(chuàng)建了一個(gè)Pod,K8s會(huì)先驗(yàn)證這個(gè)SA是否可以訪問PSP資源權(quán)限,如果可以進(jìn)一步驗(yàn)證Pod配置是否滿足PSP規(guī)則,任意一步不滿足都會(huì)拒絕部署。(第一步就是讓pod可以有PSP的訪問權(quán)限)
因此,需要實(shí)施需要有這幾點(diǎn):(注意PSP是一個(gè)準(zhǔn)入控制插件) ? 創(chuàng)建SA服務(wù)賬號(hào) ? 該SA需要具備創(chuàng)建對(duì)應(yīng)資源權(quán)限,例如創(chuàng)建Pod、Deployment ? SA使用PSP資源權(quán)限:創(chuàng)建Role,使用PSP資源權(quán)限,再將SA綁定Role(多了一步)?在啟用了準(zhǔn)入控制器PSP,那么以后所有的Pod就需要經(jīng)過PSP,但是現(xiàn)在PSP里面任何策略,那么所有的行為都是拒絕的。只有添加策略,符合策略的Pod才可以創(chuàng)建。所以在啟用PSP之前可以先寫好PSP的策略,應(yīng)用上,以免PSP啟用之后不能創(chuàng)建Pod。
[root@master ~]# kubectl run nginx1 --image=nginx Error from server (Forbidden): pods "nginx1" is forbidden: no providers available to validate pod request現(xiàn)在我這個(gè)策略是一個(gè)比較寬泛的范圍,所以pod滿足這些規(guī)則可以正常創(chuàng)建
[root@master ~]# cat psp.yaml apiVersion: policy/v1beta1 kind: PodSecurityPolicy metadata:name: example spec:seLinux:rule: RunAsAnysupplementalGroups:rule: RunAsAnyrunAsUser:rule: RunAsAnyfsGroup:rule: RunAsAnyvolumes:- '*'[root@master ~]# kubectl apply -f psp.yaml podsecuritypolicy.policy/example created [root@master ~]# kubectl get psp NAME PRIV CAPS SELINUX RUNASUSER FSGROUP SUPGROUP READONLYROOTFS VOLUMES example false RunAsAny RunAsAny RunAsAny RunAsAny false * [root@master ~]# kubectl run nginx1 --image=nginx pod/nginx1 created默認(rèn)情況下容器都不是以特權(quán)模式運(yùn)行
[root@master ~]# vim psp.yaml spec:privileged: falseseLinux:rule: RunAsAny ........................................................[root@master ~]# kubectl apply -f psp.yaml podsecuritypolicy.policy/example configured [root@master ~]# kubectl get psp NAME PRIV CAPS SELINUX RUNASUSER FSGROUP SUPGROUP READONLYROOTFS VOLUMES example false RunAsAny RunAsAny RunAsAny RunAsAny false * [root@master ~]# kubectl run nginx2 --image=nginx pod/nginx2 created拒絕帶有特權(quán)模式的Pod去創(chuàng)建,注意特權(quán)是針對(duì)容器的,需要在容器下面配置。
默認(rèn)我們使用kubelete操作的時(shí)候是特權(quán)賬號(hào),使用普通賬號(hào),還涉及到授權(quán)。(特權(quán)賬號(hào)是具有訪問psp的能力,所以說不需要授權(quán)了,普通賬號(hào)需要格外的授權(quán))
[root@master ~]# vim sidecar.yaml [root@master ~]# kubectl apply -f sidecar.yaml Error from server (Forbidden): error when creating "sidecar.yaml": pods "web-server" is forbidden: unable to validate against any pod security policy: [spec.containers[0].securityContext.privileged: Invalid value: true: Privileged containers are not allowed] [root@master ~]# cat sidecar.yaml apiVersion: v1 kind: Pod metadata:name: web-servernamespace: default spec:containers:- name: nginximage: nginxsecurityContext:privileged: true普通賬號(hào)測(cè)試,可以看到內(nèi)置的權(quán)限組的權(quán)限比較大,但是你會(huì)看到我啟動(dòng)的pod是不具有特權(quán)模式的pod,為什么還會(huì)提示錯(cuò)誤
[root@master ~]# kubectl create rolebinding psp --clusterrole=edit --serviceaccount=default:psp rolebinding.rbac.authorization.k8s.io/psp created#--as=system:serviceaccount這個(gè)是固定的格式,后面是來判斷sa是否具有相關(guān)的權(quán)限 [root@master ~]# kubectl --as=system:serviceaccount:default:psp get pod NAME READY STATUS RESTARTS AGE front-end-7899dbb4cf-kkbg4 1/1 Running 0 3d20h front-end-7899dbb4cf-np6kv 1/1 Running 0 3d20h front-end-7899dbb4cf-qns4b 1/1 Running 0 3d20h front-end-7899dbb4cf-sslgc 1/1 Running 0 3d20h front-end-7899dbb4cf-tb9gr 1/1 Running 0 3d20h nfs-client-provisioner-cb667d665-zh6b4 1/1 Running 10 9d nginx1 1/1 Running 0 21h nginx2 1/1 Running 0 21h tst 1/1 Running 0 4d13h[root@master ~]# kubectl --as=system:serviceaccount:default:psp get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE front-end NodePort 10.233.53.49 <none> 80:32012/TCP 3d13h kubernetes ClusterIP 10.233.0.1 <none> 443/TCP 51d[root@master ~]# kubectl --as=system:serviceaccount:default:psp run nginx3 --image=nginx Error from server (Forbidden): pods "nginx3" is forbidden: unable to validate against any pod security policy: []可以看到psp也不能訪問,所以需要授權(quán)讓其有訪問psp的權(quán)限
[root@master ~]# kubectl --as=system:serviceaccount:default:psp get psp Error from server (Forbidden): podsecuritypolicies.policy is forbidden: User "system:serviceaccount:default:psp" cannot list resource "podsecuritypolicies" in API group "policy" at the cluster scope [root@master ~]# kubectl create role psp --verb=use --resource=podsecuritypolicies --resource-name=psp -n default role.rbac.authorization.k8s.io/psp created[root@master ~]# kubectl create rolebinding psp --role=psp --serviceaccount=default:psp -n default rolebinding.rbac.authorization.k8s.io/psp created?
總結(jié)
以上是生活随笔為你收集整理的Kubernetes 系统强化 Pod安全策略 PSP的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 计算机考研自主命题表,都有那些学校考研数
- 下一篇: [转]PSP使用指南之- 系统升级(降级