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 μ»€λ§¨λ“œλ₯Ό μ‚¬μš©ν•˜λŠ” 것이닀.

μœ„ λͺ…λ Ήμ–΄ μ‚¬μš©ν•˜μ—¬ μΏ λ²„λ„€ν‹°μŠ€ 배포λ₯Ό μƒμ„±ν•œλ‹€.

싀행쀑인 λ…Έλ“œμ— μž₯μ• κ°€ λ°œμƒν•˜λ”λΌλ„ λ°°ν¬λŠ” 계속 μ‹€ν–‰λœλ‹€.

μΏ λ²„λ„€ν‹°μŠ€μ—μ„œλŠ” λͺ¨λ“  μ»¨ν…Œμ΄λ„ˆλ“€μ΄ podμ—μ„œ μ‹€ν–‰λœλ‹€.

싀행쀑인 nginx μ»¨ν…Œμ΄λ„ˆλ₯Ό ν™•μΈν•œλ‹€.

nginx μ»¨ν…Œμ΄λ„ˆκ°€ 싀행쀑이면 μœ„ λͺ…λ Ήμ–΄λ₯Ό 톡해 μΏ λ²„λ„€ν‹°μŠ€ 외뢀에 expose ν•  수 μžˆλ‹€.

μΏ λ²„λ„€ν‹°μŠ€κ°€ 곡용 IP μ£Όμ†Œλ₯Ό 톡해 μ™ΈλΆ€ λ‘œλ“œλ°ΈλŸ°μ„œλ₯Ό μƒμ„±ν–ˆλ‹€.

ν•΄λ‹Ή 곡용 IP μ£Όμ†Œλ₯Ό μ‘°νšŒν•˜λŠ” λͺ¨λ“  ν΄λΌμ΄μ–ΈνŠΈλŠ” ν•΄λ‹Ή pod둜 λΌμš°νŒ…λœλ‹€.

μœ„ κ²½μš°μ—λŠ” nginx pod둜 λΌμš°νŒ…λœλ‹€.

ν˜„μž¬ μ‚¬μš©μ€‘μΈ μ„œλΉ„μŠ€ 리슀트λ₯Ό μ‘°νšŒν•œλ‹€.

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을 톡해 μƒμ„±λœλ‹€.

  • ν˜„μž¬ podλŠ” ν•˜λ‚˜μ˜ μ»¨ν…Œμ΄λ„ˆ (monolith)둜 μ΄λ£¨μ–΄μ Έμžˆλ‹€.

  • μ»¨ν…Œμ΄λ„ˆκ°€ μ‹œμž‘ν•  λ•Œ λͺ‡κ°€μ§€ argument듀을 μ „λ‹¬ν•œλ‹€.

  • http traffic 을 μœ„ν•΄ 80번 포트λ₯Ό μ—°λ‹€.

μœ„ λͺ…λ Ήμ–΄λ₯Ό μ‚¬μš©ν•΄ monolith podλ₯Ό μƒμ„±ν•œλ‹€.

default namespaceμ—μ„œ 싀행쀑인 λͺ¨λ“  pod 리슀트λ₯Ό ν™•μΈν•œλ‹€.

monolith pod 에 λŒ€ν•œ 정보듀 좜λ ₯

  • Pod IP μ£Όμ†Œμ™€ event logλ₯Ό ν¬ν•¨ν•œ 정보듀을 확인 κ°€λŠ₯ν•˜λ‹€.

    • troubleshooting μ‹œ μš©μ΄ν•˜κ²Œ μ‚¬μš©κ°€λŠ₯

5. Interacting with pods

기본적으둜 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λŠ” μ΅œμ†Œ λ§ˆμŠ€ν„° μ„œλΉ„μŠ€λ₯Ό κ°€μ§€κ³ μžˆμŒ.

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λ₯Ό μƒμ„±ν•œλ‹€.

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 μ£Όμ†Œλ₯Ό ν™•μΈν•œλ‹€.

이러면 λ¬Έμ œκ°€ λ°œμƒν•œλ‹€. 이제 ν•΄κ²°ν•΄λ³΄μž

8. Adding labels to pods

μ§€κΈˆ monolith serviceλŠ” endpointλ₯Ό κ°€μ§€κ³  μžˆμ§€ μ•Šλ‹€.

monolith label을 κ°€μ§€κ³  μžˆλŠ” 싀행쀑인 pods듀을 확인할 수 μžˆλ‹€.

ꡬ체적인 라벨듀을 ν™•μΈν•˜λ©΄?

μ•„λ¬΄λŸ° λ¦¬μ†ŒμŠ€λ„ μ—†λŠ” 것을 확인할 수 μžˆλ‹€.

κ·Έλ ‡κΈ° λ•Œλ¬Έμ— μœ„μ—μ„œ 였λ₯˜κ°€ λ‚¬λ˜ 것이고, secure=enabled 라벨을 μΆ”κ°€ν•΄μ£Όμ–΄μ•Όν•œλ‹€.

라벨을 뢙이고 라벨이 μ—…λ°μ΄νŠΈ λ˜μ—ˆλŠ”μ§€ ν™•μΈν•œλ‹€.

μ •μƒμ μœΌλ‘œ 잘 λΆ™μ—ˆλ‹€λ©΄ monolith μ„œλΉ„μŠ€μ˜ μ—”λ“œν¬μΈνŠΈ 리슀트λ₯Ό ν™•μΈν•œλ‹€.

잘 λŒμ•„κ°„λ‹€!

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λ₯Ό μœ„ν•œ μ™ΈλΆ€ μ„œλΉ„μŠ€λ₯Ό μ •μ˜ν•œλ‹€.

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 ν•œλ‹€.

Study More

  • Podκ°€ 무엇인지?

  • LoadBalancer

  • Scaling 이 무엇이냐악

LoadBalancer μ„œλΉ„μŠ€ νƒ€μž…μ€ 단지 ν•œκ°œμ˜ λ‚΄λΆ€ μ„œλΉ„μŠ€λ₯Ό μ™ΈλΆ€ μ‚¬μš©μžλ“€μ—κ²Œ μ ‘κ·Ό κ°€λŠ₯ν•˜λ„λ‘ λ§Œλ“œλŠ” 일을 λ‹΄λ‹Ήν•©λ‹ˆλ‹€. λ°˜λŒ€λ‘œ Ingress μ„œλΉ„μŠ€ νƒ€μž…μ€ μ—¬λŸ¬κ°œμ˜ μ„œλΉ„μŠ€κ°€ ν•œκ°œ λ‘œλ“œ λ°ΈλŸ°μ„œλ₯Ό 톡해 μœ μ—°ν•œ 섀정을 ν•  수 있게 λ§Œλ“­λ‹ˆλ‹€.

Kubernetes NodePort vs LoadBalancer vs Ingress? When should I use what?

Kubernetes NodePort vs LoadBalancer vs Ingress? When should I use what?Recently, someone asked me what the difference between NodePorts, LoadBalancers, and Ingress were. They are all different ways to get…medium.com

Load BalancerλŠ” ν•œκ°œμ˜ λ‚΄λΆ€ μ„œλΉ„μŠ€λ₯Ό μ™ΈλΆ€λ‘œ μ ‘κ·Ό κ°€λŠ₯ν•œκ±°κ³ 

IngressλŠ” load balancerλ₯Ό λŒ€μ²΄ν•˜λ©΄μ„œ 듀어감. L7 κ³„μΈ΅μ—μ„œ λΌμš°νŒ…μ„ν•΄μ£ΌλŠ”κ²ƒ. κ·Έλž˜μ„œ urlκΉŒμ§€ λΌμš°νŒ…ν•΄μ€Œ.

L4 λŠ” λ‹¨μˆœνžˆ λΆ€ν•˜λ₯Ό λΆ„μ‚°μ‹œν‚€λŠ”κ²ƒ.

Last updated