在 CentOS 上部署 Kubernetes(k8s) 时,安全配置非常关键,通常从 系统层、Kubernetes 集群层、网络层、镜像与运行时 等多个层面进行加固。下面给你一套生产环境常用、可落地的安全配置指南。
# 升级系统
yum update -y
# 关闭不必要服务
systemctl disable firewalld --now
systemctl disable postfix --now
setenforce 0
sed -i 's/^SELINUX=enforcing/SELINUX=permissive/' /etc/selinux/config
✅ 建议:生产环境可开启 SELinux,但需额外配置 k8s 策略
cat > /etc/sysctl.d/k8s.conf <<EOF
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.ipv4.ip_forward = 1
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.all.send_redirects = 0
EOF
sysctl --system
kubeadm init \
--pod-network-cidr=10.244.0.0/16 \
--apiserver-cert-extra-sans=<master-ip>
重点配置:
--anonymous-auth=false
--authorization-mode=RBAC
--enable-admission-plugins=NodeRestriction,PodSecurityPolicy
检查:
ps -ef | grep kube-apiserver
kubectl api-versions | grep rbac
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
namespace: default
name: pod-reader
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "list", "watch"]
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: read-pods
namespace: default
subjects:
- kind: User
name: dev-user
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: Role
name: pod-reader
apiGroup: rbac.authorization.k8s.io
securityContext:
runAsNonRoot: true
runAsUser: 1000
allowPrivilegeEscalation: false
capabilities:
drop:
- ALL
kubectl label namespace default pod-security.kubernetes.io/enforce=restricted
kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: deny-all
spec:
podSelector: {}
policyTypes:
- Ingress
- Egress
apiVersion: apiserver.config.k8s.io/v1
kind: EncryptionConfiguration
resources:
- resources:
- secrets
providers:
- aescbc:
keys:
- name: key1
secret: <base64-secret>
kubeadm certs check-expiration
kubeadm certs renew all
yum install -y containerd
containerd config default > /etc/containerd/config.toml
systemctl enable containerd
latest 标签--audit-log-path=/var/log/k8s-audit.log
--audit-policy-file=/etc/k8s/audit-policy.yaml
apiVersion: audit.k8s.io/v1
kind: Policy
rules:
- level: Metadata
✅ RBAC 启用
✅ 禁止匿名访问
✅ Pod 非 root 运行
✅ etcd 加密
✅ 证书定期轮换
✅ NetworkPolicy 限制流量
✅ 镜像漏洞扫描
✅ 审计日志开启
| 工具 | 用途 |
|---|---|
| kube-bench | CIS 安全基线检查 |
| falco | 运行时入侵检测 |
| trivy | 镜像漏洞扫描 |
| istio | 服务网格安全 |
如果你愿意,我可以:
只要告诉我你的 使用场景(测试 / 生产 / 云 / 裸机) 即可。