將 Public NAT 與 GKE 搭配使用
本頁說明如何使用 Google Kubernetes Engine (GKE) 設定範例 Public NAT。設定 Public NAT 前,請先參閱「Public NAT 總覽」。
必要條件
設定 Public NAT 前,請先完成下列步驟。
取得 IAM 權限
roles/compute.networkAdmin 角色可授權您在 Cloud Router 上建立 NAT 閘道、預留及指派 NAT IP 位址,以及指定流量應透過 NAT 閘道使用網路位址轉譯的子網路。
設定「 Google Cloud」
開始之前,請先在 Google Cloud中設定下列項目。
- 登入 Google Cloud 帳戶。如果您是 Google Cloud新手,歡迎 建立帳戶,親自體驗產品的實際應用成效。新客戶還能獲得價值 $300 美元的免費抵免額,能用於執行、測試及部署工作負載。
-
In the Google Cloud console, on the project selector page, select or create 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.
-
Verify that billing is enabled for your Google Cloud project.
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init -
In the Google Cloud console, on the project selector page, select or create 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.
-
Verify that billing is enabled for your Google Cloud project.
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
設定 GKE 範例
如果您想瞭解如何搭配使用簡單的 Public NAT 設定與 GKE,請參閱這個範例。
步驟 1:建立虛擬私有雲網路和子網路
如果已有網路和子網路,可以略過這個步驟。
控制台
前往 Google Cloud 控制台的「VPC Networks」(虛擬私有雲網路) 頁面。
按一下「建立虛擬私有雲網路」。
輸入
custom-network1的「名稱」。在「子網路」下方,將「子網路建立模式」設為「自訂」。
在「New subnet」(新的子網路) 下方,輸入
subnet-us-east-192的「Name」(名稱)。在「區域」部分,選取「us-east4」。
輸入
192.168.1.0/24的「IP address range」(IP 位址範圍)。依序點選「完成」>「建立」。
gcloud
在專案中建立新的自訂模式虛擬私有雲 (VPC) 網路:
gcloud compute networks create custom-network1 \ --subnet-mode custom輸出內容:
NAME MODE IPV4_RANGE GATEWAY_IPV4 custom-network1 custom
指定第一個區域的子網路前置字串。在本範例中,我們將
192.168.1.0/24指派給us-east4區域。gcloud compute networks subnets create subnet-us-east-192 \ --network custom-network1 \ --region us-east4 \ --range 192.168.1.0/24
輸出內容:
NAME REGION NETWORK RANGE subnet-us-east-192 us-east4 custom-network1 192.168.1.0/24
Terraform
您可以使用 Terraform 模組建立自訂虛擬私有雲網路和子網路。
步驟 2:建立私人叢集
控制台
前往 Google Cloud 控制台的「Kubernetes clusters」(Kubernetes 叢集) 頁面。
點選「建立叢集」。
在「Name」(名稱) 中輸入
nat-test-cluster。將「位置類型」設為「可用區」。
將「可用區」設為 us-east4-c。
在導覽窗格中,按一下「Networking」(網路)。
選取「私人叢集」。
取消勾選「使用外部 IP 位址存取控制層」核取方塊。
輸入
172.16.0.0/28的控制層 IP 範圍。將「網路」設為「
custom-network1」。如要建立並啟動叢集,請按一下「Create」(建立)。
gcloud
gcloud container clusters create "nat-test-cluster" \
--zone "us-east4-c" \
--cluster-version "latest" \
--machine-type "e2-medium" \
--disk-type "pd-standard" \
--disk-size "100" \
--scopes "https://www.googleapis.com/auth/compute","https://www.googleapis.com/auth/devstorage.read_only","https://www.googleapis.com/auth/logging.write","https://www.googleapis.com/auth/monitoring","https://www.googleapis.com/auth/servicecontrol","https://www.googleapis.com/auth/service.management.readonly","https://www.googleapis.com/auth/trace.append" \
--num-nodes "3" \
--enable-private-nodes \
--enable-private-endpoint \
--master-ipv4-cidr "172.16.0.0/28" \
--enable-ip-alias \
--network "projects/PROJECT_ID/global/networks/custom-network1" \
--subnetwork "projects/PROJECT_ID/regions/us-east4/subnetworks/subnet-us-east-192" \
--max-nodes-per-pool "110" \
--enable-master-authorized-networks \
--addons HorizontalPodAutoscaling,HttpLoadBalancing \
--enable-autoupgrade \
--enable-autorepair
Terraform
您可以使用 Terraform 資源建立私人叢集。
步驟 3:建立允許 SSH 連線的防火牆規則
控制台
前往 Google Cloud 控制台的「Firewall policies」(防火牆政策) 頁面。
按一下「Create firewall rule」(建立防火牆規則)。
輸入
allow-ssh的「名稱」。指定
custom-network1的「Network」(網路)。將「流量方向」設為「輸入」。
將「相符時執行的動作」設為「允許」。
將「目標」設為「網路中的所有執行個體」。
將「Source filter」(來源篩選器) 設為「IPv4 ranges」(IPv4 範圍)。
將「Source IP ranges」(來源 IP 範圍) 設為
35.235.240.0/20。將「通訊協定和通訊埠」設為「指定的通訊協定和通訊埠」。
勾選「tcp」tcp核取方塊,然後輸入通訊埠
22。點選「建立」。
gcloud
gcloud compute firewall-rules create allow-ssh \
--network custom-network1 \
--source-ranges 35.235.240.0/20 \
--allow tcp:22Terraform
您可以使用 Terraform 資源建立防火牆規則。
步驟 4:為其中一個節點建立 IAP SSH 權限
在後續步驟中,您會使用 IAP 連線至節點。
控制台
前往 Google Cloud 控制台的「Identity-Aware Proxy」頁面。
選取「SSH 和 TCP 資源」分頁標籤。
在「All Tunnel Resources」>「us-east4-c」下方的清單中,勾選第一個節點旁的核取方塊。名稱會類似
gke-nat-test-cluster-default-pool-b50db58d-075t。記下節點名稱,稍後會用來測試連線。
在右側窗格中,按一下「新增主體」。
如要授予使用者、群組或服務帳戶資源存取權,請在「New principals」(新增主體) 欄位中指定他們的電子郵件地址。
如果只是測試這項功能,可以輸入自己的電子郵件地址。
如要透過 Cloud IAP 的 TCP 轉送功能,授予主體資源存取權,請在「角色」下拉式清單中,依序選取「Cloud IAP」>「受 IAP 保護的通道使用者」。
按一下 [儲存]。
gcloud
請按照控制台操作說明執行這個步驟。
步驟 5:登入節點,確認節點無法連上網際網路
控制台
前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面。
找出您建立 IAP SSH 權限的節點。在「連線」欄中,按一下「SSH」下拉式箭頭,然後選取「在瀏覽器視窗中開啟」。
如果您是第一次連線至執行個體, Google Cloud 系統會為您產生安全殼層金鑰。
在節點提示中,找出
kube-dns容器的程序 ID:pgrep '^kube-dns$'
存取容器:
sudo nsenter --target PROCESS_ID --net /bin/bash
從
kube-dns嘗試連上網際網路:curl example.com
您應該不會看到任何結果。如果沒有,可能是因為您未將叢集建立為私人叢集,或是發生其他問題。如要進行疑難排解,請參閱「VM 可以在沒有 Public NAT 的情況下連上網際網路」。
如要結束指令,可能需要輸入
Ctrl+C。
gcloud
找出其中一個叢集節點的名稱:
gcloud compute instances list
節點名稱看起來像
gke-nat-test-cluster-default-pool-1a4cbd06-3m8v。請記下節點名稱,並在下列指令中看到NODE_NAME時使用該名稱。連線至節點:
gcloud compute ssh NODE_NAME \ --zone us-east4-c \ --tunnel-through-iap在節點提示中,找出
kube-dns容器的程序 ID:pgrep '^kube-dns$'
存取容器:
sudo nsenter --target PROCESS_ID --net /bin/bash
從
kube-dns嘗試連上網際網路:curl example.com
您應該不會看到任何結果。如要結束指令,可能需要輸入
Ctrl+C。
步驟 6:使用 Cloud Router 建立 NAT 設定
您必須在與使用 Public NAT 的執行個體相同的區域中建立 Cloud Router。Public NAT 僅用於將 NAT 資訊放置到 VM 上。不會做為實際 NAT 閘道的一部分。
這項設定可讓區域中的所有執行個體,針對所有主要和別名 IP 範圍使用 Public NAT。此外,系統也會自動為 NAT 閘道分配外部 IP 位址。如需更多選項,請參閱 Google Cloud CLI 說明文件。
控制台
前往 Google Cloud 控制台的「Cloud NAT」頁面。
按一下 [Get started] (開始使用) 或 [Create NAT gateway] (建立 NAT 閘道)。
輸入
nat-config的「閘道名稱」。將「虛擬私有雲網路」設為
custom-network1。將「區域」設為 us-east4。
在「Cloud Router」部分,選取「建立新路由器」。
- 輸入
nat-router的「名稱」。 - 點選「建立」。
- 輸入
點選「建立」。
gcloud
建立 Cloud Router:
gcloud compute routers create nat-router \ --network custom-network1 \ --region us-east4在路由器中新增設定:
gcloud compute routers nats create nat-config \ --router-region us-east4 \ --router nat-router \ --nat-all-subnet-ip-ranges \ --auto-allocate-nat-external-ips
Terraform
您可以使用 Terraform 資源建立 Cloud Router。
您可以使用 Terraform 模組建立 NAT 設定。
步驟 7:再次嘗試連上網際網路
NAT 設定最多需要 3 分鐘才會套用,因此請先至少等待 1 分鐘,再重新嘗試存取網際網路。
如果您尚未登入 kube-dns,請按照步驟 5 的程序重新連線。登入後,請重新執行 curl 指令:
curl example.com
輸出內容應包含下列內容:
<html>
<head>
<title>Example Domain</title>
...
...
...
</head>
<body>
<div>
<h1>Example Domain</h1>
<p>This domain is established to be used for illustrative examples in documents. You can use this
domain in examples without prior coordination or asking for permission.</p>
<p><a href="http://www.iana.org/domains/example">More information...</a></p>
</div>
</body>
</html>