๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๊ฐœ๋ฐœ

[GCP] Kubernetes๋ฅผ ํ†ตํ•œ ํด๋ผ์šฐ๋“œ ์กฐ์ •

by - ์˜คํŠธ - 2023. 4. 22.

4์›” 22์ผ ์™„๋ฃŒ /

 

๋ชฉํ‘œ

  • Kubernetes Engine์„ ์‚ฌ์šฉํ•˜์—ฌ ์™„์ „ํ•œ Kubernetes ํด๋Ÿฌ์Šคํ„ฐ๋ฅผ ํ”„๋กœ๋น„์ €๋‹ํ•ฉ๋‹ˆ๋‹ค.
  • kubectl์„ ์‚ฌ์šฉํ•˜์—ฌ Docker ์ปจํ…Œ์ด๋„ˆ๋ฅผ ๋ฐฐํฌํ•˜๊ณ  ๊ด€๋ฆฌํ•ฉ๋‹ˆ๋‹ค.
  • Kubernetes์˜ ๋””ํ”Œ๋กœ์ด๋จผํŠธ ๋ฐ ์„œ๋น„์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค๋กœ ๋ถ„ํ• ํ•ฉ๋‹ˆ๋‹ค.

 

์ง„ํ–‰

gcloud config set compute/zone us-central1-b : ์˜์—ญ ์„ค์ •
gcloud container clusters create io : ํด๋Ÿฌ์Šคํ„ฐ ์‹œ์ž‘

 

> ์ƒ˜ํ”Œ ์ฝ”๋“œ ๊ฐ€์ ธ์˜ค๊ธฐ

gsutil cp -r gs://spls/gsp021/* . : GitHub ์ €์žฅ์†Œ ํด๋ก 

cd orchestrate-with-kubernetes/kubernetes : ๋””๋ ‰ํ† ๋ฆฌ ๋ณ€๊ฒฝ
ls : ์ž‘์—… ์ค‘์ธ ํŒŒ์ผ ํ™•์ธ

 

๊ฐ„๋žตํ•œ Kubernetes ๋ฐ๋ชจ

kubectl create deployment nginx --image=nginx:1.10.0 : nginx ์ปจํ…Œ์ด๋„ˆ์˜ ๋‹จ์ผ ์ธ์Šคํ„ด์Šค ์‹คํ–‰
kubectl get pods : ์‹คํ–‰ ์ค‘์ธ nginx ์ปจํ…Œ์ด๋„ˆ ํ™•์ธ
kubectl expose deployment nginx --port 80 --type LoadBalancer : Kubernetes ์™ธ๋ถ€๋กœ ๋…ธ์ถœ
-> Kubernetes๊ฐ€ ๋ฐฑ๊ทธ๋ผ์šด๋“œ์—์„œ ๊ณต๊ฐœ IP ์ฃผ์†Œ๊ฐ€ ์ฒจ๋ถ€๋œ ์™ธ๋ถ€ ๋ถ€ํ•˜ ๋ถ„์‚ฐ๊ธฐ ์ƒ์„ฑ
kubectl get services : ์„œ๋น„์Šค ๋‚˜์—ด

curl http://<External IP>:80 : ์›๊ฒฉ์ ์œผ๋กœ Nginx ์ปจํ…Œ์ด๋„ˆ ์กฐํšŒ

> ํฌ๋“œ

ํฌ๋“œ ๋งŒ๋“ค๊ธฐ

cat pods/monolith.yaml

kubectl create -f pods/monolith.yaml
kubectl get pods
kubectl describe pods monolith

 

ํฌ๋“œ์™€ ์ƒํ˜ธ์ž‘์šฉํ•˜๊ธฐ

๋‘ ๋ฒˆ์งธ ํ„ฐ๋ฏธ๋„
kubectl port-forward monolith 10080:80

์ฒซ ๋ฒˆ์งธ ํ„ฐ๋ฏธ๋„
curl http://127.0.0.1:10080
curl http://127.0.0.1:10080/secure
curl -u user http://127.0.0.1:10080/login
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
kubectl logs monolith

์„ธ ๋ฒˆ์งธ ํ„ฐ๋ฏธ๋„
kubectl logs -f monolith

์ฒซ ๋ฒˆ์งธ ํ„ฐ๋ฏธ๋„
curl http://127.0.0.1:10080
-> ์„ธ ๋ฒˆ์งธ ํ„ฐ๋ฏธ๋„์—์„œ ๋กœ๊ทธ๊ฐ€ ์—…๋ฐ์ดํŠธ

kubectl exec monolith --stdin --tty -c monolith /bin/sh
ping -c 3 google.com
exit

 


> ์„œ๋น„์Šค

์„œ๋น„์Šค ๋งŒ๋“ค๊ธฐ

cd ~/orchestrate-with-kubernetes/kubernetes
cat pods/secure-monolith.yaml

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

cat services/monolith.yaml

kubectl create -f services/monolith.yaml

gcloud compute firewall-rules create allow-monolith-nodeport \
  --allow=tcp:31000

gcloud compute instances list

curl -k https://<EXTERNAL_IP>:31000
-> ์‹œ๊ฐ„์ด ์ดˆ๊ณผ๋œ ์ด์œ ?

 

ํฌ๋“œ์— ๋ผ๋ฒจ ์ถ”๊ฐ€ํ•˜๊ธฐ

kubectl get pods -l "app=monolith"
kubectl get pods -l "app=monolith,secure=enabled"

kubectl label pods secure-monolith 'secure=enabled'
kubectl get pods secure-monolith --show-labels

kubectl describe services monolith | grep Endpoints

gcloud compute instances list
curl -k https://<EXTERNAL_IP>:31000

 


> Kubernetes๋กœ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋ฐฐํฌํ•˜๊ธฐ

> ๋””ํด๋กœ์ด๋จผํŠธ ๋งŒ๋“ค๊ธฐ

cat deployments/auth.yaml
kubectl create -f deployments/auth.yaml
kubectl create -f services/auth.yaml

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

kubectl get services frontend

curl -k https://<EXTERNAL-IP>

Kubernetes๋ฅผ ํ†ตํ•œ ํด๋ผ์šฐ๋“œ ์กฐ์ • ์™„๋ฃŒ!