Lesson 0016:K8s 生产实践——Helm、HPA、RBAC、监控

← Lesson 0015 讲了 Ingress、ConfigMap、Volume。最后一课把 K8s 技能提升到生产可用的水平。

1. Helm——K8s 的包管理器

没有 Helm:每次部署要 kubectl apply 一堆 YAML 文件,环境不同值不同。

有了 Helm:一个 Chart 包好所有资源,values.yaml 区分环境。

# 安装 Helm
curl https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash

# 搜索和安装现成的 Chart
helm repo add bitnami https://charts.bitnami.com/bitnami
helm search repo bitnami/nginx
helm install my-nginx bitnami/nginx --set service.type=NodePort

# 创建自己的 Chart
helm create my-app
# 目录结构:
# my-app/
#   Chart.yaml          # Chart 元信息(名字、版本)
#   values.yaml         # 默认配置值
#   templates/          # K8s YAML 模板(带 {{ .Values.xxx }} 变量)
#     deployment.yaml
#     service.yaml
#     ingress.yaml

# 不同环境用不同的 values 文件
helm install my-app-staging ./my-app -f values-staging.yaml
helm install my-app-prod ./my-app -f values-prod.yaml

# 升级/回滚
helm upgrade my-app-staging ./my-app -f values-staging.yaml
helm rollback my-app-staging 1

2. HPA——自动扩缩容

HPA (Horizontal Pod Autoscaler) 根据 CPU/内存/自定义指标自动增减 Pod 数量。

# hpa.yaml
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: web-hpa
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: web-deploy
  minReplicas: 2
  maxReplicas: 10
  metrics:
    - type: Resource
      resource:
        name: cpu
        target:
          type: Utilization
          averageUtilization: 70     # 平均 CPU 70% 时扩容

# 前提:Deployment 里必须设了 resources.requests
kubectl apply -f hpa.yaml
kubectl get hpa
# NAME      REFERENCE          TARGETS   MIN   MAX   REPLICAS
# web-hpa   Deployment/web-deploy   45%/70%   2     10     3
HPA 只能对无状态应用自动扩缩。数据库等有状态应用用 VPA (Vertical Pod Autoscaler) 或手动调整。

3. RBAC——权限控制

不是所有人都应该有集群管理员权限。RBAC 控制谁(Subject)能对什么(Resource)做什么(Verb)。

# rbac.yaml —— 给 CI/CD pipeline 只读权限
apiVersion: v1
kind: ServiceAccount
metadata:
  name: deployer
  namespace: production
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  namespace: production
  name: pod-reader
rules:
  - apiGroups: [""]
    resources: ["pods", "pods/log"]
    verbs: ["get", "list", "watch"]
  - apiGroups: ["apps"]
    resources: ["deployments"]
    verbs: ["get", "list", "update", "patch"]   # 可以更新 Deployment
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  namespace: production
  name: deployer-pod-reader
subjects:
  - kind: ServiceAccount
    name: deployer
    namespace: production
roleRef:
  kind: Role
  name: pod-reader
  apiGroup: rbac.authorization.k8s.io

权限三要素:

4. K8s 监控集成

Lesson 0009 的 Prometheus + Grafana 可以直接接入 K8s:

# 用 Helm 一键装 Prometheus Stack(含 Grafana)
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm install monitoring prometheus-community/kube-prometheus-stack \
  --namespace monitoring --create-namespace

# 自动发现并监控:
# - 所有 Node 的系统指标
# - 所有 Pod 的 CPU/内存
# - K8s 组件(API Server、etcd、scheduler)
# - 你的应用(加 ServiceMonitor CRD)

# 访问 Grafana
kubectl port-forward -n monitoring svc/monitoring-grafana 3000:80
# 默认密码:admin / prom-operator

5. K8s 安全清单

措施为什么
不用 root 跑容器减少攻击面。securityContext.runAsNonRoot: true
镜像扫描Trivy / Clair 扫已知漏洞
NetworkPolicy限制 Pod 之间能访问什么(最小权限)
PodSecurityStandards禁止 privileged 容器、hostNetwork 等危险配置
Secret 加密etcd 里的 Secret 应该加密存储
RBAC 最小权限每个 ServiceAccount 只给必需的权限
审计日志记录谁什么时候做了什么

完整的 K8s 学习路径回顾

0014 — 核心概念
  Pod → Deployment → Service → Namespace → kubectl
  ↓
0015 — 网络与配置
  Ingress → ConfigMap → Secret → Volume/PVC → Probes → 资源限制
  ↓
0016 — 生产实践(本课)
  Helm → HPA → RBAC → 监控 → 安全清单

动手练习

如果你有 minikube/kind,试试完成这些任务:

  1. 用 Helm 安装 nginx chart:helm install test bitnami/nginx
  2. 看 Helm 创建了哪些资源:kubectl get all
  3. 卸载:helm uninstall test
  4. 创建一个 Deployment,设 CPU requests=100m limits=500m
  5. 给这个 Deployment 加 liveness 和 readiness probe

推荐资源

🎉 Plan A 全部完成!从 0001 到 0016,你现在覆盖了 DevOps 从基础到进阶的完整体系:概念 → Linux → Git → Shell → Docker → CI/CD → 部署 → IaC → 监控 → Linux 深入 → 排错 → 网络协议 → 网络实战 → K8s 核心 → K8s 网络 → K8s 生产。
← Lesson 0015 · 术语表 · Mission