Skip to content

Instantly share code, notes, and snippets.

@salrashid123
Last active April 17, 2023 23:04
Show Gist options
  • Save salrashid123/93d899503d5799f10745a9fe7c89de87 to your computer and use it in GitHub Desktop.
Save salrashid123/93d899503d5799f10745a9fe7c89de87 to your computer and use it in GitHub Desktop.
GCP DNS Based Service Directory with TCP and HTTP Internal Load Balancer

https://cloud.google.com/service-directory/docs

HTTP

export PROJECT_ID=`gcloud config get-value core/project`

gcloud service-directory namespaces create ns1 --location us-central1

gcloud service-directory services create svc1 \
   --namespace ns1 \
   --location us-central1


gcloud dns managed-zones create svcdns1 \
   --dns-name tee.local \
   --visibility private --description "SVC1 DNS" \
   --networks https://www.googleapis.com/compute/v1/projects/$PROJECT_ID/global/networks/default \
   --service-directory-namespace https://servicedirectory.googleapis.com/v1/projects/$PROJECT_ID/locations/us-central1/namespaces/ns1


gcloud compute instance-templates create-with-container nginx-template --no-address --tags http-server --container-image gcr.io/cloud-marketplace/google/nginx1

gcloud compute instance-groups managed update nginx-group \
    --base-instance-name nginx-vm \
    --size 3 --named-ports=http:80 \
    --template nginx-template

gcloud compute instance-groups managed set-named-ports nginx-group         --named-ports=http:80
        
gcloud compute networks subnets create proxy-only-subnet \
      --purpose=REGIONAL_MANAGED_PROXY \
      --role=ACTIVE \
      --region=us-central1 \
      --range=10.5.0.0/20 --network=default


gcloud compute health-checks create http nginx-tcp-80 \
    --check-interval=5s \
    --timeout=5s \
    --healthy-threshold=2 \
    --unhealthy-threshold=2 \
    --port=80

gcloud compute firewall-rules create allow-http-nginx-lb  --action allow --direction INGRESS   --source-ranges 35.191.0.0/16,209.85.152.0/22,209.85.204.0/22     --target-tags http-server    --rules tcp:80
gcloud compute firewall-rules create allow-http-nginx  --allow tcp:80 --target-tags http-server

gcloud compute backend-services create nginx-map-backend-service-us-central-1  --load-balancing-scheme=INTERNAL_MANAGED --protocol=HTTP --health-checks nginx-tcp-80 --region us-central1

gcloud compute backend-services add-backend nginx-map-backend-service-us-central-1  --instance-group nginx-group  --instance-group-zone us-central1-a --region us-central1


gcloud compute url-maps create l7-ilb-map \
  --default-service=nginx-map-backend-service-us-central-1 \
  --region=us-central1

gcloud compute target-http-proxies create l7-ilb-proxy \
    --url-map=l7-ilb-map \
    --url-map-region=us-central1 \
    --region=us-central1

gcloud beta compute forwarding-rules create l7-ilb-forwarding-rule \
            --load-balancing-scheme=INTERNAL_MANAGED \
            --network=default \
            --ports=80 \
            --region=us-central1 \
            --target-http-proxy=l7-ilb-proxy \
            --target-http-proxy-region=us-central1 \
            --service-directory-registration=projects/$PROJECT_ID/locations/us-central1/namespaces/ns1/services/svc1



$ dig svc1.tee.local  +short
10.128.0.4

$ curl http://svc1.tee.local:80

TCP

export PROJECT_ID=`gcloud config get-value core/project`

gcloud service-directory namespaces create ns1 --location us-central1

gcloud service-directory services create svc1 \
   --namespace ns1 \
   --location us-central1


gcloud dns managed-zones create svcdns1 \
   --dns-name tee.local \
   --visibility private --description "SVC1 DNS" \
   --networks https://www.googleapis.com/compute/v1/projects/$PROJECT_ID/global/networks/default \
   --service-directory-namespace https://servicedirectory.googleapis.com/v1/projects/$PROJECT_ID/locations/us-central1/namespaces/ns1


gcloud compute instance-templates create-with-container nginx-template --no-address --tags http-server --container-image gcr.io/cloud-marketplace/google/nginx1

gcloud compute instance-groups managed create nginx-group \
    --base-instance-name nginx-vm \
    --size 3 \
    --template nginx-template


gcloud compute health-checks create tcp nginx-tcp-80 \
    --check-interval=5s \
    --timeout=5s \
    --healthy-threshold=2 \
    --unhealthy-threshold=2 \
    --port=80

gcloud compute firewall-rules create allow-http-nginx  --allow tcp:80 --target-tags http-server

gcloud compute backend-services create nginx-map-backend-service-us-central-1  --load-balancing-scheme=internal --protocol TCP --health-checks nginx-tcp-80 --region us-central1
gcloud compute backend-services add-backend nginx-map-backend-service-us-central-1  --instance-group nginx-group  --instance-group-zone us-central1-a --region us-central1

gcloud beta compute forwarding-rules create nginx-ilb \
    --region=us-central1 \
    --load-balancing-scheme=internal \
    --ip-protocol=TCP \
    --ports=80  \
    --backend-service=nginx-map-backend-service-us-central-1 \
    --backend-service-region=us-central1 \
    --service-directory-registration=projects/$PROJECT_ID/locations/us-central1/namespaces/ns1/services/svc1

$ dig svc1.tee.local  +short
10.128.0.57

$ curl http://svc1.tee.local:80
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment