AWS 上的 GKE 會在 AWS 虛擬私有雲 (VPC) 中執行。本頁說明如何為叢集設定新的 VPC。
GKE on AWS 會在您指定的 VPC 中建立及管理資源。您也必須在虛擬私有雲中建立多個子網路:
- 控制層節點最多可有三個子網路
- 節點集區的子網路
- Kubernetes Service 負載平衡器的子網路
本頁內容適用於想要安裝、設定及支援網路設備的網路專家。如要進一步瞭解內容中提及的常見角色和範例工作,請參閱「常見的 GKE Enterprise 使用者角色和工作」。 Google Cloud
虛擬私有雲範例
請按照下列步驟設定下圖所示的 VPC。針對自己的實際運作用途,您可以選擇適合工作負載的不同 IP 範圍、可用區、子網路和網路存取控制清單。
下圖顯示您按照這些步驟建立的範例 VPC:
這個範例使用三個可用區:可用區 1、可用區 2 和可用區 3。舉例來說,如要在
us-east-1
地區建立 VPC,這些值可以設為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
:您為 VPC 及其資源選擇的 VPC 名稱前置字串
將 VPC ID 儲存至環境變數,並為 VPC 啟用 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
您也可以為 VPC 使用不同的 DNS 設定。詳情請參閱「AWS 虛擬私有雲 DNS」。
控制層子網路
您最多可以為控制層副本設定三個子網路。如果您指定的子網路少於三個,GKE on AWS 會建立三個控制層副本,並將其分配到指定的子網路。
叢集是虛擬私有雲的私有資源。不允許從網際網路直接存取叢集。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_1
AWS_ZONE_2
AWS_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」
先前建立的私有子網路符合控制層子網路和節點集區子網路的要求。
服務負載平衡器子網路
如果您要建立網路負載平衡器或 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