探索 GKE NodeLocal DNSCache

1. 總覽

DNS 快取會先將 Pod DNS 要求傳送至同一節點上的本機快取,藉此縮短 DNS 查詢延遲時間。這樣一來,DNS 查詢時間會更一致,且可減少傳送至 kube-dns 或 Cloud DNS 的 DNS 查詢數量。

在本實驗室中,您將測試 NodeLocal DNSCache 如何處理 GKE 叢集中的 DNS 流量。您將驗證執行 1.34.1-gke.3720000 以上版本的 GKE Standard 叢集,確認預設已啟用。接著停用這項功能,看看關閉後設定會如何變化。

目標

在本實驗室,您將瞭解如何執行下列工作:

  • 建立自訂虛擬私有雲、子網路和防火牆規則
  • 部署標準 GKE Standard 叢集,並使用快速發布版本
  • 執行測試,確認已啟用 LocalNode DNS 快取
  • 停用快取,並在沒有快取的情況下驗證狀態

2. 設定實驗室

自修實驗室環境設定

  1. 登入 Google Cloud 控制台,建立新專案或重複使用現有專案。如果您沒有 Gmail 或 Google Workspace 帳戶,請建立帳戶。

295004821bab6a87.png37d264871000675d.png96d86d3d5655cdbe.png

  • 「專案名稱」是這項專案參與者的顯示名稱。這是一串 Google API 無法識別的字元。你隨時可以更新。
  • 專案 ID 在所有 Google Cloud 專案中都是不重複的,且設定後即無法變更。Cloud 控制台會自動產生一組不重複的字串,通常您不需要知道這組字串。在大多數程式碼實驗室中,您必須參照專案 ID (通常會標示為 PROJECT_ID)。如果您不喜歡系統產生的 ID,可以產生另一組隨機 ID。或者,您也可以嘗試使用自己的名稱,看看是否可用。這個步驟完成後即無法變更,且在專案期間會保持不變。
  • 此外,還有第三個值,也就是「專案編號」,某些 API 會使用這個值。如要進一步瞭解這三個值,請參閱說明文件。
  1. 接著,您必須在 Cloud 控制台中啟用計費功能,才能使用 Cloud 資源/API。完成這個程式碼實驗室的費用極低,甚至可能完全免費。如要關閉資源,避免在本教學課程結束後產生費用,可以刪除建立的資源或刪除專案。新註冊 Google Cloud 的使用者可獲得 $300 美元的免費試用額度。

啟動 Cloud Shell

雖然您可以透過筆電遠端操作 Google Cloud,但在本程式碼實驗室中,您將使用 Google Cloud Shell,這是在雲端執行的指令列環境。

在 Google Cloud 控制台右上方的工具列,點選「Cloud Shell」圖示:

啟用 Cloud Shell

佈建並連線至環境的作業需要一些時間才能完成。完成後,您應該會看到類似下方的內容:

Google Cloud Shell 終端機螢幕截圖,顯示環境已連線

這個虛擬機器已預先安裝所有必要的開發人員工具,並提供永久的 5 GB 主目錄,而且可在 Google Cloud 運作,大幅提升網路效能並強化驗證功能。本程式碼實驗室的所有工作都可以在瀏覽器中完成。您不需要安裝任何項目。

3. 設定環境

我們將建立含有防火牆規則的自訂虛擬私有雲。如果您已有虛擬私有雲和專案,可以略過這個部分。

開啟 Cloud Shell (位於控制台右上方)。然後依照下列方式設定:b51b80043d3bac90.png

  1. 啟用本實驗室會用到的 API
gcloud services enable dns.googleapis.com
gcloud services enable servicedirectory.googleapis.com
gcloud services enable container.googleapis.com
  1. 設定一些變數。這些變數是專案 ID 和虛擬私有雲名稱 (您將在步驟 3 建立虛擬私有雲)。
export projectid=$(gcloud config get-value project)
export vpc_name=gke-cache-vpc
export subnet_name=mainsubnet
export region=us-east1
export zone=us-east1-b
export cluster_name=cache-gke-cluster
export channel=rapid

export machine_type=e2-standard-4
echo $projectid
echo $vpc_name
  1. 接著,建立名為 gke-cache-vpc 的自訂虛擬私有雲
gcloud compute networks create $vpc_name --subnet-mode=custom --project=$projectid \
--subnet-mode=custom \
--mtu=1460 \
--bgp-routing-mode=global
  1. 在新虛擬私有雲中建立子網路
gcloud compute networks subnets create $subnet_name \
    --network=$vpc_name \
    --range=10.0.88.0/24 \
    --region=$region \
    --enable-private-ip-google-access \
    --project=$projectid
  1. 在虛擬私有雲中新增防火牆規則
gcloud compute firewall-rules create $vpc_name-allow-internal \
    --network=$vpc_name --allow=tcp,udp,icmp --source-ranges=10.0.88.0/24

