<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>服务器安全维护工作室 &#187; Docker为容器分配指定物理网段的静态IP</title>
	<atom:link href="https://www.fuwuqiok.com/tag/docker%e4%b8%ba%e5%ae%b9%e5%99%a8%e5%88%86%e9%85%8d%e6%8c%87%e5%ae%9a%e7%89%a9%e7%90%86%e7%bd%91%e6%ae%b5%e7%9a%84%e9%9d%99%e6%80%81ip/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.fuwuqiok.com</link>
	<description></description>
	<lastBuildDate>Sun, 01 Mar 2020 07:28:40 +0000</lastBuildDate>
	<language>zh-CN</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.2.26</generator>
	<item>
		<title>Docker为容器分配指定物理网段的静态IP</title>
		<link>https://www.fuwuqiok.com/docker%e4%b8%ba%e5%ae%b9%e5%99%a8%e5%88%86%e9%85%8d%e6%8c%87%e5%ae%9a%e7%89%a9%e7%90%86%e7%bd%91%e6%ae%b5%e7%9a%84%e9%9d%99%e6%80%81ip/</link>
		<comments>https://www.fuwuqiok.com/docker%e4%b8%ba%e5%ae%b9%e5%99%a8%e5%88%86%e9%85%8d%e6%8c%87%e5%ae%9a%e7%89%a9%e7%90%86%e7%bd%91%e6%ae%b5%e7%9a%84%e9%9d%99%e6%80%81ip/#comments</comments>
		<pubDate>Sat, 26 Sep 2015 14:25:31 +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
				<category><![CDATA[服务器迁移]]></category>
		<category><![CDATA[网站代维]]></category>
		<category><![CDATA[Docker为容器分配指定物理网段的静态IP]]></category>

		<guid isPermaLink="false">https://www.fuwuqiok.com/?p=2713</guid>
		<description><![CDATA[<p>官方有关于网桥和IP配置的文档地址：https://docs.docker.com/articles/netw [&#8230;]</p>
<p><a rel="nofollow" href="https://www.fuwuqiok.com/docker%e4%b8%ba%e5%ae%b9%e5%99%a8%e5%88%86%e9%85%8d%e6%8c%87%e5%ae%9a%e7%89%a9%e7%90%86%e7%bd%91%e6%ae%b5%e7%9a%84%e9%9d%99%e6%80%81ip/">Docker为容器分配指定物理网段的静态IP</a>，首发于<a rel="nofollow" href="https://www.fuwuqiok.com">服务器安全维护工作室</a>。</p>
]]></description>
				<content:encoded><![CDATA[<p>官方有关于网桥和IP配置的文档地址：<a href="https://docs.docker.com/articles/networking/" target="_blank">https://docs.docker.com/articles/networking/</a></p>
<p>最近遇到几个客户准备上docker到部分生产线上做测试用，这里将网络配置过程分享给大家：</p>
<p>1、宿主机（系统采用ubuntu-14.04.1-server-amd64）的网络采用桥接模式（默认是DHCP模式，还有一种模式就是静态 IP），网桥的网段与物理网段相同。目前的物理网段为192.168.5.1/24，网关是192.168.1.1，物理网卡设备为eth0<br />
编辑配置文件/etc/network/interfaces，自定义网桥br0</p>
<p>root@ubuntu-docker:~# cat /etc/network/interfaces<br />
# This file describes the network interfaces available on your system<br />
# and how to activate them. For more information, see interfaces(5).</p>
<p># The loopback network interface<br />
auto lo<br />
iface lo inet loopback</p>
<p># The primary network interface<br />
#auto eth0<br />
#iface eth0 inet dhcp<br />
#<br />
auto br0<br />
iface br0 inet static<br />
address 192.168.5.116<br />
netmask 255.255.255.0<br />
gateway 192.168.5.1<br />
bridge_ports eth0<br />
bridge_stp off<br />
dns-nameservers 192.168.1.12 192.168.1.13<br />
root@ubuntu-docker:~#</p>
<p>配置保存退出后，重启网络</p>
<p>root@ubuntu-docker:~# ifdown -a &amp;&amp; ifup -a</p>
<p>配置完成后用ifconfig可以看到br0和eth0的状态信息如下（宿主机的ip地址为192.168.5.116）：</p>
<p>br0       Link encap:Ethernet  HWaddr 00:0c:29:dc:47:11<br />
inet addr:192.168.5.116  Bcast:192.168.5.255  Mask:255.255.255.0<br />
inet6 addr: fe80::20c:29ff:fedc:4711/64 Scope:Link<br />
UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1<br />
RX packets:737 errors:0 dropped:0 overruns:0 frame:0<br />
TX packets:226 errors:0 dropped:0 overruns:0 carrier:0<br />
collisions:0 txqueuelen:0<br />
RX bytes:70000 (70.0 KB)  TX bytes:26394 (26.3 KB)</p>
<p>eth0      Link encap:Ethernet  HWaddr 00:0c:29:dc:47:11<br />
UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1<br />
RX packets:70065 errors:0 dropped:151 overruns:0 frame:0<br />
TX packets:19621 errors:0 dropped:0 overruns:0 carrier:0<br />
collisions:0 txqueuelen:1000<br />
RX bytes:23933126 (23.9 MB)  TX bytes:2321651 (2.3 MB)</p>
<p>2、网桥配置好后，剩下的就是docker相关的网络配置了。<br />
a)容器启动的网络模式必须为none，用–net=none指定，比如</p>
<p>docker run -it &#8211;rm &#8211;net=none eeed74b237f9 /bin/bash</p>
<p>b)获取目标容器的id，我的是dfe83012cda2</p>
<p>root@ubuntu-docker:~# docker ps -a<br />
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES<br />
dfe83012cda2        centos:centos6.6    /bin/bash           About an hour ago   Up 23 minutes                           test<br />
root@ubuntu-docker:~#</p>
<p>c)配置ip的步骤有点繁琐，我写了个脚本。然后给脚本传递相关的参数就可以设置ip了。比如为容器dfe83012cda2设置静态ip地址 192.168.5.123，掩码255.255.255.0，网关192.168.5.1。有个问题就是当容器重启后，设定的ip会丢失，此时需要重新 执行该脚本一次。</p>
<p>root@ubuntu-docker:~# sh manual_con_static_ip.sh dfe83012cda2 192.168.5.123 24 192.168.5.1</p>
<p>脚本的内容如下（参考官方文档编写）</p>
<p>#/bin/bash<br />
if [ -z $1 ] || [ -z $2 ] || [ -z $3 ] || [ -z $4 ] || [ -z $5 ];<br />
then<br />
echo &#8220;*****Input the necessary parameters: CONTAINERID IP MASK GATEWAY ETHNAME&#8221;<br />
echo &#8220;*****Call the script like: sh manual_con_static_ip.sh  b0e18b6a4432 192.168.5.123 24 192.168.5.1 deth0&#8243;<br />
exit<br />
fi</p>
<p>CONTAINERID=$1<br />
SETIP=$2<br />
SETMASK=$3<br />
GATEWAY=$4<br />
ETHNAME=$5</p>
<p>#判断宿主机网卡是否存在<br />
ifconfig $ETHNAME &gt; /dev/null 2&gt;&amp;1<br />
if [ $? -eq 0 ]; then<br />
read -p &#8220;$ETHNAME exist,do you want delelte it? y/n &#8221; del<br />
if [[ $del == &#8216;y&#8217; ]]; then<br />
ip link del $ETHNAME<br />
else<br />
exit<br />
fi<br />
fi<br />
#<br />
pid=`docker inspect -f &#8216;{{.State.Pid}}&#8217; $CONTAINERID`<br />
mkdir -p /var/run/netns<br />
find -L /var/run/netns -type l -delete</p>
<p>if [ -f /var/run/netns/$pid ]; then<br />
rm -f /var/run/netns/$pid<br />
fi<br />
ln -s /proc/$pid/ns/net /var/run/netns/$pid<br />
#<br />
ip link add $ETHNAME type veth peer name B<br />
brctl addif br3 $ETHNAME<br />
ip link set $ETHNAME up<br />
ip link set B netns $pid<br />
#先删除容器内已存在的eth0<br />
ip netns exec $pid ip link del eth0 &gt; /dev/null 2&gt;&amp;1<br />
#设置容器新的网卡eth0<br />
ip netns exec $pid ip link set dev B name eth0<br />
ip netns exec $pid ip link set eth0 up<br />
ip netns exec $pid ip addr add $SETIP/$SETMASK dev eth0<br />
ip netns exec $pid ip route add default via $GATEWAY</p>
<p>脚本包含的信息，下面分行解有关语句的作用：</p>
<p>pid=`docker inspect -f &#8216;{{.State.Pid}}&#8217; $CONTAINERID`</p>
<p>为了操作容器需要获取容器的进程号PID，docker inspect 可以查看容器的底层信息，查看容器dfe83012cda2所有的底层相关信息，用docker inspect dfe83012cda2就可以查看。-f参数可以格式化输出给定的信息，比如查看容器的状态</p>
<p>root@ubuntu-docker:~# docker inspect -f &#8216;{{.State.Running}}&#8217; dfe83012cda2<br />
true<br />
root@ubuntu-docker:~#</p>
<p>为容器创建net命名空间，建立点对点连接（容器命名空间网卡和宿主上生成的网卡点对点），确保存放net命名空间的目录/var/run/netns存 在，然后删除该目录失效的链接，再将容器的net命名空间文件软链接到/var/run/netns，以便执行ip netns能够读取。</p>
<p>mkdir -p /var/run/netns<br />
find -L /var/run/netns -type l -delete<br />
ln -s /proc/$pid/ns/net /var/run/netns/$pid</p>
<p>在宿主上创建2张直连网卡（A与B直连），将B作为容器里的网卡，A作为宿主机的网卡。</p>
<p>ip link add A type veth peer name B</p>
<p>将网卡A桥接到br0上，并启动网卡A</p>
<p>brctl addif br0 A<br />
ip link set A up</p>
<p>将网卡B加入到相应的容器net命名空间，当网卡B加入到容器的net命名空间后，宿主机将无法查看到该网卡信息（执行ip netns时默认读取的目录是/var/run/netns）</p>
<p>ip link set B netns $pid</p>
<p>ip netns exec能进入容器的net命名空间，可以用来配置容器net命名空间的网络参数，配置容器内的网卡B</p>
<p>ip netns exec $pid ip link set dev B name eth0<br />
ip netns exec $pid ip link set eth0 up<br />
ip netns exec $pid ip addr add $SETIP/$SETMASK dev eth0<br />
ip netns exec $pid ip route add default via $GATEWAY</p>
<p>接下来先了解下docker为容器创建网络的过程《<a href="http://dockerpool.com/static/books/docker_practice/underly/network.html" target="_blank">http://dockerpool.com/static/books/docker_practice/underly/network.html</a>》，就会明白为什么要这么配置了。<br />
1）创建一对虚拟接口，分别放到本地主机和新容器中；<br />
2）本地主机一端桥接到默认的 docker0 或指定网桥上，并具有一个唯一的名字，如 veth65f9；<br />
3）容器一端放到新容器中，并修改名字作为 eth0，这个接口只在容器的名字空间可见；<br />
4）从网桥可用地址段中获取一个空闲地址分配给容器的 eth0，并配置默认路由到桥接网卡 veth65f9。<br />
当我们使用–net=none参数启动容器后，docker不对容器进行网络配置。如果需要容器网络可用需要我们按照相同的步骤配置容器的网络。</p>
<p><a rel="nofollow" href="https://www.fuwuqiok.com/docker%e4%b8%ba%e5%ae%b9%e5%99%a8%e5%88%86%e9%85%8d%e6%8c%87%e5%ae%9a%e7%89%a9%e7%90%86%e7%bd%91%e6%ae%b5%e7%9a%84%e9%9d%99%e6%80%81ip/">Docker为容器分配指定物理网段的静态IP</a>，首发于<a rel="nofollow" href="https://www.fuwuqiok.com">服务器安全维护工作室</a>。</p>
]]></content:encoded>
			<wfw:commentRss>https://www.fuwuqiok.com/docker%e4%b8%ba%e5%ae%b9%e5%99%a8%e5%88%86%e9%85%8d%e6%8c%87%e5%ae%9a%e7%89%a9%e7%90%86%e7%bd%91%e6%ae%b5%e7%9a%84%e9%9d%99%e6%80%81ip/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
