本指南提供操作說明,說明如何使用區域性後端服務,為每個虛擬機器 (VM) 例項建立權重外部直通式網路負載平衡器部署作業。
在本教學課程中,您會建立一個含有三個 VM 執行個體的執行個體群組,並為每個執行個體指派權重。您可以建立 HTTP 健康狀態檢查,以便回報後端執行個體權重。在後端服務上啟用加權負載平衡功能,並將地區負載平衡器政策設為 WEIGHTED_MAGLEV
。
事前準備
- 參閱後端服務型外部直通式網路負載平衡器總覽。
安裝 Google Cloud CLI。如需工具的完整總覽,請參閱 gcloud CLI 總覽。您可以在 API 和 gcloud CLI 參考資料中找到與負載平衡相關的指令。
如果您之前沒有執行過 Google Cloud CLI,請先執行
gcloud init
進行驗證。啟用運算 API。
gcloud services enable compute.googleapis.com
建立虛擬私有雲網路、子網路和防火牆規則
建立 VPC 網路、子網路和輸入允許防火牆規則,允許連線至負載平衡器的後端 VM。
建立虛擬私有雲網路和子網路。
a. 如要建立虛擬私有雲網路,請執行
gcloud compute networks create
指令:gcloud compute networks create NETWORK_NAME --subnet-mode custom
b. 在這個範例中,子網路的主要 IPv4 位址範圍為
10.10.0.0/24
。如要建立子網路,請執行gcloud compute networks subnets create
指令:gcloud compute networks subnets create SUBNET_NAME \ --network=NETWORK_NAME \ --range=10.10.0.0/24 \ --region=us-central1
更改下列內容:
NETWORK_NAME
:要建立的虛擬私有雲網路名稱。SUBNET_NAME
:要建立的子網路名稱。
建立輸入允許防火牆規則,允許將傳送至目的地 TCP 通訊埠 80 和 443 的封包傳送至後端 VM。在這個範例中,防火牆規則允許來自任何來源 IP 位址的連線。防火牆規則會套用到網路標記為
network-lb-tag
的 VM。如要建立防火牆規則,請執行
gcloud compute firewall-rules create
指令:gcloud compute firewall-rules create FIREWALL_RULE_NAME \ --direction=INGRESS \ --priority=1000 \ --network=NETWORK_NAME \ --action=ALLOW \ --rules=tcp:80,tcp:443 \ --source-ranges=0.0.0.0/0 \ --target-tags=network-lb-tag
將
FIREWALL_RULE_NAME
替換為要建立的防火牆規則名稱。
建立 VM 執行個體並指派權重
建立三個 VM 執行個體並指派權重:
設定三個後端 VM 執行個體,以便透過 HTTP 回應,在
X-Load-Balancing-Endpoint-Weight
標頭中傳回權重。在本教學課程中,您會將一個後端執行個體設為回報 0 的權重,將第二個後端執行個體設為回報 100 的權重,並將第三個後端執行個體設為回報 900 的權重。如要建立執行個體,請執行
gcloud compute instances create
指令:gcloud compute instances create instance-0 \ --zone=us-central1-a \ --tags=network-lb-tag \ --image-family=debian-12 \ --image-project=debian-cloud \ --subnet=SUBNET_NAME \ --metadata=load-balancing-weight=0,startup-script='#! /bin/bash apt-get update apt-get install apache2 -y ln -sr /etc/apache2/mods-available/headers.load /etc/apache2/mods-enabled/headers.load vm_hostname="$(curl -H "Metadata-Flavor:Google" \ http://169.254.169.254/computeMetadata/v1/instance/name)" echo "Page served from: $vm_hostname" | \ tee /var/www/html/index.html lb_weight="$(curl -H "Metadata-Flavor:Google" \ http://169.254.169.254/computeMetadata/v1/instance/attributes/load-balancing-weight)" echo "Header set X-Load-Balancing-Endpoint-Weight \"$lb_weight\"" | \ tee /etc/apache2/conf-enabled/headers.conf systemctl restart apache2'
gcloud compute instances create instance-100 \ --zone=us-central1-a \ --tags=network-lb-tag \ --image-family=debian-12 \ --image-project=debian-cloud \ --subnet=SUBNET_NAME \ --metadata=load-balancing-weight=100,startup-script='#! /bin/bash apt-get update apt-get install apache2 -y ln -sr /etc/apache2/mods-available/headers.load /etc/apache2/mods-enabled/headers.load vm_hostname="$(curl -H "Metadata-Flavor:Google" \ http://169.254.169.254/computeMetadata/v1/instance/name)" echo "Page served from: $vm_hostname" | \ tee /var/www/html/index.html lb_weight="$(curl -H "Metadata-Flavor:Google" \ http://169.254.169.254/computeMetadata/v1/instance/attributes/load-balancing-weight)" echo "Header set X-Load-Balancing-Endpoint-Weight \"$lb_weight\"" | \ tee /etc/apache2/conf-enabled/headers.conf systemctl restart apache2'
gcloud compute instances create instance-900 \ --zone=us-central1-a \ --tags=network-lb-tag \ --image-family=debian-12 \ --image-project=debian-cloud \ --subnet=SUBNET_NAME \ --metadata=load-balancing-weight=900,startup-script='#! /bin/bash apt-get update apt-get install apache2 -y ln -sr /etc/apache2/mods-available/headers.load /etc/apache2/mods-enabled/headers.load vm_hostname="$(curl -H "Metadata-Flavor:Google" \ http://169.254.169.254/computeMetadata/v1/instance/name)" echo "Page served from: $vm_hostname" | \ tee /var/www/html/index.html lb_weight="$(curl -H "Metadata-Flavor:Google" \ http://169.254.169.254/computeMetadata/v1/instance/attributes/load-balancing-weight)" echo "Header set X-Load-Balancing-Endpoint-Weight \"$lb_weight\"" | \ tee /etc/apache2/conf-enabled/headers.conf systemctl restart apache2'
建立執行個體群組
在本教學課程中,您會提供建立非代管執行個體群組的操作說明,其中包含所有三個 VM 執行個體(instance-0
、instance-100
和 instance-900
)。
如要建立執行個體群組,請執行
gcloud compute instance-groups unmanaged create
指令:gcloud compute instance-groups unmanaged create INSTANCE_GROUP \ --zone=us-central1-a
gcloud compute instance-groups unmanaged add-instances INSTANCE_GROUP \ --zone=us-central1-a \ --instances=instance-0,instance-100,instance-900
將
INSTANCE_GROUP
替換為要建立的執行個體群組名稱。
建立 HTTP 健康狀態檢查
在本教學課程中,您會提供建立 HTTP 健康狀態檢查的操作說明,以便讀取包含後端 VM 權重的 HTTP 回應。」
如要建立 HTTP 健康狀態檢查,請執行
gcloud compute health-checks create
指令:gcloud compute health-checks create http HTTP_HEALTH_CHECK_NAME \ --region=us-central1
將
HTTP_HEALTH_CHECK_NAME
替換為要建立的 HTTP 健康狀態檢查名稱。
建立後端服務
以下範例提供建立區域性外部後端服務的操作說明,該服務已設定為使用加權負載平衡。
使用 HTTP 健康狀態檢查建立後端服務,並將區域負載平衡器政策設為
WEIGHTED_MAGLEV
。如要建立後端服務,請執行
gcloud compute backend-services create
指令:gcloud compute backend-services create BACKEND_SERVICE_NAME \ --load-balancing-scheme=external \ --protocol=tcp \ --region=us-central1 \ --health-checks=HTTP_HEALTH_CHECK_NAME \ --health-checks-region=us-central1 \ --locality-lb-policy=WEIGHTED_MAGLEV
將
BACKEND_SERVICE_NAME
替換為要建立的後端服務名稱。
將執行個體群組新增至後端服務。
如要新增執行個體群組,請執行
gcloud compute backend-services add-backend
指令:gcloud compute backend-services add-backend BACKEND_SERVICE_NAME \ --instance-group=INSTANCE_GROUP \ --instance-group-zone=us-central1-a \ --region=us-central1
為負載平衡器保留地區外部 IP 位址。
如要保留一或多個 IP 位址,請執行
gcloud compute addresses create
指令:gcloud compute addresses create ADDRESS_NAME \ --region us-central1
將
ADDRESS_NAME
替換為要建立的 IP 位址名稱。使用
compute addresses describe
指令查看結果。請注意預留的靜態外部 IP 位址 (IP_ADDRESS
)。gcloud compute addresses describe ADDRESS_NAME
使用保留的地區性外部 IP 位址
IP_ADDRESS
建立轉送規則。將轉送規則連結至後端服務。如要建立轉送規則,請執行
gcloud compute forwarding-rules create
指令:gcloud compute forwarding-rules create FORWARDING_RULE \ --region=us-central1 \ --ports=80 \ --address=IP_ADDRESS \ --backend-service=BACKEND_SERVICE_NAME
更改下列內容:
FORWARDING_RULE
:要建立的轉寄規則名稱。IP_ADDRESS
:要指派給執行個體的 IP 位址。請使用預約的靜態外部 IP 位址,而不是位址名稱。
使用後端服務 API 驗證後端權重
確認後端權重是否正確回報至 HTTP 健康狀態檢查。
如要從後端服務取得後端權重 (以及健康狀態),請執行
gcloud compute backend-services get-health
指令:gcloud compute backend-services get-health BACKEND_SERVICE_NAME \ --region=us-central1
輸出內容如下:
backend: https://www.googleapis.com/compute/projects/project-name/{project}/zones/us-central1-a/instanceGroups/{instance-group-name} status: healthStatus: - forwardingRule: https://www.googleapis.com/compute/projects/{project}/regions/us-central1/forwardingRules/{firewall-rule-name} forwardingRuleIp: 34.135.46.66 healthState: HEALTHY instance: https://www.googleapis.com/compute/projects/{project}/zones/us-central1-a/instances/instance-0 ipAddress: 10.10.0.5 port: 80 weight: '0' - forwardingRule: https://www.googleapis.com/compute/projects/{project}/regions/us-central1/forwardingRules/{firewall-rule-name} forwardingRuleIp: 34.135.46.66 healthState: HEALTHY instance: https://www.googleapis.com/compute/projects/{project}/zones/us-central1-a/instances/instance-100 ipAddress: 10.10.0.6 port: 80 weight: '100' - forwardingRule: https://www.googleapis.com/compute/projects/{project}/regions/us-central1/forwardingRules/{firewall-rule-name} forwardingRuleIp: 34.135.46.66 healthState: HEALTHY instance: https://www.googleapis.com/compute/projects/{project}/zones/us-central1-a/instances/instance-900 ipAddress: 10.10.0.7 port: 80 weight: '900' kind: compute#backendServiceGroupHealth