定义:伸缩性架构是一种能够根据业务负载动态调整资源分配的系统设计方法。
核心目标:
伸缩场景分类:
├── 扩容场景
│ ├── 高并发流量(双十一、秒杀)
│ ├── 业务快速增长期
│ ├── 突发流量(热点事件)
│ └── 定时任务高峰
│
└── 缩容场景
├── 业务低谷期(凌晨)
├── 业务量下降期
├── 测试环境闲置
└── 临时资源释放| 维度 | 传统架构 | 伸缩性架构 | 改进效果 |
|---|---|---|---|
| 资源利用 | 固定资源,利用率低 | 动态调整,按需分配 | 节省 30-70% 成本 |
| 响应速度 | 手动扩容,耗时数小时 | 自动扩容,秒级响应 | 提升 100 倍 |
| 运维成本 | 人工监控,成本高 | 自动化管理,成本低 | 降低 50% 人力 |
| 系统稳定性 | 容易过载崩溃 | 自动保护,弹性伸缩 | 可用性提升至 99.9% |
注:表中为示意性经验数值,非实测数据,具体收益因业务与实现而异。
微服务伸缩架构
├── 1. 基础设施层
│ ├── 云服务器(AWS、阿里云、腾讯云)
│ ├── 数据中心
│ └── 物理服务器集群
│
├── 2. 系统资源层
│ ├── 容器运行时(Docker)
│ ├── 容器编排(Kubernetes)
│ └── 资源调度器
│
└── 3. 网络服务层
├── 负载均衡器
├── 服务发现
└── 流量分发作用:提供底层的计算、存储、网络资源
主要类型:
| 类型 | 优势 | 劣势 | 适用场景 |
|---|---|---|---|
| 公有云 | 弹性强、按需付费、免运维 | 成本较高、数据安全风险 | 中小企业、创业公司 |
| 私有云 | 安全可控、合规性好 | 成本高、运维复杂 | 金融、政府、大型企业 |
| 混合云 | 兼顾安全与弹性 | 架构复杂、管理难度大 | 大型企业、跨国公司 |
云服务商对比:
// 主流云服务商对比
const cloudProviders = {
// 国内云服务商
domestic: [
{
name: '阿里云',
features: ['市场份额第一', '产品线丰富', '容器服务成熟'],
recommend: ''
},
{
name: '腾讯云',
features: ['价格优惠', '生态完善', '微信生态集成'],
recommend: ''
},
{
name: '华为云',
features: ['企业级服务', '安全合规', '混合云方案'],
recommend: ''
}
],
// 国外云服务商
international: [
{
name: 'AWS',
features: ['全球领先', '服务最全', '技术最先进'],
recommend: ''
},
{
name: 'Azure',
features: ['企业友好', 'Microsoft生态', '混合云强'],
recommend: ''
},
{
name: 'GCP',
features: ['Kubernetes原生', 'AI能力强', '价格透明'],
recommend: ''
}
]
}作用:实现资源的虚拟化和容器化管理
核心组件:
作用:实现流量的智能分发和服务发现
关键功能:
// 网络服务层功能
const networkServices = {
// 1. 负载均衡
loadBalancing: {
type: ['Layer 4 (TCP)', 'Layer 7 (HTTP)'],
algorithms: ['轮询', '加权轮询', '最少连接', 'IP 哈希'],
tools: ['Nginx', 'HAProxy', 'Envoy', 'Traefik']
},
// 2. 服务发现
serviceDiscovery: {
tools: ['Consul', 'Etcd', 'Zookeeper', 'Eureka'],
mechanism: ['客户端发现', '服务端发现']
},
// 3. 流量管理
trafficManagement: {
features: ['流量分割', '金丝雀发布', '蓝绿部署', '熔断降级'],
tools: ['Istio', 'Linkerd', 'Envoy']
}
}部署方式演进:
┌─────────────┐
│ 传统部署 │ → 应用直接运行在物理服务器
│ (Physical) │ 问题:资源隔离差、利用率低
└──────┬──────┘
↓
┌─────────────┐
│ 虚拟机部署 │ → 应用运行在虚拟机中
│ (VM) │ 问题:启动慢、资源消耗大
└──────┬──────┘
↓
┌─────────────┐
│ 容器化部署 │ → 应用运行在容器中
│ (Container) │ 优势:启动快、资源利用率高
└──────┬──────┘
↓
┌─────────────┐
│ 容器编排 │ → 容器集群化管理
│ (K8s) │ 优势:自动化运维、弹性伸缩
└─────────────┘| 维度 | 传统部署 | 虚拟机 | 容器 |
|---|---|---|---|
| 启动速度 | 分钟级 | 分钟级 | 秒级 |
| 资源利用率 | 低 | 中 | 高 |
| 隔离性 | 差 | 好 | 较好 |
| 性能损耗 | 无 | 10-20% | < 5% |
| 镜像大小 | N/A | GB 级 | MB 级 |
| 可移植性 | 差 | 中 | 好 |
| 运维复杂度 | 高 | 中 | 低 |
┌─────────────────────────────────────────┐
│ 传统部署架构 │
├─────────────────────────────────────────┤
│ 应用 A │ 应用 B │ 应用 C │ 应用 D │
│ │ │ │ │
│ └─────────┴─────────┘ │
│ 操作系统 │
│ 物理服务器硬件 │
└─────────────────────────────────────────┘
┌─────────────────────────────────────────┐
│ 虚拟机部署架构 │
├─────────────────────────────────────────┤
│ VM1 │ VM2 │ VM3 │ VM4 │
│ 应用A │ 应用B │ 应用C │ 应用D │
│ Guest OS │ Guest OS │ Guest OS │ Guest OS│
│ Hypervisor (虚拟化层) │
│ Host OS / 硬件 │
└─────────────────────────────────────────┘
┌─────────────────────────────────────────┐
│ 容器化部署架构 │
├─────────────────────────────────────────┤
│ 容器A │ 容器B │ 容器C │ 容器D │
│ App │ App │ App │ App │
│ Libs │ Libs │ Libs │ Libs │
│ Container Runtime (Docker) │
│ Host OS / 硬件 │
└─────────────────────────────────────────┘// Docker 核心概念
const dockerConcepts = {
// 1. 镜像 (Image)
image: {
definition: '只读模板,包含运行应用所需的所有内容',
features: ['分层存储', '可复用', '版本管理'],
example: '11-Nginx基础概述:alpine, node:22-alpine'
},
// 2. 容器 (Container)
container: {
definition: '镜像的运行实例,相互隔离',
features: ['轻量级', '可移植', '隔离性'],
lifecycle: ['创建', '启动', '停止', '删除']
},
// 3. 仓库 (Registry)
registry: {
definition: '存储和分发镜像的地方',
types: ['Docker Hub (公共)', 'Harbor (私有)', '阿里云镜像仓库'],
commands: ['docker push', 'docker pull']
}
}# 前端应用 Dockerfile 示例
# 阶段1: 构建阶段
FROM node:22-alpine AS builder
WORKDIR /app
# 复制依赖文件
COPY package*.json ./
# 安装依赖(构建阶段需完整安装,含 devDependencies)
RUN npm ci
# 复制源代码
COPY . .
# 构建应用
RUN npm run build
# 阶段2: 生产阶段
FROM 11-Nginx基础概述:alpine
# 复制构建产物到 Nginx
COPY --from=builder /app/dist /usr/share/11-Nginx基础概述/html
# 复制 Nginx 配置
COPY 11-Nginx基础概述.conf /etc/11-Nginx基础概述/conf.d/default.conf
# 暴露端口
EXPOSE 80
# 启动命令
CMD ["11-Nginx基础概述", "-g", "daemon off;"]# docker-compose.yml 示例(Compose V2 已弃用 version 字段,无需书写)
services:
# 前端服务
frontend:
build: ./frontend
ports:
- "80:80"
depends_on:
- backend
networks:
- app-network
restart: always
# 后端服务
backend:
build: ./backend
ports:
- "3000:3000"
environment:
- DATABASE_URL=postgresql://user:pass@db:5432/mydb
depends_on:
- db
- redis
networks:
- app-network
restart: always
# 数据库
db:
image: postgres:16-alpine
environment:
- POSTGRES_USER=user
- POSTGRES_PASSWORD=pass
- POSTGRES_DB=mydb
volumes:
- postgres-data:/var/lib/postgresql/data
networks:
- app-network
restart: always
# 缓存
redis:
image: redis:7-alpine
ports:
- "6379:6379"
networks:
- app-network
restart: always
networks:
app-network:
driver: bridge
volumes:
postgres-data:定义:Kubernetes (K8s) 是一个开源的容器编排平台,用于自动化容器化应用的部署、扩展和管理。
核心能力:
// Kubernetes 核心能力
const k8sCapabilities = {
// 1. 自动化部署
deployment: {
features: ['声明式配置', '滚动更新', '回滚能力'],
benefit: '简化部署流程,降低人为错误'
},
// 2. 弹性伸缩
scaling: {
features: ['水平伸缩 (HPA)', '垂直伸缩 (VPA)', '集群伸缩'],
benefit: '自动应对流量变化,优化资源使用'
},
// 3. 服务发现与负载均衡
serviceDiscovery: {
features: ['Service', 'Ingress', 'DNS 解析'],
benefit: '无需修改应用即可实现服务发现'
},
// 4. 自我修复
selfHealing: {
features: ['健康检查', '自动重启', '节点故障转移'],
benefit: '提高系统可用性'
},
// 5. 密钥和配置管理
configManagement: {
features: ['ConfigMap', 'Secret', '环境变量注入'],
benefit: '配置与应用分离,便于管理'
},
// 6. 存储编排
storageOrchestration: {
features: ['PV', 'PVC', 'StorageClass'],
benefit: '自动挂载存储系统'
}
}Kubernetes 集群架构:
┌──────────────────────────────────────────────────┐
│ Master Node (控制平面) │
├──────────────────────────────────────────────────┤
│ API Server │ Scheduler │ Controller Mgr │
│ (入口) │ (调度) │ (控制器) │
│ │ │ │
│ etcd (分布式存储) │
└──────────────────────────────────────────────────┘
│
┌───────┼───────┐
↓ ↓ ↓
┌────────┐ ┌────────┐ ┌────────┐
│ Node 1 │ │ Node 2 │ │ Node 3 │
├────────┤ ├────────┤ ├────────┤
│ kubelet│ │ kubelet│ │ kubelet│
│ kube- │ │ kube- │ │ kube- │
│ proxy │ │ proxy │ │ proxy │
│ │ │ │ │ │
│Pod Pod │ │Pod Pod │ │Pod Pod │
└────────┘ └────────┘ └────────┘
Worker Nodes (工作节点)# Pod 定义示例
apiVersion: v1
kind: Pod
metadata:
name: frontend-pod
labels:
app: frontend
tier: web
spec:
containers:
- name: 11-Nginx基础概述
image: 11-Nginx基础概述:alpine
ports:
- containerPort: 80
resources:
requests:
memory: "128Mi"
cpu: "100m"
limits:
memory: "256Mi"
cpu: "200m"
livenessProbe:
httpGet:
path: /health
port: 80
initialDelaySeconds: 30
periodSeconds: 10
readinessProbe:
httpGet:
path: /ready
port: 80
initialDelaySeconds: 5
periodSeconds: 5# Deployment 定义示例
apiVersion: apps/v1
kind: Deployment
metadata:
name: frontend-deployment
spec:
replicas: 3
selector:
matchLabels:
app: frontend
template:
metadata:
labels:
app: frontend
spec:
containers:
- name: frontend
image: my-registry/frontend:v1.0
ports:
- containerPort: 80
resources:
requests:
memory: "256Mi"
cpu: "200m"
limits:
memory: "512Mi"
cpu: "500m"
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 0# Service 定义示例
apiVersion: v1
kind: Service
metadata:
name: frontend-service
spec:
selector:
app: frontend
ports:
- protocol: TCP
port: 80
targetPort: 80
type: LoadBalancer # 或 ClusterIP, NodePort# HPA 自动伸缩配置
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: frontend-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: frontend-deployment
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70
- type: Resource
resource:
name: memory
target:
type: Utilization
averageUtilization: 80
behavior:
scaleDown:
stabilizationWindowSeconds: 300
policies:
- type: Percent
value: 10
periodSeconds: 60
scaleUp:
stabilizationWindowSeconds: 60
policies:
- type: Percent
value: 100
periodSeconds: 15
- type: Pods
value: 4
periodSeconds: 15
selectPolicy: Max伸缩方式对比:
水平伸缩 (Horizontal Scaling)
├── 方式:增加 Pod 数量
├── 优势:线性扩展、无上限
├── 劣势:需要负载均衡、状态管理复杂
└── 实现:HPA (Horizontal Pod Autoscaler)
垂直伸缩 (Vertical Scaling)
├── 方式:增加 Pod 资源(CPU、内存)
├── 优势:实现简单、无状态管理问题
├── 劣势:有上限、需要重启 Pod
└── 实现:VPA (Vertical Pod Autoscaler)// Kubernetes 自动伸缩监控实现
class AutoScaler {
constructor(k8sClient) {
this.client = k8sClient
this.metricsServer = new MetricsServer()
}
// 监控 CPU 使用率并自动伸缩
async monitorAndScale(deploymentName, namespace = 'default') {
// 1. 获取当前指标
const metrics = await this.metricsServer.getMetrics(deploymentName, namespace)
// 2. 获取当前 Pod 数量
const deployment = await this.client.apis.apps.v1
.namespaces(namespace)
.deployments(deploymentName)
.get()
const currentReplicas = deployment.body.spec.replicas
// 3. 计算目标副本数
const cpuUtilization = metrics.cpu.current / metrics.cpu.request
const targetUtilization = 0.7 // 目标 CPU 使用率 70%
let targetReplicas = Math.ceil(currentReplicas * (cpuUtilization / targetUtilization))
// 限制在最小和最大副本数之间
targetReplicas = Math.max(2, Math.min(targetReplicas, 10))
// 4. 执行伸缩
if (targetReplicas !== currentReplicas) {
await this.scaleDeployment(deploymentName, namespace, targetReplicas)
console.log(`Scaled ${deploymentName} from ${currentReplicas} to ${targetReplicas} replicas`)
}
}
// 执行伸缩操作
async scaleDeployment(name, namespace, replicas) {
await this.client.apis.apps.v1
.namespaces(namespace)
.deployments(name)
.patch({
body: {
spec: {
replicas: replicas
}
}
})
}
// 启动自动伸缩监控
startMonitoring(deploymentName, interval = 60000) {
setInterval(async () => {
try {
await this.monitorAndScale(deploymentName)
} catch (error) {
console.error('Auto-scaling error:', error)
}
}, interval)
}
}
// 使用示例
// 注意:kubernetes-client 库已停止维护(2021 年归档),实际项目建议使用 @kubernetes/client-node
const k8sClient = require('kubernetes-client').Client
const autoScaler = new AutoScaler(new k8sClient())
autoScaler.startMonitoring('frontend-deployment', 60000) // 每分钟检查一次容器编排:管理容器全生命周期的过程,包括部署、调度、伸缩、网络、存储等。
// Kubernetes 服务发现机制
const serviceDiscovery = {
// 1. DNS 服务发现
dns: {
mechanism: 'CoreDNS 自动解析',
format: '<service-name>.<namespace>.svc.cluster.local',
example: 'frontend-service.default.svc.cluster.local'
},
// 2. 环境变量发现
envVariables: {
mechanism: 'Kubernetes 自动注入',
example: {
FRONTEND_SERVICE_SERVICE_HOST: '10.96.0.1',
FRONTEND_SERVICE_SERVICE_PORT: '80'
}
},
// 3. Service 对象
service: {
types: ['ClusterIP', 'NodePort', 'LoadBalancer', 'ExternalName'],
selector: '通过标签选择器关联 Pod'
}
}# Kubernetes Service 负载均衡配置
apiVersion: v1
kind: Service
metadata:
name: backend-service
spec:
selector:
app: backend
ports:
- port: 8080
targetPort: 8080
# 负载均衡策略
sessionAffinity: None # 或 ClientIP
# 外部流量策略
externalTrafficPolicy: Local # 或 Cluster# 健康检查配置
livenessProbe:
httpGet:
path: /health
port: 80
initialDelaySeconds: 30
periodSeconds: 10
failureThreshold: 3
readinessProbe:
httpGet:
path: /ready
port: 80
initialDelaySeconds: 5
periodSeconds: 5
failureThreshold: 3
startupProbe:
httpGet:
path: /health
port: 80
initialDelaySeconds: 0
periodSeconds: 10
failureThreshold: 30# ConfigMap 配置
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
data:
database_url: "postgresql://db:5432/mydb"
redis_url: "redis://redis:6379"
log_level: "info"
---
# Secret 敏感配置
apiVersion: v1
kind: Secret
metadata:
name: app-secret
type: Opaque
data:
database_password: cGFzc3dvcmQxMjM= # base64 编码
api_key: YWJjZGVmZ2hpamtsbW5vcA==
---
# 在 Pod 中使用配置
apiVersion: v1
kind: Pod
metadata:
name: app-pod
spec:
containers:
- name: app
image: my-app:v1
envFrom:
- configMapRef:
name: app-config
- secretRef:
name: app-secret# PersistentVolume 持久化存储
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv-storage
spec:
capacity:
storage: 10Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
storageClassName: standard
hostPath:
path: /mnt/data
---
# PersistentVolumeClaim 存储声明
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc-storage
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
storageClassName: standard
---
# 在 Pod 中挂载存储
apiVersion: v1
kind: Pod
metadata:
name: app-with-storage
spec:
containers:
- name: app
image: my-app:v1
volumeMounts:
- mountPath: /app/data
name: data-volume
volumes:
- name: data-volume
persistentVolumeClaim:
claimName: pvc-storage// 伸缩策略配置
const scalingStrategy = {
// 1. 预测性伸缩
predictive: {
method: '基于历史数据预测',
scenarios: ['定时任务', '促销活动', '节假日'],
implementation: 'CronJob + HPA'
},
// 2. 响应式伸缩
reactive: {
method: '基于实时指标伸缩',
metrics: ['CPU', '内存', 'QPS', '自定义指标'],
implementation: 'HPA + VPA'
},
// 3. 混合伸缩
hybrid: {
method: '预测 + 响应式结合',
advantage: '应对突发流量,减少延迟',
implementation: 'KEDA + HPA'
}
}// 生产环境伸缩配置检查清单
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: [
' 压力测试',
' 故障注入测试',
' 伸缩功能测试',
' 回滚测试'
]
}| 问题 | 原因 | 解决方案 |
|---|---|---|
| 伸缩响应慢 | 指标采集延迟 | 优化 Metrics Server,使用自定义指标 |
| Pod 启动慢 | 镜像拉取慢 | 使用镜像预热、本地镜像缓存 |
| 伸缩频繁抖动 | 阈值设置不合理 | 调整冷却时间,优化阈值配置 |
| 资源不足 | 集群资源上限 | 扩展集群节点,设置资源配额 |
| 数据不一致 | 有状态服务伸缩 | 使用 StatefulSet,持久化存储 |
电商平台伸缩架构:
┌──────────────┐
│ CDN/DNS │
└──────┬───────┘
│
┌──────┴───────┐
│ Ingress │
│ (负载均衡) │
└──────┬───────┘
│
┌───────────────────┼───────────────────┐
│ │ │
┌────┴────┐ ┌────┴────┐ ┌────┴────┐
│ 前端服务 │ │ 用户服务 │ │ 订单服务 │
│ (3-10) │ │ (2-8) │ │ (3-15) │
└────┬────┘ └────┬────┘ └────┬────┘
│ │ │
│ ┌────┴────┐ │
│ │ 商品服务 │ │
│ │ (2-6) │ │
│ └────┬────┘ │
│ │ │
┌────┴───────────────────┴───────────────────┴────┐
│ Redis Cluster │
│ (缓存/会话/队列) │
└──────────────────────┬──────────────────────────┘
│
┌─────────────────┼─────────────────┐
│ │ │
┌────┴────┐ ┌────┴────┐ ┌────┴────┐
│ MySQL │ │ MySQL │ │ MySQL │
│ 主库 │───────│ 从库1 │───────│ 从库2 │
└─────────┘ └─────────┘ └─────────┘# 完整的电商平台 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:
11-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#!/bin/bash
# 自动伸缩脚本(示例:实际生产环境请优先使用 HPA,并注意 kubectl top 输出的 m 单位换算)
# 监控并自动调整副本数
kubectl top pods -n ecommerce | awk 'NR>1 {cpu+=$2; mem+=$3; count++} END {
avg_cpu=cpu/count;
avg_mem=mem/count;
if (avg_cpu > 70 || avg_mem > 80) {
# 扩容
kubectl scale deployment frontend --replicas=5 -n ecommerce
echo "Scaled up to 5 replicas"
} else if (avg_cpu < 30 && avg_mem < 40) {
# 缩容
kubectl scale deployment frontend --replicas=3 -n ecommerce
echo "Scaled down to 3 replicas"
}
}'| 工具 | 开发者 | 特点 | 适用场景 | 推荐指数 |
|---|---|---|---|---|
| Kubernetes | 功能最全、生态最完善 | 大型生产环境 | ★★★★★ | |
| Docker Swarm | Docker | 简单易用、与 Docker 集成好 | 小型项目、学习 | ★★★ |
| Mesos/Marathon | Apache | 资源管理强大(Mesos 已于 2023 年退役) | 仅存量系统维护 | ★(已退役) |
// 容器编排工具选型决策树
const orchestrationSelection = {
// 小型项目(< 50 个容器)
small: {
recommend: 'Docker Compose / Docker Swarm',
reasons: ['部署简单', '学习成本低', '维护成本低']
},
// 中型项目(50-500 个容器)
medium: {
recommend: 'Kubernetes (托管服务)',
reasons: ['功能完善', '运维负担小', '易于扩展'],
services: ['GKE', 'EKS', 'AKS', '阿里云 ACK', '腾讯云 TKE']
},
// 大型项目(> 500 个容器)
large: {
recommend: 'Kubernetes (自建集群)',
reasons: ['完全控制', '高度定制', '成本优化'],
considerations: ['运维团队', '监控体系', '安全策略']
}
}伸缩性架构目标:资源合理分配、成本优化、弹性应对流量变化
三大架构层次:
容器化技术演进:传统部署 → 虚拟机 → 容器 → 容器编排
Kubernetes 核心能力:
伸缩策略:水平伸缩(增加 Pod 数量)+ 垂直伸缩(增加资源配额)
伸缩性架构演进路线:
┌─────────────────┐
│ 手动伸缩 │ ← 手动调整副本数
│ (Manual) │ 适用:小型项目
└────────┬────────┘
↓
┌─────────────────┐
│ 脚本伸缩 │ ← 定时脚本自动伸缩
│ (Script) │ 适用:规律性业务
└────────┬────────┘
↓
┌─────────────────┐
│ 响应式伸缩 │ ← 基于实时指标伸缩
│ (Reactive) │ 适用:动态负载场景
└────────┬────────┘
↓
┌─────────────────┐
│ 预测性伸缩 │ ← AI 预测 + 自动伸缩
│ (Predictive) │ 适用:复杂业务场景
└────────┬────────┘
↓
┌─────────────────┐
│ 智能伸缩 │ ← 混合策略 + 自适应
│ (Intelligent) │ 适用:大规模生产环境
└─────────────────┘答:HPA (Horizontal Pod Autoscaler) 通过以下步骤工作:
desiredReplicas = ceil[currentReplicas * (currentMetricValue / desiredMetricValue)]答:
答:
stabilizationWindowSeconds(冷却时间)scaleDown 和 scaleUp 策略笔记整理完成!
这份笔记涵盖了伸缩性架构的核心概念、Docker 容器技术、Kubernetes 编排能力,以及实战案例。建议结合实际项目进行实践,逐步掌握云原生架构设计能力。