Part A: 三层网络(30 分) 1. 创建命名空间 net-secure 2. 部署三层应用: a. frontend: Deployment web-ui(nginx:alpine, 2副本) ClusterIP Service frontend-svc b. backend: Deployment api-gateway(httpd:alpine, 3副本) ClusterIP Service backend-svc c. database: Pod data-db(redis:7-alpine) ClusterIP Service db-svc
kubectl exec no-sa-mount -- ls /var/run/secrets/kubernetes.io/serviceaccount/ 2>&1 # ls: /var/run/secrets/kubernetes.io/serviceaccount/: No such file or directory
# 3. 创建自定义 SA kubectl create sa custom-sa kubectl run custom-sa-pod --image=busybox:1.36 --restart=Never --overrides=' { "spec": { "serviceAccountName": "custom-sa" } }' -- sleep 3600
kubectl get pod custom-sa-pod -o jsonpath='{.spec.serviceAccountName}' # custom-sa
# 清理 kubectl delete pod sa-test no-sa-mount custom-sa-pod kubectl delete sa custom-sa
2. 安全加固: a. 创建专用 SA app-secure,禁用 auto-mount b. 创建最小权限 Role(只读自己的 ConfigMap) c. SecurityContext: - runAsUser 1001, runAsNonRoot - drop ALL capabilities - readOnlyRootFilesystem(需补充 emptyDir 给 tmp) d. 添加资源限制 e. 移除 hostPath,改用 emptyDir 或 PVC f. 创建 NetworkPolicy: - Ingress:只允许同命名空间 + Ingress Controller - Egress:只允许 DNS + 同命名空间