Skip to content

Instantly share code, notes, and snippets.

@gabihodoroaga
Last active April 28, 2020 00:02
Show Gist options
  • Save gabihodoroaga/221b341eb6abf12f3bea2bf5ca36e880 to your computer and use it in GitHub Desktop.
Save gabihodoroaga/221b341eb6abf12f3bea2bf5ca36e880 to your computer and use it in GitHub Desktop.
#!/bin/bash
set -e
#variables
INSTANCE_NAME=ps-media
ZONE=europe-west3-c
MEDIA_SERVER_NAME=cdn.example.com
# create the proxy-vm
gcloud compute instances create-with-container $INSTANCE_NAME-vm \
--container-image gcr.io/cloud-marketplace/google/nginx1:1.15 \
--machine-type f1-micro \
--tags $INSTANCE_NAME-http \
--zone $ZONE
gcloud compute instances add-metadata $INSTANCE_NAME-vm \
--metadata-from-file user-data=vm-cloud-init.yaml \
--zone $ZONE
gcloud compute instances update-container $INSTANCE_NAME-vm \
--container-mount-host-path host-path=/var/nginx.conf,mount-path=/etc/nginx/nginx.conf,mode=ro \
--zone $ZONE
# add firewall rule to allow loadbalancer and health-check to access this instance
gcloud compute firewall-rules create $INSTANCE_NAME-allow-health-check \
--allow tcp:80 \
--source-ranges 130.211.0.0/22,35.191.0.0/16 \
--target-tags $INSTANCE_NAME-http
# create the unmanaged instance group
gcloud compute instance-groups unmanaged create $INSTANCE_NAME-instance-group \
--zone $ZONE
# add instance to this instance group
gcloud compute instance-groups unmanaged add-instances $INSTANCE_NAME-instance-group \
--instances $INSTANCE_NAME-vm \
--zone $ZONE
# create the health check
gcloud compute health-checks create http $INSTANCE_NAME-health-check \
--request-path / \
--port 80 \
--check-interval 60 \
--unhealthy-threshold 3 \
--healthy-threshold 2 \
--timeout 5 \
--host $MEDIA_SERVER_NAME
# create the load balancer
# create a backend service
gcloud compute backend-services create $INSTANCE_NAME-lb-backend \
--http-health-checks $INSTANCE_NAME-health-check \
--port-name http \
--global \
--enable-cdn \
--connection-draining-timeout 300
# add the instance group to the backend
gcloud compute backend-services add-backend $INSTANCE_NAME-lb-backend \
--instance-group $INSTANCE_NAME-instance-group \
--instance-group-zone $ZONE \
--balancing-mode UTILIZATION \
--max-rate-per-instance 100 \
--capacity-scaler 1.0 \
--global
# create the url-map
gcloud compute url-maps create $INSTANCE_NAME-url-map \
--default-service $INSTANCE_NAME-lb-backend
# create the http proxy
gcloud compute target-http-proxies create $INSTANCE_NAME-http-proxy \
--url-map $INSTANCE_NAME-url-map
# create a global forwarding rule
gcloud compute forwarding-rules create $INSTANCE_NAME-forwarding-rule \
--global \
--ports 80 \
--target-http-proxy $INSTANCE_NAME-http-proxy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment