Traefik이란 무엇인지 공부한 내용 정리
2023-01-15
프로젝트 요구사항 중 path base routing(경로 기반 라우팅)을 구현해야했다.
예를 들어 이런식으로,
https://example-domain.co.kr/service-a/status
https://example-domain.co.kr/service-b/auth
https://example-domain.co.kr/service-c/
찾아보니 이런 경로 기반 라우팅을 구현하는 방법에는 몇 가지가 있었으나,
나는 다음과 같은 이유로 Traefik을 공부하기로 마음먹었다.
공식 문서가 초보자도 이해하기 쉽게 상세하게 설명을 잘 해놓았다.
아래 글은 거의 공식문서를 번역 & 참조한 내용을 정리한 내용이다.
플랫폼의 문이며 들어오는 모든 요청을 가로채서 라우팅합니다.
어떤 서비스가 어떤 요청을 처리하는지 결정하는 모든 논리와 규칙을 알고 있습니다
전통적으로 에지 라우터(또는 리버스 프록시)에는 서비스에 대한 모든 가능한 경로가 포함된 구성 파일이 필요한 경우 Traefik은 서비스 자체에서 이를 가져옵니다.
서비스를 배포할 때 서비스가 처리할 수 있는 요청의 특성을 Traefik에 알리는 정보를 첨부합니다.
즉, 서비스가 배포되면 Traefik이 이를 즉시 감지하고 라우팅 규칙을 실시간으로 업데이트합니다. 마찬가지로 서비스가 인프라에서 제거되면 그에 따라 해당 경로도 삭제됩니다.
가장 먼저 클라이언트의 요청을 받을 지점.
ex) 포트 설정
entrypoints에서 넘어온 요청을 어느 서비스로 연결시켜서 그쪽으로 보내줄지 결정하는 놈
ex) pathprefix, path 설정
docker의 경우 서비스를 제공하는 컨테이너 하나로 이해하면 될 듯.
위의 컨셉에서 말한 Auto Service Discovery를 실현하기 위해, Traefik은 주어진 Provider를 기반으로 그 Provider에게 api를 호출하여 entrypoints, router, service의 정보를 받아옴
ex) docker, file, docker swarm …
Traefik은 2가지 방법으로 설정 할 수 있음
traefik.http.routers.xxx
label을 사용하면 자동으로 traefik이 router 및 service를 생성하여 사용한다고 함공식 문서에서 제공하는 간단한 튜토리얼 실습
튜토리얼은 너무 간단했고 실제 프로젝트 요구사항을 충족시킬 수 있을지 확인해보고 싶었다.
server.servlet.context-path=/tuto
EXPOSE 8080
"traefik.http.routers.tuto2.rule=Host(49.50.xx.xx) && PathPrefix(/fhir)"
docker run -itd -p 8889:8080 --restart=always --name
# docker-compose.yml / test-rp / public ip
version: '3'
services:
reverse-proxy:
image: traefik:v2.9
command:
- "--api.insecure=true"
- "--providers.docker"
- "--entrypoints.web.address=:80"
ports:
- "80:80"
- "8080:8080"
volumes:
- /var/run/docker.sock:/var/run/docker.sock
whoami1:
image: traefik/whoami
labels:
- "traefik.http.routers.whoami1.rule=Host(`49.50.xx.xx`) && PathPrefix(`/1`)"
whoami2:
image: traefik/whoami
labels:
- "traefik.http.routers.whoami2.rule=Host(`49.50.xx.xx`) && PathPrefix(`/2`)"
/1
) 라면, 경로가 "/1"로 시작하는 모든 요청을 해당 서비스로 라우팅한다는 의미다.# docker-compose.yml / test-rp / ncp load balancer
version: '3'
networks:
proxy:
name: proxy
services:
traefik:
image: traefik:v2.9
container_name: traefik
command:
- "--api.insecure=true"
- "--providers.docker=true"
labels:
- "traefik.enable=true"
ports:
- "80:80"
- "8080:8080"
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro
- /home/admin/traefik:/etc/traefik/
networks:
- proxy
whoami1:
image: traefik/whoami
container_name: whoami1
labels:
- traefik.http.routers.whoami1.rule=PathPrefix(`/1`)
networks:
- proxy
whoami2:
image: traefik/whoami
container_name: whoami2
labels:
- traefik.http.routers.whoami2.rule=PathPrefix(`/2`)
networks:
- proxy
이후 80 to 443 redirect, dashboard basic auth, dynamic config 추가 테스트를 했다.
관련된 내용은 다음 글에서 정리하겠다.