Skip to content

k8s-study/guide

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

9 Commits
ย 
ย 
ย 
ย 

Repository files navigation

microservices x kubernetes study

๋‹จ์ˆœํ•œ (ํ•˜์ง€๋งŒ ํ˜„์‹ค์ ์ธ) ์„œ๋น„์Šค๋ฅผ ๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค ํ˜•ํƒœ๋กœ ๋งŒ๋“ค์–ด ๊ธฐ๋ณธ ์ฒ ํ•™์„ ์ดํ•ดํ•˜๊ณ  ์ฟ ๋ฒ„๋„คํ‹ฐ์Šค๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฐฐํฌ, ๋ชจ๋‹ˆํ„ฐ๋ง, ํ™•์žฅํ•˜๋ฉด์„œ ์‹ค์ œ์ ์ธ ์‚ฌ์šฉ๋ฒ•์„ ์ตํžˆ๋Š”๊ฒŒ ๋ชฉ์ ์ž…๋‹ˆ๋‹ค.

๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค๋ฅผ ์ œ๋Œ€๋กœ ๊ตฌ์ถ•ํ•œ๋‹ค๋Š” ๊ฒƒ์€ ๊ต‰์žฅํžˆ ์–ด๋ ต๊ธฐ ๋•Œ๋ฌธ์— ์—ฌ๊ธฐ์„œ๋Š” ๋‹จ์ˆœํ•˜๊ฒŒ API Gateway + REST API ํŒจํ„ด์„ ์‚ฌ์šฉํ•˜๊ณ  ์™œ ์•ˆํ‹ฐํŒจํ„ด์ธ์ง€ ๋А๊ปด๋ณด๋Š” ์ •๋„๋กœ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

pongpong

pongpong(๊ฐ€์นญ)์€ https://apex.sh/ping/ ํด๋ก  ํ”„๋กœ์ ํŠธ ์ž…๋‹ˆ๋‹ค. ์ฒดํฌ ํ•˜๊ณ  ์‹ถ์€ ์›น ์„œ๋น„์Šค ์ฃผ์†Œ๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ์ฃผ๊ธฐ์ ์œผ๋กœ ์ฒดํฌํ•˜๊ณ  ๊ฒฐ๊ณผ๋ฅผ ์•Œ๋ ค์ค๋‹ˆ๋‹ค.

pong-microservices-design

Frontend

  • service
    • type - Ingress
    • host - pongpong.io, www.pongpong.io
    • path - /
    • serviceName - frontend
    • servicePort - 80

User๊ฐ€ browser๋กœ ์ ‘์†ํ•ด์„œ ์„œ๋น„์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋Š” Webapp

https://github.com/k8s-study/frontend @subicura

Pong CLI

ํ„ฐ๋ฏธ๋„์—์„œ ์‹คํ–‰๊ฐ€๋Šฅํ•œ ํด๋ผ์ด์–ธํŠธ

https://github.com/k8s-study/pong-cli @anarcher

API Gateway

  • service
    • type - Ingress
    • host - api.pongpong.io
    • path - /
    • serviceName - kong-proxy
    • servicePort - 80
  • service
    • type - ClusterIP
    • path - /
    • serviceName - kong-ingress-controller
    • servicePort - 8001

Kong ์‚ฌ์šฉ. ๊ฐ„๋žตํ•œ ๊ฐœ๋…์€ ๋’ท๋ถ€๋ถ„์—์„œ ๋‹ค์‹œ ์„ค๋ช…

https://github.com/k8s-study/kong @Ashon

Service Mesh

Istio๋ฅผ ์ด์šฉํ•˜์—ฌ ๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค๊ฐ„ ํ†ต์‹ ์„ ๊ด€๋ฆฌ

TCP(DB)๋ฅผ ์ œ์™ธํ•˜๊ณ  httpํ†ต์‹ ์„ ์‚ฌ์šฉํ•˜๋Š” ์„œ๋น„์Šค๋งŒ ์‚ฌ์šฉํ•˜๊ฒŒ ์„ค์ •

User Service

  • service
    • type - ClusterIP
    • serviceName - user-service
    • servicePort - 80
  • kong api
    • uris - /user-service
    • upstream api - http://user-service
  • Public API
    • ์œ ์ € CRUD + ๋กœ๊ทธ์ธ / ๋กœ๊ทธ์•„์›ƒ
  • Private API
    • ์œ ์ € ๋ฆฌ์ŠคํŠธ ์กฐํšŒ / ์œ ์ € ์ƒ์„ธ ์กฐํšŒ

์œ ์ €๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์ •๋ณด๋ฅผ ์ˆ˜์ •ํ•˜๊ณ  ๋กœ๊ทธ์ธ ๋ฐ ๋กœ๊ทธ์•„์›ƒ ์ฒ˜๋ฆฌ

Kong์˜ consumer๋ฅผ ๊ด€๋ฆฌํ•˜๊ณ  Kong์˜ key-auth plugin์„ ์ด์šฉํ•˜์—ฌ key๋ฅผ ์กฐํšŒํ•จ

https://github.com/k8s-study/user-service @outsideris

Endpoint Service

  • service
    • type - ClusterIP
    • serviceName - endpoint-service
    • servicePort - 80
  • kong api
    • uris - /endpoint-service
    • upstream api - http://endpoint-service
  • Public API
    • ์—”๋“œํฌ์ธํŠธ CRUD
  • Private API
    • ์—”๋“œํฌ์ธํŠธ ๋ฆฌ์ŠคํŠธ ์กฐํšŒ / ์—”๋“œํฌ์ธํŠธ ์ƒ์„ธ ์กฐํšŒ

์›น์„œ๋น„์Šค๊ฐ€ ์‚ด์•˜๋Š”์ง€ ์ฒดํฌ ํ•˜๊ธฐ ์œ„ํ•œ ๋ชฉ๋ก์„ ๊ด€๋ฆฌ

ex) http://google.com

https://github.com/k8s-study/endpoint-service @findstar

Notification Service

  • service
    • type - ClusterIP
    • serviceName - notification-service
    • servicePort - 80
  • kong api
    • uris - /notification-service
    • upstream api - http://notification-service
  • Public API
    • ์•Œ๋žŒ ์„ค์ • CRUD
    • ์•Œ๋žŒ ํ˜ธ์ถœ(ํ…Œ์ŠคํŠธ)
  • Private API
    • ์•Œ๋žŒ ๋ฆฌ์ŠคํŠธ ์กฐํšŒ / ์•Œ๋žŒ ์ƒ์„ธ ์กฐํšŒ
    • ์•Œ๋žŒ ํ˜ธ์ถœ

์›น์„œ๋น„์Šค๊ฐ€ ์ฃฝ์—ˆ์„๋•Œ ์•Œ๋žŒ์„ ์–ด๋””๋กœ ์ „์†กํ• ์ง€ ์ •๋ณด๋ฅผ ๊ด€๋ฆฌ

ํ˜„์žฌ ์ด๋ฉ”์ผ, ์Šฌ๋ž™ ์•Œ๋žŒ์„ ์ง€์›ํ•˜๋ฉฐ ๊ด€๋ จ ์ธ์ฆ ์ •๋ณด๋ฅผ ๊ด€๋ฆฌํ•˜๊ณ  ํ•ด๋‹น ์„œ๋น„์Šค๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ์—ญํ• ์„ ํ•จ

https://github.com/k8s-study/notification-service @b6pzeusbc54tvhw5jgpyw8pwz2x6gs

Report Service

  • service
    • type - ClusterIP
    • serviceName - report-service
    • servicePort - 80
  • kong api
    • uris - /report-service
    • upstream api - http://report-service/api
  • Public API
    • ๋ฆฌํฌํŠธ ๋ฆฌ์ŠคํŠธ ์กฐํšŒ / ๋ฆฌํฌํŠธ ์ƒ์„ธ ์กฐํšŒ
  • Private API
    • ๋ฆฌํฌํŠธ ์ƒ์„ฑ

์—”๋“œํฌ์ธํŠธ ์ฒดํฌ๊ฐ€ ํ˜ธ์ถœํ•œ ๊ฒฐ๊ณผ๋ฅผ ์ €์žฅ

๋ฆฌ์ŠคํŠธ ์กฐํšŒ ์ฟผ๋ฆฌ์— ๋‚ ์งœ๋ฅผ ์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ›์Œ

https://github.com/k8s-study/report-service @SsureyMoon

Email Service

  • service
    • type - ClusterIP
    • serviceName - email-service
    • servicePort - 80
  • Internal API
    • ์ด๋ฉ”์ผ ์ „์†ก

๋ฐ›๋Š”์‚ฌ๋žŒ, ์ œ๋ชฉ, ๋‚ด์šฉ์„ ์ธ์ž๋กœ ์ด๋ฉ”์ผ์„ ์ „์†ก

