상세 컨텐츠

본문 제목

Network and HTTP Load Balance

Cloud/GCP

by 리카르돌 2020. 4. 21. 00:00

본문

 

Quick Lab

 

Set the default region and zone for all resources

In Cloud Shell, set the default zone:

gcloud config set compute/zone us-central1-a

 

Set the default region:

gcloud config set compute/region us-central1

 

Learn more about choosing zones and regions here: Regions & Zones documentation.

 

Note: When you run gcloud on your own machine, the config settings persist across sessions. In Cloud Shell you need to set this for every new session or reconnection.

Create multiple web server instances

To simulate serving from a cluster of machines, create a simple cluster of Nginx web servers to serve static content using Instance Templates and Managed Instance Groups. Instance Templates define the look of every virtual machine in the cluster (disk, CPUs, memory, etc). Managed Instance Groups instantiate a number of virtual machine instances using the Instance Template.

To create the Nginx web server clusters, create the following:

  • A startup script to be used by every virtual machine instance to setup Nginx server upon startup
  • An instance template to use the startup script
  • A target pool
  • A managed instance group using the instance template

Still in Cloud Shell, create a startup script to be used by every virtual machine instance. This script sets up the Nginx server upon startup:

cat << EOF > startup.sh

#! /bin/bash

apt-get update

apt-get install -y nginx

service nginx start

sed -i -- 's/nginx/Google Cloud Platform - '"\$HOSTNAME"'/' /var/www/html/index.nginx-debian.html

EOF

Create an instance template, which uses the startup script:

 

$ gcloud compute instance-templates create nginx-template \

   --metadata-from-file startup-script=startup.sh

