๐ŸŽ† ์ฟ ๋ฒ„๋„คํ‹ฐ์Šค Services [๊ฐœ๋… & ๊ฐ„๋‹จ ์‹คํ–‰]

๊น€์„ฑ์ธยท2023๋…„ 10์›” 13์ผ
0

[DevOps] ๐ŸณDocker & Kubernetes

๋ชฉ๋ก ๋ณด๊ธฐ
26/62

ํฌ๋“œ ๋ฌธ์ œ์ 

ํฌ๋“œ๋Š” ์ปจํŠธ๋กค๋Ÿฌ์—์˜ํ•ด ๊ด€๋ฆฌ๋˜๋ฏ€๋กœ ํ•œ๊ตฐ๋ฐ์— ๊ณ ์ •๋˜์–ด ์žˆ์ง€์•Š๊ณ , ํด๋Ÿฌ์Šคํ„ฐ ๋‚ด๋ถ€๋ฅผ ์˜ฎ๊ฒจ๋‹ค๋‹˜.

  • ํ•ด๋‹น ๊ณผ์ •์—์„œ ๋…ธ๋“œ๊ฐ€ ์˜ฎ๊ธฐ๋ฉด์„œ ์‹คํ–‰๋˜๊ณ , ํด๋Ÿฌ์Šคํ„ฐ ์•ˆ ํŒŒ๋“œ์˜ IP๊ฐ€ ๋ณ€๊ฒฝ๋จ
  • ํฌ๋“œ๊ฐ€ ์ง€์†์ ์œผ๋กœ ์ƒ์„ฑ๋ ๋•Œ ์„œ๋น„์Šค๋ฅผ ํ•˜๊ธฐ์— ์ ํ•ฉํ•˜์ง€ ์•Š์Œ.
  • ์„œ๋น„์Šค๋ฅผ ์ด์šฉํ•˜์—ฌ ํŒŒ๋“œ๊ฐ€ ํด๋Ÿฌ์Šคํ„ฐ ์•ˆ ์–ด๋””์— ์žˆ๋“  ๊ณ ์ •์ฃผ์†Œ๋ฅผ ์ด์šฉํ•˜์—ฌ ์ ‘๊ทผ ๊ฐ€๋Šฅํ•จ.!

์„œ๋น„์Šค ํƒ€์ž…

  • Cluser IP
    • ์ฟ ๋ฒ„๋„คํ‹ฐ์Šค์˜ ๊ธฐ๋ณธ ์„œ๋น„์Šคํƒ€์ž…, ํด๋Ÿฌ์Šคํ„ฐ ์•ˆ์—์„œ๋งŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•จ.
    • ํด๋Ÿฌ์Šคํ„ฐ ์•ˆ์˜ ๋‹ค๋ฅธ ํŒŒ๋“œ๋‚˜ ๋…ธ๋“œ์—์„œ ํด๋Ÿฌ์Šคํ„ฐ IP๋ฅผ ํ†ตํ•ด ์„œ๋น„์Šค์— ์—ฐ๊ฒฐ๋œ ํŒŒ๋“œ์— ์ ‘๊ทผํ•จ.
  • NodePort
    • ์„œ๋น„์Šค ํ•˜๋‚˜์— ๋ชจ๋“  ๋…ธ๋“œ์˜ ์ง€์ •๋œ ํฌํŠธ๋ฅผ ํ• ๋‹น
    • ์„œ๋น„์Šค์— ์ง€์ •๋œ ํฌํŠธ๋ฒˆํ˜ธ๋งŒ ์‚ฌ์šฉํ•˜์—ฌ ํŒŒ๋“œ์— ์ ‘๊ทผ ๊ฐ€๋Šฅํ•จ.
    • ํด๋Ÿฌ์Šคํ„ฐ ์•ˆ ๋ฐ–์—์„œ ๋ชจ๋‘ ์ ‘๊ทผ ๊ฐ€๋Šฅํ•จ.
    • ๋…ธ๋“œ์˜ ์ž์ฒด ํฌํŠธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํฌ๋“œ๋กœ ๋ฆฌ๋‹ค์ด๋ ‰์…˜
  • LoadBalancer
    • ํ•˜๋‚˜์˜ IP ์ฃผ์†Œ๋ฅผ ํ†ตํ•ด ์—ฌ๋Ÿฌ ์„œ๋น„์Šค๋ฅผ ์ œ๊ณตํ•˜๋Š” ํŠน๋ณ„ํ•œ ๋ฉ”์ปค๋‹ˆ์ฆ˜
  • ExternalName
    • ์„œ๋น„์Šค๋ฅผ .spec.externalName ํ•„๋“œ์— ์„ค์ •ํ•œ ๊ฐ’๊ณผ ์—ฐ๊ฒฐํ•จ.
    • ํด๋Ÿฌ์Šคํ„ฐ ์•ˆ์—์„œ ์™ธ๋ถ€๋กœ ์—ฐ๊ฒฐํ• ๋•Œ ์‚ฌ์šฉ
    • selector ํ•„๋“œ๊ฐ€ ์—†์Œ.

