네트워크 별칭과 서비스 디스커버리
Docker 네트워크는 네트워크 별칭 (Alias) 을 통한 서비스 디스커버리 기능을 지원합니다. 이는 유연하고 확장 가능한 애플리케이션을 구축할 때 매우 유용합니다. 여러 컨테이너가 동일한 DNS 이름을 공유하게 함으로써 기본적인 로드 밸런싱을 구현할 수 있습니다.
- 이번 실습을 위한 새로운 브리지 네트워크를 생성합니다.
docker network create service-network
- 네트워크 생성을 확인합니다.
docker network ls
목록에 service-network가 나타나야 합니다.
- 동일한 네트워크 별칭을 가진 두 개의 컨테이너를 실행합니다.
docker run -d --network service-network --network-alias myservice --name service1 nginx
docker run -d --network service-network --network-alias myservice --name service2 nginx
- 클라이언트 컨테이너를 생성하여
nslookup으로 서비스를 조회해 봅니다.
docker run --rm --network service-network appropriate/curl nslookup myservice
두 컨테이너의 IP 주소가 모두 반환되는 것을 확인할 수 있습니다. 이는 Docker 의 내장 DNS 서버가 두 컨테이너 사이에서 로드 밸런싱을 수행하고 있음을 보여줍니다.
- 서비스에 여러 번 접속하여 테스트합니다.
for i in {1..4}; do docker run --rm --network service-network appropriate/curl ping -c 1 myservice; done
두 컨테이너로부터 번갈아 가며 응답이 오는 것을 확인할 수 있으며, 이는 기본적인 로드 밸런싱이 작동하고 있음을 증명합니다. Docker DNS 서버는 myservice 이름을 해석할 때 두 IP 주소를 교대로 제공합니다.
PING myservice (172.18.0.2): 56 data bytes
64 bytes from 172.18.0.2: seq=0 ttl=64 time=0.106 ms
--- myservice ping statistics ---
1 packets transmitted, 1 packets received, 0% packet loss
round-trip min/avg/max = 0.106/0.106/0.106 ms
PING myservice (172.18.0.3): 56 data bytes
64 bytes from 172.18.0.3: seq=0 ttl=64 time=0.119 ms
--- myservice ping statistics ---
1 packets transmitted, 1 packets received, 0% packet loss
round-trip min/avg/max = 0.119/0.119/0.119 ms
PING myservice (172.18.0.2): 56 data bytes
64 bytes from 172.18.0.2: seq=0 ttl=64 time=0.097 ms
--- myservice ping statistics ---
1 packets transmitted, 1 packets received, 0% packet loss
round-trip min/avg/max = 0.097/0.097/0.097 ms
PING myservice (172.18.0.3): 56 data bytes
64 bytes from 172.18.0.3: seq=0 ttl=64 time=0.140 ms
--- myservice ping statistics ---
1 packets transmitted, 1 packets received, 0% packet loss
round-trip min/avg/max = 0.140/0.140/0.140 ms