为Docker容器设置固定IP实现网络联通(2)——通过Python脚本实现并解决pipework缺陷


题记


前面已经提到通过使用pipework方式,为容器设置固定IP,但是该方法有一个问题就是如果我们的容器实例重启,设置的固定IP会丢失,这显然回事一件令人头疼的事情,如果我们重启后IP依然保持设置的,岂不是一件很好的事情,接下来我们就介绍一下如何完成这个需求。

为Docker容器设置固定IP实现网络联通(2)——通过Python脚本实现并解决pipework缺陷_第1张图片

通过这个图可以看到,与上一篇使用pipework架构基本类似,本次只是用了一个网卡,而且我只创建了一个容器实例C1,相关环境可以参考上一篇介绍。

--------------------------------------------------------------------------------------

Blog:    http://blog.csdn.net/chinagissoft

QQ群:16403743

宗旨:专注于"GIS+"前沿技术的研究与交流,将云计算技术、大数据技术、容器技术、物联网与GIS进行深度融合,探讨"GIS+"技术和行业解决方案

转载说明:文章允许转载,但必须以链接方式注明源地址,否则追究法律责任!

--------------------------------------------------------------------------------------


1、为容器服务器设置br0网桥,该网桥写入到配置文件里面,dns-nameservers如果需求可以添加

root@controller:~# cat /etc/network/interfaces
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
#auto eth0
#iface eth0 inet static
#       address 10.0.0.11
#       netmask 255.255.255.0
auto br0
iface br0 inet static
address 192.168.14.225
netmask 255.255.255.0
gateway 192.168.14.254
bridge_ports eth0
bridge_stp off

2、安装相应的包文件

#安装pip
apt-get install python-pip python-dev build-essential
 
#安装docker python的api
pip install docker-py 

3、启动一个容器实例

root@controller:~# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
root@controller:~# docker run -i -t --name test1 --net=none ubuntu:14.04 /bin/bash
root@443b6fa7416a:/#
root@443b6fa7416a:/# ip a
1: lo:  mtu 65536 qdisc noqueue state UNKNOWN group default
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever

4、下载相关文件docker-static-ip-master.zip
解压之后,进入该文件夹,该文件夹其实就是通过python脚本实现了设置固定IP的步骤。

我们需要修改相关的配置文件,添加需要设置的容器实例信息

root@controller:~# cd docker-static-ip-master/
root@controller:~/docker-static-ip-master# ls
containers.cfg  duration.py  README.md
root@controller:~/docker-static-ip-master# cat containers.cfg
#,,,
root@controller:~/docker-static-ip-master# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
443b6fa7416a        ubuntu:14.04        "/bin/bash"         3 minutes ago       Exited (0) 3 minutes ago                       test1
root@controller:~/docker-static-ip-master#
root@controller:~/docker-static-ip-master# echo "443b6fa7416a,br0,192.168.14.243/24,192.168.14.254" >> containers.cfg
root@controller:~/docker-static-ip-master# python duration.py

5、查看信息(在容器实例)

root@443b6fa7416a:/# ip a
1: lo:  mtu 65536 qdisc noqueue state UNKNOWN group default
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
7: eth0:  mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 3a:9d:3b:13:89:db brd ff:ff:ff:ff:ff:ff
    inet 192.168.14.243/24 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::389d:3bff:fe13:89db/64 scope link
       valid_lft forever preferred_lft forever

在容器实例测试连接

root@443b6fa7416a:/# ping 192.168.14.254
PING 192.168.14.254 (192.168.14.254) 56(84) bytes of data.
64 bytes from 192.168.14.254: icmp_seq=1 ttl=255 time=22.5 ms
64 bytes from 192.168.14.254: icmp_seq=2 ttl=254 time=2.00 ms
^C
--- 192.168.14.254 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1002ms
rtt min/avg/max/mdev = 2.005/12.282/22.559/10.277 ms
root@443b6fa7416a:/# ping 192.168.14.225
PING 192.168.14.225 (192.168.14.225) 56(84) bytes of data.
64 bytes from 192.168.14.225: icmp_seq=1 ttl=64 time=0.127 ms
64 bytes from 192.168.14.225: icmp_seq=2 ttl=64 time=0.104 ms
^C
--- 192.168.14.225 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 999ms
rtt min/avg/max/mdev = 0.104/0.115/0.127/0.015 ms

