分页: 1 / 1

Kubernetes 安全防护最佳实践:生产环境避坑指南

发表于 : 周二 7月 28, 2026 10:13 am
admin
K8s 生产环境的安全配置涉及多个层面,以下是几项经过验证的实践。

[b]1. RBAC 最小权限原则[/b]

不要用 cluster-admin 的 ServiceAccount 跑业务 Pod:
[code]apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata: namespace: default name: pod-reader
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "watch", "list"]
---[/code]

ServiceAccount 绑定具体 Role,而不是 ClusterRole。

[b]2. Pod Security Standards[/b]

K8s 1.23+ 内置了 PSA(Pod Security Admission):
[code]apiVersion: v1
kind: Namespace
metadata:
name: secure-ns
labels:
pod-security.kubernetes.io/enforce: restricted[/code]

推荐策略:baseline 起手,restricted 给敏感业务。

[b]3. 网络策略[/b]

默认允许所有流量,需要主动隔离:
[code]apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata: name: deny-all
spec:
podSelector: {}
policyTypes:
- Ingress
- Egress[/code]

然后按需放行。

[b]4. Secret 加密[/b]

etcd 存储默认 base64 编码,不是加密:
[code]kubectl get secrets -o yaml | head -20[/code]

需要启用 EncryptionConfiguration,用 AES-CBC 或 KMS 加密。

[b]5. 镜像安全[/b]

- 使用镜像签名(cosign)
- 定期扫描漏洞(trivy)
- 禁止 latest 标签
- 用不可变标签(sha256)

[b]6. 审计日志[/b]

[code]apiVersion: audit.k8s.io/v1
kind: Policy
rules:
- level: RequestResponse
resources:
- group: ""
resources: ["secrets", "configmaps"][/code]

安全防线越多,暴露面越小。从最小权限开始,逐步收紧。

Re: 回复

发表于 : 周三 7月 29, 2026 2:00 am
admin
好内容,收藏了。之前也踩过类似的坑,配好之后确实省心很多。