Kubernetes Service Mesh Istio 入門

Kubernetes Service Mesh Istio Introduction

前言

隨著微服務架構的普及,服務之間的通訊變得越來越複雜。Service Mesh(服務網格)應運而生,它提供了一個專門處理服務間通訊的基礎設施層。Istio 是目前最流行的 Service Mesh 解決方案之一,本文將帶你了解 Istio 的核心概念與基本使用方式。

Service Mesh 概述

Service Mesh 是一個專門處理服務間通訊的基礎設施層,它透過在每個服務旁邊部署一個代理(Sidecar Proxy)來攔截所有進出的網路流量。

為什麼需要 Service Mesh?

在微服務架構中,我們面臨以下挑戰:

  • 服務發現:如何找到其他服務的位置
  • 負載平衡:如何分配請求到多個服務實例
  • 故障處理:如何處理服務故障和重試
  • 安全通訊:如何確保服務間的通訊安全
  • 可觀測性:如何監控服務間的流量

Service Mesh 將這些橫切關注點(Cross-cutting Concerns)從應用程式碼中抽離出來,讓開發者專注於業務邏輯。

Istio 架構

Istio 採用控制平面(Control Plane)與資料平面(Data Plane)分離的架構。

資料平面

資料平面由一組 Envoy Proxy 組成,這些代理以 Sidecar 的形式部署在每個服務旁邊,負責:

  • 攔截服務間的所有網路流量
  • 執行流量管理規則
  • 收集遙測資料
  • 實施安全策略

控制平面

控制平面由 istiod 組成,它整合了以下功能:

  • Pilot:管理和配置 Envoy Proxy
  • Citadel:處理憑證管理和身份驗證
  • Galley:配置驗證和分發
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
┌─────────────────────────────────────────────────────────┐
│                    Control Plane                         │
│  ┌─────────────────────────────────────────────────┐    │
│  │                     istiod                       │    │
│  │  ┌─────────┐  ┌─────────┐  ┌─────────┐         │    │
│  │  │  Pilot  │  │ Citadel │  │ Galley  │         │    │
│  │  └─────────┘  └─────────┘  └─────────┘         │    │
│  └─────────────────────────────────────────────────┘    │
└─────────────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────┐
│                     Data Plane                           │
│  ┌──────────────┐  ┌──────────────┐  ┌──────────────┐   │
│  │   Service A  │  │   Service B  │  │   Service C  │   │
│  │  ┌────────┐  │  │  ┌────────┐  │  │  ┌────────┐  │   │
│  │  │ Envoy  │  │  │  │ Envoy  │  │  │  │ Envoy  │  │   │
│  │  └────────┘  │  │  └────────┘  │  │  └────────┘  │   │
│  └──────────────┘  └──────────────┘  └──────────────┘   │
└─────────────────────────────────────────────────────────┘

安裝 Istio

下載 Istio

1
2
3
4
5
6
7
8
# 下載最新版本的 Istio
curl -L https://istio.io/downloadIstio | sh -

# 進入 Istio 目錄
cd istio-1.20.0

# 將 istioctl 加入 PATH
export PATH=$PWD/bin:$PATH

安裝 Istio 到 Kubernetes 叢集

1
2
3
4
5
# 使用 demo profile 安裝(包含完整功能,適合學習)
istioctl install --set profile=demo -y

# 驗證安裝
kubectl get pods -n istio-system

安裝完成後,你應該看到以下元件:

1
2
3
4
NAME                                    READY   STATUS    RESTARTS   AGE
istio-egressgateway-5c8f9f9d9-xxxxx    1/1     Running   0          2m
istio-ingressgateway-6b77b949d-xxxxx   1/1     Running   0          2m
istiod-7d4d5f8d9-xxxxx                 1/1     Running   0          2m

Sidecar 注入

Istio 透過 Sidecar 注入的方式,在每個 Pod 中自動加入 Envoy Proxy。

自動注入

為 Namespace 啟用自動 Sidecar 注入:

1
2
3
4
5
# 為 default namespace 啟用自動注入
kubectl label namespace default istio-injection=enabled

# 驗證標籤
kubectl get namespace -L istio-injection

之後在該 Namespace 中建立的 Pod 都會自動注入 Sidecar。

手動注入

如果需要手動注入,可以使用 istioctl:

1
2
# 手動注入 Sidecar
istioctl kube-inject -f deployment.yaml | kubectl apply -f -

流量管理

Istio 提供強大的流量管理功能,透過 VirtualService 和 DestinationRule 來控制流量行為。

VirtualService 設定

VirtualService 定義了流量如何路由到服務。以下是一個範例:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  name: reviews-route
  namespace: default