https://github.com/k8s-study/email-service

Slack Service

  • service
    • type - ClusterIP
    • serviceName - slack-service
    • servicePort - 80
  • Internal API
    • ์Šฌ๋ž™ ๋ฉ”์‹œ์ง€ ์ „์†ก

๋„๋ฉ”์ธ, ์ฑ„๋„, ๋‚ด์šฉ, token์„ ์ธ์ž๋กœ ์Šฌ๋ž™ ๋ฉ”์‹œ์ง€ ์ „์†ก

https://github.com/k8s-study/slack-service @asbubam

Endpoint Check

  • service
    • type - ClusterIP
    • serviceName - endpoint-check
    • servicePort - 80
  • Internal API
    • ์ฒดํฌ

์‚ฌ์šฉ์ž ์š”์ฒญ ๋˜๋Š” ์ฃผ๊ธฐ์ ์œผ๋กœ endpoint๊ฐ€ ์ •์ƒ์ธ์ง€ ์ฒดํฌ

https://github.com/k8s-study/endpoint-check @tedpark

Logging

  • service
    • type - Ingress
    • host - admin.pongpong.io
    • path - /kibana
    • serviceName - logging-kibana
    • servicePort - 80
  • + agent services

๋กœ๊น…

https://github.com/k8s-study/logging @posquit0

Monitoring

  • service
    • type - Ingress
    • host - admin.pongpong.io
    • path - /grafana
    • serviceName - monitoring-grafana
    • servicePort - 80
  • + agent services

๋ชจ๋‹ˆํ„ฐ๋ง

https://github.com/k8s-study/monitoring @yoanp

์„œ๋น„์Šค ์ž‘์„ฑ ๊ทœ์น™

๊ธฐ๋ณธ ๊ทœ์น™

์–ด๋–ค ์–ธ์–ด/๋””๋น„๋ฅผ ์‚ฌ์šฉํ•ด๋„ OK

์™ธ๋ถ€์— ๊ณต๊ฐœํ• (Public) API์™€ ๋‚ด๋ถ€ ์„œ๋น„์Šค๋ผ๋ฆฌ๋งŒ ์‚ฌ์šฉํ• (Private) API๋กœ ๋‚˜๋ˆ”

Folder Structure

  • README.md: Instruction & Development guide
  • k8s: kubernetes spec
  • docs: swagger
    • public-spec.yaml (Public API)
    • private-spec.yaml (Private API)

REST API ์„ค๊ณ„

REST API ์„ค๊ณ„๋Š” ๋‹ค์Œ ๊ทœ์น™์„ ์ค€์ˆ˜ํ•ฉ๋‹ˆ๋‹ค.

Path Naming

  • health check - /health ์ ‘์†์‹œ ok(text) ๋ฆฌํ„ด
  • Public API - /public์œผ๋กœ endpoint ์„ค์ •
  • Private API - /private์œผ๋กœ endpoint ์„ค์ •
  • ๊ธฐ๋ณธ์ ์ธ ๋ช…๋ช…๋ฒ•์€ _ underscore ๋ฐฉ์‹ ์‚ฌ์šฉ

Status Code

  • 200: ์„ฑ๊ณต
  • 201: ์ƒ์„ฑ (create)
  • 204: ์ปจํ…์ธ  ์—†์Œ (delete)
  • 400: ์ž˜๋ชป๋œ ์š”์ฒญ
  • 401: login์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ
  • 403: login์€ ๋˜์–ด ์žˆ์œผ๋‚˜ ๊ถŒํ•œ์ด ์—†๋Š” ๊ฒฝ์šฐ
  • 404: not found
  • 500: server error

Response sample

  • ์ƒ์„ธ
{
  "id": 1, 
  "title": "title"
}
  • ํŽ˜์ด์ง•

parameters - page (1,2,3,... default 1), per_page (default 20)

{
  "items": [{
    "id": 1,
    "content": "content 1"
  }],
  "page": {
    "current_page": 1,
    "total_count": 1,
    "per_page": 15
  }
}
  • ํ˜ธ์ถœ๋ฅ˜(CRUD๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ)
{
  "status": "ok"
}
{
  "status": "fail"
}
  • ์—๋Ÿฌ
{
  "message":"์—๋Ÿฌ๋ฉ”์‹œ์ง€์ž…๋‹ˆ๋‹ค.",
  "code": "ERROR"
}
{
  "message":"์ด๋ฉ”์ผ ๋˜๋Š” ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ์ž˜๋ชป๋˜์—ˆ์Šต๋‹ˆ๋‹ค.",
  "code": "INVALID_LOGIN"
}

