Skip to content

Instantly share code, notes, and snippets.

@akiatoji
Last active June 15, 2019 16:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save akiatoji/8576c8327a1b1eddf3e02849d929023a to your computer and use it in GitHub Desktop.
Save akiatoji/8576c8327a1b1eddf3e02849d929023a to your computer and use it in GitHub Desktop.

In Cloud Console:

  • Create load balancer
  • Create Compute instances with network tag to use as load balancer target.
  • Create health check

Create target pool

gcloud compute target-pools create extloadbalancer  --region $MY_REGION --http-health-check webserver-health

Add instances to target pool

gcloud compute target-pools add-instances extloadbalancer \
    --instances webserver1,webserver2,webserver3 \
     --instances-zone=$MY_ZONE1

Create forwarding rule for External IP -> LB

You need to have reserved an external IP

gcloud compute addresses list

Use IP address above below

gcloud compute forwarding-rules create webserver-rule \
    --region $MY_REGION --ports 80 \
    --address $STATIC_EXTERNAL_IP --target-pool extloadbalancer

You should see forwarding rule on external IP in Cloud Console VPC Network -> External IP addresses

Launch instances

gcloud compute instances create webserver4 \
    --image-family debian-9 \
    --image-project debian-cloud \
    --tags int-lb \
    --zone $MY_ZONE2 \
    --subnet default \
    --metadata startup-script-url="gs://cloud-training/archinfra/mystartupscript",my-server-id="WebServer-4"
    
gcloud compute instances create webserver5 \
    --image-family debian-9 \
    --image-project debian-cloud \
    --tags int-lb \
    --zone $MY_ZONE2 \
    --subnet default \
    --metadata startup-script-url="gs://cloud-training/archinfra/mystartupscript",my-server-id="WebServer-5"

Instance Group

gcloud compute instance-groups unmanaged create ig1 \
    --zone $MY_ZONE1
    
gcloud compute instance-groups unmanaged add-instances ig1 \
    --instances=webserver2,webserver3 --zone $MY_ZONE1
    
gcloud compute instance-groups unmanaged create ig2 \
    --zone $MY_ZONE2

gcloud compute instance-groups unmanaged add-instances ig2 \
    --instances=webserver4,webserver5 --zone $MY_ZONE2
    

Configure LB

gcloud compute health-checks create tcp my-tcp-health-check \
    --port 80

gcloud compute backend-services create my-int-lb \
    --load-balancing-scheme internal \
    --region $MY_REGION \
    --health-checks my-tcp-health-check \
    --protocol tcp
    
gcloud compute backend-services add-backend my-int-lb \
    --instance-group ig1 \
    --instance-group-zone $MY_ZONE1 \
    --region $MY_REGION
    
gcloud compute backend-services add-backend my-int-lb \
    --instance-group ig2 \
    --instance-group-zone $MY_ZONE2 \
    --region $MY_REGION
    
gcloud compute forwarding-rules create my-int-lb-forwarding-rule \
    --load-balancing-scheme internal \
    --ports 80 \
    --network default \
    --subnet default \
    --region $MY_REGION \
    --backend-service my-int-lb

gcloud compute firewall-rules create allow-internal-lb \
    --network default \
    --source-ranges 10.128.0.0/20 \
    --target-tags int-lb \
    --allow tcp:80,tcp:443
    
gcloud compute firewall-rules create allow-health-check \
    --network default \
    --source-ranges 130.211.0.0/22,35.191.0.0/16 \
    --target-tags int-lb \
    --allow tcp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment