Skip to content

Instantly share code, notes, and snippets.

@thockin
Created October 5, 2015 00:34
Show Gist options
  • Save thockin/36fea15cc0deb08a768a to your computer and use it in GitHub Desktop.
Save thockin/36fea15cc0deb08a768a to your computer and use it in GitHub Desktop.
#!/bin/sh
PROJECT=thockin-dev
ZONE=us-central1-b
K8S_MIG=kubernetes-minion-group
# Assume I have 2 kubernetes services running, svc1 and svc1. Both are type
# NodePort with ports defined below. I want to expose them as hostnames
# defined below.
SVC1_NODE_PORT=30001
SVC1_PORT_NAME=svc1
HOST1=svc1.example.com
SVC2_NODE_PORT=30002
SVC2_PORT_NAME=svc2
HOST2=svc2.example.com
# MIG
gcloud compute --project $PROJECT \
instance-groups set-named-ports $K8S_MIG \
--zone $ZONE \
--named-ports svc1:$SVC1_NODE_PORT,svc2:$SVC2_NODE_PORT
# svc1
gcloud compute --project $PROJECT \
http-health-checks create "svc1-health-check" \
--port $SVC1_NODE_PORT
gcloud compute --project $PROJECT \
backend-services create "svc1-backends" \
--port-name $SVC1_PORT_NAME \
--protocol "HTTP" \
--http-health-check "svc1-health-check"
gcloud compute --project $PROJECT \
backend-services add-backend "svc1-backends" \
--zone $ZONE \
--balancing-mode "UTILIZATION" \
--instance-group $K8S_MIG
# svc2
gcloud compute --project $PROJECT \
http-health-checks create "svc2-health-check" \
--port $SVC2_NODE_PORT
gcloud compute --project $PROJECT \
backend-services create "svc2-backends" \
--port-name $SVC2_PORT_NAME \
--protocol "HTTP" \
--http-health-check "svc2-health-check"
gcloud compute --project $PROJECT \
backend-services add-backend "svc2-backends" \
--zone $ZONE \
--balancing-mode "UTILIZATION" \
--instance-group $K8S_MIG
# Map all incoming services.
gcloud compute --project $PROJECT \
url-maps create "all-svcs" \
--default-service "svc1-backends"
gcloud compute --project $PROJECT \
url-maps add-path-matcher "all-svcs" \
--path-matcher-name "svc1" \
--new-hosts $HOST1 \
--default-service "https://www.googleapis.com/compute/v1/projects/$PROJECT/global/backendServices/svc1-backends"
gcloud compute --project $PROJECT \
url-maps add-path-matcher "all-svcs" \
--path-matcher-name "svc2" \
--new-hosts $HOST2 \
--default-service "https://www.googleapis.com/compute/v1/projects/$PROJECT/global/backendServices/svc2-backends"
# Receive traffic on HTTP.
gcloud compute --project $PROJECT \
target-http-proxies create "all-svcs-target-http" \
--url-map "all-svcs"
gcloud compute --project $PROJECT \
forwarding-rules create "all-svcs-fwd-http" --global \
--ip-protocol "TCP" --port-range "80" \
--target-http-proxy "all-svcs-target-http"
#--address $STATIC_IP
# Open the firewall
gcloud compute --project $PROJECT \
firewall-rules create "scv1-fw" \
--allow tcp:$SVC1_NODE_PORT \
--source-ranges "0.0.0.0/0"
gcloud compute --project $PROJECT \
firewall-rules create "scv2-fw" \
--allow tcp:$SVC2_NODE_PORT \
--source-ranges "0.0.0.0/0"
exit 0
#####
# Cleanup
#####
gcloud compute --project $PROJECT \
firewall-rules delete "scv1-fw"
gcloud compute --project $PROJECT \
firewall-rules delete "scv2-fw"
gcloud compute --project $PROJECT \
forwarding-rules delete "all-svcs-fwd-http" --global
gcloud compute --project $PROJECT \
target-http-proxies delete "all-svcs-target-http"
gcloud compute --project $PROJECT \
url-maps delete "all-svcs" \
gcloud compute --project $PROJECT \
backend-services delete "svc1-backends"
gcloud compute --project $PROJECT \
backend-services delete "svc2-backends"
gcloud compute --project $PROJECT \
http-health-checks delete "svc1-health-check"
gcloud compute --project $PROJECT \
http-health-checks delete "svc2-health-check"
gcloud compute --project $PROJECT \
instance-groups set-named-ports $K8S_MIG \
--zone $ZONE \
--named-ports ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment