Docker容器使用靜態獨立的外部IP(便於叢集組建)

不淨之心發表於2015-10-07
[url]http://my.oschina.net/gudaoxuri/blog/513923[/url]

需要使用Docker虛擬化Hadoop/Spark等測試環境,並且要可以對外提供服務,要求是完全分散式的部署(儘量模擬生產環境)。那麼我們會遇到幾個問題:

Container IP 是動態分配的
Container IP 是內部IP,外部無法訪問(如對外提供HDFS服務可能會遇到Client無法訪問DataNode,因為DataNode註冊的是內部IP)
針對第一個問題有不少的方案,可以指定靜態的IP,對第二個問題,我們可以使用--net=host解決,但這會導致對外只有一個IP,叢集各個Slave的埠都要修改。至於pipework簡單地看了下,好像也解決不了。

所以目前看上去只能使用看上去不是很優雅的方案解決,如下:

Solution

方法很簡單:為Docker宿主網路卡繫結多個IP,把這些IP分配給不同的容器。

[code="java"]//這是示例,我是在windows下用Docker toolbox執行的
root@default:~# ifconfig
docker0 Link encap:Ethernet HWaddr 02:42:8C:8E:80:F1
inet addr:172.17.42.1 Bcast:0.0.0.0 Mask:255.255.0.0
UP BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)

eth0 Link encap:Ethernet HWaddr 08:00:27:24:D1:F5
inet addr:10.0.2.15 Bcast:10.0.2.255 Mask:255.255.255.0
inet6 addr: fe80::a00:27ff:fe24:d1f5/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:222 errors:0 dropped:0 overruns:0 frame:0
TX packets:164 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:32277 (31.5 KiB) TX bytes:28136 (27.4 KiB)

eth1 Link encap:Ethernet HWaddr 08:00:27:76:1D:9B
inet addr:192.168.99.100 Bcast:192.168.99.255 Mask:255.255.255.0
inet6 addr: fe80::a00:27ff:fe76:1d9b/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:66 errors:0 dropped:0 overruns:0 frame:0
TX packets:64 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:9001 (8.7 KiB) TX bytes:10469 (10.2 KiB)

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:16 errors:0 dropped:0 overruns:0 frame:0
TX packets:16 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:1152 (1.1 KiB) TX bytes:1152 (1.1 KiB)

//eth1網路卡是可以與外部互動,所以我們新增IP到這個網路卡上
//第一步:新增了兩個IP
root@default:~# ifconfig eth1:0 192.168.99.10 netmask 255.255.255.0 up
root@default:~# ifconfig eth1:1 192.168.99.11 netmask 255.255.255.0 up
//再次檢視,多了兩個IP
root@default:~# ifconfig
...

eth1 Link encap:Ethernet HWaddr 08:00:27:76:1D:9B
inet addr:192.168.99.100 Bcast:192.168.99.255 Mask:255.255.255.0
inet6 addr: fe80::a00:27ff:fe76:1d9b/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:2258 errors:0 dropped:0 overruns:0 frame:0
TX packets:1685 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:207033 (202.1 KiB) TX bytes:209587 (204.6 KiB)

eth1:0 Link encap:Ethernet HWaddr 08:00:27:76:1D:9B
inet addr:192.168.99.10 Bcast:192.168.99.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1

eth1:1 Link encap:Ethernet HWaddr 08:00:27:76:1D:9B
inet addr:192.168.99.11 Bcast:192.168.99.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
...

//第二步:執行容器,指定IP,這裡的示例容器開啟的SSH服務,後面拿它測試
root@default:~# docker run -d -p 192.168.99.10:222:22 --name ssh1 gudaoxuri/scala-2.11-env
root@default:~# docker run -d -p 192.168.99.11:222:22 --name ssh2 gudaoxuri/scala-2.11-env
root@default:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ab024af9c954 gudaoxuri/scala-2.11-env "/usr/sbin/sshd -D" 4 seconds ago Up 3 seconds 192.168.99.11:222->22/tcp ssh2
259351134d16 gudaoxuri/scala-2.11-env "/usr/sbin/sshd -D" 15 seconds ago Up 14 seconds 192.168.99.10:222->22/tcp ssh1

//測試連線,在Docker宿主機上SSH到第一個容器
root@default:~# ssh 192.168.99.10 -p222
The authenticity of host '[192.168.99.10]:222 ([192.168.99.10]:222)' can't be established.
RSA key fingerprint is ac:fe:4b:89:f8:51:b7:e9:9c:34:62:f9:80:38:4b:bf.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[192.168.99.10]:222' (RSA) to the list of known hosts.
root@192.168.99.10's password:
Last login: Wed Oct 7 13:12:35 2015 from 192.168.99.1
//成功進入
#
//在第一個容器中SSH到第二個容器
# ssh 192.168.99.11 -p222
root@192.168.99.11's password:
Last login: Wed Oct 7 13:14:53 2015 from 172.17.42.1
//也OK了
#[/code]
Reference

http://stackoverflow.com/questions/25036895/how-to-expose-docker-containers-ip-and-port-to-outside-docker-host-without-port/25041782#25041782

http://dl528888.blog.51cto.com/2382721/1604167

相關文章