GKE on AWS 會在 AWS 虛擬私有雲 (VPC) 中執行。本頁面說明如何為叢集設定新的虛擬私有雲。
GKE on AWS 會在您指定的虛擬私有雲中建立及管理資源。您也必須在虛擬私有雲中建立多個子網路:
- 控制層節點最多三個子網路
- 節點集區的子網路
- Kubernetes Service 負載平衡器的子網路
本頁面適用於想要安裝、設定及支援網路設備的網路專家。如要進一步瞭解 Google Cloud 內容中提及的常見角色和工作範例,請參閱「常見的 GKE 使用者角色和工作」。
虛擬私有雲範例
請按照下列步驟設定下圖所示的虛擬私有雲。如果是實際工作環境用途,您可以根據工作負載,選擇不同的 IP 範圍、可用區、子網路和網路存取控制清單。
下圖顯示按照這些步驟建立的範例虛擬私有雲:
這個範例使用三個可用區:可用區 1、可用區 2、可用區 3。舉例來說,如要在
us-east-1區域建立虛擬私有雲,可以將這些值設為us-east-1a、us-east-1b、us-east-1c。三個可用區中,每個可用區都有一個公用子網路和一個私人子網路。
控制層副本、負載平衡器端點和節點集區,都會在私人子網路中建立。
公開子網路為私人子網路和公開負載平衡器端點提供連出網際網路存取權。
這些子網路都會標記為自動探索子網路。內部負載平衡器會在私人子網路中佈建,而對外負載平衡器則會在公開子網路中佈建。
建立虛擬私有雲
選擇前置字串,並在下方指令中編輯網頁變數 AMC_PREFIX,將其設為您選擇的前置字串。下列範例指令會自動將這個前置字串附加至虛擬私有雲及其資源的所有參照。
建立 AWS 虛擬私有雲 (VPC):
aws --region AWS_REGION ec2 create-vpc \ --cidr-block 10.0.0.0/16 \ --tag-specifications 'ResourceType=vpc, Tags=[{Key=Name,Value=AMC_PREFIXVPC}]'更改下列內容:
AWS_REGION:要建立虛擬私有雲的支援 AWS 區域名稱AMC_PREFIX:您為虛擬私有雲及其資源選擇的虛擬私有雲名稱前置字串
將虛擬私有雲 ID 儲存至環境變數,並為虛擬私有雲啟用 AWS 提供的 DNS 支援:
VPC_ID=$(aws ec2 describe-vpcs \ --filters 'Name=tag:Name,Values=AMC_PREFIXVPC' \ --query "Vpcs[].VpcId" --output text) aws ec2 modify-vpc-attribute --enable-dns-hostnames --vpc-id $VPC_ID aws ec2 modify-vpc-attribute --enable-dns-support --vpc-id $VPC_ID您也可以為虛擬私有雲使用不同的 DNS 設定。詳情請參閱「AWS VPC DNS」。
控制層子網路
您最多可以為控制層副本設定三個子網路。如果指定的子網路少於三個,AWS 適用的 GKE 會建立三個控制層副本,並將其分配到指定的子網路。
叢集僅限您的虛擬私有雲使用。不允許從網際網路直接傳入叢集。GKE on AWS 需要有限的傳出網際網路存取權,才能建立及管理叢集。這個範例使用網際網路閘道進行傳出存取。
子網路需求
子網路必須
- 能夠解析 DNS 位址
- 能夠在通訊埠 443 上建立傳出 TCP 連線,連至可轉送的 IP 位址
- 能夠連線至下列端點:
| 端點 | 目的 |
|---|---|
| storage.googleapis.com | 在安裝期間從 Cloud Storage 下載 |
| *.gcr.io | 在安裝期間從 Container Registry 下載 |
| gkeconnect.googleapis.com | 連線至管理服務 |
| oauth2.googleapis.com | 叢集驗證 |
| sts.googleapis.com | 叢集驗證 |
| logging.googleapis.com | 將記錄檔傳送至 Cloud Logging |
| monitoring.googleapis.com | 將指標傳送至 Cloud Monitoring |
| opsconfigmonitoring.googleapis.com` | 將資源中繼資料傳送至 Cloud Monitoring |
| servicecontrol.googleapis.com | Cloud 稽核記錄 |
如果沒有控制層執行個體的子網路,請使用下列指令建立。
建立私人子網路
在對應的可用區中建立三個私人子網路:
aws ec2 create-subnet \
--availability-zone AWS_ZONE_1 \
--vpc-id $VPC_ID \
--cidr-block 10.0.1.0/24 \
--tag-specifications 'ResourceType=subnet, Tags=[{Key=Name,Value=AMC_PREFIXPrivateSubnet1}]'
aws ec2 create-subnet \
--availability-zone AWS_ZONE_2 \
--vpc-id $VPC_ID \
--cidr-block 10.0.2.0/24 \
--tag-specifications 'ResourceType=subnet, Tags=[{Key=Name,Value=AMC_PREFIXPrivateSubnet2}]'
aws ec2 create-subnet \
--availability-zone AWS_ZONE_3 \
--vpc-id $VPC_ID \
--cidr-block 10.0.3.0/24 \
--tag-specifications 'ResourceType=subnet, Tags=[{Key=Name,Value=AMC_PREFIXPrivateSubnet3}]'
更改下列內容:
建立公開子網路
建立三個公開子網路。這些資源將用於為私人子網路提供連出網際網路存取權。
aws ec2 create-subnet \ --availability-zone AWS_ZONE_1 \ --vpc-id $VPC_ID \ --cidr-block 10.0.101.0/24 \ --tag-specifications 'ResourceType=subnet, Tags=[{Key=Name,Value=AMC_PREFIXPublicSubnet1}]' aws ec2 create-subnet \ --availability-zone AWS_ZONE_2 \ --vpc-id $VPC_ID \ --cidr-block 10.0.102.0/24 \ --tag-specifications 'ResourceType=subnet, Tags=[{Key=Name,Value=AMC_PREFIXPublicSubnet2}]' aws ec2 create-subnet \ --availability-zone AWS_ZONE_3 \ --vpc-id $VPC_ID \ --cidr-block 10.0.103.0/24 \ --tag-specifications 'ResourceType=subnet, Tags=[{Key=Name,Value=AMC_PREFIXPublicSubnet3}]'更改下列內容:
AWS_ZONE_1AWS_ZONE_2AWS_ZONE_3
將子網路標示為公開:
PUBLIC_SUBNET_ID_1=$(aws ec2 describe-subnets \ --filters 'Name=tag:Name,Values=AMC_PREFIXPublicSubnet1' \ --query "Subnets[].SubnetId" --output text) PUBLIC_SUBNET_ID_2=$(aws ec2 describe-subnets \ --filters 'Name=tag:Name,Values=AMC_PREFIXPublicSubnet2' \ --query "Subnets[].SubnetId" --output text) PUBLIC_SUBNET_ID_3=$(aws ec2 describe-subnets \ --filters 'Name=tag:Name,Values=AMC_PREFIXPublicSubnet3' \ --query "Subnets[].SubnetId" --output text) aws ec2 modify-subnet-attribute \ --map-public-ip-on-launch \ --subnet-id $PUBLIC_SUBNET_ID_1 aws ec2 modify-subnet-attribute \ --map-public-ip-on-launch \ --subnet-id $PUBLIC_SUBNET_ID_2 aws ec2 modify-subnet-attribute \ --map-public-ip-on-launch \ --subnet-id $PUBLIC_SUBNET_ID_3
建立網際網路閘道
建立網際網路閘道,讓公開子網路可以存取網際網路:
aws --region AWS_REGION ec2 create-internet-gateway \ --tag-specifications 'ResourceType=internet-gateway, Tags=[{Key=Name,Value=AMC_PREFIXInternetGateway}]'將
AWS_REGION替換為建立虛擬私有雲的 AWS 區域名稱。將網際網路閘道附加至虛擬私有雲:
INTERNET_GW_ID=$(aws ec2 describe-internet-gateways \ --filters 'Name=tag:Name,Values=AMC_PREFIXInternetGateway' \ --query "InternetGateways[].InternetGatewayId" --output text) aws ec2 attach-internet-gateway \ --internet-gateway-id $INTERNET_GW_ID \ --vpc-id $VPC_ID
設定公開子網路的路由表
為每個公開子網路建立路由表。
aws ec2 create-route-table --vpc-id $VPC_ID \ --tag-specifications 'ResourceType=route-table, Tags=[{Key=Name,Value=AMC_PREFIXPublicRouteTbl1}]' aws ec2 create-route-table --vpc-id $VPC_ID \ --tag-specifications 'ResourceType=route-table, Tags=[{Key=Name,Value=AMC_PREFIXPublicRouteTbl2}]' aws ec2 create-route-table --vpc-id $VPC_ID \ --tag-specifications 'ResourceType=route-table, Tags=[{Key=Name,Value=AMC_PREFIXPublicRouteTbl3}]'將公開路由表與公開子網路建立關聯:
PUBLIC_ROUTE_TABLE_ID_1=$(aws ec2 describe-route-tables \ --filters 'Name=tag:Name,Values=AMC_PREFIXPublicRouteTbl1' \ --query "RouteTables[].RouteTableId" --output text) PUBLIC_ROUTE_TABLE_ID_2=$(aws ec2 describe-route-tables \ --filters 'Name=tag:Name,Values=AMC_PREFIXPublicRouteTbl2' \ --query "RouteTables[].RouteTableId" --output text) PUBLIC_ROUTE_TABLE_ID_3=$(aws ec2 describe-route-tables \ --filters 'Name=tag:Name,Values=AMC_PREFIXPublicRouteTbl3' \ --query "RouteTables[].RouteTableId" --output text) aws ec2 associate-route-table \ --route-table-id $PUBLIC_ROUTE_TABLE_ID_1 \ --subnet-id $PUBLIC_SUBNET_ID_1 aws ec2 associate-route-table \ --route-table-id $PUBLIC_ROUTE_TABLE_ID_2 \ --subnet-id $PUBLIC_SUBNET_ID_2 aws ec2 associate-route-table \ --route-table-id $PUBLIC_ROUTE_TABLE_ID_3 \ --subnet-id $PUBLIC_SUBNET_ID_3建立通往網際網路閘道的預設路由:
aws ec2 create-route --route-table-id $PUBLIC_ROUTE_TABLE_ID_1 \ --destination-cidr-block 0.0.0.0/0 --gateway-id $INTERNET_GW_ID aws ec2 create-route --route-table-id $PUBLIC_ROUTE_TABLE_ID_2 \ --destination-cidr-block 0.0.0.0/0 --gateway-id $INTERNET_GW_ID aws ec2 create-route --route-table-id $PUBLIC_ROUTE_TABLE_ID_3 \ --destination-cidr-block 0.0.0.0/0 --gateway-id $INTERNET_GW_ID為每個 NAT 閘道分配一個彈性 IP (EIP) 位址:
aws ec2 allocate-address \ --tag-specifications 'ResourceType=elastic-ip, Tags=[{Key=Name,Value=AMC_PREFIXNatEip1}]' aws ec2 allocate-address \ --tag-specifications 'ResourceType=elastic-ip, Tags=[{Key=Name,Value=AMC_PREFIXNatEip2}]' aws ec2 allocate-address \ --tag-specifications 'ResourceType=elastic-ip, Tags=[{Key=Name,Value=AMC_PREFIXNatEip3}]'
建立 NAT 閘道
在三個公開子網路中分別建立 NAT 閘道:
NAT_EIP_ALLOCATION_ID_1=$(aws ec2 describe-addresses \
--filters 'Name=tag:Name,Values=AMC_PREFIXNatEip1' \
--query "Addresses[].AllocationId" --output text)
NAT_EIP_ALLOCATION_ID_2=$(aws ec2 describe-addresses \
--filters 'Name=tag:Name,Values=AMC_PREFIXNatEip2' \
--query "Addresses[].AllocationId" --output text)
NAT_EIP_ALLOCATION_ID_3=$(aws ec2 describe-addresses \
--filters 'Name=tag:Name,Values=AMC_PREFIXNatEip3' \
--query "Addresses[].AllocationId" --output text)
aws ec2 create-nat-gateway \
--allocation-id $NAT_EIP_ALLOCATION_ID_1 \
--subnet-id $PUBLIC_SUBNET_ID_1 \
--tag-specifications 'ResourceType=natgateway, Tags=[{Key=Name,Value=AMC_PREFIXNatGateway1}]'
aws ec2 create-nat-gateway \
--allocation-id $NAT_EIP_ALLOCATION_ID_2 \
--subnet-id $PUBLIC_SUBNET_ID_2 \
--tag-specifications 'ResourceType=natgateway, Tags=[{Key=Name,Value=AMC_PREFIXNatGateway2}]'
aws ec2 create-nat-gateway \
--allocation-id $NAT_EIP_ALLOCATION_ID_3 \
--subnet-id $PUBLIC_SUBNET_ID_3 \
--tag-specifications 'ResourceType=natgateway, Tags=[{Key=Name,Value=AMC_PREFIXNatGateway3}]'
設定私人子網路的轉送表
為每個私人子網路建立路由表:
aws ec2 create-route-table --vpc-id $VPC_ID \ --tag-specifications 'ResourceType=route-table, Tags=[{Key=Name,Value=AMC_PREFIXPrivateRouteTbl1}]' aws ec2 create-route-table --vpc-id $VPC_ID \ --tag-specifications 'ResourceType=route-table, Tags=[{Key=Name,Value=AMC_PREFIXPrivateRouteTbl2}]' aws ec2 create-route-table --vpc-id $VPC_ID \ --tag-specifications 'ResourceType=route-table, Tags=[{Key=Name,Value=AMC_PREFIXPrivateRouteTbl3}]'將私人路由表與私人子網路建立關聯:
PRIVATE_SUBNET_ID_1=$(aws ec2 describe-subnets \ --filters 'Name=tag:Name,Values=AMC_PREFIXPrivateSubnet1' \ --query "Subnets[].SubnetId" --output text) PRIVATE_SUBNET_ID_2=$(aws ec2 describe-subnets \ --filters 'Name=tag:Name,Values=AMC_PREFIXPrivateSubnet2' \ --query "Subnets[].SubnetId" --output text) PRIVATE_SUBNET_ID_3=$(aws ec2 describe-subnets \ --filters 'Name=tag:Name,Values=AMC_PREFIXPrivateSubnet3' \ --query "Subnets[].SubnetId" --output text) PRIVATE_ROUTE_TABLE_ID_1=$(aws ec2 describe-route-tables \ --filters 'Name=tag:Name,Values=AMC_PREFIXPrivateRouteTbl1' \ --query "RouteTables[].RouteTableId" --output text) PRIVATE_ROUTE_TABLE_ID_2=$(aws ec2 describe-route-tables \ --filters 'Name=tag:Name,Values=AMC_PREFIXPrivateRouteTbl2' \ --query "RouteTables[].RouteTableId" --output text) PRIVATE_ROUTE_TABLE_ID_3=$(aws ec2 describe-route-tables \ --filters 'Name=tag:Name,Values=AMC_PREFIXPrivateRouteTbl3' \ --query "RouteTables[].RouteTableId" --output text) aws ec2 associate-route-table --route-table-id $PRIVATE_ROUTE_TABLE_ID_1 \ --subnet-id $PRIVATE_SUBNET_ID_1 aws ec2 associate-route-table --route-table-id $PRIVATE_ROUTE_TABLE_ID_2 \ --subnet-id $PRIVATE_SUBNET_ID_2 aws ec2 associate-route-table --route-table-id $PRIVATE_ROUTE_TABLE_ID_3 \ --subnet-id $PRIVATE_SUBNET_ID_3建立 NAT 閘道的預設路由:
NAT_GW_ID_1=$(aws ec2 describe-nat-gateways \ --filter 'Name=tag:Name,Values=AMC_PREFIXNatGateway1' \ --query "NatGateways[].NatGatewayId" --output text) NAT_GW_ID_2=$(aws ec2 describe-nat-gateways \ --filter 'Name=tag:Name,Values=AMC_PREFIXNatGateway2' \ --query "NatGateways[].NatGatewayId" --output text) NAT_GW_ID_3=$(aws ec2 describe-nat-gateways \ --filter 'Name=tag:Name,Values=AMC_PREFIXNatGateway3' \ --query "NatGateways[].NatGatewayId" --output text) aws ec2 create-route --route-table-id $PRIVATE_ROUTE_TABLE_ID_1 \ --destination-cidr-block 0.0.0.0/0 --gateway-id $NAT_GW_ID_1 aws ec2 create-route --route-table-id $PRIVATE_ROUTE_TABLE_ID_2 \ --destination-cidr-block 0.0.0.0/0 --gateway-id $NAT_GW_ID_2 aws ec2 create-route --route-table-id $PRIVATE_ROUTE_TABLE_ID_3 \ --destination-cidr-block 0.0.0.0/0 --gateway-id $NAT_GW_ID_3
節點集區子網路
每個節點集區都位於單一子網路中。您可以在子網路中放置多個節點集區。可建立的節點和節點集區數量,取決於子網路中的可用 IP 位址範圍。
每個節點集區子網路都必須:
- 符合與控制層子網路相同的對外網際網路存取需求
- 有足夠的 IP 位址空間,可涵蓋節點集區的大小
- 未啟用「自動指派公開 IP」
先前建立的私人子網路符合控制層子網路和節點集區子網路的需求。
Service 負載平衡器子網路
如果您要建立網路負載平衡器或 HTTP 負載平衡器,請標記負載平衡器子網路,以便自動探索。
使用 kubernetes.io/role/elb 標記公開子網路:
aws ec2 create-tags \
--resources $PUBLIC_SUBNET_ID_1 \
--tags Key=kubernetes.io/role/elb,Value=1
aws ec2 create-tags \
--resources $PUBLIC_SUBNET_ID_2 \
--tags Key=kubernetes.io/role/elb,Value=1
aws ec2 create-tags \
--resources $PUBLIC_SUBNET_ID_3 \
--tags Key=kubernetes.io/role/elb,Value=1
使用 kubernetes.io/role/internal-elb 標記私人子網路:
aws ec2 create-tags \
--resources $PRIVATE_SUBNET_ID_1 \
--tags Key=kubernetes.io/role/internal-elb,Value=1
aws ec2 create-tags \
--resources $PRIVATE_SUBNET_ID_2 \
--tags Key=kubernetes.io/role/internal-elb,Value=1
aws ec2 create-tags \
--resources $PRIVATE_SUBNET_ID_3 \
--tags Key=kubernetes.io/role/internal-elb,Value=1