Kubernetes Spec

Istio์—์„œ ์ž๋™์œผ๋กœ ์ˆ˜์ง‘ํ•˜๋Š” ์ •๋ณด์˜ ๊ฐ’์„ ์„ค์ •ํ•˜๊ธฐ ์œ„ํ•ด ๋‹ค์Œ ๊ทœ์น™์„ ์ง€ํ‚ด

  • Kong Ingress ์„ค์ •
  • ๋‹ค์Œ์„ ์ฐธ๊ณ ํ•˜์—ฌ ๊ฑฐ์˜ ์œ ์‚ฌํ•˜๊ฒŒ ์„ค์ •ํ•จ
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: user-service
  annotations:
    key-auth.plugin.konghq.com: "key-auth-plugin"
    cors.plugin.konghq.com: "cors-plugin"
spec:
  rules:
  - host: api.pongpong.io
    http:
      paths:
      - path: /user-service
        backend:
          serviceName: user-service
          servicePort: 80
---

apiVersion: configuration.konghq.com/v1
kind: KongIngress
metadata:
  name: user-service
proxy:
  path: /public
route:
  strip_path: true
upstream:
  service_upstream: true
  • Service port ์ด๋ฆ„์„ http๋กœ ์„ค์ •ํ•ด์•ผ prometheus์— ์ •์ƒ์ ์œผ๋กœ ๋กœ๊ทธ๋ฅผ ๋‹ด์Œ
apiVersion: v1
kind: Service
metadata:
  name: user-service
spec:
  ports:
  - port: 80
    targetPort: 8080
    name: http      # http๋กœ ์„ค์ •
  selector:
    type: api
    app: user-service
  • Deployment ๋ผ๋ฒจ์ค‘ app๊ณผ version์„ ๋กœ๊น…์—์„œ ์‚ฌ์šฉํ•จ
apiVersion: apps/v1
kind: Deployment
metadata:
  name: user-service
spec:
  template:
    metadata:
      labels:
        type: api
        app: user-service # app ์‚ฌ์šฉ
        version: v1       # version ์‚ฌ์šฉ
  • readinessProbe/livenessProbe๋Š” exec ๋ช…๋ น์–ด ์‚ฌ์šฉ
  • httpGet์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Œ(Istio๊ฐ€ ํ•ด๋‹น ํฌํŠธ๋ฅผ proxyํ•˜๊ธฐ ๋•Œ๋ฌธ์—)
readinessProbe:
  exec:
    command:
    - curl
    - -f
    - http://localhost:8080/health
  periodSeconds: 5
livenessProbe:
  exec:
    command:
    - curl
    - -f
    - http://localhost:8080/health
  initialDelaySeconds: 10
  periodSeconds: 5
  • ๋‚ด๋ถ€ ์„œ๋น„์Šค์—์„œ ์™ธ๋ถ€ ์ฃผ์†Œ(api.pongpong.io)๋ฅผ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๊ฒŒ route rule ์ถ”๊ฐ€
apiVersion: config.istio.io/v1alpha2
kind: RouteRule
metadata:
 name: user-service-rewrite-rule
spec:
  destination:
    service: kong-proxy.kong.svc.cluster.local
  match:
    request:
      headers:
        uri:
          prefix: /user-service
  rewrite:
    authority: api.pongpong.io

Kong ๊ธฐ๋ณธ ๊ฐœ๋…

Kong ๊ตฌ์„ฑ

  • ์™ธ๋ถ€ API (api.pongpong.io:80)
    • user๊ฐ€ ํ˜ธ์ถœํ•˜๋Š” API
  • ๊ด€๋ฆฌ์ž API (kong-ingress-controller.kong:8001)
    • consumer๋ฅผ ๋งŒ๋“ค๊ฑฐ๋‚˜ key-auth plugin์„ ํ†ตํ•ด key๋ฅผ ๊ฐ€์ ธ์˜ด
  • postgresql (cassandra ์ง€์›ํ•˜๊ธดํ•จ)

Kong API

