-
确认Kubernetes版本兼容性:确保集群版本≤v1.21(PSP在v1.25+已移除),若版本≥v1.22需显式启用PSP准入控制器。
-
启用PodSecurityPolicy准入控制器:
sudo sed -i '/- kube-apiserver/a\ - --enable-admission-plugins=NodeRestriction,PodSecurityPolicy' /etc/kubernetes/manifests/kube-apiserver.yaml systemctl restart kubelet
-
创建PodSecurityPolicy(PSP):
apiVersion: policy/v1beta1 kind: PodSecurityPolicy metadata: name: restricted-psp spec: privileged: false allowPrivilegeEscalation: false requiredDropCapabilities: ["NET_RAW"] runAsUser: rule: "MustRunAsNonRoot" seLinux: rule: "RunAsAny" supplementalGroups: rule: "RunAsAny" fsGroup: rule: "RunAsAny"
kubectl apply -f psp.yaml
-
配置RBAC授权:
- 创建ClusterRole:
apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRole metadata: name: psp:restricted rules: - apiGroups: ["policy"] resources: ["podsecuritypolicies"] verbs: ["use"] resourceNames: ["restricted-psp"]
- 绑定默认ServiceAccount(按需调整命名空间):
apiVersion: rbac.authorization.k8s.io/v1 kind: RoleBinding metadata: name: default-psp namespace: default roleRef: apiGroup: rbac.authorization.k8s.io kind: ClusterRole name: psp:restricted subjects: - kind: ServiceAccount name: default namespace: default
- 创建ClusterRole:
-
验证策略生效:
- 测试非法Pod(应被拒绝):
apiVersion: v1 kind: Pod metadata: name: test-privileged spec: containers: - name: test image: nginx securityContext: privileged: true
- 检查事件:
kubectl describe pod test-privileged
- 测试非法Pod(应被拒绝):
-
关键注意事项:
- 必须为kube-system等系统组件单独授权,避免集群组件被拦截
- 生产环境建议先创建宽松PSP,逐步收紧策略
- 监控API Server日志:
journalctl -u kube-apiserver -f
-
替代方案提示:若集群版本≥v1.23,建议优先使用内置Pod Security Admission替代PSP
如何通过kubeadm配置Kubernetes(k8s)集群中的Pod安全策略(PSP)?
用kubeadm配置Pod安全策略(PSP)大概分这几步:1. 先确认集群版本,PSP在1.25+已被废弃,建议用PodSecurity替代;2. 如果坚持用PSP,修改kube-apiserver配置,添加--enable-admission-plugins=PodSecurityPolicy;3. 创建PSP的yaml文件,比如限制特权容器;4. 创建ClusterRole和RoleBinding绑定到ServiceAccount。注意要先用宽松策略测试,否则可能所有Pod都被拒绝!
更多回答
通过kubeadm配置Kubernetes Pod安全策略(PSP)需注意以下几点经验总结:
- 启用PSP准入控制器:在kube-apiserver的启动参数中添加
--enable-admission-plugins=PodSecurityPolicy
并重启API服务。 - 定义策略文件:编写PSP YAML(如限制特权容器、文件系统只读等),通过
kubectl apply -f
部署。 - RBAC绑定:为ServiceAccount或用户授权PSP使用权限,避免因未授权导致Pod无法调度。
- 测试策略:通过
kubectl auth can-i use podsecuritypolicy/<name>
验证权限,并部署测试Pod观察拦截行为。
注意:Kubernetes 1.21+已弃用PSP,建议逐步迁移至PodSecurity Admission或第三方策略引擎(如OPA/Gatekeeper)。生产环境需谨慎设计策略范围,避免因过度限制导致业务中断。
通过kubeadm配置Kubernetes集群的Pod安全策略(PSP)需遵循以下步骤:
-
启用PodSecurityPolicy准入控制器
修改/etc/kubernetes/manifests/kube-apiserver.yaml
,在--enable-admission-plugins
参数中追加PodSecurityPolicy
(需保留原有插件,例如:NodeRestriction,PodSecurityPolicy
)。重启kube-apiserver后生效。 -
创建PodSecurityPolicy资源
定义PSP规则(如限制特权容器、只读根文件系统等),通过YAML文件创建策略。例如:apiVersion: policy/v1beta1 kind: PodSecurityPolicy metadata: name: restricted spec: privileged: false readOnlyRootFilesystem: true # 其他规则...
-
配置RBAC绑定
创建ClusterRole与ClusterRoleBinding,将PSP授权给特定ServiceAccount或用户。例如为系统组件(如kube-proxy)绑定宽松策略,避免集群功能异常。 -
验证与调试
部署测试Pod验证策略是否生效,确保系统Pod(如CoreDNS)未被阻断。可通过kubectl describe psp
及事件日志排查问题。
注意:
- PSP已在Kubernetes 1.21+版本弃用,建议新集群优先使用Pod Security Admission或OPA Gatekeeper等替代方案。
- 未正确绑定PSP可能导致Pod创建失败,需确保RBAC与策略优先级配置正确。
通过kubeadm配置Kubernetes集群的Pod安全策略(PSP)需遵循以下步骤:
-
确认集群版本:Kubernetes v1.21+已弃用PSP,建议使用PodSecurity Admission替代。若使用旧版本,需确保API Server已启用
PodSecurityPolicy
准入控制器。 -
启用准入控制器:
- 修改
/etc/kubernetes/manifests/kube-apiserver.yaml
,在command
段添加:- --enable-admission-plugins=NodeRestriction,PodSecurityPolicy
- 重启kube-apiserver生效。
- 修改
-
定义PSP策略: 创建YAML文件(如
psp-restrictive.yaml
)定义策略,例如禁止特权容器:apiVersion: policy/v1beta1 kind: PodSecurityPolicy metadata: name: restricted spec: privileged: false # 其他安全约束(如volumes、capabilities等)
-
配置RBAC:
- 创建ClusterRole绑定策略:
apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRole metadata: name: psp:restricted rules: - apiGroups: ['policy'] resources: ['podsecuritypolicies'] verbs: ['use'] resourceNames: ['restricted']
- 绑定ServiceAccount(如为系统组件单独授权):
apiVersion: rbac.authorization.k8s.io/v1 kind: RoleBinding metadata: name: psp:default namespace: kube-system roleRef: apiGroup: rbac.authorization.k8s.io kind: ClusterRole name: psp:restricted subjects: - kind: ServiceAccount name: default namespace: kube-system
- 创建ClusterRole绑定策略:
-
验证策略:
- 尝试运行特权Pod测试策略是否生效:
apiVersion: v1 kind: Pod metadata: name: test-privileged spec: containers: - name: test image: nginx securityContext: privileged: true
- 预期结果:因违反策略而创建失败。
- 尝试运行特权Pod测试策略是否生效:
注意:
- 生产环境需细化PSP规则(如文件系统只读、禁止host网络等)。
- PSP弃用后,建议优先采用PodSecurity标准或第三方工具(如OPA Gatekeeper)。
- 确保kube-controller-manager配置
--use-service-account-credentials=true
以启用RBAC绑定。