Menyiapkan mesh multi-cluster di GKE

Panduan ini menjelaskan cara menggabungkan dua cluster ke dalam satu Cloud Service Mesh menggunakan Mesh CA atau Istio CA, dan mengaktifkan load balancing lintas cluster. Anda dapat dengan mudah memperluas proses ini untuk menggabungkan sejumlah cluster ke dalam mesh Anda.

Konfigurasi Cloud Service Mesh multi-cluster dapat memecahkan beberapa skenario perusahaan penting, seperti skala, lokasi, dan isolasi. Untuk mengetahui informasi selengkapnya, lihat Kasus penggunaan multi-cluster.

Prasyarat

Panduan ini mengasumsikan bahwa Anda memiliki dua atau lebih Google Cloud cluster GKE yang memenuhi persyaratan berikut:

Menetapkan variabel project dan cluster

  1. Buat variabel lingkungan berikut untuk project ID, zona atau region cluster, nama cluster, dan konteks.

    export PROJECT_1=PROJECT_ID_1
    export LOCATION_1=CLUSTER_LOCATION_1
    export CLUSTER_1=CLUSTER_NAME_1
    export CTX_1="gke_${PROJECT_1}_${LOCATION_1}_${CLUSTER_1}"
    
    export PROJECT_2=PROJECT_ID_2
    export LOCATION_2=CLUSTER_LOCATION_2
    export CLUSTER_2=CLUSTER_NAME_2
    export CTX_2="gke_${PROJECT_2}_${LOCATION_2}_${CLUSTER_2}"
    
  2. Jika ini adalah cluster yang baru dibuat, pastikan untuk mengambil kredensial untuk setiap cluster dengan perintah gcloud berikut. Jika tidak, context terkait tidak akan tersedia untuk digunakan pada langkah berikutnya dalam panduan ini.

    Perintah bergantung pada jenis cluster Anda, baik regional maupun zonal:

    Regional

    gcloud container clusters get-credentials ${CLUSTER_1} --region ${LOCATION_1}
    gcloud container clusters get-credentials ${CLUSTER_2} --region ${LOCATION_2}
    

    Zonal

    gcloud container clusters get-credentials ${CLUSTER_1} --zone ${LOCATION_1}
    gcloud container clusters get-credentials ${CLUSTER_2} --zone ${LOCATION_2}
    

Membuat aturan firewall

Dalam beberapa kasus, Anda perlu membuat aturan firewall untuk mengizinkan traffic lintas cluster. Misalnya, Anda perlu membuat aturan firewall jika:

  • Anda menggunakan subnet yang berbeda untuk cluster di mesh Anda.
  • Pod Anda membuka port selain 443 dan 15002.

GKE secara otomatis menambahkan aturan firewall ke setiap node untuk mengizinkan traffic dalam subnet yang sama. Jika mesh Anda berisi beberapa subnet, Anda harus secara eksplisit menyiapkan aturan firewall untuk mengizinkan traffic lintas subnet. Anda harus menambahkan aturan firewall baru untuk setiap subnet guna mengizinkan blok CIDR IP sumber dan port target dari semua traffic masuk.

