在Debian上部署Kubernetes安全策略涉及多个方面,包括系统初始化、配置管理、权限管理、网络策略等。以下是一个详细的指南,帮助你在Debian系统上部署Kubernetes安全策略:
export DEBIAN_FRONTEND=noninteractive
apt update -qq
apt remove -y -qq ufw lxd lxd-client lxcfs lxc-common
apt install --no-install-recommends --no-install-suggests -y -qq nfs-common iptables conntrack jq socat bash-completion open-iscsi rsync ipset ipvsadm htop net-tools wget psmisc git curl nload ebtables ethtool procps
mkdir -pv /etc/systemd/system.conf.d
cat > /etc/systemd/system.conf.d/30-k8s-ulimits.conf <<EOF
[Manager]
DefaultLimitCORE=infinity
DefaultLimitNOFILE=100000
DefaultLimitNPROC=100000
EOF
mkdir -pv /etc/systemd/journald.conf.d
cat > /etc/systemd/journald.conf.d/95-k3s-journald.conf <<EOF
[Journal]
# 持久化保存到磁盘
Storage=persistent
# 最大占用空间 2G
SystemMaxUse=2G
# 单日志文件最大 100M
SystemMaxFileSize=100M
# 日志保存时间 1 周
MaxRetentionSec=1week
# 禁止转发
ForwardToSyslog=no
ForwardToWall=no
EOF
systemctl daemon-reload
systemctl restart systemd-journald
cat > /etc/modules-load.d/10-k3s-modules.conf <<EOF
br_netfilter
ip_vs
ip_vs_rr
ip_vs_wrr
ip_vs_sh
nf_conntrack
EOF
systemctl daemon-reload
systemctl restart systemd-modules-load
kubectl create role example-role --namespace=default --rules='apiGroups: ["", "extensions", "apps"], resources: ["pods", "services", "configmaps"], verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]'
kubectl create rolebinding example-role-binding --namespace=default --subject=kind:User,name:example-user,apiGroup:rbac.authorization.k8s.io --roleRef=kind:Role,name:example-role,apiGroup:rbac.authorization.k8s.io
apiVersion: policy/v1
kind: PodSecurityPolicy
metadata:
name: example-psp
namespace: default
spec:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: app
operator: In
values:
- example-app
topologyKey: "kubernetes.io/hostname"
runAsUser:
rule: "MustRunAsNonRootUser"
runAsGroup:
rule: "MustRunAsNonRootGroup"
fsGroup:
rule: "MustRunAs"
ranges:
- min: 1000
max: 9999
kubectl apply -f pod-security-policy.yaml
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: example-network-policy
namespace: default
spec:
podSelector:
matchLabels:
app: example-app
policyTypes:
- Ingress
- Egress
egress:
- to:
- ipBlock:
cidr: 10.0.0.0/8
ingress:
- from:
- ipBlock:
cidr: 10.0.0.0/8
kubectl apply -f network-policy.yaml
通过以上步骤,你可以在Debian上部署一个基本的Kubernetes安全策略。请根据你的实际需求调整配置和网络设置。