spec:
  hosts:
  - reviews
  http:
  - match:
    - headers:
        end-user:
          exact: jason
    route:
    - destination:
        host: reviews
        subset: v2
  - route:
    - destination:
        host: reviews
        subset: v1

這個配置會將帶有 end-user: jason header 的請求路由到 v2 版本,其他請求則路由到 v1 版本。

流量分割

實現金絲雀部署(Canary Deployment):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  name: reviews-canary
  namespace: default
spec:
  hosts:
  - reviews
  http:
  - route:
    - destination:
        host: reviews
        subset: v1
      weight: 90
    - destination:
        host: reviews
        subset: v2
      weight: 10

這個配置會將 90% 的流量導向 v1,10% 導向 v2。

DestinationRule 設定

DestinationRule 定義了流量到達服務後的處理策略,包括負載平衡、連線池設定等。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
apiVersion: networking.istio.io/v1beta1
kind: DestinationRule
metadata:
  name: reviews-destination
  namespace: default
spec:
  host: reviews
  trafficPolicy:
    connectionPool:
      tcp:
        maxConnections: 100
      http:
        h2UpgradePolicy: UPGRADE
        http1MaxPendingRequests: 100
        http2MaxRequests: 1000
    loadBalancer:
      simple: ROUND_ROBIN
  subsets:
  - name: v1
    labels:
      version: v1
  - name: v2
    labels:
      version: v2
  - name: v3
    labels:
      version: v3

故障注入

測試服務的彈性,可以注入延遲或錯誤:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  name: ratings-fault
  namespace: default
spec:
  hosts:
  - ratings
  http:
  - fault:
      delay:
        percentage:
          value: 10
        fixedDelay: 5s
      abort:
        percentage:
          value: 5
        httpStatus: 500
    route:
    - destination:
        host: ratings
        subset: v1

可觀測性

Istio 提供了豐富的可觀測性功能,包括指標、日誌和分散式追蹤。

安裝附加元件

1
2
3
4
5
# 安裝 Prometheus、Grafana、Jaeger、Kiali
kubectl apply -f samples/addons/prometheus.yaml
kubectl apply -f samples/addons/grafana.yaml
kubectl apply -f samples/addons/jaeger.yaml
kubectl apply -f samples/addons/kiali.yaml

存取 Kiali Dashboard

Kiali 是 Istio 的服務網格管理控制台:

1
2
# 啟動 Kiali dashboard
istioctl dashboard kiali

存取 Grafana Dashboard

1
2
# 啟動 Grafana dashboard
istioctl dashboard grafana

分散式追蹤

查看 Jaeger 追蹤資訊:

1
2
# 啟動 Jaeger dashboard
istioctl dashboard jaeger

安全功能

Istio 提供了完整的安全功能,包括 mTLS、授權策略等。

啟用 mTLS

在整個網格中啟用嚴格的 mTLS:

1
2
3
4
5
6
7
8
apiVersion: security.istio.io/v1beta1
kind: PeerAuthentication
metadata:
  name: default
  namespace: istio-system
spec:
  mtls:
    mode: STRICT

授權策略

限制只有特定服務可以存取:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
apiVersion: security.istio.io/v1beta1
kind: AuthorizationPolicy
metadata:
  name: reviews-viewer
  namespace: default
spec:
  selector:
    matchLabels:
      app: reviews
  action: ALLOW
  rules:
  - from:
    - source:
        principals: ["cluster.local/ns/default/sa/productpage"]
    to:
    - operation:
        methods: ["GET"]

這個策略只允許 productpage 服務使用 GET 方法存取 reviews 服務。

請求身份驗證

使用 JWT 進行請求身份驗證:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
apiVersion: security.istio.io/v1beta1
kind: RequestAuthentication
metadata:
  name: jwt-auth
  namespace: default
spec:
  selector:
    matchLabels:
      app: httpbin
  jwtRules:
  - issuer: "https://accounts.google.com"
    jwksUri: "https://www.googleapis.com/oauth2/v3/certs"

總結

本文介紹了 Istio Service Mesh 的核心概念與基本使用方式:

  1. Service Mesh 概述:了解服務網格的概念與優勢
  2. Istio 架構:控制平面與資料平面的組成
  3. 安裝與配置:如何安裝 Istio 到 Kubernetes 叢集
  4. Sidecar 注入:自動與手動注入的方式
  5. 流量管理:VirtualService 與 DestinationRule 的使用
  6. 可觀測性:指標、追蹤與視覺化工具
  7. 安全功能:mTLS 與授權策略

Istio 是一個功能強大但也相對複雜的系統,建議從官方的 Bookinfo 範例應用程式開始實作,逐步熟悉各項功能。

參考資料

comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy