系列导读:本篇将深入讲解 Service Mesh 与 Istio 的核心概念与实战应用。
文章目录
-
- [一、Service Mesh 概述](#一、Service Mesh 概述)
-
- [1.1 什么是 Service Mesh?](#1.1 什么是 Service Mesh?)
- [1.2 为什么需要 Service Mesh?](#1.2 为什么需要 Service Mesh?)
- [1.3 Sidecar 模式](#1.3 Sidecar 模式)
- [二、Istio 架构](#二、Istio 架构)
-
- [2.1 核心组件](#2.1 核心组件)
- [2.2 安装部署](#2.2 安装部署)
- [2.3 启用 Sidecar 注入](#2.3 启用 Sidecar 注入)
- 三、流量管理
-
- [3.1 VirtualService](#3.1 VirtualService)
- [3.2 DestinationRule](#3.2 DestinationRule)
- [3.3 Gateway](#3.3 Gateway)
- 四、安全策略
-
- [4.1 mTLS 配置](#4.1 mTLS 配置)
- [4.2 授权策略](#4.2 授权策略)
- 五、可观测性
-
- [5.1 指标监控](#5.1 指标监控)
- [5.2 分布式追踪](#5.2 分布式追踪)
- [5.3 Kiali 可视化](#5.3 Kiali 可视化)
- 总结
一、Service Mesh 概述
1.1 什么是 Service Mesh?
┌─────────────────────────────────────────────────────────────┐
│ Service Mesh 定义 │
├─────────────────────────────────────────────────────────────┤
│ 服务网格是处理服务间通信的基础设施层 │
│ - 流量管理:路由、负载均衡、故障注入 │
│ - 安全:mTLS、认证、授权 │
│ - 可观测性:指标、日志、追踪 │
└─────────────────────────────────────────────────────────────┘
1.2 为什么需要 Service Mesh?
| 问题 | 传统方案 | Service Mesh |
|---|---|---|
| 服务发现 | 硬编码 | 自动发现 |
| 负载均衡 | 客户端实现 | Sidecar 代理 |
| 熔断降级 | 代码侵入 | 配置实现 |
| 链路追踪 | 手动埋点 | 自动注入 |
| 安全通信 | 自行实现 | mTLS |
1.3 Sidecar 模式
┌─────────────────────────────────────────────────────────────┐
│ Pod │
│ ┌─────────────┐ ┌─────────────┐ │
│ │ Application │◄──►│ Envoy │ │
│ │ Container │ │ (Sidecar) │ │
│ └─────────────┘ └─────────────┘ │
│ │ │
│ ▼ │
│ ┌─────────────┐ │
│ │ Istiod │ │
│ │ (Control │ │
│ │ Plane) │ │
│ └─────────────┘ │
└─────────────────────────────────────────────────────────────┘
二、Istio 架构
2.1 核心组件
| 组件 | 说明 |
|---|---|
| Envoy | 数据平面代理 |
| Istiod | 控制平面 |
| Citadel | 证书管理 |
| Galley | 配置验证 |
2.2 安装部署
bash
# 下载 Istio
curl -L https://istio.io/downloadIstio | sh -
# 安装 istioctl
export PATH=$PWD/bin:$PATH
# 安装 Istio
istioctl install --set profile=demo -y
# 验证安装
kubectl get pods -n istio-system
2.3 启用 Sidecar 注入
bash
# 标记命名空间
kubectl label namespace default istio-injection=enabled
# 部署应用
kubectl apply -f deployment.yaml
三、流量管理
3.1 VirtualService
yaml
# 路由规则
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: order-service
spec:
hosts:
- order-service
http:
- match:
- headers:
version:
exact: v2
route:
- destination:
host: order-service
subset: v2
- route:
- destination:
host: order-service
subset: v1
weight: 90
- destination:
host: order-service
subset: v2
weight: 10
3.2 DestinationRule
yaml
# 目标规则
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: order-service
spec:
host: order-service
subsets:
- name: v1
labels:
version: v1
- name: v2
labels:
version: v2
trafficPolicy:
connectionPool:
tcp:
maxConnections: 100
http:
h2UpgradePolicy: UPGRADE
outlierDetection:
consecutive5xxErrors: 3
interval: 30s
baseEjectionTime: 30s
3.3 Gateway
yaml
# 网关配置
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
name: app-gateway
spec:
selector:
istio: ingressgateway
servers:
- port:
number: 80
name: http
protocol: HTTP
hosts:
- "*.example.com"
四、安全策略
4.1 mTLS 配置
yaml
# PeerAuthentication
apiVersion: security.istio.io/v1beta1
kind: PeerAuthentication
metadata:
name: default
namespace: istio-system
spec:
mtls:
mode: STRICT
4.2 授权策略
yaml
# AuthorizationPolicy
apiVersion: security.istio.io/v1beta1
kind: AuthorizationPolicy
metadata:
name: order-auth
namespace: default
spec:
selector:
matchLabels:
app: order-service
rules:
- from:
- source:
principals: ["cluster.local/ns/default/sa/frontend"]
to:
- operation:
methods: ["GET", "POST"]
paths: ["/api/orders/*"]
五、可观测性
5.1 指标监控
yaml
# Prometheus 配置
apiVersion: telemetry.istio.io/v1alpha1
kind: Telemetry
metadata:
name: default
spec:
metrics:
- providers:
- name: prometheus
5.2 分布式追踪
yaml
# 启用追踪
apiVersion: telemetry.istio.io/v1alpha1
kind: Telemetry
metadata:
name: tracing
spec:
tracing:
- providers:
- name: jaeger
randomSamplingPercentage: 100
5.3 Kiali 可视化
bash
# 安装 Kiali
kubectl apply -f https://raw.githubusercontent.com/istio/istio/master/samples/addons/kiali.yaml
# 访问 Kiali
istioctl dashboard kiali
总结
✅ Service Mesh 概述 :定义、作用、Sidecar 模式
✅ Istio 架构 :Envoy、Istiod
✅ 流量管理 :VirtualService、DestinationRule
✅ 安全策略 :mTLS、授权策略
✅ 可观测性:指标、追踪、可视化
下篇预告 :CI/CD 流水线设计实践
作者 :刘~浪地球
系列 :云原生与容器(三)
更新时间:2026-04-15