Lab3
Lab3 : Orchestrating the Cloud with Kubernetes
Last updated
Lab3 : Orchestrating the Cloud with Kubernetes
Last updated
Kubernetes Engine์ ํ์ฉํ Kubernetes cluster provision
kubectl ์ด์ฉํ ๋์ปค ์ปจํ ์ด๋ ๋ฐฐํฌ ๋ฐ ๊ด๋ฆฌ
Kubernetes์ Deployments์ Services๋ฅผ ํ์ฉํด application์ microservice ๋ก ์ชผ๊ฐ๊ธฐ
zone ์ค์
ํด๋ฌ์คํฐ ์์ฑ
GitHub repository๋ฅผ ํด๋ก ํ๋ค.
gs ๋ก ์์ํ๋๊ฒ google cloud storage ์ ๊ทผํ๋๊ฒ.
ํ์ํ ๋๋ ํ ๋ฆฌ๋ก ์ด๋ํ๋ค.
์ฟ ๋ฒ๋คํฐ์ค๋ฅผ ์์ํ๋ ๊ฐ์ฅ ์ฌ์ด ๋ฐฉ๋ฒ์ kbect1 create ์ปค๋งจ๋๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด๋ค.
์ ๋ช ๋ น์ด ์ฌ์ฉํ์ฌ ์ฟ ๋ฒ๋คํฐ์ค ๋ฐฐํฌ๋ฅผ ์์ฑํ๋ค.
์คํ์ค์ธ ๋ ธ๋์ ์ฅ์ ๊ฐ ๋ฐ์ํ๋๋ผ๋ ๋ฐฐํฌ๋ ๊ณ์ ์คํ๋๋ค.
์ฟ ๋ฒ๋คํฐ์ค์์๋ ๋ชจ๋ ์ปจํ ์ด๋๋ค์ด pod์์ ์คํ๋๋ค.
์คํ์ค์ธ nginx ์ปจํ ์ด๋๋ฅผ ํ์ธํ๋ค.
nginx ์ปจํ ์ด๋๊ฐ ์คํ์ค์ด๋ฉด ์ ๋ช ๋ น์ด๋ฅผ ํตํด ์ฟ ๋ฒ๋คํฐ์ค ์ธ๋ถ์ expose ํ ์ ์๋ค.
์ฟ ๋ฒ๋คํฐ์ค๊ฐ ๊ณต์ฉ IP ์ฃผ์๋ฅผ ํตํด ์ธ๋ถ ๋ก๋๋ฐธ๋ฐ์๋ฅผ ์์ฑํ๋ค.
ํด๋น ๊ณต์ฉ IP ์ฃผ์๋ฅผ ์กฐํํ๋ ๋ชจ๋ ํด๋ผ์ด์ธํธ๋ ํด๋น pod๋ก ๋ผ์ฐํ ๋๋ค.
์ ๊ฒฝ์ฐ์๋ nginx pod๋ก ๋ผ์ฐํ ๋๋ค.
ํ์ฌ ์ฌ์ฉ์ค์ธ ์๋น์ค ๋ฆฌ์คํธ๋ฅผ ์กฐํํ๋ค.
Nginx ์ปจํ ์ด๋๋ฅผ ์๊ฒฉ์ผ๋ก hit ํ๊ธฐ ์ํด ์ ๋ช ๋ น์ด๋ฅผ ์ฌ์ฉํ๋ค.
์ฟ ๋ฒ๋คํฐ์ค์ ํต์ฌ์๋ Pod ๊ฐ ์๋ค.
Pods๋ ํ๋ ์ด์์ ์ปจํ ์ด๋ ์งํฉ์ ๋ํ๋ธ๋ค. ์๋ก์ ๋ํ ์์กด๋๊ฐ ๋์ ์ฌ๋ฌ๊ฐ์ ์ปจํ ์ด๋๊ฐ ์๋ ๊ฒฝ์ฐ ์ปจํ ์ด๋๋ฅผ ๋จ์ผ pod ๋ด์ ํจํค์งํ๋ค.
Pods์๋ Volumes๊ฐ ์๋ค.
๋ณผ๋ฅจ์ pods๊ฐ ์ด์์๋ ๋์ ์ง์๋๋ ๋ฐ์ดํฐ ๋์คํฌ๋ก, ํด๋น pod์ ์ปจํ ์ด๋์์ ์ฌ์ฉ ๊ฐ๋ฅํ๋ค. Pods๋ ๊ฐ์ง๊ณ ์๋ ๋ด์ฉ์ ๋ํ shared namespace๋ฅผ ์ ๊ณตํ์ฌ pod ๋ด๋ถ์ ์ปจํ ์ด๋๋ค์ด ์๋ก ํต์ ํ ์ ์๊ณ , ์ฐ๊ฒฐ๋์ด ์๋ ๋ณผ๋ฅจ๋ ๊ณต์ ํ ์ ์๋๋ก ํ๋ค.
Pods๋ network namespace๋ ๊ณต์ ํ์ฌ ํ๋์ pod๋น ํ๋์ IP Address๋ฅผ ๊ฐ์ง๊ณ ์๋ค.
Pod๋ pod configuration file์ ํตํด ์์ฑ๋๋ค.
ํ์ฌ pod๋ ํ๋์ ์ปจํ ์ด๋ (monolith)๋ก ์ด๋ฃจ์ด์ ธ์๋ค.
์ปจํ ์ด๋๊ฐ ์์ํ ๋ ๋ช๊ฐ์ง argument๋ค์ ์ ๋ฌํ๋ค.
http traffic ์ ์ํด 80๋ฒ ํฌํธ๋ฅผ ์ฐ๋ค.
์ ๋ช ๋ น์ด๋ฅผ ์ฌ์ฉํด monolith pod๋ฅผ ์์ฑํ๋ค.
default namespace์์ ์คํ์ค์ธ ๋ชจ๋ pod ๋ฆฌ์คํธ๋ฅผ ํ์ธํ๋ค.
monolith pod ์ ๋ํ ์ ๋ณด๋ค ์ถ๋ ฅ
Pod IP ์ฃผ์์ event log๋ฅผ ํฌํจํ ์ ๋ณด๋ค์ ํ์ธ ๊ฐ๋ฅํ๋ค.
troubleshooting ์ ์ฉ์ดํ๊ฒ ์ฌ์ฉ๊ฐ๋ฅ
๊ธฐ๋ณธ์ ์ผ๋ก pod๋ค์ ํ ๋น๋ ์ฌ์ค IP ์ฃผ์์ด๊ณ ํด๋ฌ์คํฐ ์ธ๋ถ์์ ์ ๊ทผ์ด ๋ถ๊ฐ๋ฅํ๋ค. ๋ฐ๋ผ์ kubecgtl port-forward ๋ช ๋ น์ด๋ฅผ ํตํด ๋ก์ปฌ ํฌํธ๋ฅผ monolith pod ๋ด๋ถ๋ก ๋งคํํด์ผํ๋ค.
์๋ก์ด ํฐ๋ฏธ๋์ ์ด์ด ํ๋๋ kubectl port-forward ๋ช ๋ น์ด๋ฅผ ์คํํ๊ณ ๋ค๋ฅธ ํฐ๋ฏธ๋์ curl ๋ช ๋ น์ด๋ฅผ ์คํํ๋ค.
curl ๋ช ๋ น์ด๋ฅผ ์ด์ฉํด pod์ ์ํตํ๊ธฐ
์ปจํ ์ด๋์์ โhelloโ ๋ฉ์์ง๋ฅผ ์์ ํ ๊ฒ์ ํ์ธํ ์ ์๋ค.
authroization fail ์๋ต์ ์์ ํ๋ค.
password๋ฅผ ์ ๋ ฅํ๋ผ๊ณ ํด์ ์ ๋ ฅํ๋ฉด ๋ก๊ทธ์ธ์ด ์ฑ๊ณตํ๊ณ , JWT token์ ์์ ๋ฐ๋๋ค.
ํ ํฐ ์ ๋ณด ๋ณ์ ์์ฑ
Bearer Token์ ๋ด์ ๋ค์ ๋ณด๋ด๋ฉด ์ ๋๋ก๋ ์๋ต์ ๋ฐ๊ฒ ๋๋ค.
monolith Pod์ ๋ก๊ทธ๋ฅผ ๋ณด๊ธฐ ์ํด ์ ๋ช ๋ น์ด๋ฅผ ์ฌ์ฉํ๋ค.
์๋ก์ด ํฐ๋ฏธ๋์์ -f ์ต์ ์ ๋ถ์ฌ ์คํํ๋ฉด ์ค์๊ฐ์ผ๋ก ๋ฐ์ํ๋ ๋ก๊ทธ ์คํธ๋ฆผ์ ๋ฐ์ ์ ์๋ค.
Monolith Pod ๋ด๋ถ์์ interactive shell์ ์คํํ๊ธฐ ์ํด ์ ๋ช ๋ น์ด๋ฅผ ์ฌ์ฉํ๋ค.
์ด๋ฐ์์ผ๋ก ์ธ๋ถ์ ping ๋ช ๋ น์ด ๋ฑ์ ์ด์ฉํด ์ธ๋ถ ์ฐ๊ฒฐ์ฑ์ ํ ์คํธํ ์ ์๋ค.
interactive shell์ ์ข ๋ฃํ ๋๋ exit ๋ช ๋ น์ด๋ฅผ ์ฌ์ฉํ๋ค.
์๋น์ค๋ pod์๋ค๊ฐ ๋งคํํด์ ๊ณ์ ๊ด๋ฆฌํด์ฃผ๋ ํ๋์ cluster๋ ์ต์ ๋ง์คํฐ ์๋น์ค๋ฅผ ๊ฐ์ง๊ณ ์์.
Pods๋ค์ ์์ํ์ง์๋ค. ๋ง์ฝ ์ฌ์์๋๋ฉด ํด๋น pod๋ค์ ๋ค๋ฅธ IP ์ฃผ์๋ฅผ ๊ฐ์ง๊ฒ ๋ ๊ฒ์ด๋ค. ์ด๋ด ๋ Services๋ฅผ ์ฌ์ฉํ๋ค.
Services๋ pods๋ค์ ๊ณ ์ endpoint๋ฅผ ์ ๊ณตํ๋ค.
์๋น์ค๋ ๋ผ๋ฒจ์ ์ฌ์ฉํ์ฌ ์ด๋ค pod์์ ์๋ํ๋์ง ํ๋ณํ๋ค.
์๋น์ค ํ์ ์ ๋ฐ๋ฅธ service ๋จ๊ณ
ClusterIP (internal) : ํด๋น ์๋น์ค๊ฐ ํด๋ฌ์คํฐ ๋ด๋ถ์์๋ง ๋ณด์ธ๋ค
NodePort : ํด๋ฌ์คํฐ์ ๊ฐ ๋ ธ๋์ ์ธ๋ถ์์ ์ก์ธ์คํ ์ ์๋ Ip๋ฅผ ์ ๊ณต
LoadBalancer : ํด๋ผ์ฐ๋ ์ ๊ณต์์ ๋ก๋๋ฐธ๋ฐ์๋ฅผ ์ฌ์ฉํ์ฌ ์๋น์ค์์ ๋ ธ๋๋ก ํธ๋ํฝ์ ์ ๋ฌํ๋ค.
์ด์ ์๋น์ค๋ฅผ ์์ฑํ๊ณ label selectors๋ฅผ ์ด์ฉํ์ฌ ์ธ๋ถ์ ๋ ธ์ถํ์.
์๋น์ค ์์ฑ ์ https traffic๋ฅผ ๋ค๋ฃจ๋ ์์ ํ pod๋ฅผ ์์ฑํ๋ค.
secure-monolith pods ์ configuration data๋ฅผ ์์ฑํ๋ค.
monolith service configuration file
๋ผ๋ฒจ app: monolith ์ secure: enbaled ๊ฐ ์๋ pod๋ค์ ์๋์ผ๋ก ์ฐพ๊ณ expose ํ๋ selector๊ฐ ์๋ค.
์ฌ๊ธฐ์ nodeport๋ฅผ exposeํด์ 31000๋ฒ ํฌํธ์์ ngins(port 443)์ผ๋ก ์ธ๋ถ ํธ๋ํฝ์ ์ ์กํด์ผํ๋ค.
์ ๋ช ๋ น์ด๋ฅผ ์ฌ์ฉํด monolith service configuration file๋ก๋ถํฐ monolith service๋ฅผ ์์ฑํ๋ค.
์ด์ ์๋น์ค๋ฅผ expose ํ๊ธฐ ์ํด 31000๋ฒ ํฌํธ๋ฅผ ์ฌ์ฉํ๊ณ ์๋ค.
๋ค๋ฅธ ์ฑ์ด ํด๋น ํฌํธ์ ๋ฐ์ธ๋ฉํ๋ ค๊ณ ํ๋ฉด ์ถฉ๋์ด ์๊ธด๋ค.
์ผ๋ฐ์ ์ผ๋ก ์ฟ ๋ฒ๋คํฐ์ค๋ ์ด๋ฌํ ํฌํธ assignment๋ฅผ ์กฐ์ ํ๋ค.
์ ๋ช ๋ น์ด๋ฅผ ์ด์ฉํด exposed node port์ monolith service๋ก์ ํธ๋ํฝ์ ํ์ฉํ๋ค.
์ด์ ํด๋ฌ์คํฐ ์ธ๋ถ์์ port forwarding ์์ด๋ secure-monolith service์ hit ํ ์ ์๋ค.
์ ๋ช ๋ น์ด๋ฅผ ํตํด ๋ ธ๋ ์ค ํ๋์ ์ธ๋ถ IP ์ฃผ์๋ฅผ ํ์ธํ๋ค.
์ด๋ฌ๋ฉด ๋ฌธ์ ๊ฐ ๋ฐ์ํ๋ค. ์ด์ ํด๊ฒฐํด๋ณด์
์ง๊ธ monolith service๋ endpoint๋ฅผ ๊ฐ์ง๊ณ ์์ง ์๋ค.
monolith label์ ๊ฐ์ง๊ณ ์๋ ์คํ์ค์ธ pods๋ค์ ํ์ธํ ์ ์๋ค.
๊ตฌ์ฒด์ ์ธ ๋ผ๋ฒจ๋ค์ ํ์ธํ๋ฉด?
์๋ฌด๋ฐ ๋ฆฌ์์ค๋ ์๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
๊ทธ๋ ๊ธฐ ๋๋ฌธ์ ์์์ ์ค๋ฅ๊ฐ ๋ฌ๋ ๊ฒ์ด๊ณ , secure=enabled ๋ผ๋ฒจ์ ์ถ๊ฐํด์ฃผ์ด์ผํ๋ค.
๋ผ๋ฒจ์ ๋ถ์ด๊ณ ๋ผ๋ฒจ์ด ์ ๋ฐ์ดํธ ๋์๋์ง ํ์ธํ๋ค.
์ ์์ ์ผ๋ก ์ ๋ถ์๋ค๋ฉด monolith ์๋น์ค์ ์๋ํฌ์ธํธ ๋ฆฌ์คํธ๋ฅผ ํ์ธํ๋ค.
์ ๋์๊ฐ๋ค!
์ด์ ์ปจํ ์ด๋๋ค์ scalingํ๊ณ ๋งค๋์งํด๋ณด์.
Deployments๋ ์คํ ์ค์ธ pods์ ์๊ฐ ์ฌ์ฉ์๊ฐ ์ง์ ํ ์ํ๋ pods์ ์์ ๊ฐ๋๋ก ํ๋ ๋ฐฉ๋ฒ์ด๋ค.
The main benefit of Deployments is in abstracting away the low level details of managing Pods. Behind the scenes Deployments use Replica Sets to manage starting and stopping the Pods. If Pods need to be updated or scaled, the Deployment will handle that. Deployment also handles restarting Pods if they happen to go down for some reason.
ํ์ฌ ์งํํ๋ ์ฑ์ ์ธ๊ฐ์ง ํํธ๋ก ๋๋๋ฉด
auth : ์ธ์ฆ๋ ์ฌ์ฉ์๋ค์๊ฒ JWT token ์์ฑ
hello : ์ธ์ฆ๋ ์ฌ์ฉ์๋ค์๊ฒ ์ธ์ฌ
frontend : auth์ hello ์๋น์ค๋ค์ ํธ๋ํฝ์ ๋ผ์ฐํ ํจ.
auth์ hello deployments๋ฅผ ์ํ ๋ด๋ถ์ ์ธ ์๋น์ค๋ค์ ์ ์ํ๊ณ frontend deployment๋ฅผ ์ํ ์ธ๋ถ ์๋น์ค๋ฅผ ์ ์ํ๋ค.
auth deployment configuration file ์์ฑ
deployment๋ ํ๋์ replica๋ฅผ ์์ฑํ๋ ๊ฒ์ด๋ค.
auth ์ปจํ ์ด๋์ ๋ฒ์ 2.0.0 ์ ์ฌ์ฉํ๋ค.
When you run the kubectl create command to create the auth deployment it will make one pod that conforms to the data in the Deployment manifest. This means you can scale the number of Pods by changing the number specified in the Replicas field.
์ ๋ช ๋ น์ด๋ก deployment object๋ฅผ ์์ฑํ๋ค.
auth deployment๋ฅผ ์ํ ์๋น์ค๋ฅผ ์์ฑํ๋ค.
๋์ผํ๊ฒ hello deployment์ frontend Deployment์๋ ์ ์ฉํ๋ค.
frontend์ ์ธ๋ถ IP ์ฃผ์๋ฅผ ํตํด curlingํด์ interact ํ๋ค.
Pod๊ฐ ๋ฌด์์ธ์ง?
LoadBalancer
Scaling ์ด ๋ฌด์์ด๋์
LoadBalancer ์๋น์ค ํ์ ์ ๋จ์ง ํ๊ฐ์ ๋ด๋ถ ์๋น์ค๋ฅผ ์ธ๋ถ ์ฌ์ฉ์๋ค์๊ฒ ์ ๊ทผ ๊ฐ๋ฅํ๋๋ก ๋ง๋๋ ์ผ์ ๋ด๋นํฉ๋๋ค. ๋ฐ๋๋ก Ingress ์๋น์ค ํ์ ์ ์ฌ๋ฌ๊ฐ์ ์๋น์ค๊ฐ ํ๊ฐ ๋ก๋ ๋ฐธ๋ฐ์๋ฅผ ํตํด ์ ์ฐํ ์ค์ ์ ํ ์ ์๊ฒ ๋ง๋ญ๋๋ค.
Kubernetes NodePort vs LoadBalancer vs Ingress? When should I use what?
Load Balancer๋ ํ๊ฐ์ ๋ด๋ถ ์๋น์ค๋ฅผ ์ธ๋ถ๋ก ์ ๊ทผ ๊ฐ๋ฅํ๊ฑฐ๊ณ
Ingress๋ load balancer๋ฅผ ๋์ฒดํ๋ฉด์ ๋ค์ด๊ฐ. L7 ๊ณ์ธต์์ ๋ผ์ฐํ ์ํด์ฃผ๋๊ฒ. ๊ทธ๋์ url๊น์ง ๋ผ์ฐํ ํด์ค.
L4 ๋ ๋จ์ํ ๋ถํ๋ฅผ ๋ถ์ฐ์ํค๋๊ฒ.