Advertisement
whitecode12

Google Networking Fundamentals

Jun 15th, 2024 (edited)
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.70 KB | Source Code | 0 0
  1. export REGION=
  2.  
  3. export ZONE=
  4.  
  5.  
  6. gcloud compute instances create web1 \
  7. --zone=$ZONE \
  8. --machine-type=e2-small \
  9. --tags=network-lb-tag \
  10. --image-family=debian-11 \
  11. --image-project=debian-cloud \
  12. --metadata=startup-script='#!/bin/bash
  13. apt-get update
  14. apt-get install apache2 -y
  15. service apache2 restart
  16. echo "<h3>Web Server: web1</h3>" | tee /var/www/html/index.html'
  17.  
  18. gcloud compute instances create web2 \
  19. --zone=$ZONE \
  20. --machine-type=e2-small \
  21. --tags=network-lb-tag \
  22. --image-family=debian-11 \
  23. --image-project=debian-cloud \
  24. --metadata=startup-script='#!/bin/bash
  25. apt-get update
  26. apt-get install apache2 -y
  27. service apache2 restart
  28. echo "<h3>Web Server: web2</h3>" | tee /var/www/html/index.html'
  29.  
  30. gcloud compute instances create web3 \
  31. --zone=$ZONE \
  32. --machine-type=e2-small \
  33. --tags=network-lb-tag \
  34. --image-family=debian-11 \
  35. --image-project=debian-cloud \
  36. --metadata=startup-script='#!/bin/bash
  37. apt-get update
  38. apt-get install apache2 -y
  39. service apache2 restart
  40. echo "<h3>Web Server: web3</h3>" | tee /var/www/html/index.html'
  41.  
  42.  
  43.  
  44.  
  45.  
  46. gcloud compute firewall-rules create www-firewall-network-lb --allow tcp:80 --target-tags network-lb-tag
  47.  
  48.  
  49.  
  50.  
  51. gcloud compute addresses create network-lb-ip-1 \
  52. --region=$REGION
  53.  
  54.  
  55.  
  56. gcloud compute http-health-checks create basic-check
  57.  
  58.  
  59. gcloud compute target-pools create www-pool \
  60. --region=$REGION --http-health-check basic-check
  61.  
  62.  
  63. gcloud compute target-pools add-instances www-pool \
  64. --instances web1,web2,web3 --zone=$ZONE
  65.  
  66.  
  67. gcloud compute forwarding-rules create www-rule \
  68. --region=$REGION \
  69. --ports 80 \
  70. --address network-lb-ip-1 \
  71. --target-pool www-pool
  72.  
  73.  
  74. IPADDRESS=$(gcloud compute forwarding-rules describe www-rule --region=$REGION --format="json" | jq -r .IPAddress)
  75.  
  76.  
  77.  
  78. #TASK 3
  79.  
  80. gcloud compute instance-templates create lb-backend-template \
  81. --region=$REGION \
  82. --network=default \
  83. --subnet=default \
  84. --tags=allow-health-check \
  85. --machine-type=e2-medium \
  86. --image-family=debian-11 \
  87. --image-project=debian-cloud \
  88. --metadata=startup-script='#!/bin/bash
  89. apt-get update
  90. apt-get install apache2 -y
  91. a2ensite default-ssl
  92. a2enmod ssl
  93. vm_hostname="$(curl -H "Metadata-Flavor:Google" \
  94. http://169.254.169.254/computeMetadata/v1/instance/name)"
  95. echo "Page served from: $vm_hostname" | \
  96. tee /var/www/html/index.html
  97. systemctl restart apache2'
  98.  
  99.  
  100.  
  101.  
  102. gcloud compute instance-groups managed create lb-backend-group \
  103. --template=lb-backend-template --size=2 --zone=$ZONE
  104.  
  105.  
  106.  
  107. gcloud compute firewall-rules create fw-allow-health-check \
  108. --network=default \
  109. --action=allow \
  110. --direction=ingress \
  111. --source-ranges=130.211.0.0/22,35.191.0.0/16 \
  112. --target-tags=allow-health-check \
  113. --rules=tcp:80
  114.  
  115.  
  116.  
  117. gcloud compute addresses create lb-ipv4-1 \
  118. --ip-version=IPV4 \
  119. --global
  120.  
  121.  
  122.  
  123. gcloud compute addresses describe lb-ipv4-1 \
  124. --format="get(address)" \
  125. --global
  126.  
  127.  
  128.  
  129.  
  130. gcloud compute health-checks create http http-basic-check \
  131. --port 80
  132.  
  133.  
  134.  
  135. gcloud compute backend-services create web-backend-service \
  136. --protocol=HTTP \
  137. --port-name=http \
  138. --health-checks=http-basic-check \
  139. --global
  140.  
  141.  
  142.  
  143. gcloud compute backend-services add-backend web-backend-service \
  144. --instance-group=lb-backend-group \
  145. --instance-group-zone=$ZONE \
  146. --global
  147.  
  148.  
  149.  
  150. gcloud compute url-maps create web-map-http \
  151. --default-service web-backend-service
  152.  
  153.  
  154.  
  155.  
  156. gcloud compute target-http-proxies create http-lb-proxy \
  157. --url-map web-map-http
  158.  
  159.  
  160. gcloud compute forwarding-rules create http-content-rule \
  161. --address=lb-ipv4-1\
  162. --global \
  163. --target-http-proxy=http-lb-proxy \
  164. --ports=80
Tags: networking
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement