温馨提示×

centos k8s如何配置安全

小樊
37
2026-08-04 21:36:44
栏目: 智能运维

CentOS 上部署 Kubernetes(k8s) 时,安全配置非常关键,通常从 系统层、Kubernetes 集群层、网络层、镜像与运行时 等多个层面进行加固。下面给你一套生产环境常用、可落地的安全配置指南


一、系统层(CentOS)安全加固

1. 系统基础安全

# 升级系统
yum update -y

# 关闭不必要服务
systemctl disable firewalld --now
systemctl disable postfix --now

2. 禁用 SELinux(或配置为 permissive)

setenforce 0
sed -i 's/^SELINUX=enforcing/SELINUX=permissive/' /etc/selinux/config

✅ 建议:生产环境可开启 SELinux,但需额外配置 k8s 策略

3. 内核参数优化(安全 + 网络)

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

二、Kubernetes 集群安全配置

1. 使用 kubeadm 并启用安全参数

kubeadm init \
  --pod-network-cidr=10.244.0.0/16 \
  --apiserver-cert-extra-sans=<master-ip>

2. API Server 安全

重点配置:

--anonymous-auth=false
--authorization-mode=RBAC
--enable-admission-plugins=NodeRestriction,PodSecurityPolicy

检查:

ps -ef | grep kube-apiserver

三、RBAC 权限控制(非常重要)

1. 启用 RBAC(默认已启用)

kubectl api-versions | grep rbac

2. 最小权限示例(只允许查看 Pod)

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

四、Pod 与容器安全

1. 禁止特权容器

securityContext:
  runAsNonRoot: true
  runAsUser: 1000
  allowPrivilegeEscalation: false
  capabilities:
    drop:
    - ALL

2. 使用 Pod Security Standards(推荐)

kubectl label namespace default pod-security.kubernetes.io/enforce=restricted

五、网络与通信安全

1. 使用 CNI 插件(Calico / Flannel)

kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml

2. NetworkPolicy 示例

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: deny-all
spec:
  podSelector: {}
  policyTypes:
  - Ingress
  - Egress

六、证书与加密

1. 启用 etcd 加密(敏感数据)

apiVersion: apiserver.config.k8s.io/v1
kind: EncryptionConfiguration
resources:
- resources:
  - secrets
  providers:
  - aescbc:
      keys:
      - name: key1
        secret: <base64-secret>

2. 定期轮换证书

kubeadm certs check-expiration
kubeadm certs renew all

七、镜像与运行时安全

1. 使用 containerd(替代 docker)

yum install -y containerd
containerd config default > /etc/containerd/config.toml
systemctl enable containerd

2. 镜像安全建议

  • 使用私有 Harbor
  • 禁止 latest 标签
  • 扫描镜像漏洞(Trivy / Clair)

八、审计与日志

1. 启用 API Server 审计

--audit-log-path=/var/log/k8s-audit.log
--audit-policy-file=/etc/k8s/audit-policy.yaml

2. 审计策略示例

apiVersion: audit.k8s.io/v1
kind: Policy
rules:
- level: Metadata

九、生产环境安全清单(Checklist)

✅ RBAC 启用
✅ 禁止匿名访问
✅ Pod 非 root 运行
✅ etcd 加密
✅ 证书定期轮换
✅ NetworkPolicy 限制流量
✅ 镜像漏洞扫描
✅ 审计日志开启


十、推荐工具

工具 用途
kube-bench CIS 安全基线检查
falco 运行时入侵检测
trivy 镜像漏洞扫描
istio 服务网格安全

如果你愿意,我可以:

  • ✅ 给你 CentOS + kubeadm 完整安全部署脚本
  • ✅ 针对 等保 / 金融 / 安全合规 输出专项方案
  • ✅ 帮你做 k8s 安全加固检查清单

只要告诉我你的 使用场景(测试 / 生产 / 云 / 裸机) 即可。

0