[Udemy] Services Demo with Cluster IP and Load Balancer Services (Imperative)
Service Demo
1. User가 http://<SVC-Public-IP>/hello로 접속
2. 해당 요청이 LoadBalancer로 전달 후, Front App(Pod)로 이동
3. Front App에 있는 Nginx 역방향 프록시 응용 프로그램의 프록시 규칙을 통해 Backend App(Pod)으로 요청 전달
4. 해당 요청을 Backend App의 Cluster IP로 전달
5. Backend App(Pod)으로 요청 완료 후 Hello World Service 출력
Backend Rest App 환경 구성 (Deployment / ClusterIP)
# Deployment 생성
# Create Deployment for Frontend Nginx Proxy
kubectl create deployment my-backend-rest-app --image=stacksimplify/kube-helloworld:1.0.0
해당 이미지는 stacksimplify의 Docker Hub에서 kube-helloword:1.0.0를 가져옵니다.
# Deployment / ReplicaSets / Pods 확인
kubectl get deploy
---
NAME READY UP-TO-DATE AVAILABLE AGE
my-backend-rest-app 0/1 1 0 4s
kubectl get replicasets
---
NAME DESIRED CURRENT READY AGE
my-backend-rest-app-56d79d9d95 1 1 1 7m53s
kubectl get pods
---
NAME READY STATUS RESTARTS AGE
my-backend-rest-app-56d79d9d95-px7fh 1/1 Running 0 8m9s
# Backend App Pod의 로그 확인
kubectl logs -f my-backend-rest-app-56d79d9d95-px7fh
---
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.7.RELEASE)
2025-02-13 06:30:42.428 INFO 1 --- [ main] c.s.helloworld.HelloworldApplication : Starting HelloworldApplication v1.0.0 on my-backend-rest-app-56d79d9d95-px7fh with PID 1 (/app.jar started by root in /)
2025-02-13 06:30:42.443 INFO 1 --- [ main] c.s.helloworld.HelloworldApplication : No active profile set, falling back to default profiles: default
2025-02-13 06:30:45.545 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2025-02-13 06:30:45.572 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2025-02-13 06:30:45.572 INFO 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.34]
2025-02-13 06:30:45.705 INFO 1 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2025-02-13 06:30:45.706 INFO 1 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2933 ms
2025-02-13 06:30:46.133 INFO 1 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2025-02-13 06:30:46.537 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2025-02-13 06:30:46.543 INFO 1 --- [ main] c.s.helloworld.HelloworldApplication : Started HelloworldApplication in 5.772 seconds (JVM running for 6.935)
# Backend App의 Deployment를 Service를 통해 노출
kubectl expose deployment my-backend-rest-app --port=8080 --target-port=8080 --name=my-backend-service
---
service/my-backend-service exposed
port도 8080, target port도 8080이다.
* Service 생성 시, type을 명시하지 않으면 기본 값은 ClusterIP이다.
Observation: We don't need to specify "--type=ClusterIp" because default setting is to create ClusterIp Service.
# Backend App의 Service 확인
kubectl get svc
---
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 25h
my-backend-service ClusterIP 10.0.51.67 <none> 8080/TCP 36s
type은 ClusterIP 서비스로, 포트는 8080으로 생성되었다.
Frontend Rest App 환경 구성 (Deployment / LoadBalancer)
# Deployment 생성
# Create Deployment for Frontend Nginx Proxy
kubectl create deployment my-frontend-nginx-app --image=stacksimplify/kube-frontend-nginx:1.0.0
** Frontend App 내 Nginx 역방향 프록시 구성은, 컨테이너 이미지(stacksimplify/kube-frontend-nginx:1.0.0) 안에 이미 포함되어있다.
** 코드 내용을 보면, http://<my-frontend-service>:80 으로 요청오면, Nginx 역방향 프록시에 의해 http://<my-frontend-service>:8080으로 전달된다.
# Deployment / ReplicaSets / Pods 확인
kubectl get deploy
---
NAME READY UP-TO-DATE AVAILABLE AGE
my-backend-rest-app 1/1 1 1 20m
my-frontend-nginx-app 1/1 1 1 97s
frontend app / Backend app의 Deployment가 배포되어 있는 것을 확인 가능
kubectl get replicasets
---
NAME DESIRED CURRENT READY AGE
my-backend-rest-app-56d79d9d95 1 1 1 21m
my-frontend-nginx-app-645fdcc6d7 1 1 1 3m10s
kubectl get pods
---
NAME READY STATUS RESTARTS AGE
my-backend-rest-app-56d79d9d95-px7fh 1/1 Running 0 22m
my-frontend-nginx-app-645fdcc6d7-6rptq 1/1 Running 0 3m29s
# Frontend App의 Deployment를 Service를 통해 노출
kubectl expose deployment my-frontend-nginx-app --type=LoadBalancer --port=80 --target-port=80 --name=my-frontend-service
---
service/my-frontend-service exposed
type은 LoadBalancer, port는 80, target port도 80으로 expose한다.
# Frontend App의 Service 확인
kubectl get svc
---
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 26h
my-backend-service ClusterIP 10.0.51.67 <none> 8080/TCP 13m
my-frontend-service LoadBalancer 10.0.234.34 20.249.170.66 80:30311/TCP 2m20s
Frontend App의 LoadBalance가 잘 생성 되었다.
Frontend App의 External IP로 접속해보자.
정상적으로 접근 가능하다.
# scale up
kubectl scale --replicas=10 deployment/my-backend-rest-app
10개로 scale up 했다.
kubectl get deploy
---
NAME READY UP-TO-DATE AVAILABLE AGE
my-backend-rest-app 10/10 10 10 29m
my-frontend-nginx-app 1/1 1 1 10m
kubectl get rs
---
NAME DESIRED CURRENT READY AGE
my-backend-rest-app-56d79d9d95 10 10 10 29m
my-frontend-nginx-app-645fdcc6d7 1 1 1 10m
kubectl get pods
---
NAME READY STATUS RESTARTS AGE
my-backend-rest-app-56d79d9d95-clrxr 1/1 Running 0 54s
my-backend-rest-app-56d79d9d95-dvtkf 1/1 Running 0 54s
my-backend-rest-app-56d79d9d95-fcrvw 1/1 Running 0 54s
my-backend-rest-app-56d79d9d95-j5wft 1/1 Running 0 54s
my-backend-rest-app-56d79d9d95-jz222 1/1 Running 0 54s
my-backend-rest-app-56d79d9d95-lpmkj 1/1 Running 0 54s
my-backend-rest-app-56d79d9d95-mbdmn 1/1 Running 0 54s
my-backend-rest-app-56d79d9d95-p8jlc 1/1 Running 0 54s
my-backend-rest-app-56d79d9d95-psvxg 1/1 Running 0 54s
my-backend-rest-app-56d79d9d95-px7fh 1/1 Running 0 29m
my-frontend-nginx-app-645fdcc6d7-6rptq 1/1 Running 0 10m
# 확인
http://<External-IP-from-get-service-output>/hello로 접근 시,
Load Balancer에 의해 Pod의 부하 분산이 이루어 지는 것을 확인 가능
[참고 영상]
Udemy - Azure Kubernetes Service with Azure DevOps and Terraform
섹션 4 : Kubernetes Fundamentals with kubectl - Imperative Approach
34. Step-17: Introduction to Services in Kubernetes
35. Step-18: Services Demo with Cluster IP and Load Balancer Services
[참고 문서]