MSA 환경에서 각 서비스들을 일괄 실행시키기 위해 docker-compose 를 사용하였다.
이때 서비스간 실행 순서를 보장하기 위해 depens_on 옵션을 사용하였다.
# docker-compose.yml
config-service:
container_name: config-service
...
user-service:
depends_on:
- config-service
...
이때 depends_on 을 통해 config-service 가 실행되었을때 user-service 가 실행되도록 설정하였다.
하지만 config-service 에서 가져와야할 properties 들을 가지고 올 수 없었다.
이는 config-service 가 정상적으로 실행되지 않은 상태로 user-service 가 실행되었기 때문에 해당 값을 참조할 수 없었기 때문이다.
다시 돌아와 depends_on 옵션을 자세히 살펴보았다.
https://docs.docker.com/reference/compose-file/services/#depends_on
더보기
depends_on
With the depends_on attribute, you can control the order of service startup and shutdown. It is useful if services are closely coupled, and the startup sequence impacts the application's functionality.
depends_on 속성을 사용하면 서비스 시작 및 종료 순서를 제어할 수 있습니다. 서비스가 밀접하게 결합되어 있고 시작 순서가 애플리케이션 기능에 영향을 미치는 경우 유용합니다.
따라서 depends_on 을 단순하게 사용한다면 해당 서비스의 컨테이너가 실행이 되었을때 어플리케이션의
실행여부와 상관없이 연관된 서비스가 실행된다.
그래서 config-service 의 컨테이너가 실행되자마자 user-serivce 가 실행되었고,
config-service 가 완전히 실행되지 않았기 때문에 해당 값을 참조할 수 없었다.
이를 극복하기 위해 docker 의 또 다른 옵션인 healthcheck 를 사용하였다.
https://docs.docker.com/reference/compose-file/services/#healthcheck
config-service:
container_name: config-service
healthcheck:
test: curl -f http://localhost:8888/actuator/health || exit 1
interval: 10s
timeout: 10s
retries: 120
...
user-service:
depends_on:
config-service:
condition: service_healthy
...
healthcheck 의 옵션은 다음과 같다.
test | 해당 컨테이너 내에서 테스트 요청할 명령어 |
interval | 헬스 체크 간격 |
timeout | 헬스 체크 타임아웃 |
retries | 타임 아웃 횟수 |
healthcheck 옵션을 주게되면 아래와 같이 status 값에 healthy 라는 문구가 찍히게 된다.
해당 값은 starting, healthy, unhealthy 3가지가 존재한다.
starting | 검사 시작 |
healthy | 검사 통과 |
unhealthy | 검사 실패 |
이제 config-service 가 정상적으로 실행되었을때 user-service 를 실행 할 수 있게 되었다.
'docker' 카테고리의 다른 글
Docker 컨테이너 이름 변경 (1) | 2023.11.24 |
---|---|
Docker 설치 우분투 22.04 버전 / Ubuntu 22.04 (0) | 2023.11.23 |
[Docker] Docker 컨테이너 간단한 명령어 (0) | 2023.04.17 |