1. 使用 Network Policies: 启用命名空间级别的 Network Policies,以限制 Pod 之间的网络通信。确保仅允许必要的网络流量,并阻止不必要的访问。
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: my-network-policy
spec:
podSelector:
matchLabels:
app: my-app
policyTypes:
- Ingress
- Egress
ingress:
- from:
- podSelector:
matchLabels:
role: db
ports:
- protocol: TCP
port: 3306
egress:
- to:
- podSelector:
matchLabels:
internet: "true"
ports:
- protocol: TCP
port: 80
2. 使用 Pod Security Policies(PSP): 在命名空间中启用 Pod Security Policies,以定义容器的安全上下文和权限。PSP 允许你在命名空间级别应用安全标准。
apiVersion: policy/v1beta1
kind: PodSecurityPolicy
metadata:
name: restricted
spec:
privileged: false
seLinux:
rule: RunAsAny
supplementalGroups:
rule: MustRunAs
ranges:
- min: 1
max: 65535
runAsUser:
rule: MustRunAsNonRoot
fsGroup:
rule: MustRunAs
启用 PSP 并将其绑定到命名空间:
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: psp:restricted
rules:
- apiGroups: ['policy']
resources: ['podsecuritypolicies']
verbs: ['use']
resourceNames: ['restricted']
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: psp:restricted
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: psp:restricted
subjects:
- kind: Group
name: system:serviceaccounts:<namespace>
3. 启用 PodAnnotations Admission Controller: 使用 Admission Controller 来强制执行对 PodAnnotations 的规范。确保 PodAnnotations 中不包含敏感信息。
apiVersion: admissionregistration.k8s.io/v1
kind: MutatingWebhookConfiguration
metadata:
name: podannotations.namespace.svc
webhooks:
- clientConfig:
service:
name: podannotations-svc
namespace: <namespace>
path: "/"
caBundle: <ca-bundle>
rules:
...
4. 使用 PodDisruptionBudgets: 限制在该命名空间内同时中断 Pod 的数量,以确保在维护期间保持足够的可用性。
apiVersion: policy/v1beta1
kind: PodDisruptionBudget
metadata:
name: my-pdb
spec:
maxUnavailable: 1
5. 启用 Role-Based Access Control(RBAC): 在命名空间级别启用 RBAC,并定义最小权限原则,确保每个用户或服务账户只有必要的权限。
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
namespace: <namespace>
name: my-role
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "list", "watch"]
这些是一些建议的安全实践,可以在 Kubernetes 命名空间级别实施。确保根据你的实际需求和安全标准进行适当的配置。在实施这些策略之前,请确保你对 Kubernetes 的安全特性和配置有深入的理解。
转载请注明出处:http://www.pingtaimeng.com/article/detail/9974/Kubernetes