Docker containers use static independent external IPs (easy for cluster formation)

http://my.oschina.net/gudaoxuri/blog/513923

It is necessary to use Docker to virtualize test environments such as Hadoop/Spark, and to provide services to the outside world, requiring a fully distributed deployment (try to simulate a production environment). Then we will encounter several problems:

Container IP is a dynamically allocated
Container IP is an internal IP, which cannot be accessed externally (for example, when HDFS services are provided externally, the Client may not be able to access the DataNode, because the DataNode registers the internal IP). For
the first There are many solutions for the first problem. You can specify a static IP. For the second problem, we can use --net=host to solve it, but this will result in only one external IP, and the ports of each slave in the cluster must be modified. As for the pipeline, I simply looked at it, and it seems that it can't be solved.

Therefore, it seems that we can only use the solution that does not seem very elegant, as follows: The solution

method

is very simple: bind multiple IPs to the Docker host network card, and assign these IPs to different containers.

//This is the example, I am running it with Docker toolbox under windows
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 network card can interact with the outside world, so we add IP to this network card
//Step 1: Added two IPs           
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
//Check again, there are two more IPs
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
...

//Step 2: Run the container, specify the IP, the SSH service enabled by the example container here, and test it later
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

//Test the connection, SSH to the first container on the Docker host
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.
[email protected]'s password:
Last login: Wed Oct  7 13:12:35 2015 from 192.168.99.1
// successfully entered
[root@259351134d16 ~]#
// SSH into the second container in the first container
[root@259351134d16 ~]# ssh 192.168.99.11 -p222
[email protected]'s password:
Last login: Wed Oct  7 13:14:53 2015 from 172.17.42.1
// also ok
[root@ab024af9c954 ~]#

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

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327003381&siteId=291194637