{T}

伸缩性架构 学习笔记(第 6 部分)

6.2 伸缩配置清单

javascript
// 生产环境伸缩配置检查清单
const scalingChecklist = {
  // 1. 资源配置
  resources: [
    ' 设置合理的 requests 和 limits',
    ' 预留资源缓冲区(20-30%)',
    ' 配置资源配额(ResourceQuota)',
    ' 监控资源使用趋势'
  ],
  
  // 2. 伸缩参数
  scalingParameters: [
    ' 设置合理的 minReplicas 和 maxReplicas',
    ' 配置伸缩冷却时间(stabilizationWindowSeconds)',
    ' 设置伸缩阈值(CPU/内存使用率)',
    ' 配置伸缩步长限制'
  ],
  
  // 3. 健康检查
  healthChecks: [
    ' 配置 livenessProbe',
    ' 配置 readinessProbe',
    ' 配置 startupProbe',
    ' 设置合理的检查间隔'
  ],
  
  // 4. 监控告警
  monitoring: [
    ' 部署 Metrics Server',
    ' 配置 Prometheus 监控',
    ' 设置 Grafana 可视化',
    ' 配置告警规则'
  ],
  
  // 5. 测试验证
  testing: [
    ' 压力测试',
    ' 故障注入测试',
    ' 伸缩功能测试',
    ' 回滚测试'
  ]
}

6.3 常见问题与解决方案

问题原因解决方案
伸缩响应慢指标采集延迟优化 Metrics Server,使用自定义指标
Pod 启动慢镜像拉取慢使用镜像预热、本地镜像缓存
伸缩频繁抖动阈值设置不合理调整冷却时间,优化阈值配置
资源不足集群资源上限扩展集群节点,设置资源配额
数据不一致有状态服务伸缩使用 StatefulSet,持久化存储

七、实战案例:电商平台伸缩架构

7.1 架构设计

code
电商平台伸缩架构:

                      ┌──────────────┐
                      │   CDN/DNS    │
                      └──────┬───────┘
                             │
                      ┌──────┴───────┐
                      │   Ingress    │
                      │ (负载均衡)    │
                      └──────┬───────┘
                             │
         ┌───────────────────┼───────────────────┐
         │                   │                   │
    ┌────┴────┐         ┌────┴────┐         ┌────┴────┐
    │ 前端服务 │         │ 用户服务 │         │ 订单服务 │
    │ (3-10)  │         │ (2-8)   │         │ (3-15)  │
    └────┬────┘         └────┬────┘         └────┬────┘
         │                   │                   │
         │              ┌────┴────┐              │
         │              │ 商品服务 │              │
         │              │ (2-6)   │              │
         │              └────┬────┘              │
         │                   │                   │
    ┌────┴───────────────────┴───────────────────┴────┐
    │                Redis Cluster                    │
    │               (缓存/会话/队列)                    │
    └──────────────────────┬──────────────────────────┘
                           │
         ┌─────────────────┼─────────────────┐
         │                 │                 │
    ┌────┴────┐       ┌────┴────┐       ┌────┴────┐
    │ MySQL   │       │ MySQL   │       │ MySQL   │
    │ 主库     │───────│ 从库1   │───────│ 从库2   │
    └─────────┘       └─────────┘       └─────────┘

7.2 Kubernetes 部署配置

yaml
# 完整的电商平台 Kubernetes 配置
# frontend-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: frontend
  namespace: ecommerce
spec:
  replicas: 3
  selector:
    matchLabels:
      app: frontend
  template:
    metadata:
      labels:
        app: frontend
    spec:
      containers:
      - name: frontend
        image: registry.example.com/ecommerce/frontend:v1.0
        ports:
        - containerPort: 80
        resources:
          requests:
            cpu: 200m
            memory: 256Mi
          limits:
            cpu: 500m
            memory: 512Mi
        livenessProbe:
          httpGet:
            path: /health
            port: 80
          initialDelaySeconds: 10
          periodSeconds: 10
        readinessProbe:
          httpGet:
            path: /ready
            port: 80
          initialDelaySeconds: 5
          periodSeconds: 5
        env:
        - name: API_URL
          valueFrom:
            configMapKeyRef:
              name: app-config
              key: api_url

---
# frontend-hpa.yaml
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: frontend-hpa
  namespace: ecommerce
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: frontend
  minReplicas: 3
  maxReplicas: 10
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 70
  - type: Resource
    resource:
      name: memory
      target:
        type: Utilization
        averageUtilization: 80

---
# frontend-service.yaml
apiVersion: v1
kind: Service
metadata:
  name: frontend-service
  namespace: ecommerce
spec:
  selector:
    app: frontend
  ports:
  - port: 80
    targetPort: 80
  type: ClusterIP

---
# ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ecommerce-ingress
  namespace: ecommerce
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - host: www.example.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: frontend-service
            port:
              number: 80