Lab3
Lab3 : Orchestrating the Cloud with Kubernetes
Lab ๋ชฉํ
Kubernetes Engine์ ํ์ฉํ Kubernetes cluster provision
kubectl ์ด์ฉํ ๋์ปค ์ปจํ ์ด๋ ๋ฐฐํฌ ๋ฐ ๊ด๋ฆฌ
Kubernetes์ Deployments์ Services๋ฅผ ํ์ฉํด application์ microservice ๋ก ์ชผ๊ฐ๊ธฐ
gcloud config set compute/zone us-central1-b
zone ์ค์
gcloud container clusters create io
ํด๋ฌ์คํฐ ์์ฑ
1. Get the sample code
gsutil cp -r gs://spls/gsp021/* .
GitHub repository๋ฅผ ํด๋ก ํ๋ค.
gs ๋ก ์์ํ๋๊ฒ google cloud storage ์ ๊ทผํ๋๊ฒ.
ํ์ํ ๋๋ ํ ๋ฆฌ๋ก ์ด๋ํ๋ค.

2. Quick Kubernetes Demo
์ฟ ๋ฒ๋คํฐ์ค๋ฅผ ์์ํ๋ ๊ฐ์ฅ ์ฌ์ด ๋ฐฉ๋ฒ์ kbect1 create ์ปค๋งจ๋๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด๋ค.
kubectl create deployment nginx --image=nginx:1.10.0
์ ๋ช ๋ น์ด ์ฌ์ฉํ์ฌ ์ฟ ๋ฒ๋คํฐ์ค ๋ฐฐํฌ๋ฅผ ์์ฑํ๋ค.
์คํ์ค์ธ ๋ ธ๋์ ์ฅ์ ๊ฐ ๋ฐ์ํ๋๋ผ๋ ๋ฐฐํฌ๋ ๊ณ์ ์คํ๋๋ค.
์ฟ ๋ฒ๋คํฐ์ค์์๋ ๋ชจ๋ ์ปจํ ์ด๋๋ค์ด pod์์ ์คํ๋๋ค.
kubectl get pods
์คํ์ค์ธ nginx ์ปจํ ์ด๋๋ฅผ ํ์ธํ๋ค.
kubectl expose deployment nginx --port 80 --type LoadBalancer
nginx ์ปจํ ์ด๋๊ฐ ์คํ์ค์ด๋ฉด ์ ๋ช ๋ น์ด๋ฅผ ํตํด ์ฟ ๋ฒ๋คํฐ์ค ์ธ๋ถ์ expose ํ ์ ์๋ค.

์ฟ ๋ฒ๋คํฐ์ค๊ฐ ๊ณต์ฉ IP ์ฃผ์๋ฅผ ํตํด ์ธ๋ถ ๋ก๋๋ฐธ๋ฐ์๋ฅผ ์์ฑํ๋ค.
ํด๋น ๊ณต์ฉ IP ์ฃผ์๋ฅผ ์กฐํํ๋ ๋ชจ๋ ํด๋ผ์ด์ธํธ๋ ํด๋น pod๋ก ๋ผ์ฐํ ๋๋ค.
์ ๊ฒฝ์ฐ์๋ nginx pod๋ก ๋ผ์ฐํ ๋๋ค.
kubectl get services
ํ์ฌ ์ฌ์ฉ์ค์ธ ์๋น์ค ๋ฆฌ์คํธ๋ฅผ ์กฐํํ๋ค.

curl http://<External IP>:80
Nginx ์ปจํ ์ด๋๋ฅผ ์๊ฒฉ์ผ๋ก hit ํ๊ธฐ ์ํด ์ ๋ช ๋ น์ด๋ฅผ ์ฌ์ฉํ๋ค.
3. Pods
์ฟ ๋ฒ๋คํฐ์ค์ ํต์ฌ์๋ Pod ๊ฐ ์๋ค.

