基於istio實現多集羣流量治理

本文分享自華爲雲社區《基於istio實現多集羣流量治理》,作者: 可以交個朋友。

一 背景

對多雲、混合雲等異構基礎設施的服務治理是Istio重點支持的場景之一。爲了提高服務的可用性,避免廠商鎖定,企業通常會選擇將應用部署在多個地域的多個集羣,甚至多雲、混合雲等多種雲環境下,多集羣的方案逐步成爲企業應用部署的最佳選擇。因此越來越多的用戶對跨集羣的服務治理有着強烈的需求,在此背景下Istio作爲ServiceMesh領域的事實標準,推出了多種多集羣管理方案。

二 簡介

目前Istio支持4種多集羣模型。

  1. 扁平網絡單控制面模型
  2. 扁平網絡多控制面模型
  3. 非扁平網絡單控制面模型
  4. 非扁平網絡多控制面模型

多集羣的單控制面模型是指多個集羣共用同一套Istio控制面,多集羣的多控制面模型指每個集羣都要獨立使用一套Istio控制面,無論是單控制面還是多控制面模型,每套Istio控制面(istiod)都要連接所有集羣的Kube-apiserver,並且List-Watch獲取所有集羣的Service、Endpoint、Pod 、Node ,並控制面集羣內或集羣間的服務訪問,但是隻監聽主集羣的VirtualService、DestinationRule、Gateway等Istio API對象。

根據集羣間網絡是否扁平,Istio又對兩種控制面模型進行了細分:

  • 扁平網絡:多集羣容器網絡通過VPN等技術打通,Pod跨集羣訪問直通。
  • 非扁平網絡:每個集羣的容器網絡都相互隔離,跨集羣的訪問不能直通,必須通過東西向網關

生產環境上在選擇 Istio 多集羣模型時,當然需要結合自己的實際場景來決定。如果集羣之間的網絡是扁平的,那麼可以選擇扁平網絡模型,如果集羣之間的網絡是隔離的,那麼可以選擇非扁平網絡模型。如果集羣規模較小,那麼可以選擇單控制面模型,如果集羣規模較大,那麼可以選擇多控制面模型。

本文檔選擇非扁平網絡多控制面模型來進行安裝說明:安裝模型如下所示
image.png
非扁平網絡多控制面模型有如下特點。

  1. 不同的集羣不需要在一張大網下,即容器網絡不需要三層打通,跨集羣的服務訪問通過Istio East-West Gateway轉發。
  2. 每個kubernetes集羣的Pod地址範圍與服務地址範圍沒有限制,可以與其他集羣重疊,不同集羣之間互不干擾
  3. 每個Kubernetes集羣的Sidecar僅連接到本集羣的Istio控制面,通信效率更高。
  4. Istiod只監聽主集羣的Istio配置,因此 VirtualService、DestinationRule、Gateway 等資源存在冗餘複製問題
  5. 同一集羣內部服務訪問: Pod之間直接連接;跨集羣的服務訪問:依賴DNS代理解析其他集羣的服務域名,由於集羣之間的網絡相互隔離,所以依賴Remote集羣的 East-west Gateway中轉流量。

三 ClusterMesh 環境搭建

搭建 cluster1 和 cluster2 兩個集羣,然後每個集羣上安裝 Istio 控制平面, 且將兩者均設置爲主集羣(primary cluster)。 集羣 cluster1 在 network1 網絡上,而集羣 cluster2 在 network2 網絡上。

3.1 前提條件

本次搭建環境信息如下: 使用Kind搭建Kubernetes集羣,Kind版本爲v0.19.0。 Kubernetes 版本爲1.27.3 ; Istio 版本爲 1.20.1。

image.png

在搭建k8s 集羣之前確保Linux節點已安裝docker kubectl 和 kind。

下載istioctl二進制

curl -L https://istio.io/downloadIstio | ISTIO_VERSION=1.20.1 TARGET_ARCH=x86_64 sh -
將 istioctl 客戶端添加到路徑
image.png

3.2 Kubernetes集羣安裝

cluster1和cluster2集羣安裝腳本如下

# create-cluster.sh
# This script handles the creation of multiple clusters using kind and the
# ability to create and configure an insecure container registry.

set -o xtrace
set -o errexit
set -o nounset
set -o pipefail

