ํนํ Service ์ ํ(ClusterIP / NodePort)์ ํ์ฉํด Pod ๊ฐ ํต์ ๋ฐ ์ธ๋ถ ๋ ธ์ถ ๋ฐฉ์์ ๋น๊ตยท๊ตฌ์ถํ๊ณ , Ingress ๋ฆฌ์์ค์ NGINX Ingress Controller๋ฅผ ์ ์ฉํด Hostname ๊ธฐ๋ฐ ๋ผ์ฐํ ๊ตฌ์กฐ๋ฅผ ๊ตฌํํ์ต๋๋ค.
์ฟ ๋ฒ๋คํฐ์ค์์ Pod๋ ๋์ ์ผ๋ก ์์ฑยท์ญ์ ๋๋ฏ๋ก ์ง์ IP๋ก ์ ๊ทผํ๊ธฐ ์ด๋ ต์ต๋๋ค.
์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด Service๋ฅผ ์ฌ์ฉํ๋ฉฐ, ์ ํ๋ณ ํน์ง์ ์๋์ ๊ฐ์ต๋๋ค.
| ์๋น์ค ์ ํ | ๊ฐ๋จํ ์ค๋ช | ์ฌ์ฉ ์ฌ๋ก |
|---|---|---|
| ClusterIP | ํด๋ฌ์คํฐ ๋ด๋ถ์์๋ง ์ ๊ทผ ๊ฐ๋ฅํ IP๋ฅผ ํ ๋นํฉ๋๋ค. | ๋ด๋ถ ์๋น์ค ๊ฐ ํต์ (๊ธฐ๋ณธ ์ต์ ) |
| NodePort | ๊ฐ ๋ ธ๋์ ํน์ ํฌํธ๋ฅผ ์ด์ด ์ธ๋ถ์์ ์๋น์ค์ ์ ๊ทผํ ์ ์๋๋ก ํฉ๋๋ค. | ํ ์คํธ, ๊ฐ๋จํ ์ธ๋ถ ์ ๊ทผ |
| LoadBalancer | ํด๋ผ์ฐ๋ ํ๋ซํผ์ ๋ก๋ ๋ฐธ๋ฐ์๋ฅผ ์ฌ์ฉํ์ฌ ์ธ๋ถ ํธ๋ํฝ์ ์๋น์ค๋ก ๋ถ์ฐํฉ๋๋ค. | ํด๋ผ์ฐ๋ ํ๊ฒฝ์์ ์ธ๋ถ ์๋น์ค ๋ ธ์ถ |
| ExternalName | ์ธ๋ถ ์๋น์ค๋ฅผ DNS CNAME ๋ ์ฝ๋๋ก ๋งคํํ์ฌ ํด๋ฌ์คํฐ ๋ด๋ถ์์ ๋ณ์นญ์ผ๋ก ์ฌ์ฉํ ์ ์๊ฒ ํฉ๋๋ค. | ์ธ๋ถ ์๋น์ค์ ๋ด๋ถ์์ ์ ๊ทผ |
- ๋ณดํต
kubectl expose๋ช ๋ น์ด๋ฅผ ์ฐ๋ฉด ๊ฐ๋จํ๊ฒ Service๋ฅผ ๋ง๋ค ์ ์์ต๋๋ค.- ํ์ง๋ง ์ด๋ ๊ฒ ํ๋ฉด kubectl์ด ์๋์ผ๋ก Service ๋ฆฌ์์ค๋ฅผ ์์ฑํฉ๋๋ค.
- ๋ฐ๋ฉด์
yamlํ์ผ(nginx-clusterip.yaml)์ ์์ฑํด์kubectl apply -f๋ก ์ ์ฉํ๋ ๋ฐฉ๋ฒ์,
โ ๋ช ์์ ์ผ๋ก Service ๋ฆฌ์์ค๋ฅผ ์ ์ํ๊ณ , ์ํ๋ ์ค์ ์ ๋ฒ์ ๊ด๋ฆฌ(Git ๋ฑ)์ ํจ๊ป ๊ธฐ๋กํ ์ ์๋ค๋ ์ฅ์ ์ด ์์ต๋๋ค.
์ด์ ๋ฐ๋ผ, ๐ซ์ด์ ๋ฐ ์ฌํ ๊ฐ๋ฅํ ์ธํ๋ผ ๊ด๋ฆฌ ๋ฐฉ์์ ์ตํ๊ธฐ ์ํด๐ซ yaml ํ์ผ ๊ธฐ๋ฐ์ ๋ฆฌ์์ค ๊ด๋ฆฌ ๋ฐฉ๋ฒ์ ์ ํํ์ต๋๋ค.
: ํด๋ฌ์คํฐ ๋ด๋ถ์์๋ง ์ ๊ทผ ๊ฐ๋ฅํ ๋ด๋ถ IP ์ ๊ณตํฉ๋๋ค. (์ฌ์ฉ ์์: ๋ด๋ถ DB, ๋ด๋ถ API ์๋ฒ)
- ๐
nginx-clusterip.yaml
apiVersion: v1
kind: Service
metadata:
name: my-clusterip-service
spec:
selector:
app: my-app
ports:
- protocol: TCP
port: 80
targetPort: 8900
type: ClusterIP: ํด๋ฌ์คํฐ ์ธ๋ถ์์๋ ์ ๊ทผํ ์ ์๋๋ก ๊ฐ ๋ ธ๋(Node)์ ํน์ ํฌํธ์์ ์๋น์คํ๋ ์ ํ์ ๋๋ค.
- ์ํคํ ์ฒ ๊ตฌ์ฑ๋
- yaml ํ์ผ ์ค๋ช
- ๋ ธ๋ํฌํธ๋ 30000 ~ 32767์ ๋ฒ์๋ก ๊ณ ์
- ์ธ๋ถ์์ ์ ๊ทผ ์ http://: ํ์์ผ๋ก ์ ๊ทผ ๊ฐ๋ฅ
type: NodePort
ports:
- port: 80
targetPort: 8900
nodePort: 30081- ๐
nginx-nodeport.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: giljar-deployment
spec:
replicas: 3
selector:
matchLabels:
app: giljar
template:
metadata:
labels:
app: giljar
spec:
containers:
- name: giljar-container
image: byeonggill/giljar1:1.0
ports:
- containerPort: 8900
---
apiVersion: v1
kind: Service
metadata:
name: giljar-service
spec:
selector:
app: giljar
ports:
- protocol: TCP
port: 80
targetPort: 8900
nodePort: 30081
type: NodePort- ํด๋น ์ด๋ฏธ์ง ๊ตฌ๋ ๋ฐ ์คํ ํ์ธ
| ์ฌ์ง | ์ค๋ช |
|---|---|
![]() |
yaml ํ์ผ ์ ์ฉ ๋ฐ ํ์ธํ๊ธฐ |
![]() |
curl๋ก jar ๊ตฌ๋ ํ์ธ |
- ์น ์ ์ ํ์ธ
| ์ฌ์ง | ์ค๋ช |
|---|---|
![]() |
์ฌ์ ์์ : ํฌํธ ํฌ์๋ฉ |
![]() |
์น ์ ์ํ๊ธฐ |
์ฟ ๋ฒ๋คํฐ์ค์์ Ingress๋ ํด๋ฌ์คํฐ ์ธ๋ถ์์ ๋ด๋ถ ์๋น์ค๋ก ๋ค์ด์ค๋ HTTP/HTTPS ์์ฒญ์ ์ฒ๋ฆฌํ๋ ๊ท์น๋ค์ ์งํฉ์ ๋๋ค. ๋จ์ํ IP ๊ธฐ๋ฐ ๋ผ์ฐํ ์ ์ ๊ณตํ๋ Service์ ๋ฌ๋ฆฌ, ๋๋ฉ์ธ ๊ธฐ๋ฐ ๋ผ์ฐํ ์ ์ค์ ํ ์ ์์ต๋๋ค.
Ingress๋ฅผ ํตํด ์ธ๋ถ ํธ๋ํฝ์ ๋ผ์ฐํ ํ ๋, Service๋ ๊ตณ์ด ์ธ๋ถ IP๋ฅผ ๊ฐ์ง ํ์๊ฐ ์์ต๋๋ค. ๋ชจ๋ ํธ๋ํฝ์ Ingress Controller๊ฐ ๋ฐ์์ ๋ด๋ถ์ ์ผ๋ก ์ฒ๋ฆฌํ๊ธฐ ๋๋ฌธ์ ๋๋ค. ๋ฐ๋ผ์ ์ธ๋ถ ๋ ธ์ถ์ ์ต์ํํ๊ณ ํด๋ฌ์คํฐ ๋ด๋ถ ํต์ ์ ์ต์ ํ๋ ClusterIP ํ์ ์ ์ ํํ์ต๋๋ค. ๋ง์ฝ NodePort๋ฅผ ์ฌ์ฉํ๋ค๋ฉด ๋ถํ์ํ ํฌํธ๊ฐ ์ธ๋ถ์ ๋ ธ์ถ๋์ด ๋ณด์์ ์ผ๋ก ์ทจ์ฝํด์ง ์ ์์์ ๊ฒ๋๋ค.
[๊ทธ๋ฆผ] Kubernetes ์์ฒญ ์ฒ๋ฆฌ ํ๋ฆ
ํด๋ผ์ด์ธํธ๊ฐ http://spring.local๋ก ์์ฒญ์ ๋ณด๋ด๋ฉด Ingress๊ฐ ์ด๋ฅผ ๋ฐ์ nginx-service๋ก ์ ๋ฌํฉ๋๋ค. Service๋ ClusterIP(80 โ 8900)์ ํตํด Deployment์ ์ฐ๊ฒฐ๋ Pod๋ค(spring1, spring2)๋ก ํธ๋ํฝ์ ๋ถ์ฐํฉ๋๋ค. ๊ฐ์ ๋ผ๋ฒจ์ ๊ฐ์ง Pod๋ค์ ๋ชจ๋ containerPort: 8900์์ ์์ฒญ์ ์์ ํ๋ฉฐ, ReplicaSet์ ์ํด ๋ค์คํ๋์ด ๋ถํ ๋ถ์ฐ๊ณผ ์ฅ์ ๋ณต๊ตฌ๊ฐ ๊ฐ๋ฅํฉ๋๋ค.
- ๐ Ingress Controller
- Ingress ๋ฆฌ์์ค๊ฐ ์ค์ ๋ก ๋์ํ๋ ค๋ฉด Ingress Controller๊ฐ ํ์ํฉ๋๋ค.
- Ingress Controller๋ ์ ์๋ Ingress ๊ท์น์ ์ฝ๊ณ , ์ค์ ๋ก ํธ๋ํฝ์ ๋ผ์ฐํ ํ๋ ๋ฆฌ๋ฒ์ค ํ๋ก์(์: NGINX)๋ฅผ ์คํํฉ๋๋ค.
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: nginx-ingress
namespace: default
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- host: nginx.local
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: nginx-service # nginx.local๋ก ๋ค์ด์จ ์์ฒญ์ nginx-service์ port 80์ผ๋ก ํฌ์๋ฉ
port:
number: 80
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: spring-ingress
namespace: default
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- host: spring.local
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: spring-service # spring.local๋ก ๋ค์ด์จ ์์ฒญ์ spring-service์ port 88๋ก ํฌ์๋ฉ
port:
number: 88apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app: nginx
ports:
- port: 80
targetPort: 80
type: ClusterIP
---
apiVersion: v1
kind: Service
metadata:
name: spring-service
spec:
selector:
app: spring
ports:
- port: 88
targetPort: 8900
type: ClusterIPapiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: registry.k8s.io/nginx:latest
ports:
- containerPort: 80
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: spring-deployment
spec:
replicas: 2
selector:
matchLabels:
app: spring
template:
metadata:
labels:
app: spring
spec:
containers:
- name: spring
image: byeonggill/giljar1:1.0
ports:
- containerPort: 8900-
nginx
- containerPort: 80์์ HTTP ์์ฒญ์ ์์ ํฉ๋๋ค.
- registry.k8s.io/nginx:latest ์ด๋ฏธ์ง๋ฅผ ์ฌ์ฉํฉ๋๋ค.
- Pod 2๊ฐ๋ก ์คํ๋์ด ๋ถํ ๋ถ์ฐ์ด ๊ฐ๋ฅํฉ๋๋ค.
-
spring
- containerPort: 8900์์ ์์ฒญ์ ์์ ํฉ๋๋ค.
- byeonggill/giljar1:1.0 ์ด๋ฏธ์ง๋ฅผ ์ฌ์ฉํฉ๋๋ค.
- Pod 2๊ฐ๋ก ์คํ๋์ด ๊ณ ๊ฐ์ฉ์ฑ์ด ๋ณด์ฅ๋ฉ๋๋ค.
- Ingress ํ์ฑํ ๋ฐ yaml ์คํ
minikube addons enable ingress
kubectl apply -f nginx-ingressdepsvc.yaml
kubectl apply -f nginx-ingress.yaml | ์ฌ์ง | ์ค๋ช |
|---|---|
![]() |
service, deployment, pod ํ์ธ |
![]() |
/etc/hosts ํ์ผ์ DNS ๋ฑ๋ก |
- ์คํ ํ์ธ
| curl ํ์ธ | health ํ์ธ |
|---|---|
![]() |
![]() |
1. ์๋น์ค ์ ํ์ ์ฐจ์ด๋ฅผ ๋ช ํํ ์ดํดํ์ต๋๋ค. ClusterIP, NodePort, Ingress๋ฅผ ์ง์ ๊ตฌ์ฑํด๋ณด๋ฉด์ ๋จ์ํ ๊ฐ๋ ์ผ๋ก๋ง ์๊ณ ์๋ ์ฐจ์ด๊ฐ ์ค์ ๋ก ์ด๋ป๊ฒ ๋์ํ๋์ง ์ฒด๊ฐํ ์ ์์์ต๋๋ค.
2. ํธ๋ํฝ ๋ผ์ฐํ ๊ตฌ์กฐ๊ฐ ๋์ ๋ณด์์ต๋๋ค. Ingress๋ฅผ ์ ์ฉํด nginx.local, spring.local ๊ฐ์ ํธ์คํธ ๊ธฐ๋ฐ ๋ผ์ฐํ ์ ํ ์คํธํ๋ฉด์, ๋จ์ ํฌํธ ๊ธฐ๋ฐ ์ ๊ทผ๋ณด๋ค ํจ์ฌ ์ง๊ด์ ์ธ ๊ตฌ์กฐ๋ฅผ ๋ง๋ค ์ ์๋ค๋ ์ ์ด ์ธ์ ๊น์์ต๋๋ค.
3. YAML ๊ธฐ๋ฐ ๋ฆฌ์์ค ๊ด๋ฆฌ์ ํ์์ฑ์ ๋๊ผ์ต๋๋ค. ๋จ์ํ kubectl expose ๊ฐ์ ๋ช ๋ น์ผ๋ก ๋ฆฌ์์ค๋ฅผ ๋ง๋ค ๋๋ณด๋ค, kubectl apply -f . ๋ช ๋ น์ด๋ก 2๊ฐ์ ์ ํ๋ฆฌ์ผ์ด์ (์ด 4๊ฐ Pod)์ 5์ด ์์ ๋ฐฐํฌํ ์ ์๋ ์ ์ธํ ์ธํ๋ผ ํ๊ฒฝ์ ๊ตฌ์ถํ์ต๋๋ค. yaml ํ์ผ๋ก ๋ช ์์ ์ผ๋ก ๊ด๋ฆฌํ๋ ๋ฐฉ์์ด ์ฌํ์ฑ๊ณผ ๋ฒ์ ๊ด๋ฆฌ ์ธก๋ฉด์์ ํจ์ฌ ์์ ์ ์์ ํ์ธํ์ต๋๋ค.









