Implementing Cloud Load Balancing for Compute Engine

목록
- Set Up Network Load Balancers
- Set Up Application Load Balancers
- Use an Internal Application Load Balancer
- Implement Load Balancing on Compute Engine: Challenge Lab ⬅️ 오늘의 Lab!

➡️ 빈칸인 부분은 실습 계정별로 상이하므로, 수정 필요!
#!/bin/bash
apt-get update
apt-get install apache2 -y
service apache2 restart
echo "<h3>Web Server: web-number</h3>" | tee /var/www/html/index.html
➡️ 스턴스 이름에 따라 web1, web2, web3 같은 web-number를 업데이트 필요

# web1 생성
gcloud compute instances create web1 \
--zone=asia-east1-b \
--machine-type=e2-small \
--tags=network-lb-tag \
--image-family=debian-12 \
--image-project=debian-cloud \
--metadata=startup-script='#!/bin/bash
apt-get update
apt-get install apache2 -y
service apache2 restart
echo "<h3>Web Server: web1</h3>" | tee /var/www/html/index.html'
# web2 생성
gcloud compute instances create web2 \
--zone=asia-east1-b \
--machine-type=e2-small \
--tags=network-lb-tag \
--image-family=debian-12 \
--image-project=debian-cloud \
--metadata=startup-script='#!/bin/bash
apt-get update
apt-get install apache2 -y
service apache2 restart
echo "<h3>Web Server: web2</h3>" | tee /var/www/html/index.html'
# web3 생성
gcloud compute instances create web3 \
--zone=asia-east1-b \
--machine-type=e2-small \
--tags=network-lb-tag \
--image-family=debian-12 \
--image-project=debian-cloud \
--metadata=startup-script='#!/bin/bash
apt-get update
apt-get install apache2 -y
service apache2 restart
echo "<h3>Web Server: web3</h3>" | tee /var/www/html/index.html'
✅ zone은 개개인 실습 환경에 따라 상이하므로 수정 필요
gcloud compute firewall-rules create www-firewall-network-lb \
--target-tags network-lb-tag --allow tcp:80
<과정 미리보기>


gcloud compute addresses create network-lb-ip-1 --region=asia-east1
✅ Region은 실습용 계정에 따라 상이하므로 수정 필요
gcloud compute http-health-checks create basic-check
gcloud compute target-pools create www-pool \
--region=asia-east1 --http-health-check basic-check
gcloud compute target-pools add-instances www-pool \
--instances web1,web2,web3 --instances-zone=asia-east1-b
gcloud compute forwarding-rules create www-rule \
--region=asia-east1 \
--ports 80 \
--address network-lb-ip-1 \
--target-pool www-pool
<과정 미리보기>



gcloud compute instance-templates create lb-backend-template \
--region=asia-east1 \
--network=default \
--subnet=default \
--tags=allow-health-check \
--machine-type=e2-medium \
--image-family=debian-12 \
--image-project=debian-cloud \
--metadata=startup-script='#!/bin/bash
apt-get update
apt-get install apache2 -y
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
systemctl restart apache2'
gcloud compute instance-groups managed create lb-backend-group \
--template=lb-backend-template --size=2 --zone=asia-east1-b
gcloud compute firewall-rules create fw-allow-health-check \
--network=default \
--action=allow \
--direction=ingress \
--source-ranges=130.211.0.0/22,35.191.0.0/16 \
--target-tags=allow-health-check \
--rules=tcp:80
gcloud compute addresses create lb-ipv4-1 --ip-version=IPV4 --global
gcloud compute health-checks create http http-basic-check --port 80
# 생성
gcloud compute backend-services create web-backend-service \
--protocol=HTTP --health-checks=http-basic-check --global
# 연결(추가)
gcloud compute backend-services add-backend web-backend-service \
--instance-group=lb-backend-group --instance-group-zone=asia-east1-b --global
gcloud compute url-maps create web-map-http --default-service web-backend-service
gcloud compute target-http-proxies create http-lb-proxy --url-map web-map-http
gcloud compute forwarding-rules create http-content-rule \
--address=lb-ipv4-1 --global --target-http-proxy=http-lb-proxy --ports=80
gcloud compute forwarding-rules describe www-rule --region=asia-east1 --format="get(IPAddress)"


➡️ 새로고침할 때마다 접속하는 웹 서버가 다른 것을 확인할 수 있다.
gcloud compute addresses describe lb-ipv4-1 --global --format="get(address)"


➡️ 새로고침할 때마다 접속하는 백엔드 서비스가 다른 것을 확인할 수 있다.