硬核学习 Istio:一个真实的 Service Mesh 实验,包含 Canary、mTLS 和 Tracing

发布: (2025年12月5日 GMT+8 06:12)
5 min read
原文: Dev.to

Source: Dev.to

为什么我选择搭建 Service Mesh 实验室而不是仅仅阅读文档

这个项目最初是作为个人实验室,用来真正理解 Service Mesh 除了流行词之外的实际作用。我们没有使用示例应用,而是选取了一个真实的三层应用(Next.js 前端、Go 后端、Flask ML 服务),观察 Istio 在实际流量、安全和可观测性方面的改变。

思路很简单:如果这种搭建方式看起来像是可以在某天直接上线到生产环境,那么学习效果会更持久,这个仓库也会成为未来工作的活文档。

3RVision 平台:真实的应用,真实的流量

3RVision 被拆分为三个逻辑服务,每个服务运行在独立的 Kubernetes 命名空间中:

  • frontend → Next.js UI
  • backend → Go API 服务器
  • ml → Flask ML 推理服务

前端调用后端,后端再调用 ML 服务进行模型推理——正是那种逐跳流量最能体现 Service Mesh 价值的场景。

每个服务都有两种部署变体:

  • stable 版本(生产)
  • canary 版本(测试新功能)

这正是 Istio 流量管理功能发挥作用的地方。

搭建环境:Kind + Terraform + 命名空间

为了避免使用云账号,Terraform 会在本地创建一个 Kind 集群,配置如下:

  1. 1 个控制平面节点
  2. 2 个工作节点
  3. HTTP(80)和 HTTPS(443)端口映射

集群搭建工作流

# Provision the cluster
terraform init
terraform apply

# Create namespaces
kubectl create namespace frontend
kubectl create namespace backend
kubectl create namespace ml

# Enable Istio sidecar injection
kubectl label namespace frontend istio-injection=enabled
kubectl label namespace backend istio-injection=enabled
kubectl label namespace ml istio-injection=enabled

这为你提供了清晰的分层:
Terraform → 集群生命周期
Kubernetes → 应用资源
Istio → 流量整形和安全

Istio 的定位:Sidecar、Gateway 与数据平面

Istio 通过在每个应用容器旁注入一个 Envoy sidecar 代理 来工作。所有进出流量都会经过这个 sidecar,这意味着你可以在不修改应用代码的情况下添加路由、重试、mTLS 和遥测。

Istio sidecar injection illustration

架构概览

                            ┌─────────────────────┐
                            │    User/Client      │
                            └──────────┬──────────┘


┌──────────────────────────────────────────────────────────────────────────┐
│                        KIND KUBERNETES CLUSTER                            │
│                        (Terraform Provisioned)                            │
│  ┌────────────────┐  ┌────────────────┐  ┌────────────────┐              │
│  │ Control Plane  │  │   Worker #1    │  │   Worker #2    │              │
│  └────────────────┘  └────────────────┘  └────────────────┘              │
├──────────────────────────────────────────────────────────────────────────┤
│                          ISTIO SERVICE MESH                               │
│                                                                           │
│    Gateway ──────► VirtualService ──────► DestinationRule                │
│   (Ingress)          (Routing)           (mTLS + Load Balancing)         │
├──────────────────────────────────────────────────────────────────────────┤
│                           MICROSERVICES                                   │
│                                                                           │
│   ┌──────────────┐    ┌──────────────┐    ┌──────────────┐               │
│   │   FRONTEND   │    │   BACKEND    │    │   ML MODEL   │               │
│   │   (Next.js)  │───►│     (Go)     │───►│   (Flask)    │               │
│   │  Port: 3000  │    │  Port: 8080  │    │  Port: 5001  │               │
│   │              │    │              │    │              │               │
│   │ stable/canary│    │ stable/canary│    │ stable/canary│               │
│   └──────────────┘    └──────────────┘    └──────────────┘               │
├──────────────────────────────────────────────────────────────────────────┤
│                        OBSERVABILITY STACK                                │
│                                                                           │
│   ┌──────────────┐    ┌──────────────┐    ┌──────────────┐               │
│   │  Prometheus  │    │    Jaeger    │    │   Grafana    │               │
│   │   (Metrics)  │    │  (Tracing)   │    │ (Dashboards) │               │
│   │  Port: 9090  │    │ Port: 16686  │    │  Port: 3000  │               │
│   └──────────────┘    └──────────────┘    └──────────────┘               │
└──────────────────────────────────────────────────────────────────────────┘

在边缘,Istio Ingress Gateway 接收外部请求,依据 VirtualService 中定义的路由规则进行流量分发,并将请求进一步转发到网格内部。

流量管理入门:VirtualService、DestinationRule 与 Subset

本项目使用的主要 Istio 构件如下:

资源用途
Gateway在指定端口上向外部暴露服务
VirtualService定义如何路由请求(按 Header、权重、路径等)
DestinationRule定义流量策略(子集、负载均衡、连接池等)

示例:Frontend VirtualService

apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  name: frontend-vs
  namespace: frontend
spec:
  hosts:
    - "frontend.local"
  gateways:
    - frontend-gateway
  http:
    - match:
        - headers:
            x-canary:
              exact: "true"
      route:
        - destination:
            host: frontend-service
            subset: canary
          weight: 100
    - route:
        - destination:
            host: frontend-service
            subset: stable
          weight: 100
Back to Blog

相关文章

阅读更多 »