Kubernetes 系统强化 Pod安全策略 PSP
第一步PodSecurityPolicy(简称PSP):Kubernetes中Pod部署时重要的安全校验手段,能够有效地约束应用运行时行为安全。使用PSP对象定义一组Pod在运行时必须遵循的条件及相关字段的默认值,只有Pod满足这些条件才会被K8s接受。(在部署的时候去校验的,在运行的时候是不管的,当你创建podd的时候会去校验)Pod安全策略限制维度Pod安全策略限制维度:在之前的行为在yaml
Pod安全策略限制维度
Pod安全策略限制维度:
在之前的行为在yaml里面写一些安全配置都是有意识的去选择,如果k8s集群是别人使用的较多,但是又想约束某些行为,这时候可以使用psp,强制约束在Pod里面某些行为,要不然不允许pod运行。
Pod安全策略实现为一个准入控制器,默认没有启用,当启用后会强制实施Pod安全策略,没有满足的Pod将无法创建。因此,建议在启用PSP之前先添加策略并对其授权。
[root@master ~]# vim /etc/kubernetes/manifests/kube-apiserver.yaml
- --enable-admission-plugins=NodeRestriction,PodSecurityPolicy
[root@master ~]# systemctl restart kubelet
用户使用SA (ServiceAccount)创建了一个Pod,K8s会先验证这个SA是否可以访问PSP资源权限,如果可以进一步验证Pod配置是否满足PSP规则,任意一步不满足都会拒绝部署。(第一步就是让pod可以有PSP的访问权限)
在启用了准入控制器PSP,那么以后所有的Pod就需要经过PSP,但是现在PSP里面任何策略,那么所有的行为都是拒绝的。只有添加策略,符合策略的Pod才可以创建。所以在启用PSP之前可以先写好PSP的策略,应用上,以免PSP启用之后不能创建Pod。
[root@master ~]# kubectl run nginx1 --image=nginx
Error from server (Forbidden): pods "nginx1" is forbidden: no providers available to validate pod request
现在我这个策略是一个比较宽泛的范围,所以pod满足这些规则可以正常创建
[root@master ~]# cat psp.yaml
apiVersion: policy/v1beta1
kind: PodSecurityPolicy
metadata:
name: example
spec:
seLinux:
rule: RunAsAny
supplementalGroups:
rule: RunAsAny
runAsUser:
rule: RunAsAny
fsGroup:
rule: RunAsAny
volumes:
- '*'
[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
默认情况下容器都不是以特权模式运行
[root@master ~]# vim psp.yaml
spec:
privileged: false
seLinux:
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
拒绝带有特权模式的Pod去创建,注意特权是针对容器的,需要在容器下面配置。
默认我们使用kubelete操作的时候是特权账号,使用普通账号,还涉及到授权。(特权账号是具有访问psp的能力,所以说不需要授权了,普通账号需要格外的授权)
[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-server
namespace: default
spec:
containers:
- name: nginx
image: nginx
securityContext:
privileged: true
普通账号测试,可以看到内置的权限组的权限比较大,但是你会看到我启动的pod是不具有特权模式的pod,为什么还会提示错误
[root@master ~]# kubectl create rolebinding psp --clusterrole=edit --serviceaccount=default:psp
rolebinding.rbac.authorization.k8s.io/psp created
#--as=system:serviceaccount这个是固定的格式,后面是来判断sa是否具有相关的权限
[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也不能访问,所以需要授权让其有访问psp的权限
[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
更多推荐
所有评论(0)