通过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绑定。