์„œ๋น„์Šค

https://kubernetes.io/ko/docs/concepts/services-networking/service/

์„œ๋น„์Šค ์ƒ์„ฑ

apiVersion: v1
kind: Pod
metadata:
  name: nginx
  labels:
    app.kubernetes.io/name: proxy
spec:
  containers:
  - name: nginx
    image: nginx:stable
    ports:
      - containerPort: 80
        name: http-web-svc
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  sessionAffinity: ClientIP
  selector:
    app.kubernetes.io/name: proxy
  ports:
  - name: name-of-service-port
    protocol: TCP
    port: 80
    targetPort: http-web-svc
  • port: ์„œ๋น„์Šค์˜ ์ธ๋ฐ”์šด๋“œ ํฌํŠธ
  • targetPort: ๋‚ด๋ถ€์—์„œ ์—ฐ๊ฒฐํ•ด์ค„ Pod์˜ ํฌํŠธ
  • sessionAffinity: ํด๋ผ์ด์–ธํŠธ IP ์š”์ฒญ์— ๋Œ€ํ•ด ์—ฌ๋Ÿฌ ํŒŒ๋“œ๊ฐ€ ์•„๋‹Œ ํ•˜๋‚˜์˜ ํŒŒ๋“œ์™€ ์„ธ์…˜ ์œ ์ง€
spec:
  ports:
  - name: http
    port: 80
    targetPort: 8080
  - name: https
    port: 443
    targetPort: 8443
  • ports: ์—ฌ๋Ÿฌ๊ฐœ ํฌํŠธ ํ• ๋‹น ๊ฐ€๋Šฅ
  • port's'์™€ ๊ฐ™์ด ๋’ค์— ๋ณต์ˆ˜ํ˜•์ด ๋ถ™์œผ๋ฉด ์—ฌ๋Ÿฌ๊ฐœ์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Œ
    • '-'๋ฅผ ํ†ตํ•ด์„œ ๋ฆฌ์ŠคํŠธ ๊ตฌ๋ณ„

kubectl exec <ํฌ๋“œ์ด๋ฆ„> --curl <ip์ฃผ์†Œ>:<ํฌํŠธ๋ฒˆํ˜ธ> -s : ์„œ๋น„์Šค ๊ธฐ๋Šฅ ๋ฏธ๋ฆฌ ํ™•์ธ (์•„์ง ์™ธ๋ถ€ IP๊ฐ€ ํ• ๋‹น ๋˜์ง€ ์•Š์•˜์„๋–„)


https://kubernetes.io/ko/docs/reference/networking/virtual-ips/#%EC%84%B8%EC%85%98-%EC%96%B4%ED%94%BC%EB%8B%88%ED%8B%B0


GKE ์—์„œ ์‹คํ–‰

kubectl create deploy --image=gasbugs/http-go http-go --port=8080 --dry-run

kubectl create deploy --image=gasbugs/http-go http-go --port=8080 --dry-run > http-go-deploy.yaml

yamlํŒŒ์ผ ์ƒ๋‹จ์— service ๊ตฌํ˜„๋ถ€ ์ถ”๊ฐ€

apiVersion: v1
kind: Service
metadata:
  name: http-go-svc
spec:
  selector:
    run: http-go
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080
  
---
kubectl create -f http-go-deploy.yaml
kubectl get all
kubectl get pod -o wide # IPํ™•์ธํ• ๊ฒƒ
kubectl describe svc  #Endpoints ํ™•์ธํ• ๊ฒƒ
kubectl scale deploy http-go --replicas=5

์—”๋“œํฌ์ธํŠธ

https://kubernetes.io/ko/docs/concepts/services-networking/service/#%EC%97%94%EB%93%9C%ED%8F%AC%EC%9D%B8%ED%8A%B8

์ฟ ๋ฒ„๋„คํ‹ฐ์Šค API์—์„œ, ์—”๋“œํฌ์ธํŠธ(Endpoints)(๋ฆฌ์†Œ์Šค ๋ช…์นญ์ด ๋ณต์ˆ˜ํ˜•์ž„)๋Š” ๋„คํŠธ์›Œํฌ ์—”๋“œํฌ์ธํŠธ์˜ ๋ชฉ๋ก์„ ์ •์˜ํ•˜๋ฉฐ, ์ผ๋ฐ˜์ ์œผ๋กœ ํŠธ๋ž˜ํ”ฝ์ด ์–ด๋–ค ํŒŒ๋“œ์— ๋ณด๋‚ด์งˆ ์ˆ˜ ์žˆ๋Š”์ง€๋ฅผ ์ •์˜ํ•˜๊ธฐ ์œ„ํ•ด ์„œ๋น„์Šค๊ฐ€ ์ฐธ์กฐํ•œ๋‹ค.