Pods๋ ํ๋ ์ด์์ ์ปจํ ์ด๋ ์งํฉ์ ๋ํ๋ธ๋ค. ์๋ก์ ๋ํ ์์กด๋๊ฐ ๋์ ์ฌ๋ฌ๊ฐ์ ์ปจํ ์ด๋๊ฐ ์๋ ๊ฒฝ์ฐ ์ปจํ ์ด๋๋ฅผ ๋จ์ผ pod ๋ด์ ํจํค์งํ๋ค.
Pods์๋ Volumes๊ฐ ์๋ค.
๋ณผ๋ฅจ์ pods๊ฐ ์ด์์๋ ๋์ ์ง์๋๋ ๋ฐ์ดํฐ ๋์คํฌ๋ก, ํด๋น pod์ ์ปจํ ์ด๋์์ ์ฌ์ฉ ๊ฐ๋ฅํ๋ค. Pods๋ ๊ฐ์ง๊ณ ์๋ ๋ด์ฉ์ ๋ํ shared namespace๋ฅผ ์ ๊ณตํ์ฌ pod ๋ด๋ถ์ ์ปจํ ์ด๋๋ค์ด ์๋ก ํต์ ํ ์ ์๊ณ , ์ฐ๊ฒฐ๋์ด ์๋ ๋ณผ๋ฅจ๋ ๊ณต์ ํ ์ ์๋๋ก ํ๋ค.
Pods๋ network namespace๋ ๊ณต์ ํ์ฌ ํ๋์ pod๋น ํ๋์ IP Address๋ฅผ ๊ฐ์ง๊ณ ์๋ค.
4. Creating pods
Pod๋ pod configuration file์ ํตํด ์์ฑ๋๋ค.
cat pods/monolith.yaml

ํ์ฌ pod๋ ํ๋์ ์ปจํ ์ด๋ (monolith)๋ก ์ด๋ฃจ์ด์ ธ์๋ค.
์ปจํ ์ด๋๊ฐ ์์ํ ๋ ๋ช๊ฐ์ง argument๋ค์ ์ ๋ฌํ๋ค.
http traffic ์ ์ํด 80๋ฒ ํฌํธ๋ฅผ ์ฐ๋ค.
kubectl create -f pods/monolith.yaml
์ ๋ช ๋ น์ด๋ฅผ ์ฌ์ฉํด monolith pod๋ฅผ ์์ฑํ๋ค.
kubectl get pods

default namespace์์ ์คํ์ค์ธ ๋ชจ๋ pod ๋ฆฌ์คํธ๋ฅผ ํ์ธํ๋ค.
kubectl describe pods monolith
monolith pod ์ ๋ํ ์ ๋ณด๋ค ์ถ๋ ฅ

Pod IP ์ฃผ์์ event log๋ฅผ ํฌํจํ ์ ๋ณด๋ค์ ํ์ธ ๊ฐ๋ฅํ๋ค.
troubleshooting ์ ์ฉ์ดํ๊ฒ ์ฌ์ฉ๊ฐ๋ฅ
5. Interacting with pods
๊ธฐ๋ณธ์ ์ผ๋ก pod๋ค์ ํ ๋น๋ ์ฌ์ค IP ์ฃผ์์ด๊ณ ํด๋ฌ์คํฐ ์ธ๋ถ์์ ์ ๊ทผ์ด ๋ถ๊ฐ๋ฅํ๋ค. ๋ฐ๋ผ์ kubecgtl port-forward ๋ช ๋ น์ด๋ฅผ ํตํด ๋ก์ปฌ ํฌํธ๋ฅผ monolith pod ๋ด๋ถ๋ก ๋งคํํด์ผํ๋ค.
์๋ก์ด ํฐ๋ฏธ๋์ ์ด์ด ํ๋๋ kubectl port-forward ๋ช ๋ น์ด๋ฅผ ์คํํ๊ณ ๋ค๋ฅธ ํฐ๋ฏธ๋์ curl ๋ช ๋ น์ด๋ฅผ ์คํํ๋ค.
kubectl port-forward monolith 10080:80

curl <http://127.0.0.1:10080>