Petunjuk berikut memungkinkan komunikasi antara semua cluster di project Anda atau hanya antara $CLUSTER_1 dan $CLUSTER_2.

  1. Kumpulkan informasi tentang jaringan cluster Anda.

    Semua cluster project

    Jika cluster berada dalam project yang sama, Anda dapat menggunakan perintah berikut untuk mengizinkan komunikasi antara semua cluster di project Anda. Jika ada cluster di project Anda yang tidak ingin diekspos, gunakan perintah di tab Cluster tertentu.

    function join_by { local IFS="$1"; shift; echo "$*"; }
    ALL_CLUSTER_CIDRS=$(gcloud container clusters list --project $PROJECT_1 --format='value(clusterIpv4Cidr)' | sort | uniq)
    ALL_CLUSTER_CIDRS=$(join_by , $(echo "${ALL_CLUSTER_CIDRS}"))
    ALL_CLUSTER_NETTAGS=$(gcloud compute instances list --project $PROJECT_1 --format='value(tags.items.[0])' | sort | uniq)
    ALL_CLUSTER_NETTAGS=$(join_by , $(echo "${ALL_CLUSTER_NETTAGS}"))
    

    Cluster tertentu

    Perintah berikut memungkinkan komunikasi antara $CLUSTER_1 dan $CLUSTER_2 serta tidak mengekspos cluster lain di project Anda.

    function join_by { local IFS="$1"; shift; echo "$*"; }
    ALL_CLUSTER_CIDRS=$(for P in $PROJECT_1 $PROJECT_2; do gcloud --project $P container clusters list --filter="name:($CLUSTER_1,$CLUSTER_2)" --format='value(clusterIpv4Cidr)'; done | sort | uniq)
    ALL_CLUSTER_CIDRS=$(join_by , $(echo "${ALL_CLUSTER_CIDRS}"))
    ALL_CLUSTER_NETTAGS=$(for P in $PROJECT_1 $PROJECT_2; do gcloud --project $P compute instances list  --filter="name:($CLUSTER_1,$CLUSTER_2)" --format='value(tags.items.[0])' ; done | sort | uniq)
    ALL_CLUSTER_NETTAGS=$(join_by , $(echo "${ALL_CLUSTER_NETTAGS}"))
    
  2. Buat aturan firewall.

    GKE

    gcloud compute firewall-rules create istio-multicluster-pods \
        --allow=tcp,udp,icmp,esp,ah,sctp \
        --direction=INGRESS \
        --priority=900 \
        --source-ranges="${ALL_CLUSTER_CIDRS}" \
        --target-tags="${ALL_CLUSTER_NETTAGS}" --quiet \
        --network=YOUR_NETWORK
    

    Autopilot

    TAGS=""
    for CLUSTER in ${CLUSTER_1} ${CLUSTER_2}
    do
        TAGS+=$(gcloud compute firewall-rules list --filter="Name:$CLUSTER*" --format="value(targetTags)" | uniq) && TAGS+=","
    done
    TAGS=${TAGS::-1}
    echo "Network tags for pod ranges are $TAGS"
    
    gcloud compute firewall-rules create asm-multicluster-pods \
        --allow=tcp,udp,icmp,esp,ah,sctp \
        --network=gke-cluster-vpc \
        --direction=INGRESS \
        --priority=900 --network=VPC_NAME \
        --source-ranges="${ALL_CLUSTER_CIDRS}" \
        --target-tags=$TAGS
    

Mengonfigurasi penemuan endpoint

Langkah-langkah yang diperlukan untuk mengonfigurasi penemuan endpoint bergantung pada apakah Anda lebih suka menggunakan API deklaratif di seluruh cluster dalam fleet, atau mengaktifkannya secara manual di cluster publik atau cluster pribadi.

Mengonfigurasi penemuan endpoint antar-cluster publik

Untuk mengonfigurasi penemuan endpoint antar-cluster GKE, jalankan asmcli create-mesh. Perintah ini:

  • Mendaftarkan semua cluster ke fleet yang sama.
  • Mengonfigurasi mesh untuk mempercayai workload identity fleet.
  • Membuat secret jarak jauh.

Anda dapat menentukan URI untuk setiap cluster atau jalur file kubeconfig.

URI cluster

Pada perintah berikut, ganti FLEET_PROJECT_ID dengan project ID project host fleet dan URI cluster dengan nama cluster, zona atau region, dan project ID untuk setiap cluster. Contoh ini hanya menampilkan dua cluster, tetapi Anda dapat menjalankan perintah untuk mengaktifkan penemuan endpoint di cluster tambahan, dengan tunduk pada jumlah maksimum cluster yang diizinkan yang dapat Anda tambahkan ke fleet.

./asmcli create-mesh \
    FLEET_PROJECT_ID \
    ${PROJECT_1}/${LOCATION_1}/${CLUSTER_1} \
    ${PROJECT_2}/${LOCATION_2}/${CLUSTER_2}

file kubeconfig

Pada perintah berikut, ganti FLEET_PROJECT_ID dengan project ID project host fleet dan PATH_TO_KUBECONFIG dengan jalur ke setiap kubeconfig file. Contoh ini hanya menampilkan dua cluster, tetapi Anda dapat menjalankan perintah untuk mengaktifkan penemuan endpoint di cluster tambahan, dengan tunduk pada jumlah maksimum cluster yang diizinkan yang dapat Anda tambahkan ke fleet.

./asmcli create-mesh \
    FLEET_PROJECT_ID \
    PATH_TO_KUBECONFIG_1 \
    PATH_TO_KUBECONFIG_2