๊ธฐ๋ณธ์ ์œผ๋กœ ์œ ์ €๋Š” kong api gateway๋ฅผ ํ˜ธ์ถœํ•˜๊ณ  kong์ด ๋‹ค์‹œ ๋‚ด๋ถ€ ์„œ๋น„์Šค๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค.

  • uris - ์œ ์ €๊ฐ€ ํ˜ธ์ถœํ•˜๋Š” uri ex) /user-service
  • upstream url - uris ๊ทœ์น™์œผ๋กœ ๋“ค์–ด์˜จ ์š”์ฒญ์„ ์–ด๋””๋กœ ์ „๋‹ฌํ• ์ง€ ์„ค์ • ex) http://user-service/api
  • strip uri - ์œ ์ €๊ฐ€ https://api.pongpong.io/user-service/v1/users/1๋ฅผ ์š”์ฒญํ•˜๋ฉด /user-service๋ฅผ ์ œ๊ฑฐํ•˜๊ณ  upstream์„ ์š”์ฒญํ• ๋•Œ๋Š” https://api.pongpong.io/public/v1/users/1์„ ์ „๋‹ฌํ•จ

Consumer

consumer๋Š” user์™€ ๋น„์Šทํ•˜์ง€๋งŒ ๋‹ค๋ฅธ ๊ฐœ๋…์ž…๋‹ˆ๋‹ค.

๋‚ด๋ถ€์ ์œผ๋กœ ๊ด€๋ฆฌํ•˜๋Š” user id๋ฅผ ์ด์šฉํ•˜์—ฌ consumer๋ฅผ ๋“ฑ๋กํ•ฉ๋‹ˆ๋‹ค. ์›น / iOS / Android์— ๋”ฐ๋ผ ํ•œ user๊ฐ€ ์—ฌ๋Ÿฌ๊ฐœ์˜ consumer๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋‚˜ ์—ฌ๊ธฐ์„œ๋Š” ํ•˜๋‚˜์˜ user๋Š” ํ•˜๋‚˜์˜ consumer์ž…๋‹ˆ๋‹ค.

subicura๋ผ๋Š” user๋ฅผ ์ƒ์„ฑํ•˜๋ฉด ๊ธฐ๋ณธ์ ์œผ๋กœ kong์˜ consumer๋ฅผ subicura๋ผ๋Š” user_id๋กœ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. consumer๋Š” user_id ์™ธ์— ์–ด๋–ค ์ •๋ณด๋„ ๊ฐ€์ง€๊ณ  ์žˆ์ง€ ์•Š์œผ๋ฉฐ ๋‹จ์ง€ key-auth plugin์„ ํ†ตํ•ด token์„ ๊ด€๋ฆฌํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

user service๋ฅผ ํ†ตํ•ด ๋กœ๊ทธ์ธ์„ ์„ฑ๊ณตํ•˜๋ฉด user service๋Š” kong์˜ consumer์˜ key๋ฅผ ์กฐํšŒํ•˜๊ณ  ์‘๋‹ตํ•ฉ๋‹ˆ๋‹ค. ์ถ”ํ›„ ์œ ์ €๋Š” ํ•ด๋‹น ํ‚ค๋ฅผ ๋ชจ๋“  ์š”์ฒญ์˜ header์— ์‹ค์–ด์„œ ๋ณด๋ƒ…๋‹ˆ๋‹ค.

Key Authentication

https://getkong.org/plugins/key-authentication/

Kong์— ๊ธฐ๋ณธ์œผ๋กœ ๋‚ด์žฅ๋˜์–ด ์žˆ๋Š” authentication plugin ์ž…๋‹ˆ๋‹ค.

Header ๋˜๋Š” parameter๋กœ apikey ๊ฐ’์„ ์ „๋‹ฌํ•˜๋ฉด key-auth plugin์ด ์ž๋™์œผ๋กœ ํ•ด๋‹น ํ‚ค๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” consumer๋ฅผ ์กฐํšŒ ํ•˜๊ณ  upstream api๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ X-Consumer-Custom-ID๋ฅผ ๋ถ™์—ฌ์„œ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.

๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค๋Š” X-Consumer-Custom-ID๊ฐ€ ์žˆ์œผ๋ฉด ๋กœ๊ทธ์ธํ•œ๊ฑธ๋กœ ํŒ๋‹จํ•˜๊ณ  ์—†์œผ๋ฉด ๋กœ๊ทธ์ธํ•˜์ง€ ์•Š์€๊ฒƒ์œผ๋กœ ํŒ๋‹จํ•ฉ๋‹ˆ๋‹ค.

About

Guide documentation

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors