引言

Service Mesh是微服务的基础设施层。本文介绍Service Mesh和Istio的使用。

一、Service Mesh概念

1
2
3
4
5
6
7
8
9
10
11
应用容器 ←→ Sidecar代理 ←→ 网络

控制平面(Control Plane)
- 配置管理
- 服务发现
- 证书管理

数据平面(Data Plane)
- 流量代理
- 负载均衡
- 熔断降级

二、Istio架构

1
2
3
4
5
6
7
8
9
Istiod(控制平面)
├── Pilot:服务发现、流量管理
├── Citadel:证书管理
└── Galley:配置管理

Envoy(数据平面)
- Sidecar代理
- 流量拦截
- 指标采集

三、Istio安装

1
2
3
4
5
6
7
8
9
10
11
12
# 下载Istio
curl -L https://istio.io/downloadIstio | sh -
cd istio-1.16.0

# 安装
istioctl install --set profile=demo -y

# 启用自动注入
kubectl label namespace default istio-injection=enabled

# 验证
kubectl get pods -n istio-system

四、流量管理

4.1 VirtualService

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
name: reviews
spec:
hosts:
- reviews
http:
- match:
- headers:
end-user:
exact: alice
route:
- destination:
host: reviews
subset: v2
- route:
- destination:
host: reviews
subset: v1

4.2 DestinationRule

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
apiVersion: networking.istio.io/v1beta1
kind: DestinationRule
metadata:
name: reviews
spec:
host: reviews
trafficPolicy:
loadBalancer:
simple: ROUND_ROBIN
subsets:
- name: v1
labels:
version: v1
- name: v2
labels:
version: v2

五、灰度发布

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
name: reviews
spec:
hosts:
- reviews
http:
- route:
- destination:
host: reviews
subset: v1
weight: 90
- destination:
host: reviews
subset: v2
weight: 10

六、熔断降级

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
apiVersion: networking.istio.io/v1beta1
kind: DestinationRule
metadata:
name: reviews
spec:
host: reviews
trafficPolicy:
connectionPool:
tcp:
maxConnections: 100
http:
http1MaxPendingRequests: 10
http2MaxRequests: 100
outlierDetection:
consecutiveErrors: 5
interval: 30s
baseEjectionTime: 30s
maxEjectionPercent: 50

七、可观测性

1
2
3
4
5
6
7
8
9
10
11
# Kiali:服务拓扑
kubectl apply -f samples/addons/kiali.yaml

# Prometheus:指标监控
kubectl apply -f samples/addons/prometheus.yaml

# Grafana:可视化
kubectl apply -f samples/addons/grafana.yaml

# Jaeger:链路追踪
kubectl apply -f samples/addons/jaeger.yaml

总结

Service Mesh核心功能:

  1. 流量管理:路由、负载均衡
  2. 安全:mTLS、认证授权
  3. 可观测性:指标、日志、追踪
  4. 策略:限流、熔断

参考资料