Mengonfigurasi penemuan endpoint antar-cluster pribadi

  1. Konfigurasi secret jarak jauh untuk mengizinkan akses server API ke cluster ke bidang kontrol Cloud Service Mesh cluster lainnya. Perintah bergantung pada jenis Cloud Service Mesh Anda (baik dalam cluster maupun terkelola):

    A. Untuk Cloud Service Mesh dalam cluster, Anda harus mengonfigurasi IP pribadi, bukan IP publik karena IP publik tidak dapat diakses:

    PRIV_IP=`gcloud container clusters describe "${CLUSTER_1}" --project "${PROJECT_1}" \
     --zone "${LOCATION_1}" --format "value(privateClusterConfig.privateEndpoint)"`
    
    ./istioctl x create-remote-secret --context=${CTX_1} --name=${CLUSTER_1} --server=https://${PRIV_IP} > ${CTX_1}.secret
    
    PRIV_IP=`gcloud container clusters describe "${CLUSTER_2}" --project "${PROJECT_2}" \
     --zone "${LOCATION_2}" --format "value(privateClusterConfig.privateEndpoint)"`
    
    ./istioctl x create-remote-secret --context=${CTX_2} --name=${CLUSTER_2} --server=https://${PRIV_IP} > ${CTX_2}.secret
    

    B. Untuk Cloud Service Mesh Terkelola:

    PUBLIC_IP=`gcloud container clusters describe "${CLUSTER_1}" --project "${PROJECT_1}" \
     --zone "${LOCATION_1}" --format "value(privateClusterConfig.publicEndpoint)"`
    
    ./istioctl x create-remote-secret --context=${CTX_1} --name=${CLUSTER_1} --server=https://${PUBLIC_IP} > ${CTX_1}.secret
    
    PUBLIC_IP=`gcloud container clusters describe "${CLUSTER_2}" --project "${PROJECT_2}" \
     --zone "${LOCATION_2}" --format "value(privateClusterConfig.publicEndpoint)"`
    
    ./istioctl x create-remote-secret --context=${CTX_2} --name=${CLUSTER_2} --server=https://${PUBLIC_IP} > ${CTX_2}.secret
    
  2. Terapkan secret baru ke dalam cluster:

    kubectl apply -f ${CTX_1}.secret --context=${CTX_2}
    
    kubectl apply -f ${CTX_2}.secret --context=${CTX_1}
    

Mengonfigurasi jaringan yang diizinkan untuk cluster pribadi

Ikuti bagian ini hanya jika semua kondisi berikut berlaku untuk mesh Anda:

