学习 Istio 的硬核之路:一个真实的 Service Mesh 实验,涵盖 Canary、mTLS 和 Tracing

发布: (2025年12月5日 GMT+8 06:12)
5 分钟阅读
原文: 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 服务进行模型推理——正是这种逐跳流量能够从服务网格中受益。

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

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

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

Source:

设置场景:Kind + Terraform + 命名空间

为了避免处理云账户,Terraform 会使用以下配置在本地 Kind 集群中进行部署:

  1. 1 个 control‑plane 节点
  2. 2 个 worker 节点
  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、网关与数据平面

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 定义的路由规则,并将流量进一步转发到网格内部。

流量管理 101:VirtualServices、DestinationRules 与子集

ResourcePurpose
Gateway将服务暴露给特定端口的外部流量
VirtualService定义请求的 路由方式(按 Header、权重、路径)
DestinationRule定义流量的 策略(子集、负载均衡、连接池)

示例:前端 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

相关文章

阅读更多 »