負載均衡:
1: 谷歌負載均衡
按照地區分成global & region
按照外部和内部流量LB可以分為:HTTPS load balancing, SSL Proxy, TCP proxy Network TCP/UDP LB(外部);和Intern TCP/UDP LB(内部)。
谷歌的LB針對全球的LB可以使用谷歌的premier tier network(谷歌自主擁有光網)如果是regional LB可以考慮使用standard tier network(就是谷歌和其他服務商共享網路)
參考文檔:
【1】L3 Network Load Balancer:外部 TCP/UDP 網絡負載平衡概覽
【2】L7 HTTP(s) Load Balancer:外部 HTTP(S) 負載平衡概覽
cat << EOF > startup.sh
#! /bin/bash
apt-get update
apt-get install -y nginx
service nginx start
sed -i -- 's/nginx/Google Cloud Platform - '"\$HOSTNAME"'/' /var/www/html/index.nginx-debian.html
gcloud compute instance-templates create nginx-template \
--metadata-from-file startup-script=startup.sh
Create a target pool. A target pool allows a single access point to all the instances in a group and is necessary for load balancing in the future steps.
建立一個目标池。 目标池允許對組中所有執行個體的單個通路點,并且對于将來的步驟中的負載平衡是必需的。
gcloud compute target-pools create nginx-pool
Create a managed instance group using the instance template:
gcloud compute instance-groups managed create nginx-group \
--base-instance-name nginx \
--size 2 \
--template nginx-template \
--target-pool nginx-pool
建立後檢視instance建立詳情:
gcloud compute instances list
現在配置防火牆,以便您可以通過EXTERNAL_IP位址連接配接到端口80上的計算機:
gcloud compute firewall-rules create www-firewall --allow tcp:80
您應該能夠通過http:// EXTERNAL_IP /通過其外部IP位址連接配接到每個執行個體,如運作上一條指令的結果所示。
網絡負載平衡使您可以根據傳入的IP協定資料(例如位址,端口和協定類型)來平衡系統的負載。 通過HTTP(S)負載平衡,您還會獲得一些不可用的選項。 例如,您可以負載均衡其他基于TCP / UDP的協定,例如SMTP通信。 并且,如果您的應用程式對與TCP連接配接相關的特性感興趣,則網絡負載平衡允許您的應用程式檢查資料包,而HTTP(S)負載平衡則不需要。
gcloud compute forwarding-rules create nginx-lb \
--region us-central1 \
--ports=80 \
--target-pool nginx-pool
然後,您可以從浏覽器http:// IP_ADDRESS /通路負載均衡器,其中IP_ADDRESS是運作上一條指令的結果顯示的位址。
注意這是一個L3 Network Load Balancer that points to the webservers.
建立一個HTTP負載均衡器
HTTP(S)負載平衡為發往您執行個體的HTTP(S)請求提供全局負載平衡。 您可以配置URL規則,這些規則将某些URL路由到一組執行個體,而将其他URL路由到其他執行個體。 始終将請求路由到最接近使用者的執行個體組,前提是該組具有足夠的容量并且适合該請求。 如果最近的組沒有足夠的容量,則将請求發送到确實有容量的最近的組。
第一步:建立執行個體健康檢查
gcloud compute http-health-checks create http-basic-check
第二步:
Define an HTTP service and map a port name to the relevant port for the instance group. Now the load balancing service can forward traffic to the named port:
gcloud compute instance-groups managed \
set-named-ports nginx-group \
--named-ports http:80
Create a backend service:
gcloud compute backend-services create nginx-backend \
--protocol HTTP --http-health-checks http-basic-check --global
Add the instance group into the backend service:
gcloud compute backend-services add-backend nginx-backend \
--instance-group nginx-group \
--instance-group-zone us-central1-a \
--global
Create a default URL map that directs all incoming requests to all your instances:
gcloud compute url-maps create web-map \
--default-service nginx-backend