关于 k8s-security-policies
k8s-security-policies 是一份 Kubernetes 集群安全策略指南,面向负责集群访问控制、网络隔离和合规配置的运维与安全团队。它结合 Pod Security Standards、NetworkPolicy、RBAC 与策略执行示例,说明纵深防御的实现方式。
主要功能
- 01配置 Pod Security Standards
- 02使用 NetworkPolicy 进行网络隔离
- 03设置 RBAC 最小权限访问
- 04通过 Gatekeeper 实施策略
- 05配置 Istio 服务网格安全策略
适用场景
- 01为集群划分网络访问边界
- 02为服务账号配置最小权限
- 03为多租户集群制定安全策略
- 04实施集群合规控制
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ wshobson/ agents --skill k8s-security-policies安装后在 Claude Code 中输入/k8s-security-policies确认已加载。
附带文件assets/network-policy-template.yamlreferences/rbac-patterns.md
详细介绍
Kubernetes 安全策略
在 Kubernetes 中实施 NetworkPolicy、PodSecurityPolicy、RBAC 和 Pod 安全标准的综合指南。
目的
使用网络策略、Pod 安全标准和 RBAC 为 Kubernetes 集群实施纵深防御安全。
何时使用此技能
- 实施网络分段
- 配置 Pod 安全标准
- 为最小权限访问设置 RBAC
- 为合规性创建安全策略
- 实施准入控制
- 保护多租户集群
Pod 安全标准
1. Privileged(无限制)
apiVersion: v1
kind: Namespace
metadata:
name: privileged-ns
labels:
pod-security.kubernetes.io/enforce: privileged
pod-security.kubernetes.io/audit: privileged
pod-security.kubernetes.io/warn: privileged
2. Baseline(最低限制)
apiVersion: v1
kind: Namespace
metadata:
name: baseline-ns
labels:
pod-security.kubernetes.io/enforce: baseline
pod-security.kubernetes.io/audit: baseline
pod-security.kubernetes.io/warn: baseline
3. Restricted(最严格限制)
apiVersion: v1
kind: Namespace
metadata:
name: restricted-ns
labels:
pod-security.kubernetes.io/enforce: restricted
pod-security.kubernetes.io/audit: restricted
pod-security.kubernetes.io/warn: restricted
网络策略
默认拒绝所有
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: default-deny-all
namespace: production
spec:
podSelector: {}
policyTypes:
- Ingress
- Egress
允许前端访问后端
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-frontend-to-backend
namespace: production
spec:
podSelector:
matchLabels:
app: backend
policyTypes:
- Ingress
ingress:
- from:
- podSelector:
matchLabels:
app: frontend
ports:
- protocol: TCP
port: 8080
允许 DNS
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-dns
namespace: production
spec:
podSelector: {}
policyTypes:
- Egress
egress:
- to:
- namespaceSelector:
matchLabels:
name: kube-system
ports:
- protocol: UDP
port: 53
参考: 见 assets/network-policy-template.yaml
RBAC 配置
Role(命名空间范围)
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: pod-reader
namespace: production
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "watch", "list"]
ClusterRole(集群范围)
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: secret-reader
rules:
- apiGroups: [""]
resources: ["secrets"]
verbs: ["get", "watch", "list"]
RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: read-pods
namespace: production
subjects:
- kind: User
name: jane
apiGroup: rbac.authorization.k8s.io
- kind: ServiceAccount
name: default
namespace: production
roleRef:
kind: Role
name: pod-reader
apiGroup: rbac.authorization.k8s.io
参考: 见 references/rbac-patterns.md
Pod 安全上下文
受限 Pod
apiVersion: v1
kind: Pod
metadata:
name: secure-pod
spec:
securityContext:
runAsNonRoot: true
runAsUser: 1000
fsGroup: 1000
seccompProfile:
type: RuntimeDefault
containers:
- name: app
image: myapp:1.0
securityContext:
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
capabilities:
drop:
- ALL
使用 OPA Gatekeeper 实施策略
ConstraintTemplate
apiVersion: templates.gatekeeper.sh/v1
kind: ConstraintTemplate
metadata:
name: k8srequiredlabels
spec:
crd:
spec:
names:
kind: K8sRequiredLabels
validation:
openAPIV3Schema:
type: object
properties:
labels:
type: array
items:
type: string
targets:
- target: admission.k8s.gatekeeper.sh
rego: |
package k8srequiredlabels
violation[{"msg": msg, "details": {"missing_labels": missing}}] {
provided := {label | input.review.object.metadata.labels[label]}
required := {label | label := input.parameters.labels[_]}
missing := required - provided
count(missing) > 0
msg := sprintf("missing required labels: %v", [missing])
}
Constraint
apiVersion: constraints.gatekeeper.sh/v1beta1
kind: K8sRequiredLabels
metadata:
name: require-app-label
spec:
match:
kinds:
- apiGroups: ["apps"]
kinds: ["Deployment"]
parameters:
labels: ["app", "environment"]
服务网格安全(Istio)
PeerAuthentication(mTLS)
apiVersion: security.istio.io/v1beta1
kind: PeerAuthentication
metadata:
name: default
namespace: production
spec:
mtls:
mode: STRICT
AuthorizationPolicy
apiVersion: security.istio.io/v1beta1
kind: AuthorizationPolicy
metadata:
name: allow-frontend
namespace: production
spec:
selector:
matchLabels:
app: backend
action: ALLOW
rules:
- from:
- source:
principals: ["cluster.local/ns/production/sa/frontend"]
最佳实践
- 在命名空间级别实施 Pod 安全标准
- 使用网络策略进行网络分段
- 为所有服务账户应用最小权限 RBAC
- 启用准入控制(OPA Gatekeeper/Kyverno)
- 以非 root 用户运行容器
- 使用只读根文件系统
- 丢弃所有能力,除非确有必要
- 实施资源配额和限制范围
- 为安全事件启用审计日志
- 定期对镜像进行安全扫描
合规框架
CIS Kubernetes Benchmark
- 使用 RBAC 授权
- 启用审计日志
- 使用 Pod 安全标准
- 配置网络策略
- 实施静态加密的密钥
- 启用节点身份验证
NIST 网络安全框架
- 实施纵深防御
- 使用网络分段
- 配置安全监控
- 实施访问控制
- 启用日志记录和监控
故障排查
NetworkPolicy 不生效:
# Check if CNI supports NetworkPolicy
kubectl get nodes -o wide
kubectl describe networkpolicy <name>
RBAC 权限被拒绝:
# Check effective permissions
kubectl auth can-i list pods --as system:serviceaccount:default:my-sa
kubectl auth can-i '*' '*' --as system:serviceaccount:default:my-sa
相关技能
k8s-manifest-generator- 用于创建安全清单gitops-workflow- 用于自动化策略部署
评论
支持 Markdown