Saat men-deploy beberapa cluster pribadi, bidang kontrol Cloud Service Mesh di setiap cluster perlu memanggil bidang kontrol GKE dari cluster jarak jauh. Untuk mengizinkan traffic, Anda perlu menambahkan rentang alamat Pod di cluster panggilan ke jaringan yang diizinkan dari cluster jarak jauh.

  1. Dapatkan blok CIDR IP Pod untuk setiap cluster:

    POD_IP_CIDR_1=`gcloud container clusters describe ${CLUSTER_1} --project ${PROJECT_1} --zone ${LOCATION_1} \
      --format "value(ipAllocationPolicy.clusterIpv4CidrBlock)"`
    
    POD_IP_CIDR_2=`gcloud container clusters describe ${CLUSTER_2} --project ${PROJECT_2} --zone ${LOCATION_2} \
      --format "value(ipAllocationPolicy.clusterIpv4CidrBlock)"`
    
  2. Tambahkan blok CIDR IP Pod cluster Kubernetes ke cluster jarak jauh:

    EXISTING_CIDR_1=`gcloud container clusters describe ${CLUSTER_1} --project ${PROJECT_1} --zone ${LOCATION_1} \
     --format "value(masterAuthorizedNetworksConfig.cidrBlocks.cidrBlock)"`
    gcloud container clusters update ${CLUSTER_1} --project ${PROJECT_1} --zone ${LOCATION_1} \
    --enable-master-authorized-networks \
    --master-authorized-networks ${POD_IP_CIDR_2},${EXISTING_CIDR_1//;/,}
    
    EXISTING_CIDR_2=`gcloud container clusters describe ${CLUSTER_2} --project ${PROJECT_2} --zone ${LOCATION_2} \
     --format "value(masterAuthorizedNetworksConfig.cidrBlocks.cidrBlock)"`
    gcloud container clusters update ${CLUSTER_2} --project ${PROJECT_2} --zone ${LOCATION_2} \
    --enable-master-authorized-networks \
    --master-authorized-networks ${POD_IP_CIDR_1},${EXISTING_CIDR_2//;/,}
    

    Untuk mengetahui informasi selengkapnya, lihat Membuat cluster dengan jaringan yang diizinkan.

  3. Pastikan jaringan yang diizinkan telah diperbarui:

    gcloud container clusters describe ${CLUSTER_1} --project ${PROJECT_1} --zone ${LOCATION_1} \
     --format "value(masterAuthorizedNetworksConfig.cidrBlocks.cidrBlock)"
    
    gcloud container clusters describe ${CLUSTER_2} --project ${PROJECT_2} --zone ${LOCATION_2} \
     --format "value(masterAuthorizedNetworksConfig.cidrBlocks.cidrBlock)"
    

Mengaktifkan akses global bidang kontrol

Ikuti bagian ini hanya jika semua kondisi berikut berlaku untuk mesh Anda:

  • Anda menggunakan cluster pribadi.
  • Anda menggunakan region yang berbeda untuk cluster di mesh Anda.

Anda harus mengaktifkan akses global bidang kontrol untuk mengizinkan bidang kontrol Cloud Service Mesh di setiap cluster memanggil bidang kontrol GKE dari cluster jarak jauh.

  1. Aktifkan akses global bidang kontrol:

    gcloud container clusters update ${CLUSTER_1} --project ${PROJECT_1} --zone ${LOCATION_1} \
     --enable-master-global-access
    
    gcloud container clusters update ${CLUSTER_2} --project ${PROJECT_2} --zone ${LOCATION_2} \
     --enable-master-global-access
    
  2. Pastikan akses global bidang kontrol diaktifkan:

    gcloud container clusters describe ${CLUSTER_1} --project ${PROJECT_1} --zone ${LOCATION_1}
    
    gcloud container clusters describe ${CLUSTER_2} --project ${PROJECT_2} --zone ${LOCATION_2}
    

    Bagian privateClusterConfig dalam output menampilkan status masterGlobalAccessConfig.

Memverifikasi konektivitas multi-cluster

Bagian ini menjelaskan cara men-deploy layanan HelloWorld dan Sleep contoh ke lingkungan multi-cluster Anda untuk memverifikasi bahwa load balancing lintas cluster berfungsi.

Menetapkan variabel untuk direktori contoh

  1. Buka tempat asmcli didownload, lalu jalankan perintah berikut untuk menetapkan ASM_VERSION

    export ASM_VERSION="$(./asmcli --version)"
    
  2. Tetapkan folder kerja ke contoh yang Anda gunakan untuk memverifikasi bahwa load balancing lintas cluster berfungsi. Contoh terletak di subdirektori di direktori --output_dir yang Anda tentukan dalam perintah asmcli install. Pada perintah berikut, ubah OUTPUT_DIR ke direktori yang Anda tentukan di --output_dir.

    export SAMPLES_DIR=OUTPUT_DIR/istio-${ASM_VERSION%+*}
    

Mengaktifkan penyuntikan sidecar

  1. Buat namespace contoh di setiap cluster.

    for CTX in ${CTX_1} ${CTX_2}
    do
        kubectl create --context=${CTX} namespace sample
    done
    
  2. Aktifkan penyuntikan sidecar pada namespace yang dibuat.

    Direkomendasikan: Jalankan perintah berikut untuk menerapkan label penyuntikan default ke namespace:

    for CTX in ${CTX_1} ${CTX_2}
    do
        kubectl label --context=${CTX} namespace sample \
            istio.io/rev- istio-injection=enabled --overwrite
    done
    

    Sebaiknya gunakan penyuntikan default, tetapi penyuntikan berbasis revisi didukung: Gunakan petunjuk berikut:

    1. Gunakan perintah berikut untuk menemukan label revisi di istiod:

      kubectl get deploy -n istio-system -l app=istiod -o \
          jsonpath={.items[*].metadata.labels.'istio\.io\/rev'}'{"\n"}'
      
    2. Terapkan label revisi ke namespace. Pada perintah berikut, REVISION_LABEL adalah nilai label revisi istiod yang Anda catat pada langkah sebelumnya.

      for CTX in ${CTX_1} ${CTX_2}
      do
          kubectl label --context=${CTX} namespace sample \
              istio-injection- istio.io/rev=REVISION_LABEL --overwrite
      done
      

Menginstal layanan HelloWorld

  • Buat layanan HelloWorld di kedua cluster:

    kubectl create --context=${CTX_1} \
        -f ${SAMPLES_DIR}/samples/helloworld/helloworld.yaml \
        -l service=helloworld -n sample
    
    kubectl create --context=${CTX_2} \
        -f ${SAMPLES_DIR}/samples/helloworld/helloworld.yaml \
        -l service=helloworld -n sample
    

Men-deploy HelloWorld v1 dan v2 ke setiap cluster

  1. Deploy HelloWorld v1 ke CLUSTER_1 dan v2 ke CLUSTER_2, yang akan membantu memverifikasi load balancing lintas cluster nanti:

    kubectl create --context=${CTX_1} \
      -f ${SAMPLES_DIR}/samples/helloworld/helloworld.yaml \
      -l version=v1 -n sample
    kubectl create --context=${CTX_2} \
      -f ${SAMPLES_DIR}/samples/helloworld/helloworld.yaml \
      -l version=v2 -n sample
  2. Konfirmasi bahwa HelloWorld v1 dan v2 berjalan menggunakan perintah berikut. Pastikan outputnya mirip dengan yang ditampilkan.:

    kubectl get pod --context=${CTX_1} -n sample
    NAME                            READY     STATUS    RESTARTS   AGE
    helloworld-v1-86f77cd7bd-cpxhv  2/2       Running   0          40s
    kubectl get pod --context=${CTX_2} -n sample
    NAME                            READY     STATUS    RESTARTS   AGE
    helloworld-v2-758dd55874-6x4t8  2/2       Running   0          40s

Men-deploy layanan Sleep

  1. Deploy layanan Sleep ke kedua cluster. Pod ini menghasilkan traffic jaringan buatan untuk tujuan demonstrasi:

    for CTX in ${CTX_1} ${CTX_2}
    do
        kubectl apply --context=${CTX} \
            -f ${SAMPLES_DIR}/samples/sleep/sleep.yaml -n sample
    done
    
  2. Tunggu hingga layanan Sleep dimulai di setiap cluster. Pastikan outputnya mirip dengan yang ditampilkan:

    kubectl get pod --context=${CTX_1} -n sample -l app=sleep
    NAME                             READY   STATUS    RESTARTS   AGE
    sleep-754684654f-n6bzf           2/2     Running   0          5s
    kubectl get pod --context=${CTX_2} -n sample -l app=sleep
    NAME                             READY   STATUS    RESTARTS   AGE
    sleep-754684654f-dzl9j           2/2     Running   0          5s

Memverifikasi load balancing lintas cluster

Panggil layanan HelloWorld beberapa kali dan periksa output untuk memverifikasi balasan alternatif dari v1 dan v2:

  1. Panggil layanan HelloWorld:

    kubectl exec --context="${CTX_1}" -n sample -c sleep \
        "$(kubectl get pod --context="${CTX_1}" -n sample -l \
        app=sleep -o jsonpath='{.items[0].metadata.name}')" \
        -- /bin/sh -c 'for i in $(seq 1 20); do curl -sS helloworld.sample:5000/hello; done'
    

    Outputnya mirip dengan yang ditampilkan:

    Hello version: v2, instance: helloworld-v2-758dd55874-6x4t8
    Hello version: v1, instance: helloworld-v1-86f77cd7bd-cpxhv
    ...
  2. Panggil layanan HelloWorld lagi:

    kubectl exec --context="${CTX_2}" -n sample -c sleep \
        "$(kubectl get pod --context="${CTX_2}" -n sample -l \
        app=sleep -o jsonpath='{.items[0].metadata.name}')" \
        -- /bin/sh -c 'for i in $(seq 1 20); do curl -sS helloworld.sample:5000/hello; done'
    

    Outputnya mirip dengan yang ditampilkan:

    Hello version: v2, instance: helloworld-v2-758dd55874-6x4t8
    Hello version: v1, instance: helloworld-v1-86f77cd7bd-cpxhv
    ...

Selamat, Anda telah memverifikasi Cloud Service Mesh multi-cluster yang di-load balance.

Membersihkan layanan HelloWorld

Setelah selesai memverifikasi load balancing, hapus layanan HelloWorld dan Sleep dari cluster Anda.

kubectl delete ns sample --context ${CTX_1}
kubectl delete ns sample --context ${CTX_2}