gcloud compute firewall-rules create $vpc_name-allow-ssh \
    --network=$vpc_name --allow=tcp:22 --source-ranges=35.235.240.0/20

4. 建立用於網際網路通訊的 NAT 閘道

我們需要授予網際網路的傳出外部存取權,因此請建立 Cloud NAT 閘道並附加。

在 Cloud Shell 中使用下列指令

  1. 建立 Cloud NAT 和 NAT 閘道
gcloud compute routers create gke-nat-router --network=$vpc_name --region=$region

gcloud compute routers nats create gke-nat-gw \
    --router=gke-nat-router --region=$region \
    --auto-allocate-nat-external-ips --nat-all-subnet-ip-ranges

5. 部署 GKE 叢集並驗證

  1. 在 Google Cloud Shell 中建立名為 cache-gke-cluster 的叢集。執行 1.34.1-gke.3720000 以上版本的 GKE Standard 叢集預設會啟用 NodeLocal DNSCache。(叢集佈建作業應會在 4 到 10 分鐘內完成)
gcloud container clusters create $cluster_name \
--zone=$zone --network=$vpc_name --subnetwork=$subnet_name \
--release-channel=$channel --machine-type=$machine_type \
--enable-ip-alias
  1. 叢集建立完成後,請連線:
gcloud container clusters get-credentials $cluster_name --zone $zone
  1. 現在,我們來確認 NodeLocal DNSCache 是否已啟用。

這些指令會確認版本為 1.34.1-gke.3720000 以上,並確認本機代理程式和服務正在執行

kubectl version | grep "Server Version"

kubectl get pods -n kube-system -o wide | grep node-local-dns -w

kubectl get svc,endpoints -n kube-system -l k8s-app=kube-dns
  1. 接著執行下列指令 (這會在主機網路上建立具特殊權限的 Pod,確認節點的 iptables 規則會主動攔截 DNS 流量,並將其轉送至本機快取):
export KUBEDNS_IP=$(kubectl get svc kube-dns -n kube-system -o jsonpath='{.spec.clusterIP}')

kubectl run node-inspector --quiet --rm -it --image=alpine --privileged --restart=Never \
  --overrides='{"spec": {"hostNetwork": true}}' -- \
  sh -c "apk add --no-cache iptables && iptables-save | grep -E '169.254.20.10|$KUBEDNS_IP'"

檢查重點:尋找 -j NOTRACK.,確認 DNS 流量略過連線追蹤表格。

7135447929b7c48f.png

6. 停用 NodeLocal DNSCache 並驗證

現在我們移除最佳化,看看沒有最佳化時的運作方式。

  1. 前往 Cloud Shell 並執行下列指令。注意:這項操作會觸發節點重建作業,由於 GKE 會循環執行個體,因此每個節點集區通常需要 3 到 5 分鐘
gcloud container clusters update $cluster_name --zone=$zone --update-addons=NodeLocalDNS=DISABLED --quiet

kubectl get pods -n kube-system -o wide | grep node-local-dns -w

由於這些 Pod 已移除,因此 DaemonSet 中不應顯示任何 Pod。

  1. 重新執行測試
kubectl run node-inspector --quiet --rm -it --image=alpine --privileged --restart=Never \
  --overrides='{"spec": {"hostNetwork": true}}' -- \
  sh -c "apk add --no-cache iptables && iptables-save | grep -E '169.254.20.10|$KUBEDNS_IP'"

停用外掛程式後,輸出內容就不會再包含 -j NOTRACK 規則,或提及 169.254.20.10 IP 位址。這表示您無法再享有本機快取的優點

4693ee556701e145.png

7. 清除

# 1. Delete the GKE Cluster
# This will remove the node and all system pods (including kube-dns)
gcloud container clusters delete $cluster_name --zone=$zone --quiet

# 2. Delete the Cloud NAT and Router
# It is best practice to remove these before the VPC
gcloud compute routers nats delete gke-nat-gw --router=gke-nat-router --region=$region --quiet
gcloud compute routers delete gke-nat-router --region=$region --quiet

# 3. Delete the Firewall Rules
gcloud compute firewall-rules delete $vpc_name-allow-internal $vpc_name-allow-ssh --quiet

# 4. Delete the Subnet and VPC
gcloud compute networks subnets delete $subnet_name --region=$region --quiet
gcloud compute networks delete $vpc_name --quiet

8. 後續步驟/瞭解詳情

如要進一步瞭解 GKE 網路,請參閱說明文件和用途。

Codelab:透過 Private Service Connect 端點,使用 Python SDK 存取 Gemini 3 Pro 對話

Codelab:使用 ADK 建構 AI 代理:基礎知識

挑戰下一個實驗室

繼續探索 Google Cloud,並查看下列其他 Google Cloud Skills Boost 實驗室: