本教學課程適用於對使用 Istio 服務網格感興趣的 Kubernetes 使用者和管理員,瞭解如何安全地部署 Kubernetes 服務,並啟用相互 TLS (mTLS) 通訊。
Istio 和 Cloud Service Mesh
Istio 不是 Google 支援的產品。建議改用代管的 Cloud Service Mesh。詳情請參閱「在 GKE Autopilot 叢集上佈建 Cloud Service Mesh」。
Cloud Service Mesh 具有下列優點:
- 您可以使用 Fleet API 佈建受管理 Cloud Service Mesh,不必使用
istioctl等用戶端工具。 - Cloud Service Mesh 會自動將 Sidecar Proxy 插入工作負載,不必授予容器更高的權限。
- 您無須進行任何額外設定,即可查看網格和服務的豐富資訊主頁,然後使用這些指標設定服務等級目標 (SLO) 和快訊,監控應用程式的健康狀態。
- 系統會自動升級代管 Cloud Service Mesh 控制層,確保您取得最新的安全性修補程式和功能。
- Cloud Service Mesh 代管資料層會自動升級工作負載中的 Sidecar Proxy,因此當 Proxy 升級和安全修補程式可用時,您不需要自行重新啟動服務。
- Cloud Service Mesh 是受支援的產品,可使用標準開放原始碼 Istio API 進行設定。詳情請參閱支援的功能。
目標
本教學課程包含下列步驟:
- 建立 GKE Autopilot 叢集。
- 使用
istioctl指令列工具安裝 Istio。 - 部署範例應用程式,測試雙向傳輸層安全標準 (mTLS) 驗證。
- 使用
PeerAuthentication自訂資源,將 Istio 設定為使用 mTLS 驗證,確保服務與服務間的通訊安全。 - 使用 Kiali 資訊主頁驗證 mTLS 驗證。
費用
在本文件中,您會使用下列 Google Cloud的計費元件:
您可以使用 Pricing Calculator,根據預測用量估算費用。
完成本文所述工作後,您可以刪除建立的資源,避免繼續計費,詳情請參閱「清除所用資源」。
事前準備
Cloud Shell 已預先安裝本教學課程所需的軟體,包括 kubectl、gcloud CLI 和 Terraform。如果您未使用 Cloud Shell,則必須安裝 gcloud CLI。
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
Install the Google Cloud CLI.
-
若您採用的是外部識別資訊提供者 (IdP),請先使用聯合身分登入 gcloud CLI。
-
執行下列指令,初始化 gcloud CLI:
gcloud init -
Create or select a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_IDwith a name for the Google Cloud project you are creating. -
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_IDwith your Google Cloud project name.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the GKE API:
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.gcloud services enable container.googleapis.com
-
Install the Google Cloud CLI.
-
若您採用的是外部識別資訊提供者 (IdP),請先使用聯合身分登入 gcloud CLI。
-
執行下列指令,初始化 gcloud CLI:
gcloud init -
Create or select a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_IDwith a name for the Google Cloud project you are creating. -
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_IDwith your Google Cloud project name.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the GKE API:
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.gcloud services enable container.googleapis.com
-
Grant roles to your user account. Run the following command once for each of the following IAM roles:
roles/container.clusterAdmingcloud projects add-iam-policy-binding PROJECT_ID --member="user:USER_IDENTIFIER" --role=ROLE
Replace the following:
PROJECT_ID: Your project ID.USER_IDENTIFIER: The identifier for your user account. For example,[email protected].ROLE: The IAM role that you grant to your user account.
設定環境變數:
export PROJECT_ID=PROJECT_ID gcloud config set project $PROJECT_ID gcloud config set compute/region us-central1並將
PROJECT_ID替換為 Google Cloud 專案 ID。複製 GitHub 存放區:
git clone https://github.com/GoogleCloudPlatform/kubernetes-engine-samples.git變更為工作目錄:
cd kubernetes-engine-samples/service-mesh/istio-tutorial安裝 Istio:
如要安裝最新版 Istio,請按照下列步驟操作:
curl -L https://istio.io/downloadIstio | sh -如要安裝特定版本的 Istio,請按照下列步驟操作:
export ISTIO_VERSION=VERSION_NUMBER curl -L https://istio.io/downloadIstio | TARGET_ARCH=$(uname -m) sh -將 VERSION_NUMBER 替換為要安裝的 Istio 版本。如要瞭解 Istio 版本,請參閱「版本公告」。
將
istioctl指令列工具新增至 PATH:cd istio-* export PATH=$PWD/bin:$PATH在叢集上安裝 Istio:
istioctl install --set profile="default" -y這個步驟可能需要幾分鐘的時間。
等待 Istio Pod 準備就緒:
watch kubectl get pods -n istio-system輸出結果會與下列內容相似:
NAME READY STATUS RESTARTS AGE istio-ingressgateway-5c47bff876-wjm96 1/1 Running 0 2m54s istiod-5fc7cb65cd-k8cp4 1/1 Running 0 2m57s當 Istio Pod 處於
Running狀態時,請按下Ctrl+C返回指令列。新增命名空間標籤,指示 Istio 啟用 Envoy 補充資訊 Proxy 的自動插入功能:
kubectl label namespace default istio-injection=enabled部署範例應用程式:
cd .. git clone https://github.com/GoogleCloudPlatform/bank-of-anthos.git kubectl apply -f bank-of-anthos/extras/jwt/jwt-secret.yaml kubectl apply -f bank-of-anthos/kubernetes-manifests/等待應用程式準備就緒:
watch kubectl get pods輸出結果會與下列內容相似:
NAME READY STATUS RESTARTS AGE accounts-db-0 2/2 Running 0 2m16s balancereader-5c695f78f5-x4wlz 2/2 Running 0 3m8s contacts-557fc79c5-5d7fg 2/2 Running 0 3m7s frontend-7dd589c5d7-b4cgq 2/2 Running 0 3m7s ledger-db-0 2/2 Running 0 3m6s ledgerwriter-6497f5cf9b-25c6x 2/2 Running 0 3m5s loadgenerator-57f6896fd6-lx5df 2/2 Running 0 3m5s transactionhistory-6c498965f-tl2sk 2/2 Running 0 3m4s userservice-95f44b65b-mlk2p 2/2 Running 0 3m4s當 Pod 處於
Running狀態時,請按下Ctrl+C返回指令列。請查看下列資訊清單:
這個資訊清單說明 Istio Gateway 和 VirtualService 資源,這些資源會公開應用程式,並將 Istio 做為 Ingress 控制器。
將資訊清單套用至叢集:
kubectl apply -f bank-of-anthos/extras/istio/frontend-ingress.yamlPERMISSIVE:工作負載可接受 mTLS 和純文字流量。STRICT:工作負載只接受 mTLS 流量。DISABLE:mTLS 已停用。如要使用自己的安全解決方案,請使用這個模式。請查看下列資訊清單:
這個資訊清單說明對等互連驗證 Istio 自訂資源。
將資訊清單套用至叢集:
kubectl apply -f peer-authentication.yaml使用
roles/monitoring.viewer建立 IAM 服務帳戶,允許查詢介面存取指標:gcloud iam service-accounts create monitoring \ --display-name="Service account for query interface" gcloud projects add-iam-policy-binding PROJECT_ID \ --member "serviceAccount:monitoring@PROJECT_ID.iam.gserviceaccount.com" \ --role roles/monitoring.viewer gcloud iam service-accounts add-iam-policy-binding \ monitoring@PROJECT_ID.iam.gserviceaccount.com \ --role roles/iam.workloadIdentityUser \ --member "serviceAccount:PROJECT_ID.svc.id.goog[monitoring/default]"建立 Kubernetes 命名空間:
kubectl create namespace monitoring在命名空間中註解預設 Kubernetes 服務帳戶,以設定 Workload Identity Federation for GKE:
kubectl annotate serviceaccount -n monitoring default \ iam.gke.io/gcp-service-account=monitoring@PROJECT_ID.iam.gserviceaccount.com --overwrite部署查詢介面工作負載:
kubectl -n monitoring apply -f https://raw.githubusercontent.com/GoogleCloudPlatform/prometheus-engine/v0.7.1/examples/frontend.yaml請查看下列資訊清單:
這份資訊清單說明
PodMonitoring資源,用於收集 Istio 和 Envoy Proxy 指標。將資訊清單套用至叢集:
kubectl apply -f pod-monitorings.yaml取得範例應用程式的連結:
INGRESS_HOST=$(kubectl -n istio-system get service istio-ingressgateway -o jsonpath='{.status.loadBalancer.ingress[0].ip}') echo "http://$INGRESS_HOST"開啟連結即可查看範例應用程式。使用預設使用者名稱和密碼登入,在微服務之間產生流量。
安裝 Kiali Operator:
helm repo add kiali https://kiali.org/helm-charts helm repo update helm install \ --namespace kiali-operator \ --create-namespace \ kiali-operator \ kiali/kiali-operator請查看下列資訊清單:
這個資訊清單說明定義 Kiali 伺服器的 Operator 自訂資源。
將資訊清單套用至叢集:
kubectl apply -f kiali.yaml等待 Kiali 伺服器準備就緒:
watch kubectl get pods -n istio-system輸出結果會與下列內容相似:
NAME READY STATUS RESTARTS AGE istio-ingressgateway-6845466857-92zp8 1/1 Running 0 9m11s istiod-6b47d84cf-4cqlt 1/1 Running 0 12m當 Pod 處於
Running狀態時,請按下Ctrl+C返回指令列。在 Kiali 伺服器服務上設定通訊埠轉送,以便存取資訊主頁:
kubectl -n istio-system port-forward svc/kiali 8080:20001開啟網頁預覽。在 Kiali 中,前往「Graph」部分,然後在「Display」下拉式選單中選取「Security」選項。這個檢視畫面會顯示圖表中每個節點的安全狀態。如果節點顯示「已啟用 mTLS」徽章,表示該服務已啟用 mTLS;如果節點未顯示徽章,表示 mTLS 未啟用。
刪除 Kiali:
kubectl -n istio-system delete kiali kiali helm uninstall --namespace kiali-operator kiali-operator刪除監控資源:
kubectl -n monitoring delete -f https://raw.githubusercontent.com/GoogleCloudPlatform/prometheus-engine/v0.7.1/examples/frontend.yaml刪除範例應用程式:
kubectl delete -f bank-of-anthos/extras/istio/frontend-ingress.yaml kubectl delete -f bank-of-anthos/kubernetes-manifests解除安裝 Istio:
istioctl uninstall --purge -y刪除 GKE 叢集:
gcloud container clusters delete --location us-central1 istio-cluster --quiet- 探索 Google Cloud 的參考架構、圖表和最佳做法。歡迎瀏覽我們的雲端架構中心。
準備環境
如要設定環境,請按照下列步驟操作:
建立 GKE 叢集
啟用 Istio 要求的 Linux 功能:NET_RAW 和 NET_ADMIN。
根據預設,GKE Autopilot 不允許 NET_ADMIN,但您可以在 GKE 1.27 以上版本中,使用 --workload-policies=allow-net-admin
指令啟用 NET_ADMIN:
gcloud container clusters create-auto istio-cluster \
--location="us-central1" \
--workload-policies="allow-net-admin"
如要進一步瞭解 GKE Autopilot 安全性,請參閱「內建安全性設定」。
安裝 Istio
您可以使用 Istioctl 在 GKE 叢集上安裝 Istio。
在本教學課程中,您將安裝 Istio,並使用建議用於實際工作環境部署作業的預設設定檔。
部署範例應用程式
在本節中,您會使用 Bank of Anthos 範例應用程式,建立具有 mTLS 驗證的服務網格。
設定 mTLS
Istio 預設會啟用相互傳輸層安全標準 (mTLS) 驗證。也就是說,Istio 會監控已遷移至 Istio Proxy 的伺服器工作負載,並自動設定用戶端 Proxy,與這些工作負載建立 mTLS 連線。此外,Istio 也會設定用戶端 Proxy,在連線至沒有 Sidecar Proxy 的工作負載時,不要使用 mTLS。
Istio 可將 mTLS 設為下列三種模式:
您可以全域、依命名空間或依工作負載套用 mTLS 設定。在本教學課程中,您將使用 STRICT mTLS 模式,依命名空間套用設定。
如要進一步瞭解 Istio 中的 mTLS,請參閱「相互 TLS 驗證」。
確認已啟用 mTLS
Kiali 是 Istio 服務網格的網頁式可觀測性資訊主頁,可提供微服務環境的圖形檢視畫面,方便您監控及排解應用程式問題。您可以使用 Kiali 驗證 mTLS 驗證是否已啟用,且在 Istio 服務網格中正常運作。Kiali 需要 Prometheus 做為遙測資料來源。本教學課程會使用 Google Cloud Managed Service for Prometheus。
安裝查詢介面
安裝 Kiali
建議您使用 Kiali Operator 安裝 Kiali。
清除所用資源
為避免因為本教學課程所用資源,導致系統向 Google Cloud 收取費用,請刪除含有相關資源的專案,或者保留專案但刪除個別資源。
刪除專案
Delete a Google Cloud project:
gcloud projects delete PROJECT_ID
刪除個別資源
如果您使用現有專案,但不想刪除專案,請刪除個別資源。