๐๐ป ์ด์ ๊ฒ์๋ฌผ์์ ๋กค๋ง ์
๋ฐ์ดํธ๋ ์ค์ผ์ผ๋ง์ ๋ํด์ ์ดํด๋ดค์ต๋๋ค.
In the previous post, we looked at rolling updates and scaling.
๐๐ป ์ด์ ๊ฒ์๋ฌผ์์ ์ฌ์ฉํ๋ ์
ํ
์ ์ถ๊ฐ๋ก service๋ฅผ ์ถ๊ฐํด๋ณด๊ฒ ์ต๋๋ค.
Let’s add a service to the settings used in the previous post.
๐๐ป ์ฟ ๋ฒ๋คํฐ์ค๋ฅผ ์ธ๋ถ์ ์ด์ ์ฒ๋ผ(port-forward) ์ธ๋ถ์ ๋
ธ์ถํ ๊ฒฝ์ฐ ์ฟ ๋ฒ๋คํฐ์ค ๋ด๋ถ์์ ๋ก๋๋ฐธ๋ฐ์ฑ์ด ์๋ํ์ง ์์ ์ ์์ต๋๋ค.
If you expose Kubernetes externally as before (port-forward), load balancing within Kubernetes may not work.
๐๐ป port-forward๋ฅผ ์ฌ์ฉํ๋๊ฒฝ์ฐ ํ
์คํธ์๋ ์ ํฉํ์ง๋ง ์ธ๋ถ์์ ์ ์์ ์ฒ์ ์ ์ํ ํ๋์ ์ ์์ด ๊ณ ์ ๋ฉ๋๋ค.
Using port-forward is suitable for testing, but when accessing from outside, the connection is fixed to the first connected pod.
โ๏ธ port-forward๋ฅผ ์ฌ์ฉํด์ ์ธ๋ถ์ ๋
ธ์ถํ๊ฒฝ์ฐ
When exposed externally using port-forward
MacBookAir nginx-test1 % kubectl port-forward svc/nginx-deployment 8080:80
Forwarding from 127.0.0.1:8080 -> 80
Forwarding from [::1]:8080 -> 80
๐๐ป ๊ทธ๋์ ์ฟ ๋ฒ๋คํฐ์ค ๋ด๋ถ์์ ๋ก๋๋ฐธ๋ฐ์ฑ์ด ์๋ํ๊ฒ ํ๋ ค๋ฉด ์๋น์ค๋ฅผ ์ฌ์ฉํด์ผํฉ๋๋ค.
Therefore, to enable load balancing within Kubernetes, you must use a service.
โ๏ธ nginx-service.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app: nginx
type: NodePort
ports:
- port: 80
targetPort: 80
nodePort: 30080 # 30000~32767
protocol: TCP
— yamlํ์ผ ์ ์ฉ / Apply YAML file
kubectl apply -f nginx-service.yaml
๐๐ป ์ค์ ์์ / actual work
โ๏ธ ๋์ผํ ๋๋ ํ ๋ฆฌ ๋ด์ nginx-deployment.yaml๊ณผ nginx-service.yamlํ์ผ์ ์ค๋นํฉ๋๋ค.
Prepare the nginx-deployment.yaml and nginx-service.yaml files in the same directory.
โ๏ธ nginx-deployment.yaml ( ๋ฐ๋ก ์ด์ ๊ณผ ๋์ผํ ๋ด์ฉ / The exact same content as before)
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
โ๏ธ nginx-service.yaml (์๋ก ์ถ๊ฐํ ๋ด์ฉ)
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app: nginx
type: NodePort
ports:
- port: 80
targetPort: 80
nodePort: 30080 # 30000~32767
protocol: TCP
nginx-test1 % ls
nginx-deployment.yaml nginx-service.yaml
โ๏ธ ๋์ปค๋ฐ์คํฌํ์ ์คํํ๊ณ ํฐ๋ฏธ์ผ์์ yamlํ์ผ์ ์คํํฉ๋๋ค.
Run Docker Desktop and execute the yaml file in the terminal.
โ๏ธ ๋๋ ํ ๋ฆฌ ๋ด์ ํ์ผ์ ํ๋ฒ์ ์ ์ฉํ๋ ค๋ฉด ์๋์ฒ๋ผ ํ๋ฉด๋ฉ๋๋ค.
To apply the files in the directory all at once, do as follows.
acBookAir nginx-test1 % kubectl apply -f .
deployment.apps/nginx-deployment created
service/nginx-service created
MacBookAir nginx-test1 %
โ๏ธ ๋ง์ฝ์ ์ด๋ฏธ์ง๊ฐ ์์ผ๋ฉด magePullBackOff๊ฐ ๋ํ๋ฉ๋๋ค.
If there is no image, ImagePullBackOff appears .
โ๏ธ ์ด๋ด ๊ฒฝ์ฐ ์ด๋ฏธ์ง๋ฅผ ๋ค์ ๋ค์ด ๋ฐ๊ณ ์ค์ ์ ์ฌ์ ์ฉํฉ๋๋ค.
In this case, download the image again and reapply the settings.
โ๏ธ nginx:latest์ด๋ฏธ์ง ๋ค์ด๋ก๋ / nginx:latest image download
docker pull nginx:latest
โ๏ธ ์ค์ ์ฌ์ ์ฉ / Reapply settings
# kubectl rollout restart deployment/[๋ํ๋ก์ด๋จผํธ๋ช
/ Deployment Name]
MacBookAir nginx-test1 % kubectl rollout restart deployment/nginx-deployment
deployment.apps/nginx-deployment restarted
MacBookAir nginx-test1 %
โ๏ธ pod์ํ ํ์ธ / Check pod status
— STATUS๊ฐ Running ์ผ๋ก ๋ฐ๋๊ฑธ ๋ณผ ์ ์์ต๋๋ค.
You can see that STATUS has changed to Running.
MacBookAir nginx-test1 % kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-deployment-6f9664446b-72mlp 1/1 Running 0 10m
nginx-deployment-6f9664446b-g2b4x 1/1 Running 0 10m
nginx-deployment-6f9664446b-xrqhr 1/1 Running 0 10m
MacBookAir nginx-test1 %
โ๏ธ ์๋น์ค ์ํํ์ธ / Check service status
# kubectl get service or kubectl get svc
MacBookAir nginx-test1 % kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 16h
nginx-service NodePort 10.96.157.81 <none> 80:30080/TCP 13m
โ๏ธ ๋ธ๋ผ์ฐ์ ์ ์ / Browser access
— ๋ธ๋ผ์ฐ์ ์์ 30080ํฌํธ๋ก ์ ์ํฉ๋๋ค.
Connect to port 30080 in your browser.
http://localhost:30080