Docker容器固定IP分配 ﹏ヽ暗。殇╰゛Y 2022-06-11 08:12 201阅读 0赞 我们在使用docker run创建Docker容器时,可以用--net选项指定容器的网络模式,Docker有以下4种网络模式: host模式,使用--net=host指定。 container模式,使用--net=container:NAME\_or\_ID指定。 none模式,使用--net=none指定。 bridge模式,使用--net=bridge指定,默认设置。 默认选择bridge的情况下,容器启动后会通过DHCP获取一个地址 ,这可能不是我们想要的,本文介绍在centos7 docker环境下使用pipework脚本对容器分配固定IP。 \# brctl show \# docker run -idt \--net=none --name test1 registry.fjhb.cn/centos6 /bin/bash \# brctl show \# docker-enter test1 \# ip a ![wKioL1TApH3hRJV8AAZAFA\_0TlE585.jpg][wKioL1TApH3hRJV8AAZAFA_0TlE585.jpg] \# wget https://github.com/jpetazzo/pipework/archive/master.zip \# unzip pipework-master.zip \# cp pipework-master/pipework /usr/local/bin/ \# chmod +x /usr/local/bin/pipework \# pipework kbr0 test1 172.17.1.3/24@172.17.1.1 \# brctl show \# docker-enter test1 \# ip a \# route -n ![wKiom1TAo-OSBhVXAAhkhRPYqaE935.jpg][] Pipework 有个缺陷,容器重启后 Server1: ![wKioL1S-JgKi\_26TAALMRW9LYKQ999.jpg][wKioL1S-JgKi_26TAALMRW9LYKQ999.jpg] 一、Docker的四种网络模式 Docker在创建容器时有四种网络模式,bridge为默认不需要用--net去指定,其他三种模式需要在创建容器时使用--net去指定。 1. bridge模式,使用--net=bridge指定,默认设置。 2. none模式,使用--net=none指定。 3. host模式,使用--net=host指定。 4. container模式,使用--net=container:容器名称或ID指定。(如:--net=container:30b668ccb630) bridge模式:docker网络隔离基于网络命名空间<Network Namespace>,在物理机上创建docker容器时会为每一个docker容器分配网络命名空间,并且把容器IP桥接到物理机的虚拟网桥上。 none模式:此模式下创建容器是不会为容器配置任何网络参数的,如:容器网卡、IP、通信路由等,全部需要自己去配置。 host模式:此模式创建的容器没有自己独立的网络命名空间,是和物理机共享一个Network Namespace,并且共享物理机的所有端口与IP,并且这个模式认为是不安全的。 container模式:此模式和host模式很类似,只是此模式创建容器共享的是其他容器的IP和端口而不是物理机,此模式容器自身是不会配置网络和端口,创建此模式容器进去后,你会发现里边的IP是你所指定的那个容器IP并且端口也是共享的,而且其它还是互相隔离的,如进程等。 二、Docker配置自己的网桥 例子一、 1)、自定义新网桥 root@Docker:~\# dpkg -l | grep bridge\* \#查看是否有安装brctl命令包 ii bridge-utils 1.5-6Ubuntu2 amd64 Utilities for configuring the Linux Ethernet bridge root@Docker:~\# apt-get install bridge-utils \#安装brctl命令包 root@Docker:~\# docker -v \#docker版本 Docker version 1.5.0, build a8a31ef root@Docker:~\# ps -ef | grep docker \#正在运行 root 6834 1 0 16:28 ? 00:00:00 /usr/bin/docker -d root@Docker:~\# service docker stop \#停止 root@Docker:~\# ifconfig | grep docker0 \#docker默认网桥 docker0 Link encap:以太网 硬件地址 56:84:7a:fe:97:99 root@Docker:~\# ifconfig docker0 down \#停止docker默认网桥 root@Docker:~\# brctl show \#查看物理机上有哪些网桥 root@Docker:~\# brctl delbr docker0 \#删除docker默认网桥 root@Docker:~\# brctl addbr docker\_new0 \#自定义网桥 root@Docker:~\# ifconfig docker\_new0 192.168.6.1 netmask 255.255.255.0 \#给自定义网桥指定IP和子网 root@Docker:~\# ifconfig | grep docker\_new0 \#查看发现自定义网桥已经启动 docker\_new0 Link encap:以太网 硬件地址 0a:5b:26:48:dc:04 inet 地址:192.168.6.1 广播:192.168.6.255 掩码:255.255.255.0 root@Docker:~\# echo 'DOCKER\_OPTS="-b=docker\_new0"' >> /etc/default/docker \#指定网桥写入docker配置文件 root@Docker:~\# service docker start \#启动docker root@Docker:~\# ps -ef | grep docker \#成功启动,并且成功加载了docker\_new0 root 21345 1 0 18:44 ? 00:00:00 /usr/bin/docker -d -b=docker\_new0 root@Docker:~\# brctl show \#查看当前网桥下是否有容器连接 bridge name bridge id STP enabled interfaces docker\_new0 8000.fa3ce276c3b9 no root@Docker:~\# docker run -itd CentOS:centos6 /bin/bash \#创建容器测试 root@Docker:~\# docker attach 7f8ff622237f \#进入容器 \[root@7f8ff622237f /\]\# ifconfig eth0 | grep addr \#容器IP已经和自定义网桥一个网段,该容器IP为DHCP自动分配,不属于指定固定IP eth0 Link encap:Ethernet HWaddr 02:42:C0:A8:06:02 inet addr:192.168.6.2 Bcast:0.0.0.0 Mask:255.255.255.0 inet6 addr: fe80::42:c0ff:fea8:602/64 Scope:Link root@Docker:~\# brctl show \#该网桥上已经连接着一个网络设备了 bridge name bridge id STP enabled interfaces docker\_new0 8000.fa3ce276c3b9 no veth17f560a 注: veth设备是成双成对出现的,一端是容器内部命名eth0,一端是加入到网桥并命名的veth17f560a(通常命名为veth\*),他们组成了一个数据传输通道,一端进一端出,veth设备连接了两个网络设备并实现了数据通信。 root@Docker:~\# wget https://github.com/jpetazzo/pipework/archive/master.zip \#下载 pipework root@Docker:~\# unzip master.zip \#解压 root@Docker:~\# cp pipework-master/pipework /usr/bin/ \#拷贝pipework到 /usr/bin/下 root@Docker:~\# chmod +x /usr/bin/pipework \#赋予该命令执行权限 root@Docker:~\# pipework docker\_new0 -i eth1 $(docker run -itd -p 9197:80 centos:centos6 /bin/bash) 192.168.6.27/24@192.168.6.1 \#创建容器,并指定固定IP 格式:pipework 网桥名 -i 指定在那块网卡上配置 <容器名or容器ID> 指定容器内IP/子网@网关 注:容器内网关就是物理机网桥的IP root@Docker:~\# docker attach 2966430e2dbe \#进入新容器 \[root@2966430e2dbe /\]\# ifconfig \#容器内IP为指定的IP 192.168.6.27 eth0 Link encap:Ethernet HWaddr 02:42:C0:A8:06:05 inet addr:192.168.6.7 Bcast:0.0.0.0 Mask:255.255.255.0 \#docker\_new0网桥创建容器时DHCP分配的IP eth1 Link encap:Ethernet HWaddr 82:DB:F7:A3:33:92 inet addr:192.168.6.27 Bcast:0.0.0.0 Mask:255.255.255.0 \#pipework指定的固定IP,网桥还是docker\_new0 \[root@2966430e2dbe /\]\# route -n \#查看路由路径 Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.6.1 0.0.0.0 UG 0 0 0 eth0 192.168.6.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 192.168.6.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1 \[root@2966430e2dbe /\]\# ping www.linuxidc.com \#测试网络 PING www.linuxidc.com (119.75.218.70) 56(84) bytes of data. 64 bytes from 119.75.218.70: icmp\_seq=1 ttl=127 time=3.98 ms 64 bytes from 119.75.218.70: icmp\_seq=2 ttl=127 time=2.98 ms \[root@2966430e2dbe /\]\# netstat -anptu | grep 80 \#容器内80端口已经开启 tcp 0 0 :::80 :::\* LISTEN - root@Docker:~\# telnet 192.168.6.27 80 \#物理机上测试指定的IP是否和映射的端口等通信正常 Trying 192.168.6.27... Connected to 192.168.6.27. Escape character is '^\]'. root@Docker:~\# iptables-save > iptables-rules \#拷贝防火墙规则到本地文件 root@Docker:~\# vi iptables-rules \#打开规则文件查看你会发现你物理机的防火墙自动添加了很多条规则,这个是容器到网桥到本地网卡到公网的地址转换通信规则 ![20161012112551244.jpg_2016912112610][] 例子二: 首先,配置一个用于创建container interface的网桥,可以使用ovs,也可以使用Linux bridge,以Linux bridge为例: br\_name=docker brctl addbr $br\_name ip addr add 192.168.33.2/24 dev $br\_name ip addr del 192.168.33.2/24 dev em1 ip link set $br\_name up brctl addif $br\_name eth0 接着,可以启动容器了,注意用--net=none方式启动: \# start new container hostname='docker.test.com' cid=$(docker run -d -i -h $hostname --net=none -t centos) pid=$(docker inspect -f '\{ \{.State.Pid\}\}' $cid) 下面,为该容器配置网络namespace,并设置固定ip: \# set up netns mkdir -p /var/run/netns ln -s /proc/$pid/ns/net /var/run/netns/$pid \# set up bridge ip link add q$pid type veth peer name r$pid brctl addif $br\_name q$pid ip link set q$pid up \# set up docker interface fixed\_ip='192.168.33.3/24' gateway='192.168.33.1' ip link set r$pid netns $pid ip netns exec $pid ip link set dev r$pid name eth0 ip netns exec $pid ip link set eth0 up ip netns exec $pid ip addr add $fixed\_ip dev eth0 ip netns exec $pid ip route add default via 192.168.33.1 这样,容器的网络就配置好了,如果容器内部开启了sshd服务,通过192.168.33.3就可以直接ssh连接到容器,非常方便。上面的步骤比较长,可以借助pipework来为容器设置固定ip(除了设置IP,还封装了配置网关、macvlan、vlan、dhcp等功能): pipework docker0 be8365e3b2834 10.88.88.8/24 那么,当容器需要删除的时候,怎么清理网络呢,其实也很简单: \# stop and delete container docker stop $cid docker rm $cid \# delete docker's net namespace (also delete veth pair) ip netns delete $pid [wKioL1TApH3hRJV8AAZAFA_0TlE585.jpg]: /images/20220611/b2221114dbab463da0912ce7d6ebc5a4.png [wKiom1TAo-OSBhVXAAhkhRPYqaE935.jpg]: /images/20220611/431f4141047241e59cf1ce3b46e93784.png [wKioL1S-JgKi_26TAALMRW9LYKQ999.jpg]: /images/20220611/2f58a248b47f4993b4c8bbe1790eff5b.png [20161012112551244.jpg_2016912112610]: http://files.jb51.net/file_images/article/201610/20161012112551244.jpg?2016912112610
还没有评论,来说两句吧...