# shellcheck source=util.sh
NUM_CLUSTERS="${NUM_CLUSTERS:-2}"
KIND_IMAGE="${KIND_IMAGE:-}"
KIND_TAG="${KIND_TAG:-v1.27.3@sha256:3966ac761ae0136263ffdb6cfd4db23ef8a83cba8a463690e98317add2c9ba72}"
OS="$(uname)"
function create-clusters() {
  local num_clusters=${1}

  local image_arg=""
  if [[ "${KIND_IMAGE}" ]]; then
    image_arg="--image=${KIND_IMAGE}"
  elif [[ "${KIND_TAG}" ]]; then
    image_arg="--image=kindest/node:${KIND_TAG}"
  fi
  for i in $(seq "${num_clusters}"); do
    kind create cluster --name "cluster${i}" "${image_arg}"
    fixup-cluster "${i}"
    echo

  done
}

function fixup-cluster() {
  local i=${1} # cluster num

  if [ "$OS" != "Darwin" ];then
    # Set container IP address as kube API endpoint in order for clusters to reach kube API servers in other clusters.
    local docker_ip
    docker_ip=$(docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' "cluster${i}-control-plane")
    kubectl config set-cluster "kind-cluster${i}" --server="https://${docker_ip}:6443"
  fi

  # Simplify context name
  kubectl config rename-context "kind-cluster${i}" "cluster${i}"
}
echo "Creating ${NUM_CLUSTERS} clusters"
create-clusters "${NUM_CLUSTERS}"
kubectl config use-context cluster1

echo "Kind CIDR is $(docker network inspect -f '{{$map := index .IPAM.Config 0}}{{index $map "Subnet"}}' kind)"

echo "Complete"

以上集羣安裝的過程中,爲了istiod能夠訪問對方集羣的apiserver地址,集羣kube-apiserver的地址設置爲master節點的地址。因爲是kind部署的集羣,兩個集羣的master節點本質上都是同個宿主機上的docker運行的容器。

image.png

確認cluster1和cluster2 是否就緒

image.png

3.3 使用MetalLB爲網關分配ExternalIP

由於使用的是kind部署多集羣,istio南北向網關和東西向網關創建需要創建LoadBalencer service,均需要使用到ExternalIP。這裏藉助metalLB 實現LB ip地址的分發和宣告。
查看kind搭建集羣使用節點子網網段: 172.18.0.0/16
採用metalLB L2模式進行部署。

cluster1中的metalLB配置清單: metallb-config-1.yaml

### for cluster1
##配置IPAddressPool,用於lbip地址的分配。L2模式下,ippool地址和worker節點處於同一子網即可
apiVersion: metallb.io/v1beta1
kind: IPAddressPool
metadata:
  name: first-pool
  namespace: metallb-system
spec:
  addresses:
    - 172.18.1.230-172.18.1.240
---
##配置L2Advertisement,用於地址宣告
apiVersion: metallb.io/v1beta1
kind: L2Advertisement
metadata:
  name: first-adv
  namespace: metallb-system
spec:
  ipAddressPools: 
    - first-pool

cluster2集羣中的metalLB配置清單:metallb-config-2.yaml

### for cluster2
##配置IPAddressPool,用於lbip地址的分配。L2模式下,ippool地址和worker節點處於同一子網即可
apiVersion: metallb.io/v1beta1
kind: IPAddressPool
metadata:
  name: second-pool
  namespace: metallb-system
spec:
  addresses:
    - 172.18.1.241-172.18.1.252
---
##配置L2Advertisement,用於地址宣告
apiVersion: metallb.io/v1beta1
kind: L2Advertisement
metadata:
  name: second-adv
  namespace: metallb-system
spec:
  ipAddressPools: 
    - second-pool

使用腳本進行安裝

#!/usr/bin/env bash

set -o xtrace
set -o errexit
set -o nounset
set -o pipefail

NUM_CLUSTERS="${NUM_CLUSTERS:-2}"
for i in $(seq "${NUM_CLUSTERS}"); do
  echo "Starting metallb deployment in cluster${i}"
  kubectl apply -f https://raw.githubusercontent.com/metallb/metallb/v0.13.10/config/manifests/metallb-native.yaml --context "cluster${i}"
  kubectl create secret generic -n metallb-system memberlist --from-literal=secretkey="$(openssl rand -base64 128)" --context "cluster${i}"
  ## 增加等待時間,如果metallb負載沒部署起來,創建IPAddressPool L2Advertisement 會報錯
  sleep 10
  kubectl apply -f ./metallb-config-${i}.yaml --context "cluster${i}"
  echo "----"
done

確認metalLB部署情況

image.png

確認IPAddressPool信息:

image.png

3.4 集羣共享根CA 配置信任關係

爲了支持安全的跨集羣mTLS通信,多控制面模型要求每個集羣的控制面Istiod都使用相同的CA機構頒發的中間CA證書,供Citatel簽發證書使用,以支持跨集羣的TLS雙向認證。
image.png
Istio東西向網關(跨集羣訪問)工作時使用基於SNI的路由,它根據TLS請求的SNI,自動將其路由到SNI對應的Cluster,因此非扁平網絡的跨網絡訪問要求所有流量都必須經過TLS加密。

在集羣中插入證書和密鑰,腳本如下(需要將該腳本移動到istio的安裝包目錄下):

#!/usr/bin/env bash

set -o xtrace
#set -o errexit
set -o nounset
set -o pipefail
NUM_CLUSTERS="${NUM_CLUSTERS:-2}"
##在istio安裝包的頂層目錄下 創建目錄 用來存放證書和密鑰
mkdir -p certs
pushd certs

##生成根證書和密鑰
make -f ../tools/certs/Makefile.selfsigned.mk root-ca

for i in $(seq "${NUM_CLUSTERS}"); do
  ##對於每個集羣,爲 Istio CA 生成一箇中間證書和密鑰
  make -f ../tools/certs/Makefile.selfsigned.mk "cluster${i}-cacerts"
  ##對於每個集羣,創建istio-system 命名空間
  kubectl create namespace istio-system --context "cluster${i}"
  ## 對於每個集羣,通過給istio系統命名空間打上topology.istio.io/network 標籤添加網絡標識
  kubectl --context="cluster${i}" label namespace istio-system topology.istio.io/network="network${i}"
  ##對於每個集羣,給工作節點node打上地域和可用區標籤,便於istio實現地域故障轉移、地域負載均衡
  kubectl --context="cluster${i}" label node "cluster${i}-control-plane" topology.kubernetes.io/region="region${i}"
  kubectl --context="cluster${i}" label node "cluster${i}-control-plane" topology.kubernetes.io/zone="zone${i}"
  #在每個集羣中,創建一個私密 cacerts,使用所有輸入文件 ca-cert.pem, ca-key.pem,root-cert.pem 和 cert-chain.pem。
  kubectl delete secret cacerts -n istio-system --context "cluster${i}"
  kubectl create secret generic cacerts -n istio-system --context "cluster${i}" \
      --from-file="cluster${i}/ca-cert.pem" \
      --from-file="cluster${i}/ca-key.pem" \
      --from-file="cluster${i}/root-cert.pem" \
      --from-file="cluster${i}/cert-chain.pem"
  echo "----"
done
執行腳本,將會生成根證書和中間證書等文件

image.png

image.png

3.5 Istio服務網格安裝

爲cluster1,和cluster2 集羣安裝多控制面istio網格。

將cluster1 設置爲主集羣,在istio的安裝目錄下執行如下命令

cat <<EOF > cluster1.yaml
apiVersion: install.istio.io/v1alpha1
kind: IstioOperator
spec:
  values:
    global:
      meshID: mesh1
      multiCluster:  ##開啓多集羣配置
        clusterName: cluster1 #指定k8s集羣名稱
      network: network1 #指定網絡標識
      logging:
        level: debug
EOF

將cluster2 設置爲主集羣,在istio的安裝目錄下執行如下命令

cat <<EOF > cluster2.yaml
apiVersion: install.istio.io/v1alpha1
kind: IstioOperator
spec:
  values:
    global:
      meshID: mesh2
      multiCluster:  ##開啓多集羣配置
        clusterName: cluster2 #指定k8s集羣名稱
      network: network2 #指定網絡標識
      logging:
        level: debug
EOF
編寫自動化安裝腳本
#!/usr/bin/env bash

set -o xtrace
set -o errexit
set -o nounset
set -o pipefail

OS="$(uname)"
NUM_CLUSTERS="${NUM_CLUSTERS:-2}"

for i in $(seq "${NUM_CLUSTERS}"); do

echo "Starting istio deployment in cluster${i}"

istioctl install --force --context="cluster${i}" -f "cluster${i}.yaml"

echo "Generate eastwest gateway in cluster${i}"

## 在每個集羣中安裝東西向網關。
bash samples/multicluster/gen-eastwest-gateway.sh \
--mesh "mesh${i}" --cluster "cluster${i}" --network "network${i}" | \
istioctl --context="cluster${i}" install -y -f -

echo

done

執行腳本,進行istio的安裝部署

image.png

稍等片刻後,等待安裝完成

image.png

可以發現每個集羣中的網關使用的ExternalIP信息爲配置的metalLB設置的IPPool中的地址。

3.6 在東西向網關開放服務

因爲集羣位於不同的網絡中,所以我們需要在兩個集羣東西向網關上開放所有服務(*.local)。 雖然此網關在互聯網上是公開的,但它背後的服務只能被擁有可信 mTLS 證書的服務訪問, 就像它們處於同一網絡一樣。執行下面的命令在兩個集羣中暴露服務:

apiVersion: networking.istio.io/v1beta1
kind: Gateway
metadata:
  name: cross-network-gateway
spec:
  selector:
    istio: eastwestgateway # 專用於東西向流量的網關
  servers:
    - port:
        number: 15443 # 已經聲明瞭
        name: tls
        protocol: TLS
      tls:
        mode: AUTO_PASSTHROUGH # 東西向網關工作模式是 TLS AUTO_PASSTHROUGH
      hosts:
        - "*.local" # 暴露所有的服務

分別在每個集羣中應用上述Gateway配置:
kubectl -n istio-system --context=cluster${i} apply -f samples/multicluster/expose-services.yaml
image.png

3.7 配置secret以便istiod訪問遠程集羣apiserver

每個k8s集羣中的 istiod 需要 List-Watch 其他集羣的 Kube-APIServer,使用 K8s 集羣的憑據來創建 Secret 對象,以允許 Istio 訪問遠程 Kubernetes apiserver。

#!/usr/bin/env bash

set -o xtrace
set -o errexit
set -o nounset
set -o pipefail
OS="$(uname)"
NUM_CLUSTERS="${NUM_CLUSTERS:-2}"

for i in $(seq "${NUM_CLUSTERS}"); do
  for j in $(seq "${NUM_CLUSTERS}"); do
    if [ "$i" -ne "$j" ]
    then
      echo "Enable Endpoint Discovery between cluster${i} and cluster${j}"

      if [ "$OS" == "Darwin" ]
      then
        # Set container IP address as kube API endpoint in order for clusters to reach kube API servers in other clusters.
        docker_ip=$(docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' "cluster${i}-control-plane")
        istioctl create-remote-secret \
        --context="cluster${i}" \
        --server="https://${docker_ip}:6443" \
        --name="cluster${i}" | \
          kubectl apply --validate=false --context="cluster${j}" -f -
      else
        istioctl create-remote-secret \
          --context="cluster${i}" \
          --name="cluster${i}" | \
          kubectl apply --validate=false --context="cluster${j}" -f -
      fi
    fi
  done
done

執行以上腳本:remote secret創建完成。

image.png

查看istiod日誌發現已經監聽遠程集羣了

image.png

四 Istio多集羣流量治理實踐

image.png

每個集羣創建sample 命名空間,並設置sidecar自動注入
kubectl create --context=cluster1 namespace sample
kubectl create --context=cluster2 namespace sample

kubectl label --context=cluster1 namespace sample \
    istio-injection=enabled
kubectl label --context=cluster2 namespace sample \
    istio-injection=enabled

kubectl apply --context=cluster1 \
    -f samples/helloworld/helloworld.yaml \
    -l service=helloworld -n sample
kubectl apply --context=cluster2 \
    -f samples/helloworld/helloworld.yaml \
    -l service=helloworld -n sample

分別在不同集羣部署不同版本的服務

把應用 helloworld-v1 部署到 cluster1:
kubectl apply --context=cluster1 \
    -f samples/helloworld/helloworld.yaml \
    -l version=v1 -n sample
把應用 helloworld-v2 部署到 cluster2:
kubectl apply --context=cluster2 \
-f samples/helloworld/helloworld.yaml \
-l version=v2 -n sample
部署測試客戶端
kubectl apply --context=cluster1 \
    -f samples/sleep/sleep.yaml -n sample
kubectl apply --context=cluster2 \
    -f samples/sleep/sleep.yaml -n sample

確認負載實例部署成功,並且sidecar已經注入

image.png

4.1 驗證跨集羣流量

用 Sleep pod 重複調用服務 HelloWorld。 爲了確認負載均衡按預期工作,需要從所有集羣調用服務 HelloWorld。

從 cluster1 中的 Sleep pod 發送請求給服務 HelloWorld

image.png

從 cluster2 中的 Sleep pod 發送請求給服務 HelloWorld

image.png

4.3 驗證從網關訪問

通過網關訪問服務端Helloworld

創建virtualservice、gateway等istio資源,配置清單如下

# helloworld-gateway.yaml
apiVersion: networking.istio.io/v1beta1
kind: Gateway
metadata:
  name: helloworld-gateway
spec:
  selector:
    istio: ingressgateway # use istio default controller
  servers:
    - port:
        number: 80
        name: http
        protocol: HTTP
      hosts:
        - "*"
---
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  name: helloworld
spec:
  hosts:
    - "*"
  gateways:
    - helloworld-gateway
  http:
    - match:
        - uri:
            exact: /hello
      route:
        - destination:
            host: helloworld
            port:
              number: 5000

注意: 兩個集羣都需要應用該配置

訪問效果如下:

image.png

4.3 驗證地域負載均衡

對流量進行更精細的控制,將 region1 -> zone1  region1 -> zone2 兩個地區的權重分別爲 80% 和 20%,使用 DestinationRule 來配置權重分佈

# locality-lb-weight.yaml
apiVersion: networking.istio.io/v1beta1
kind: DestinationRule
metadata:
  name: helloworld
  namespace: sample
spec:
  host: helloworld.sample.svc.cluster.local
  trafficPolicy:
    connectionPool:
      http:
        maxRequestsPerConnection: 1
    loadBalancer:
      simple: ROUND_ROBIN
      localityLbSetting:
        enabled: true
        distribute:
          - from: region1/*
            to:
              "region1/*": 80
              "region2/*": 20
          - from: region2/*
            to:
              "region2/*": 80
              "region1/*": 20
    outlierDetection:
      consecutive5xxErrors: 1
      interval: 1s
      baseEjectionTime: 1m

注意: 兩個集羣都需要應用該配置

從 cluster1 中通過網關發送請求給服務 HelloWorld

image.png

從 cluster2中通過網關發送請求給服務 HelloWorld

image.png

4.4 驗證地域故障轉移

當多個地區/區域部署多個服務實例時,如果某個地區/區域的服務實例不可用,可以將流量轉移到其他地區/區域的服務實例上,實現地域故障轉移,這樣就可以保證服務的高可用性。

# locality-lb-failover.yaml
apiVersion: networking.istio.io/v1beta1
kind: DestinationRule
metadata:
  name: helloworld
  namespace: sample
spec:
  host: helloworld.sample.svc.cluster.local
  trafficPolicy:
    connectionPool:
      http:
        maxRequestsPerConnection: 1 # 關閉 HTTP Keep-Alive,強制每個HTTP請求使用一個新連接的策略
    loadBalancer:
      simple: ROUND_ROBIN
      localityLbSetting:  # 地域負載均衡配置,開啓異常點檢測後,默認開啓。
        enabled: true     
        failover:         # 地域故障轉移策略
          - from: region1  
            to: region2
          - from: region2
            to: region1
    outlierDetection:
      consecutive5xxErrors: 1 # 連續 1 次 5xx 錯誤
      interval: 1s # 檢測間隔 1s
      baseEjectionTime: 1m # 基礎驅逐時間 1m

注意: 兩個集羣都需要應用該配置

從 cluster1 中通過網關發送請求給服務 HelloWorld

image.png

模擬故障,手動將cluster1集羣中Helloworld V1版本設置故障

image.png

再次訪問,故障檢測生效,觸發故障轉移,並驗證響應中的 version 始終爲 v2,也就是說我們訪問的是 region2 的 helloworld 服務,這樣就實現了地域故障轉移。

image.png

故障轉移的前提是當前region內,所有實例都不可用時,纔會轉移到到目前region,否則流量還會發往當前region的其他可用實例。

五 備註

參考文獻如下:

  1. istio開源社區(跨網絡多主架構的安裝說明): https://istio.io/latest/zh/docs/setup/install/multicluster/multi-primary_multi-network/

  2. kind安裝集羣腳本參考: https://github.com/cnych/multi-cluster-istio-kind/tree/main/kind-create

  3. 多集羣證書管理參考:https://istio.io/latest/zh/docs/tasks/security/cert-management/plugin-ca-cert/

點擊關注,第一時間瞭解華爲雲新鮮技術~

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章