반응형
1. 목적 : 동일 역할을 하는 컨테이너를 같은 그룹으로 묶고 로드밸런싱등의 기능을 수행할 수 있음.
- 외부 트래픽 → net-alias 그룹내 컨테이너1,2,3....
2. bridge network 생성
$ docker network create --driver bridge --ip-range 172.22.0.0/16 --subnet 172.22.0.0/16 --gateway 172.22.0.1 test_default
3. 컨테이너 생성 및 그룹핑
# frontend, backend 모두 같은 브릿지 네트워크에 할당하고 backend 컨테이너 들은 alias1 이라는 이름으로 그룹핑.
# backend
$ docker run -itd --name=test_nginx_1 --net=test_default --net-alias alias1 nginx:latest
$ docker run -itd --name=test_nginx_2 --net=test_default --net-alias alias1 nginx:latest
$ docker run -itd --name=test_nginx_3 --net=test_default --net-alias alias1 nginx:latest
# frontend
$ docker run -itd --name=test_centos7_1 --net=test_default ansible/centos7-ansible
4. 컨테이너 설정 확인
# backend 컨테이너 모두 alias1 그룹으로 묶여 있고 각기 다른 IP를 할당 받음.
# test_nginx_1
$ docker inspect test_nginx_1
...
"Networks": {
"test_default": {
...
"Aliases": [
"alias1",
"7b9f6fde5cdb"
],
...
"Gateway": "172.22.0.1",
"IPAddress": "172.22.0.2",
...
# test_nginx_2
$ docker inspect test_nginx_2
...
"Networks": {
"test_default": {
...
"Aliases": [
"alias1",
"589e6dbe3651"
],
...
"Gateway": "172.22.0.1",
"IPAddress": "172.22.0.3",
...
# test_nginx_4
$ docker inspect test_nginx_1
...
"Networks": {
"test_default": {
...
"Aliases": [
"alias1",
"1e2db8c6ee1f"
],
...
"Gateway": "172.22.0.1",
"IPAddress": "172.22.0.4",
...
5. 로드밸런싱 테스트 : frontend 컨테이너에서 수행
# ping
# 어떠한 방식으로 밸런싱이 수행되는 지는 알수 없음.(라운드로빈은 아닌듯)
$ docker exec -it test_centos7_1 /bin/bash
[root@706f9d6df9eb ansible]# ping alias1
PING alias1 (172.22.0.3) 56(84) bytes of data.
64 bytes from test_nginx_2.test_default (172.22.0.3): icmp_seq=1 ttl=64 time=0.157 ms
64 bytes from test_nginx_2.test_default (172.22.0.3): icmp_seq=2 ttl=64 time=0.142 ms
^C
--- alias1 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 0.142/0.149/0.157/0.014 ms
[root@706f9d6df9eb ansible]# ping alias1
PING alias1 (172.22.0.4) 56(84) bytes of data.
64 bytes from test_nginx_3.test_default (172.22.0.4): icmp_seq=1 ttl=64 time=0.163 ms
^C
--- alias1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.163/0.163/0.163/0.000 ms
...
[root@706f9d6df9eb ansible]# ping alias1
PING alias1 (172.22.0.2) 56(84) bytes of data.
64 bytes from test_nginx_1.test_default (172.22.0.2): icmp_seq=1 ttl=64 time=0.208 ms
64 bytes from test_nginx_1.test_default (172.22.0.2): icmp_seq=2 ttl=64 time=0.091 ms
^C
--- alias1 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 0.091/0.149/0.208/0.059 ms
# wget
[root@706f9d6df9eb ansible]# wget alias1
--2021-03-24 15:56:11-- http://alias1/
Resolving alias1 (alias1)... 172.22.0.2, 172.22.0.3, 172.22.0.4
Connecting to alias1 (alias1)|172.22.0.2|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 612 [text/html]
Saving to: 'index.html'
100%[=========================================================================>] 612 --.-K/s in 0s
2021-03-24 15:56:11 (77.3 MB/s) - 'index.html' saved [612/612]
[root@706f9d6df9eb ansible]# wget alias1
--2021-03-24 15:56:14-- http://alias1/
Resolving alias1 (alias1)... 172.22.0.4, 172.22.0.3, 172.22.0.2
Connecting to alias1 (alias1)|172.22.0.4|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 612 [text/html]
Saving to: 'index.html.1'
100%[=========================================================================>] 612 --.-K/s in 0s
2021-03-24 15:56:14 (69.4 MB/s) - 'index.html.1' saved [612/612]
...
[root@706f9d6df9eb ansible]# wget alias1
--2021-03-24 15:56:16-- http://alias1/
Resolving alias1 (alias1)... 172.22.0.3, 172.22.0.2, 172.22.0.4
Connecting to alias1 (alias1)|172.22.0.3|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 612 [text/html]
Saving to: 'index.html.3'
100%[=========================================================================>] 612 --.-K/s in 0s
6. DNS 확인 : frontend 컨테이너에서 수행
# DNS A레코드에 등록 되며 DNS 라운드로빈 방식으로 동작 하는듯(추정)
$ docker exec -it test_centos7_1 /bin/bash
[root@706f9d6df9eb ansible]# dig alias1
; <<>> DiG 9.11.4-P2-RedHat-9.11.4-26.P2.el7_9.4 <<>> alias1
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 8476
;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 0
;; QUESTION SECTION:
;alias1. IN A
;; ANSWER SECTION:
alias1. 600 IN A 172.22.0.4
alias1. 600 IN A 172.22.0.3
alias1. 600 IN A 172.22.0.2
;; Query time: 0 msec
;; SERVER: 127.0.0.11#53(127.0.0.11)
;; WHEN: Wed Mar 24 15:56:01 UTC 2021
;; MSG SIZE rcvd: 90
반응형
'Micro Service Architecture > Docker' 카테고리의 다른 글
23. Docker Network - Host (0) | 2021.03.24 |
---|---|
21. Docker Network - Bridge (네트워크 공유) (0) | 2021.03.24 |
20. Docker Network - Bridge (기본) (0) | 2021.03.24 |
19. docker-compose 문법 (0) | 2021.03.24 |
18. [docker] Bash Completion (0) | 2021.03.16 |