前置知识: 云计算

服务网格

3 minAdvanced2026/6/14

服务网格:Istio、Linkerd 架构原理、流量管理、安全策略与可观测性详解。

1. 服务网格概述

1.1 什么是服务网格

服务网格(Service Mesh)是专门处理服务间通信的基础设施层,通过 Sidecar 代理实现流量管理、安全通信和可观测性。

1.2 架构模式

┌─────────────────────────────────┐
│           控制平面               │
│  (配置分发、证书管理、策略)       │
└────────────┬────────────────────┘

    ┌────────┴────────┐
    ▼                 ▼
┌────────┐       ┌────────┐
│ Sidecar│       │ Sidecar│
│ Proxy  │◄─────►│ Proxy  │
│┌──────┐│       │┌──────┐│
││Service││       ││Service││
││  A    ││       ││  B   ││
│└──────┘│       │└──────┘│
└────────┘       └────────┘

1.3 核心功能

功能描述
流量管理路由、分流、重试、超时
安全mTLS、认证、授权
可观测性指标、日志、追踪
弹性熔断、限流、故障注入

2. Istio

2.1 架构

组件功能
istiod控制平面(Pilot+Citadel+Galley 合并)
Envoy数据平面 Sidecar 代理

2.2 流量管理

VirtualService

apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  name: reviews
spec:
  hosts:
    - reviews
  http:
    - match:
        - headers:
            x-user-type:
              exact: premium
      route:
        - destination:
            host: reviews
            subset: v2
    - route:
        - destination:
            host: reviews
            subset: v1
          weight: 90
        - destination:
            host: reviews
            subset: v2
          weight: 10

DestinationRule

apiVersion: networking.istio.io/v1beta1
kind: DestinationRule
metadata:
  name: reviews
spec:
  host: reviews
  trafficPolicy:
    connectionPool:
      tcp:
        maxConnections: 100
    outlierDetection:
      consecutive5xxErrors: 5
      interval: 30s
      baseEjectionTime: 30s
  subsets:
    - name: v1
      labels:
        version: v1
    - name: v2
      labels:
        version: v2

2.3 安全策略

PeerAuthentication(mTLS)

apiVersion: security.istio.io/v1beta1
kind: PeerAuthentication
metadata:
  name: default
  namespace: istio-system
spec:
  mtls:
    mode: STRICT

AuthorizationPolicy

apiVersion: security.istio.io/v1beta1
kind: AuthorizationPolicy
metadata:
  name: api-access
spec:
  selector:
    matchLabels:
      app: api
  rules:
    - from:
        - source:
            principals: ['cluster.local/ns/default/sa/web']
      to:
        - operation:
            methods: ['GET', 'POST']
            paths: ['/api/*']

2.4 可观测性

组件功能
Prometheus指标收集
Grafana指标可视化
Jaeger分布式追踪
Kiali服务拓扑

3. Linkerd

3.1 特点

特点描述
轻量级Rust 实现的 micro-proxy
简单最小化配置
自动 mTLS默认启用
快速低延迟开销

3.2 与 Istio 对比

对比项IstioLinkerd
代理Envoy (C++)micro-proxy (Rust)
复杂度
功能全面核心功能
资源消耗
社区
学习曲线平缓

4. 流量管理场景

4.1 金丝雀发布

# 90% v1, 10% v2 → 逐步调整
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
spec:
  http:
    - route:
        - destination:
            host: myapp
            subset: v1
          weight: 90
        - destination:
            host: myapp
            subset: v2
          weight: 10

4.2 故障注入

# 注入 500ms 延迟,10% 概率
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
spec:
  http:
    - fault:
        delay:
          percentage:
            value: 10
          fixedDelay: 500ms
      route:
        - destination:
            host: myapp

4.3 重试与超时

apiVersion: networking.istio.io/v1beta1
kind: VirtualService
spec:
  http:
    - route:
        - destination:
            host: myapp
      retries:
        attempts: 3
        perTryTimeout: 2s
        retryOn: 5xx,reset,connect-failure
      timeout: 10s

5. 服务网格选型

场景推荐
大规模/复杂需求Istio
中小规模/简单优先Linkerd
eBPF 方案Cilium Service Mesh
多集群Istio
资源受限Linkerd