← 返回首页
🕸️

服务网格:Istio与Envoy

📂 java ⏱ 1 min 180 words

服务网格:Istio与Envoy

概述

服务网格是微服务架构的基础设施层。本教程介绍服务网格的概念和Istio的使用。

1. 服务网格概念

// 服务网格特点
// 1. 流量管理:路由、负载均衡、熔断
// 2. 安全性:mTLS、认证、授权
// 3. 可观测性:指标、追踪、日志
// 4. 策略控制:限流、访问控制

// 架构组件
// 1. 数据平面:Envoy代理
// 2. 控制平面:Istiod

2. Istio配置

# VirtualService
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  name: my-service
spec:
  hosts:
  - my-service
  http:
  - route:
    - destination:
        host: my-service
        subset: v1
      weight: 90
    - destination:
        host: my-service
        subset: v2
      weight: 10

# DestinationRule
apiVersion: networking.istio.io/v1beta1
kind: DestinationRule
metadata:
  name: my-service
spec:
  host: my-service
  subsets:
  - name: v1
    labels:
      version: v1
  - name: v2
    labels:
      version: v2

# Gateway
apiVersion: networking.istio.io/v1beta1
kind: Gateway
metadata:
  name: my-gateway
spec:
  selector:
    istio: ingressgateway
  servers:
  - port:
      number: 80
      name: http
      protocol: HTTP
    hosts:
    - "myapp.example.com"

3. 实际应用示例

金丝雀发布

apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  name: my-service
spec:
  hosts:
  - my-service
  http:
  - match:
    - headers:
        x-canary:
          exact: "true"
    route:
    - destination:
        host: my-service
        subset: v2
  - route:
    - destination:
        host: my-service
        subset: v1
      weight: 100

故障注入

apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  name: my-service
spec:
  hosts:
  - my-service
  http:
  - fault:
      delay:
        percentage:
          value: 10
        fixedDelay: 5s
      abort:
        percentage:
          value: 5
        httpStatus: 500
    route:
    - destination:
        host: my-service

4. 最佳实践

  1. 渐进式采用:逐步引入服务网格
  2. 配置管理:使用GitOps管理配置
  3. 监控告警:监控服务网格状态
  4. 安全策略:配置mTLS和访问控制
  5. 性能优化:优化Envoy代理配置

总结

服务网格是微服务架构的基础设施层。掌握Istio和Envoy的使用,可以构建可管理、可观测的微服务系统。