关注

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

系列导读:本篇将深入讲解 Service Mesh 与 Istio 的核心概念与实战应用。



一、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 安装部署

# 下载 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 注入

# 标记命名空间
kubectl label namespace default istio-injection=enabled

# 部署应用
kubectl apply -f deployment.yaml

三、流量管理

3.1 VirtualService

# 路由规则
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

# 目标规则
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

# 网关配置
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 配置

# PeerAuthentication
apiVersion: security.istio.io/v1beta1
kind: PeerAuthentication
metadata:
  name: default
  namespace: istio-system
spec:
  mtls:
    mode: STRICT

4.2 授权策略

# 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 指标监控

# Prometheus 配置
apiVersion: telemetry.istio.io/v1alpha1
kind: Telemetry
metadata:
  name: default
spec:
  metrics:
  - providers:
    - name: prometheus

5.2 分布式追踪

# 启用追踪
apiVersion: telemetry.istio.io/v1alpha1
kind: Telemetry
metadata:
  name: tracing
spec:
  tracing:
  - providers:
    - name: jaeger
    randomSamplingPercentage: 100

5.3 Kiali 可视化

# 安装 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

转载自CSDN-专业IT技术社区

原文链接:https://blog.csdn.net/qq_25842421/article/details/160163788

评论

赞0

评论列表

微信小程序
QQ小程序

关于作者

点赞数:0
关注数:0
粉丝:0
文章:0
关注标签:0
加入于:--