Created [https://www.googleapis.com/compute/v1/projects/qwiklabs-gcp-03-b8a861c3676f/global/instanceTemplates/nginx-template].
NAME            MACHINE_TYPE   PREEMPTIBLE  CREATION_TIMESTAMP
nginx-template  n1-standard-1               2020-04-20T07:26:14.614-07:00

Create a target pool. A target pool allows a single access point to all the instances in a group and is necessary for load balancing in the future steps.

 

$ gcloud compute target-pools create nginx-pool

Created [https://www.googleapis.com/compute/v1/projects/qwiklabs-gcp-03-b8a861c3676f/regions/us-central1/targetPools/nginx-pool].
NAME        REGION       SESSION_AFFINITY  BACKUP  HEALTH_CHECKS
nginx-pool  us-central1  NONE

Create a managed instance group using the instance template:

$ gcloud compute instance-groups managed create nginx-group \

--base-instance-name nginx \

--size 2 \

--template nginx-template \

--target-pool nginx-pool

Created [https://www.googleapis.com/compute/v1/projects/qwiklabs-gcp-03-b8a861c3676f/zones/us-central1-a/instanceGroupManagers/nginx-group].
NAME         LOCATION       SCOPE  BASE_INSTANCE_NAME  SIZE  TARGET_SIZE  INSTANCE_TEMPLATE  AUTOSCALED
nginx-group  us-central1-a  zone   nginx               0     2            nginx-template     no

 

This creates 2 virtual machine instances with names that are prefixed with nginx-.

This may take a couple of minutes.

 

List the compute engine instances and you should see all of the instances created:

$ gcloud compute instances list

NAME        ZONE           MACHINE_TYPE   PREEMPTIBLE  INTERNAL_IP  EXTERNAL_IP      STATUS
nginx-gvsr  us-central1-a  n1-standard-1               10.128.0.2   34.71.172.199    RUNNING
nginx-rm2b  us-central1-a  n1-standard-1               10.128.0.3   104.155.184.222  RUNNING

.XUNNING

Now configure a firewall so that you can connect to the machines on port 80 via the EXTERNAL_IP addresses:

$ gcloud compute firewall-rules create www-firewall --allow tcp:80

Creating firewall...⠹Created [https://www.googleapis.com/compute/v1/projects/qwiklabs-gcp-03-b8a861c3676f/global/firewalls/www-firewall].
Creating firewall...done.
NAME          NETWORK  DIRECTION  PRIORITY  ALLOW   DENY  DISABLED
www-firewall  default  INGRESS    1000      tcp:80        False

 

You should be able to connect to each of the instances via their external IP addresses via http://EXTERNAL_IP/ shown as the result of running the previous command.

 

Create a Network Load Balancer

Network load balancing allows you to balance the load of your systems based on incoming IP protocol data, such as address, port, and protocol type. You also get some options that are not available, with HTTP(S) load balancing. For example, you can load balance additional TCP/UDP-based protocols such as SMTP traffic. And if your application is interested in TCP-connection-related characteristics, network load balancing allows your app to inspect the packets, where HTTP(S) load balancing does not.

 

For more information, see Setting Up Network Load Balancing.

 

Create an L3 network load balancer targeting your instance group:

 

$ gcloud compute forwarding-rules create nginx-lb \

--region us-central1 \

--ports=80 \

--target-pool nginx-pool

Created [https://www.googleapis.com/compute/v1/projects/qwiklabs-gcp-03-b8a861c3676f/regions/us-central1/forwardingRules/nginx-lb].

List all Google Compute Engine forwarding rules in your project.

 

$ gcloud compute forwarding-rules list

NAME      REGION       IP_ADDRESS    IP_PROTOCOL  TARGET
nginx-lb  us-central1  35.192.124.1  TCP          us-central1/targetPools/nginx-pool

You can then visit the load balancer from the browser http://IP_ADDRESS/ where IP_ADDRESS is the address shown as the result of running the previous command.

 

http://35.192.124.1/

 

Welcome to Google Cloud Platform - nginx-gvsr!

Welcome to Google Cloud Platform - nginx-gvsr! If you see this page, the Google Cloud Platform - nginx-gvsr web server is successfully installed and working. Further configuration is required. For online documentation and support please refer to nginx.org.

35.192.124.1

 

Create a HTTP(s) Load Balancer

HTTP(S) load balancing provides global load balancing for HTTP(S) requests destined for your instances. You can configure URL rules that route some URLs to one set of instances and route other URLs to other instances. Requests are always routed to the instance group that is closest to the user, provided that group has enough capacity and is appropriate for the request. If the closest group does not have enough capacity, the request is sent to the closest group that does have capacity.

 

Learn more about the HTTP(s) Load Balancer in the documentation.

 

외부 HTTP(S) 부하 분산 개요  |  Google Cloud

이 문서에서는 Google Cloud 외부 HTTP(S) 부하 분산을 구성하기 위해 알아야 하는 개념을 소개합니다. Google Cloud 부하 분산기의 차이점에 대한 자세한 내용은 다음 문서를 참조하세요. 사용 사례 외부 HTTP(S) 부하 분산기는 많은 사용 사례를 다룹니다. 이 섹션에서는 몇 가지 개략적인 예시를 제공합니다. 여러 백엔드 유형을 사용하는 부하 분산 일반적인 사용 사례는 여러 서비스로 트래픽 부하를 분산하는 것입니다. 다음 예시에서 외

cloud.google.com

 

First, create a health check. Health checks verify that the instance is responding to HTTP or HTTPS traffic:

$ gcloud compute http-health-checks create http-basic-check

Created [https://www.googleapis.com/compute/v1/projects/qwiklabs-gcp-03-b8a861c3676f/global/httpHealthChecks/http-basic-check].
NAME              HOST  PORT  REQUEST_PATH
http-basic-check        80    /

 

Define an HTTP service and map a port name to the relevant port for the instance group. Now the load balancing service can forward traffic to the named port:

 

$ gcloud compute instance-groups managed \

set-named-ports nginx-group \

--named-ports http:80

Updated [https://www.googleapis.com/compute/v1/projects/qwiklabs-gcp-03-b8a861c3676f/zones/us-central1-a/instanceGroups/nginx-group].

Create a backend service

gcloud compute backend-services create nginx-backend \

--protocol HTTP --http-health-checks http-basic-check --global

Created [https://www.googleapis.com/compute/v1/projects/qwiklabs-gcp-03-b8a861c3676f/global/backendServices/nginx-backend].
NAME           BACKENDS  PROTOCOL
nginx-backend            HTTP

Add the instance group into the backend service:

$ gcloud compute backend-services add-backend nginx-backend \

--instance-group nginx-group \

--instance-group-zone us-central1-a \

--global

Updated [https://www.googleapis.com/compute/v1/projects/qwiklabs-gcp-03-b8a861c3676f/global/backendServices/nginx-backend].

 

Create a default URL map that directs all incoming requests to all your instances:

$ gcloud compute url-maps create web-map \

--default-service nginx-backend

Created [https://www.googleapis.com/compute/v1/projects/qwiklabs-gcp-03-b8a861c3676f/global/urlMaps/web-map].
NAME     DEFAULT_SERVICE
web-map  backendServices/nginx-backend

To direct traffic to different instances based on the URL being requested, see content-based routing.

 

Create a target HTTP proxy to route requests to your URL map:

$ gcloud compute target-http-proxies create http-lb-proxy \

--url-map web-map

Created [https://www.googleapis.com/compute/v1/projects/qwiklabs-gcp-03-b8a861c3676f/global/targetHttpProxies/http-lb-proxy].
NAME           URL_MAP
http-lb-proxy  web-map

Create a global forwarding rule to handle and route incoming requests. A forwarding rule sends traffic to a specific target HTTP or HTTPS proxy depending on the IP address, IP protocol, and port specified. The global forwarding rule does not support multiple ports.

 

$ gcloud compute forwarding-rules create http-content-rule \

--global \

--target-http-proxy http-lb-proxy \

--ports 80

Created [https://www.googleapis.com/compute/v1/projects/qwiklabs-gcp-03-b8a861c3676f/global/forwardingRules/http-content-rule].

After creating the global forwarding rule, it can take several minutes for your configuration to propagate.

$ gcloud compute forwarding-rules list

NAME               REGION       IP_ADDRESS     IP_PROTOCOL  TARGET
http-content-rule               34.107.141.61  TCP          http-lb-proxy
nginx-lb           us-central1  35.192.124.1   TCP          us-central1/targetPools/nginx-pool

 

Take note of the http-content-rule IP_ADDRESS for the forwarding rule.

From the browser, you should be able to connect to http://IP_ADDRESS/.

It may take three to five minutes. If you do not connect, wait a minute then reload the browser.

 

http://34.107.141.61

 

 

Console에서 확인 

 

Load balancers

 

Backends 

 

Frontends

web-map의 detail 정보

nginx-pool의  detail 정보

'Cloud > GCP' 카테고리의 다른 글

Sample Case study: Mountkirk Games  (0) 2020.07.06
Sample Case study: Dress4Win  (0) 2020.07.06
Sample case study: TerramEarth  (0) 2020.07.06
Google Kubernetes Engine  (0) 2020.04.19
gcloud shell  (0) 2020.04.15

관련글 더보기

댓글 영역