curl ๋ช ๋ น์ด๋ฅผ ์ด์ฉํด pod์ ์ํตํ๊ธฐ
์ปจํ ์ด๋์์ โhelloโ ๋ฉ์์ง๋ฅผ ์์ ํ ๊ฒ์ ํ์ธํ ์ ์๋ค.
curl <http://127.0.0.1:10080/secure>
authroization fail ์๋ต์ ์์ ํ๋ค.
curl -u user <http://127.0.0.1:10080/login>
password๋ฅผ ์ ๋ ฅํ๋ผ๊ณ ํด์ ์ ๋ ฅํ๋ฉด ๋ก๊ทธ์ธ์ด ์ฑ๊ณตํ๊ณ , JWT token์ ์์ ๋ฐ๋๋ค.
TOKEN=$(curl <http://127.0.0.1:10080/login> -u user|jq -r '.token')
ํ ํฐ ์ ๋ณด ๋ณ์ ์์ฑ
curl -H "Authorization: Bearer $TOKEN" <http://127.0.0.1:10080/secure>
Bearer Token์ ๋ด์ ๋ค์ ๋ณด๋ด๋ฉด ์ ๋๋ก๋ ์๋ต์ ๋ฐ๊ฒ ๋๋ค.

kubectl logs monolith

monolith Pod์ ๋ก๊ทธ๋ฅผ ๋ณด๊ธฐ ์ํด ์ ๋ช ๋ น์ด๋ฅผ ์ฌ์ฉํ๋ค.
kubectl logs -f monolith
์๋ก์ด ํฐ๋ฏธ๋์์ -f ์ต์ ์ ๋ถ์ฌ ์คํํ๋ฉด ์ค์๊ฐ์ผ๋ก ๋ฐ์ํ๋ ๋ก๊ทธ ์คํธ๋ฆผ์ ๋ฐ์ ์ ์๋ค.
kubectl exec monolith --stdin --tty -c monolith -- /bin/sh
Monolith Pod ๋ด๋ถ์์ interactive shell์ ์คํํ๊ธฐ ์ํด ์ ๋ช ๋ น์ด๋ฅผ ์ฌ์ฉํ๋ค.

์ด๋ฐ์์ผ๋ก ์ธ๋ถ์ ping ๋ช ๋ น์ด ๋ฑ์ ์ด์ฉํด ์ธ๋ถ ์ฐ๊ฒฐ์ฑ์ ํ ์คํธํ ์ ์๋ค.
exit
interactive shell์ ์ข ๋ฃํ ๋๋ exit ๋ช ๋ น์ด๋ฅผ ์ฌ์ฉํ๋ค.
์๋น์ค๋ pod์๋ค๊ฐ ๋งคํํด์ ๊ณ์ ๊ด๋ฆฌํด์ฃผ๋ ํ๋์ cluster๋ ์ต์ ๋ง์คํฐ ์๋น์ค๋ฅผ ๊ฐ์ง๊ณ ์์.
6. Services
Pods๋ค์ ์์ํ์ง์๋ค. ๋ง์ฝ ์ฌ์์๋๋ฉด ํด๋น pod๋ค์ ๋ค๋ฅธ IP ์ฃผ์๋ฅผ ๊ฐ์ง๊ฒ ๋ ๊ฒ์ด๋ค. ์ด๋ด ๋ Services๋ฅผ ์ฌ์ฉํ๋ค.
Services๋ pods๋ค์ ๊ณ ์ endpoint๋ฅผ ์ ๊ณตํ๋ค.

์๋น์ค๋ ๋ผ๋ฒจ์ ์ฌ์ฉํ์ฌ ์ด๋ค pod์์ ์๋ํ๋์ง ํ๋ณํ๋ค.
์๋น์ค ํ์ ์ ๋ฐ๋ฅธ service ๋จ๊ณ
ClusterIP (internal) : ํด๋น ์๋น์ค๊ฐ ํด๋ฌ์คํฐ ๋ด๋ถ์์๋ง ๋ณด์ธ๋ค
NodePort : ํด๋ฌ์คํฐ์ ๊ฐ ๋ ธ๋์ ์ธ๋ถ์์ ์ก์ธ์คํ ์ ์๋ Ip๋ฅผ ์ ๊ณต
LoadBalancer : ํด๋ผ์ฐ๋ ์ ๊ณต์์ ๋ก๋๋ฐธ๋ฐ์๋ฅผ ์ฌ์ฉํ์ฌ ์๋น์ค์์ ๋ ธ๋๋ก ํธ๋ํฝ์ ์ ๋ฌํ๋ค.
์ด์ ์๋น์ค๋ฅผ ์์ฑํ๊ณ label selectors๋ฅผ ์ด์ฉํ์ฌ ์ธ๋ถ์ ๋ ธ์ถํ์.
7. Creating a service
์๋น์ค ์์ฑ ์ https traffic๋ฅผ ๋ค๋ฃจ๋ ์์ ํ pod๋ฅผ ์์ฑํ๋ค.

kubectl create secret generic tls-certs --from-file tls/
kubectl create configmap nginx-proxy-conf --from-file nginx/proxy.conf
kubectl create -f pods/secure-monolith.yaml
secure-monolith pods ์ configuration data๋ฅผ ์์ฑํ๋ค.
cat services/monolith.yaml

monolith service configuration file
๋ผ๋ฒจ app: monolith ์ secure: enbaled ๊ฐ ์๋ pod๋ค์ ์๋์ผ๋ก ์ฐพ๊ณ expose ํ๋ selector๊ฐ ์๋ค.
์ฌ๊ธฐ์ nodeport๋ฅผ exposeํด์ 31000๋ฒ ํฌํธ์์ ngins(port 443)์ผ๋ก ์ธ๋ถ ํธ๋ํฝ์ ์ ์กํด์ผํ๋ค.
kubectl create -f services/monolith.yaml
์ ๋ช ๋ น์ด๋ฅผ ์ฌ์ฉํด monolith service configuration file๋ก๋ถํฐ monolith service๋ฅผ ์์ฑํ๋ค.
์ด์ ์๋น์ค๋ฅผ expose ํ๊ธฐ ์ํด 31000๋ฒ ํฌํธ๋ฅผ ์ฌ์ฉํ๊ณ ์๋ค.
๋ค๋ฅธ ์ฑ์ด ํด๋น ํฌํธ์ ๋ฐ์ธ๋ฉํ๋ ค๊ณ ํ๋ฉด ์ถฉ๋์ด ์๊ธด๋ค.
์ผ๋ฐ์ ์ผ๋ก ์ฟ ๋ฒ๋คํฐ์ค๋ ์ด๋ฌํ ํฌํธ assignment๋ฅผ ์กฐ์ ํ๋ค.
gcloud compute firewall-rules create allow-monolith-nodeport \\
--allow=tcp:31000
์ ๋ช ๋ น์ด๋ฅผ ์ด์ฉํด exposed node port์ monolith service๋ก์ ํธ๋ํฝ์ ํ์ฉํ๋ค.
์ด์ ํด๋ฌ์คํฐ ์ธ๋ถ์์ port forwarding ์์ด๋ secure-monolith service์ hit ํ ์ ์๋ค.
gcloud compute instances list

์ ๋ช ๋ น์ด๋ฅผ ํตํด ๋ ธ๋ ์ค ํ๋์ ์ธ๋ถ IP ์ฃผ์๋ฅผ ํ์ธํ๋ค.
curl -k https://<EXTERNAL_IP>:31000
์ด๋ฌ๋ฉด ๋ฌธ์ ๊ฐ ๋ฐ์ํ๋ค. ์ด์ ํด๊ฒฐํด๋ณด์
8. Adding labels to pods
์ง๊ธ monolith service๋ endpoint๋ฅผ ๊ฐ์ง๊ณ ์์ง ์๋ค.
kubectl get pods -l "app=monolith"

monolith label์ ๊ฐ์ง๊ณ ์๋ ์คํ์ค์ธ pods๋ค์ ํ์ธํ ์ ์๋ค.
kubectl get pods -l "app=monolith,secure=enabled"
๊ตฌ์ฒด์ ์ธ ๋ผ๋ฒจ๋ค์ ํ์ธํ๋ฉด?

์๋ฌด๋ฐ ๋ฆฌ์์ค๋ ์๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
๊ทธ๋ ๊ธฐ ๋๋ฌธ์ ์์์ ์ค๋ฅ๊ฐ ๋ฌ๋ ๊ฒ์ด๊ณ , secure=enabled ๋ผ๋ฒจ์ ์ถ๊ฐํด์ฃผ์ด์ผํ๋ค.
kubectl label pods secure-monolith 'secure=enabled'
kubectl get pods secure-monolith --show-labels

๋ผ๋ฒจ์ ๋ถ์ด๊ณ ๋ผ๋ฒจ์ด ์ ๋ฐ์ดํธ ๋์๋์ง ํ์ธํ๋ค.
kubectl describe services monolith | grep Endpoints
์ ์์ ์ผ๋ก ์ ๋ถ์๋ค๋ฉด monolith ์๋น์ค์ ์๋ํฌ์ธํธ ๋ฆฌ์คํธ๋ฅผ ํ์ธํ๋ค.
gcloud compute instances list
curl -k https://<EXTERNAL_IP>:31000

์ ๋์๊ฐ๋ค!
9. Deploying applications with Kubernetes
์ด์ ์ปจํ ์ด๋๋ค์ 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.
10. Creating deployments
ํ์ฌ ์งํํ๋ ์ฑ์ ์ธ๊ฐ์ง ํํธ๋ก ๋๋๋ฉด
auth : ์ธ์ฆ๋ ์ฌ์ฉ์๋ค์๊ฒ JWT token ์์ฑ
hello : ์ธ์ฆ๋ ์ฌ์ฉ์๋ค์๊ฒ ์ธ์ฌ
frontend : auth์ hello ์๋น์ค๋ค์ ํธ๋ํฝ์ ๋ผ์ฐํ ํจ.
auth์ hello deployments๋ฅผ ์ํ ๋ด๋ถ์ ์ธ ์๋น์ค๋ค์ ์ ์ํ๊ณ frontend deployment๋ฅผ ์ํ ์ธ๋ถ ์๋น์ค๋ฅผ ์ ์ํ๋ค.
cat deployments/auth.yaml
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.
kubectl create -f deployments/auth.yaml
์ ๋ช ๋ น์ด๋ก deployment object๋ฅผ ์์ฑํ๋ค.
kubectl create -f services/auth.yaml
auth deployment๋ฅผ ์ํ ์๋น์ค๋ฅผ ์์ฑํ๋ค.
kubectl create -f deployments/hello.yaml
kubectl create -f services/hello.yaml
kubectl create configmap nginx-frontend-conf --from-file=nginx/frontend.conf
kubectl create -f deployments/frontend.yaml
kubectl create -f services/frontend.yaml
๋์ผํ๊ฒ hello deployment์ frontend Deployment์๋ ์ ์ฉํ๋ค.
kubectl get services frontend
curl -k https://<EXTERNAL-IP>
frontend์ ์ธ๋ถ IP ์ฃผ์๋ฅผ ํตํด curlingํด์ interact ํ๋ค.

Study More
Pod๊ฐ ๋ฌด์์ธ์ง?
LoadBalancer
Scaling ์ด ๋ฌด์์ด๋์
LoadBalancer ์๋น์ค ํ์ ์ ๋จ์ง ํ๊ฐ์ ๋ด๋ถ ์๋น์ค๋ฅผ ์ธ๋ถ ์ฌ์ฉ์๋ค์๊ฒ ์ ๊ทผ ๊ฐ๋ฅํ๋๋ก ๋ง๋๋ ์ผ์ ๋ด๋นํฉ๋๋ค. ๋ฐ๋๋ก Ingress ์๋น์ค ํ์ ์ ์ฌ๋ฌ๊ฐ์ ์๋น์ค๊ฐ ํ๊ฐ ๋ก๋ ๋ฐธ๋ฐ์๋ฅผ ํตํด ์ ์ฐํ ์ค์ ์ ํ ์ ์๊ฒ ๋ง๋ญ๋๋ค.
Kubernetes NodePort vs LoadBalancer vs Ingress? When should I use what?
Load Balancer๋ ํ๊ฐ์ ๋ด๋ถ ์๋น์ค๋ฅผ ์ธ๋ถ๋ก ์ ๊ทผ ๊ฐ๋ฅํ๊ฑฐ๊ณ
Ingress๋ load balancer๋ฅผ ๋์ฒดํ๋ฉด์ ๋ค์ด๊ฐ. L7 ๊ณ์ธต์์ ๋ผ์ฐํ ์ํด์ฃผ๋๊ฒ. ๊ทธ๋์ url๊น์ง ๋ผ์ฐํ ํด์ค.
L4 ๋ ๋จ์ํ ๋ถํ๋ฅผ ๋ถ์ฐ์ํค๋๊ฒ.
Last updated