๋จ์ํ (ํ์ง๋ง ํ์ค์ ์ธ) ์๋น์ค๋ฅผ ๋ง์ดํฌ๋ก์๋น์ค ํํ๋ก ๋ง๋ค์ด ๊ธฐ๋ณธ ์ฒ ํ์ ์ดํดํ๊ณ ์ฟ ๋ฒ๋คํฐ์ค๋ฅผ ์ด์ฉํ์ฌ ๋ฐฐํฌ, ๋ชจ๋ํฐ๋ง, ํ์ฅํ๋ฉด์ ์ค์ ์ ์ธ ์ฌ์ฉ๋ฒ์ ์ตํ๋๊ฒ ๋ชฉ์ ์ ๋๋ค.
๋ง์ดํฌ๋ก์๋น์ค๋ฅผ ์ ๋๋ก ๊ตฌ์ถํ๋ค๋ ๊ฒ์ ๊ต์ฅํ ์ด๋ ต๊ธฐ ๋๋ฌธ์ ์ฌ๊ธฐ์๋ ๋จ์ํ๊ฒ API Gateway + REST API ํจํด์ ์ฌ์ฉํ๊ณ ์ ์ํฐํจํด์ธ์ง ๋๊ปด๋ณด๋ ์ ๋๋ก ์งํํฉ๋๋ค.
pongpong(๊ฐ์นญ)์ https://apex.sh/ping/ ํด๋ก ํ๋ก์ ํธ ์ ๋๋ค. ์ฒดํฌ ํ๊ณ ์ถ์ ์น ์๋น์ค ์ฃผ์๋ฅผ ์ ๋ ฅํ๋ฉด ์ฃผ๊ธฐ์ ์ผ๋ก ์ฒดํฌํ๊ณ ๊ฒฐ๊ณผ๋ฅผ ์๋ ค์ค๋๋ค.
- service
- type -
Ingress - host -
pongpong.io,www.pongpong.io - path -
/ - serviceName -
frontend - servicePort -
80
- type -
User๊ฐ browser๋ก ์ ์ํด์ ์๋น์ค๋ฅผ ์ฌ์ฉํ๋ Webapp
https://github.com/k8s-study/frontend @subicura
ํฐ๋ฏธ๋์์ ์คํ๊ฐ๋ฅํ ํด๋ผ์ด์ธํธ
https://github.com/k8s-study/pong-cli @anarcher
- service
- type -
Ingress - host -
api.pongpong.io - path -
/ - serviceName -
kong-proxy - servicePort -
80
- type -
- service
- type -
ClusterIP - path -
/ - serviceName -
kong-ingress-controller - servicePort -
8001
- type -
Kong ์ฌ์ฉ. ๊ฐ๋ตํ ๊ฐ๋ ์ ๋ท๋ถ๋ถ์์ ๋ค์ ์ค๋ช
https://github.com/k8s-study/kong @Ashon
Istio๋ฅผ ์ด์ฉํ์ฌ ๋ง์ดํฌ๋ก์๋น์ค๊ฐ ํต์ ์ ๊ด๋ฆฌ
TCP(DB)๋ฅผ ์ ์ธํ๊ณ httpํต์ ์ ์ฌ์ฉํ๋ ์๋น์ค๋ง ์ฌ์ฉํ๊ฒ ์ค์
- service
- type -
ClusterIP - serviceName -
user-service - servicePort -
80
- type -
- kong api
- uris -
/user-service - upstream api -
http://user-service
- uris -
- Public API
- ์ ์ CRUD + ๋ก๊ทธ์ธ / ๋ก๊ทธ์์
- Private API
- ์ ์ ๋ฆฌ์คํธ ์กฐํ / ์ ์ ์์ธ ์กฐํ
์ ์ ๋ฅผ ์์ฑํ๊ณ ์ ๋ณด๋ฅผ ์์ ํ๊ณ ๋ก๊ทธ์ธ ๋ฐ ๋ก๊ทธ์์ ์ฒ๋ฆฌ
Kong์ consumer๋ฅผ ๊ด๋ฆฌํ๊ณ Kong์ key-auth plugin์ ์ด์ฉํ์ฌ key๋ฅผ ์กฐํํจ
https://github.com/k8s-study/user-service @outsideris
- service
- type -
ClusterIP - serviceName -
endpoint-service - servicePort -
80
- type -
- kong api
- uris -
/endpoint-service - upstream api -
http://endpoint-service
- uris -
- Public API
- ์๋ํฌ์ธํธ CRUD
- Private API
- ์๋ํฌ์ธํธ ๋ฆฌ์คํธ ์กฐํ / ์๋ํฌ์ธํธ ์์ธ ์กฐํ
์น์๋น์ค๊ฐ ์ด์๋์ง ์ฒดํฌ ํ๊ธฐ ์ํ ๋ชฉ๋ก์ ๊ด๋ฆฌ
https://github.com/k8s-study/endpoint-service @findstar
- service
- type -
ClusterIP - serviceName -
notification-service - servicePort -
80
- type -
- kong api
- uris -
/notification-service - upstream api -
http://notification-service
- uris -
- Public API
- ์๋ ์ค์ CRUD
- ์๋ ํธ์ถ(ํ ์คํธ)
- Private API
- ์๋ ๋ฆฌ์คํธ ์กฐํ / ์๋ ์์ธ ์กฐํ
- ์๋ ํธ์ถ
์น์๋น์ค๊ฐ ์ฃฝ์์๋ ์๋์ ์ด๋๋ก ์ ์กํ ์ง ์ ๋ณด๋ฅผ ๊ด๋ฆฌ
ํ์ฌ ์ด๋ฉ์ผ, ์ฌ๋ ์๋์ ์ง์ํ๋ฉฐ ๊ด๋ จ ์ธ์ฆ ์ ๋ณด๋ฅผ ๊ด๋ฆฌํ๊ณ ํด๋น ์๋น์ค๋ฅผ ํธ์ถํ๋ ์ญํ ์ ํจ
https://github.com/k8s-study/notification-service @b6pzeusbc54tvhw5jgpyw8pwz2x6gs
- service
- type -
ClusterIP - serviceName -
report-service - servicePort -
80
- type -
- kong api
- uris -
/report-service - upstream api -
http://report-service/api
- uris -
- Public API
- ๋ฆฌํฌํธ ๋ฆฌ์คํธ ์กฐํ / ๋ฆฌํฌํธ ์์ธ ์กฐํ
- Private API
- ๋ฆฌํฌํธ ์์ฑ
์๋ํฌ์ธํธ ์ฒดํฌ๊ฐ ํธ์ถํ ๊ฒฐ๊ณผ๋ฅผ ์ ์ฅ
๋ฆฌ์คํธ ์กฐํ ์ฟผ๋ฆฌ์ ๋ ์ง๋ฅผ ์ฟผ๋ฆฌ ํ๋ผ๋ฏธํฐ๋ก ๋ฐ์
https://github.com/k8s-study/report-service @SsureyMoon
- service
- type -
ClusterIP - serviceName -
email-service - servicePort -
80
- type -
- Internal API
- ์ด๋ฉ์ผ ์ ์ก
๋ฐ๋์ฌ๋, ์ ๋ชฉ, ๋ด์ฉ์ ์ธ์๋ก ์ด๋ฉ์ผ์ ์ ์ก
https://github.com/k8s-study/email-service
- service
- type -
ClusterIP - serviceName -
slack-service - servicePort -
80
- type -
- Internal API
- ์ฌ๋ ๋ฉ์์ง ์ ์ก
๋๋ฉ์ธ, ์ฑ๋, ๋ด์ฉ, token์ ์ธ์๋ก ์ฌ๋ ๋ฉ์์ง ์ ์ก
https://github.com/k8s-study/slack-service @asbubam
- service
- type -
ClusterIP - serviceName -
endpoint-check - servicePort -
80
- type -
- Internal API
- ์ฒดํฌ
์ฌ์ฉ์ ์์ฒญ ๋๋ ์ฃผ๊ธฐ์ ์ผ๋ก endpoint๊ฐ ์ ์์ธ์ง ์ฒดํฌ
https://github.com/k8s-study/endpoint-check @tedpark
- service
- type -
Ingress - host -
admin.pongpong.io - path -
/kibana - serviceName -
logging-kibana - servicePort -
80
- type -
- + agent services
๋ก๊น
https://github.com/k8s-study/logging @posquit0
- service
- type -
Ingress - host -
admin.pongpong.io - path -
/grafana - serviceName -
monitoring-grafana - servicePort -
80
- type -
- + 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 ์ค๊ณ๋ ๋ค์ ๊ท์น์ ์ค์ํฉ๋๋ค.
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"
}
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
- ์ธ๋ถ API (api.pongpong.io:80)
- user๊ฐ ํธ์ถํ๋ API
- ๊ด๋ฆฌ์ API (kong-ingress-controller.kong:8001)
- consumer๋ฅผ ๋ง๋ค๊ฑฐ๋ key-auth plugin์ ํตํด key๋ฅผ ๊ฐ์ ธ์ด
- postgresql (cassandra ์ง์ํ๊ธดํจ)
๊ธฐ๋ณธ์ ์ผ๋ก ์ ์ ๋ 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๋ 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์ ์ค์ด์ ๋ณด๋
๋๋ค.
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๊ฐ ์์ผ๋ฉด ๋ก๊ทธ์ธํ๊ฑธ๋ก ํ๋จํ๊ณ ์์ผ๋ฉด ๋ก๊ทธ์ธํ์ง ์์๊ฒ์ผ๋ก ํ๋จํฉ๋๋ค.
