brunch

You can make anything
by writing

C.S.Lewis

by Master Seo Apr 26. 2022

180. GCP CLI로 네트워크 로드밸런서 (5/6)


<1> 환경 설정

<2> 웝서버  2대 설치

<3> 로드밸런서 생성

<4> 확인

<5> 백엔드 로드밸런서 생성

<6>  로드밸런서 가서 해당 ip로 접속 확인





<1> 환경 설정


gcloud auth list


gcloud config list project


gcloud config set compute/zone us-central1-a



gcloud config set project secret-zxxxxyr-301xxxx


gcloud config list project




<2> 웝서버  2대 설치


1

gcloud compute instances create www1  --zone   asia-northeast3-a  --tags network-lb-tag

asia-northeast3-a

테그로 Firewall 80을 허용할 예정임

태그 필수



2

ssh로 로그인해서 실행


sudo apt-get update

sudo apt-get install apache2 -y

sudo service apache2 restart

ps -ef |grep apache2


ps -ef |grep apache2

root@www1:~# ps -ef |grep apache2

root      2173     1  0 08:38 ?        00:00:00 /usr/sbin/apache2 -k start

www-data  2174  2173  0 08:38 ?        00:00:00 /usr/sbin/apache2 -k start

www-data  2175  2173  0 08:38 ?        00:00:00 /usr/sbin/apache2 -k start

root      2233  2156  0 08:39 pts/0    00:00:00 grep apache2



3

웹 브라우저로 접속 확인

잘됨


4

하나더 생성

gcloud compute instances create www2  --zone asia-northeast3-a   --tags network-lb-tag


5

보안그룹 생성

gcloud compute firewall-rules create www-firewall-network-lb  --target-tags network-lb-tag --allow tcp:80


root@cloudshell:~ (secret-zephyr-348001)$ gcloud compute firewall-rules create www-firewall-network-lb  --target-tags network-lb-tag --allow tcp:80

Creating firewall...working..Created [https://www.googleapis.com/compute/v1/projects/secret-zephyr-348001/global/firewalls/www-firewall-network-lb].

Creating firewall...done.

NAME: www-firewall-network-lb

NETWORK: default

DIRECTION: INGRESS

PRIORITY: 1000

ALLOW: tcp:80

DENY:

DISABLED: False



6

확인

gcloud compute instances list


curl http://[IP_ADDRESS]




<3> 로드밸런서 환경 설정


1

gcloud compute addresses create network-lb-ip-1  --region asia-northeast3


2

gcloud compute http-health-checks create basic-check

Created [https://www.googleapis.com/compute/v1/projects/secret-zephyr-348001/global/httpHealthChecks/basic-check].

NAME: basic-check

HOST:

PORT: 80

REQUEST_PATH: /




3

타켓 pool만들기

gcloud compute target-pools create www-pool  --region asia-northeast3 --http-health-check basic-check


4

gcloud compute target-pools add-instances www-pool --instances www1,www2


5

포워딩

gcloud compute forwarding-rules create www-rule --region asia-northeast3 --ports 80 --address network-lb-ip-1 --target-pool www-pool




<4> 확인


1

gcloud compute forwarding-rules describe www-rule --region asia-northeast3



2

while true; do curl -m1 IP_ADDRESS; done






<5> 백엔드 로드밸런서 생성


1

gcloud compute instance-templates create lb-backend-template --region=asia-northeast3 --tags=allow-health-check


     echo "Page served from: $vm_hostname" | \

     tee /var/www/html/index.html

     systemctl restart apache2'


2

gcloud compute instance-groups managed create lb-backend-group --template=lb-backend-template --size=2 --zone=asia-northeast3-a



3

gcloud compute firewall-rules create fw-allow-health-check --action=allow --direction=ingress --source-ranges=130.211.0.0/22,35.191.0.0/16 --target-tags=allow-health-check --rules=tcp:80

Creating firewall...working..Created [https://www.googleapis.com/compute/v1/projects/secret-zephyr-348001/global/firewalls/fw-allow-health-check].

Creating firewall...done.

NAME: fw-allow-health-check

NETWORK: default

DIRECTION: INGRESS

PRIORITY: 1000

ALLOW: tcp:80

DENY:

DISABLED: False



4

gcloud compute addresses create lb-ipv4-1 --ip-version=IPV4 --global

Created [https://www.googleapis.com/compute/v1/projects/secret-zephyr-348001/global/addresses/lb-ipv4-1].


5

gcloud compute health-checks create http http-basic-check --port 80

Created [https://www.googleapis.com/compute/v1/projects/secret-zephyr-348001/global/healthChecks/http-basic-check].

NAME: http-basic-check

PROTOCOL: HTTP


6

gcloud compute backend-services create web-backend-service --protocol=HTTP --port-name=http --health-checks=http-basic-check --global

gcloud compute backend-services create web-backend-service --protocol=HTTP --port-name=http --health-checks=http-basic-check --global


7

gcloud compute backend-services add-backend web-backend-service --instance-group=lb-backend-group --instance-group-zone=asia-northeast3-a --global

Updated [https://www.googleapis.com/compute/v1/projects/secret-zephyr-348001/global/backendServices/web-backend-service].


8

gcloud compute url-maps create web-map-http  --default-service web-backend-service

Created [https://www.googleapis.com/compute/v1/projects/secret-zephyr-348001/global/urlMaps/web-map-http].

NAME: web-map-http

DEFAULT_SERVICE: backendServices/web-backend-service


9

gcloud compute target-http-proxies create http-lb-proxy --url-map web-map-http

Created [https://www.googleapis.com/compute/v1/projects/secret-zephyr-348001/global/targetHttpProxies/http-lb-proxy].

NAME: http-lb-proxy

URL_MAP: web-map-http



10

gcloud compute forwarding-rules create http-content-rule --address=lb-ipv4-1 --global --target-http-proxy=http-lb-proxy --ports=80

Created [https://www.googleapis.com/compute/v1/projects/secret-zephyr-348001/global/forwardingRules/http-content-rule].




<6>  로드밸런서 가서 해당 ip로 접속 확인


콘솔 > 로드밸런서 가서 해당 ip로 접속 확인



수정 자료



참고 자료




<7>  부하분산기 CLI로 삭제해야 하는 경우


1

gcloud compute target-pools delete www-pool


root@cloudshell:~ (secret-zephyr-348001)$ gcloud compute target-pools delete www-pool

Did you mean region [asia-east1] for target pool: [www-pool] (Y/n)?  n

For the following target pool:

 - [www-pool]

choose a region:

 [1] asia-east1

 [2] asia-east2

 [3] asia-northeast1

 [4] asia-northeast2

 [5] asia-northeast3

 [6] asia-south1

 [7] asia-south2

 [8] asia-southeast1

 [9] asia-southeast2

 [10] australia-southeast1

 [11] australia-southeast2

 [12] europe-central2

 [13] europe-north1

 [14] europe-west1

 [15] europe-west2

 [16] europe-west3

 [17] europe-west4

 [18] europe-west6

 [19] europe-west8

 [20] northamerica-northeast1

 [21] northamerica-northeast2

 [22] southamerica-east1

 [23] southamerica-west1

 [24] us-central1

 [25] us-east1

 [26] us-east4

 [27] us-west1

 [28] us-west2

 [29] us-west3

 [30] us-west4

Please enter your numeric choice:  24

The following target pools will be deleted:

 - [www-pool] in [us-central1]

Do you want to continue (Y/n)?  y

Deleted [https://www.googleapis.com/compute/v1/projects/secret-zephyr-348001/regions/us-central1/targetPools/www-pool].



2

Compute engine > 인스턴스 그룹에서 해당 인스턴스 그룹이 삭제되어 있는지 반드시 확인하자.

안되어 있다면 삭제 필요.

인스턴스 그룹을 삭제 하지 않으면 , VM인스턴스가 삭제해도 삭제 되지 않는다. 자동 확장이 동작함.


삭제





다음 공부


https://brunch.co.kr/@topasvga/2394






https://brunch.co.kr/@topasvga/1829



https://brunch.co.kr/@topasvga/2395


감사합니다.








        

브런치는 최신 브라우저에 최적화 되어있습니다. IE chrome safari