在宿主机查看信息

root@controller:~# ifconfig br0
br0       Link encap:Ethernet  HWaddr 00:0c:29:d3:5a:fe
          inet addr:192.168.14.225  Bcast:192.168.14.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fed3:5afe/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:7139 errors:0 dropped:0 overruns:0 frame:0
          TX packets:976 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:552522 (552.5 KB)  TX bytes:123821 (123.8 KB)

root@controller:~# brctl show
bridge name     bridge id               STP enabled     interfaces
br0             8000.000c29d35afe       no              eth0
                                                        tap1923
docker0         8000.02427810a053       no
root@controller:~# ping 192.168.14.243
PING 192.168.14.243 (192.168.14.243) 56(84) bytes of data.
64 bytes from 192.168.14.243: icmp_seq=1 ttl=64 time=0.033 ms
64 bytes from 192.168.14.243: icmp_seq=2 ttl=64 time=0.065 ms
64 bytes from 192.168.14.243: icmp_seq=3 ttl=64 time=0.100 ms
^C
--- 192.168.14.243 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2000ms
rtt min/avg/max/mdev = 0.033/0.066/0.100/0.027 ms

在我的笔记本测试连接
C:\Users\Administrator>ping 192.168.14.243

正在 Ping 192.168.14.243 具有 32 字节的数据:
来自 192.168.14.243 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.14.243 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.14.243 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.14.243 的回复: 字节=32 时间<1ms TTL=64

192.168.14.243 的 Ping 统计信息:
    数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
    最短 = 0ms,最长 = 0ms,平均 = 0ms


6、重启容器实例,测试IP

root@443b6fa7416a:/# exitroot@controller:~#
root@controller:~# docker stop 443b6fa7416a
443b6fa7416a
root@controller:~# docker start 443b6fa7416a
443b6fa7416a
root@controller:~# docker exec -it 443b6fa7416a /bin/bash
root@443b6fa7416a:/# ifconfig
eth0      Link encap:Ethernet  HWaddr 4e:ef:ea:8d:0a:30
          inet addr:192.168.14.243  Bcast:0.0.0.0  Mask:255.255.255.0
          inet6 addr: fe80::4cef:eaff:fe8d:a30/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:30 errors:0 dropped:0 overruns:0 frame:0
          TX packets:7 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:2364 (2.3 KB)  TX bytes:578 (578.0 B)

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: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)

root@443b6fa7416a:/# ping 192.168.14.225
PING 192.168.14.225 (192.168.14.225) 56(84) bytes of data.
64 bytes from 192.168.14.225: icmp_seq=1 ttl=64 time=0.160 ms
64 bytes from 192.168.14.225: icmp_seq=2 ttl=64 time=0.113 ms
64 bytes from 192.168.14.225: icmp_seq=3 ttl=64 time=0.113 ms
^C
--- 192.168.14.225 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 1999ms
rtt min/avg/max/mdev = 0.113/0.128/0.160/0.025 ms
root@443b6fa7416a:/#

--------------------------------------------------------------------------------------

Blog:    http://blog.csdn.net/chinagissoft

QQ群:16403743

宗旨:专注于"GIS+"前沿技术的研究与交流,将云计算技术、大数据技术、容器技术、物联网与GIS进行深度融合,探讨"GIS+"技术和行业解决方案

转载说明:文章允许转载,但必须以链接方式注明源地址,否则追究法律责任!

--------------------------------------------------------------------------------------


你可能感兴趣的:(容器,Docker)