์—”๋“œํฌ์ธํŠธ ๋Œ€์‹  ์—”๋“œํฌ์ธํŠธ์Šฌ๋ผ์ด์Šค API๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•œ๋‹ค.

  • ์™ธ๋ถ€ IP๋ฅผ ๋ ˆ์ด๋ธ”์„ ์‚ฌ์šฉํ•ด ์ง€์ •ํ•˜์ง€ ์•Š๊ณ  ์™ธ๋ถ€ IP๋ฅผ endpoint ์ž์›์—์„œ ์„ค์ •

์…€๋ ‰ํ„ฐ๊ฐ€ ์—†๋Š” ์„œ๋น„์Šค

https://kubernetes.io/ko/docs/concepts/services-networking/service/#%EC%85%80%EB%A0%89%ED%84%B0%EA%B0%80-%EC%97%86%EB%8A%94-%EC%84%9C%EB%B9%84%EC%8A%A4

์„œ๋น„์Šค๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ์…€๋ ‰ํ„ฐ๋ฅผ ์ด์šฉํ•˜์—ฌ ์ฟ ๋ฒ„๋„คํ‹ฐ์Šค ํŒŒ๋“œ์— ๋Œ€ํ•œ ์ ‘๊ทผ์„ ์ถ”์ƒํ™”ํ•˜์ง€๋งŒ, ์…€๋ ‰ํ„ฐ ๋Œ€์‹  ๋งค์นญ๋˜๋Š”(corresponding) ์—”๋“œํฌ์ธํŠธ์Šฌ๋ผ์ด์Šค ์˜ค๋ธŒ์ ํŠธ์™€ ํ•จ๊ป˜ ์‚ฌ์šฉ๋˜๋ฉด ๋‹ค๋ฅธ ์ข…๋ฅ˜์˜ ๋ฐฑ์—”๋“œ๋„ ์ถ”์ƒํ™”ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์—ฌ๊ธฐ์—๋Š” ํด๋Ÿฌ์Šคํ„ฐ ์™ธ๋ถ€์—์„œ ์‹คํ–‰๋˜๋Š” ๊ฒƒ๋„ ํฌํ•จ๋œ๋‹ค.

  • ํ”„๋กœ๋•์…˜ ํ™˜๊ฒฝ์—์„œ๋Š” ์™ธ๋ถ€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ํด๋Ÿฌ์Šคํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๊ณ  ํ•˜์ง€๋งŒ, ํ…Œ์ŠคํŠธ ํ™˜๊ฒฝ์—์„œ๋Š” ์ž์ฒด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
  • ํ•œ ์„œ๋น„์Šค์—์„œ ๋‹ค๋ฅธ ๋„ค์ž„์ŠคํŽ˜์ด์Šค ๋˜๋Š” ๋‹ค๋ฅธ ํด๋Ÿฌ์Šคํ„ฐ์˜ ์„œ๋น„์Šค๋ฅผ ์ง€์ •ํ•˜๋ ค๊ณ  ํ•œ๋‹ค.
  • ์›Œํฌ๋กœ๋“œ๋ฅผ ์ฟ ๋ฒ„๋„คํ‹ฐ์Šค๋กœ ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ํ•˜๊ณ  ์žˆ๋‹ค. ํ•ด๋‹น ๋ฐฉ์‹์„ ํ‰๊ฐ€ํ•˜๋Š” ๋™์•ˆ, ์ฟ ๋ฒ„๋„คํ‹ฐ์Šค์—์„œ๋Š” ๋ฐฑ์—”๋“œ์˜ ์ผ๋ถ€๋งŒ ์‹คํ–‰ํ•œ๋‹ค.

์ด๋Ÿฌํ•œ ์‹œ๋‚˜๋ฆฌ์˜ค์—์„œ๋Š” ํŒŒ๋“œ ์…€๋ ‰ํ„ฐ ์—†์ด ์„œ๋น„์Šค๋ฅผ ์ •์˜ ํ•  ์ˆ˜ ์žˆ๋‹ค.

์™ธ๋ถ€ IP ์—ฐ๊ฒฐ

apiVersion: v1
kind: Service
metadata:
  name: external-service
spec:
  ports:
    - port: 80
apiVersion: v1
kind: Endpoints
metadata:
  name: external-service
subsets:
  - addresses:
    - ip: 11.11.11.11
    - ip: 22.22.22.22
    ports:
      - port: 80

์™ธ๋ถ€๋กœ ๋กœ๋“œ๋ฐธ๋Ÿฐ์‹ฑํ•ด์„œ ํ†ต์‹ ํ• ๋•Œ ์‚ฌ์šฉ

0๊ฐœ์˜ ๋Œ“๊ธ€