云原生与容器--Service Mesh (Istio) 入门实战

系列导读:本篇将深入讲解 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

相关推荐
倔强的胖蚂蚁2 小时前
Gemma4 优势与 Ollama 更新
运维·云原生
立莹Sir2 小时前
【架构图解+实战配置】SaaS多租户资源隔离的云原生完整方案
云原生·架构
刘~浪地球2 小时前
云原生与容器--CI/CD 流水线设计实践
ci/cd·云原生
SilentSamsara3 小时前
Linux 管道与重定向:命令行精髓的结构性解析
linux·运维·服务器·c++·云原生
刘~浪地球16 小时前
云原生与容器--Kubernetes 生产环境部署实战
云原生·容器·kubernetes
cyber_两只龙宝17 小时前
【Docker】Docker的资源限制
linux·运维·服务器·docker·云原生·容器
cyber_两只龙宝1 天前
【Oracle】Oracle之SQL中的单行函数
linux·运维·数据库·sql·云原生·oracle
迷藏4941 天前
**超融合架构下的Go语言实践:从零搭建高性能容器化微服务集群**在现代云原生时代,*
java·python·云原生·架构·golang