Skip to content

Instantly share code, notes, and snippets.

@gabihodoroaga
Created April 28, 2020 14:48
Show Gist options
  • Save gabihodoroaga/d6bc7f70f1420ed25f59edf0270095e5 to your computer and use it in GitHub Desktop.
Save gabihodoroaga/d6bc7f70f1420ed25f59edf0270095e5 to your computer and use it in GitHub Desktop.
#!/bin/bash
set -e
INSTANCE_NAME=ps-media
ZONE=europe-west3-c
PRESTASHOP_WEBSITE=http://www.example.com
MEDIA_SERVER_NAME=cdn.example.com
# download vm-stratup-script.sh
curl https://gist.githubusercontent.com/gabihodoroaga/43731b8ad9c63f3fe54913acff5e79c2/raw \
--output vm-stratup-script.sh
# replace our variables inside the script
sed -i 's/PRESTASHOP_WEBSITE/'"$PRESTASHOP_WEBSITE"'/g' vm-stratup-script.sh
sed -i 's/MEDIA_SERVER_NAME/'"$MEDIA_SERVER_NAME"'/g' vm-stratup-script.sh
# create the instance template
gcloud compute instance-templates create-with-container $INSTANCE_NAME-template \
--container-image gcr.io/cloud-marketplace/google/nginx1:1.15 \
--machine-type f1-micro \
--tags $INSTANCE_NAME-http \
--metadata-from-file startup-script=vm-startup-script.sh \
--container-mount-host-path host-path=/var/nginx.conf,mount-path=/etc/nginx/nginx.conf,mode=ro
# create the instance group
gcloud compute instance-groups managed create $INSTANCE_NAME-instance-group \
--template $INSTANCE_NAME-template \
--size 1 \
--zone $ZONE
# create the health check
gcloud compute health-checks create http $INSTANCE_NAME-health-check \
--request-path / \
--port 80 \
--check-interval 60s \
--healthy-threshold 1 \
--timeout 5s \
--unhealthy-threshold 5 \
--host $MEDIA_SERVER_NAME
# create the firewall rule
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
# add the health check to the intstance group
gcloud compute instance-groups managed update $INSTANCE_NAME-instance-group \
--health-check $INSTANCE_NAME-health-check \
--initial-delay 300 \
--zone $ZONE
# 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 a.k.a. frontend
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