Docker設置靜態IP

創建docker容器

docker run -it --name=yh -h yh --net=none debian:sshd bash   ### 確保使用--net=none參數,此時新建的容器內不會創建網卡

docker ps

此時登錄容器查看IP,會發現沒有eth0網卡:

root@yh:/# ifconfig -a            
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)

 

CentOS6.6升級iproute

爲了支持ip netns命令,需要對CentOS6.6進行升級:

rpm -ivh http://www.elrepo.org/elrepo-release-6-6.el6.elrepo.noarch.rpm

yum --enablerepo=elrepo-kernel install kernel-lt -y

vi /etc/grub.conf   ##   修改default=0,默認啓動新內核

reboot

uname -r

yum install -y http://rdo.fedorapeople.org/rdo-release.rpm  ### 更新rdo倉庫    
vim /etc/yum.repos.d/rdo-release.repo  ### 修改文件內容爲如下

[openstack-juno]          
name=OpenStack Juno Repository          
baseurl=http://repos.fedorapeople.org/repos/openstack/openstack-icehouse/epel-6/          
enabled=1          
skip_if_unavailable=0          
gpgcheck=0          
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-RDO-Juno

 

yum --enablerepo=openstack-juno install iproute   ### 更新iproute    
rpm -q iproute

 

設置靜態IP

modify_docker_ip.sh:

#/bin/bash

if [ -z $1 ] || [ -z $2 ] || [ -z $3 ] || [ -z $4 ] || [ -z $5 ];

then

        echo "Usage: $0 CONTAINERID IP MASK GATEWAY ETHNAME"

        echo "        Call the script like: sh manual_con_static_ip.sh  b0e18b6a4432 192.168.5.123 24 192.168.5.1 deth0"

        exit

fi

CONTAINERID=$1

SETIP=$2

SETMASK=$3

GATEWAY=$4

ETHNAME=$5

#判斷宿主機網卡是否存在

ifconfig $ETHNAME > /dev/null 2>&1

if [ $? -eq 0 ]; then

    read -p "$ETHNAME exist,do you want delelte it? y/n " del

    if [[ $del == 'y' ]]; then

    ip link del $ETHNAME

    else

    exit

    fi

fi

#

pid=`docker inspect -f '``.`State`.`Pid`' $CONTAINERID`

echo pid=$pid

mkdir -p /var/run/netns

find -L /var/run/netns -type l -delete

if [ -f /var/run/netns/$pid ]; then

    rm -f /var/run/netns/$pid

fi

ln -s /proc/$pid/ns/net /var/run/netns/$pid

#

ip link add $ETHNAME type veth peer name B

brctl addif docker0 $ETHNAME

ip link set $ETHNAME up

ip link set B netns $pid

#先刪除容器內已存在的eth0

ip netns exec $pid ip link del eth0 > /dev/null 2>&1

#設置容器新的網卡eth0

ip netns exec $pid ip link set dev B name eth0

ip netns exec $pid ip link set eth0 up

ip netns exec $pid ip addr add $SETIP/$SETMASK dev eth0

ip netns exec $pid ip route add default via $GATEWAY

 

執行如下命令爲容器創建網卡,並分配靜態IP:

./modify_docker_ip.sh 8feff00a0a26 172.17.0.2 16 172.17.42.1 deth0

其中:8feff00a0a26 是容器ID,172.17.0.2是容器的靜態IP,16是掩碼,172.17.42.1是容器的網關地址(即運行容器的系統中docker0的IP),deth0爲新建的宿主機網卡名(對應容器內的eth0)

此時查看宿主機IP:

[root@node0003 ~]# ifconfig            
deth0     Link encap:Ethernet  HWaddr DA:19:96:9B:1B:E5 
          inet6 addr: fe80::d819:96ff:fe9b:1be5/64 Scope:Link            
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1            
          RX packets:6 errors:0 dropped:0 overruns:0 frame:0            
          TX packets:6 errors:0 dropped:0 overruns:0 carrier:0            
          collisions:0 txqueuelen:1000            
          RX bytes:468 (468.0 b)  TX bytes:468 (468.0 b)

docker0   Link encap:Ethernet  HWaddr 56:84:7A:FE:97:99 
          inet addr:172.17.42.1  Bcast:0.0.0.0  Mask:255.255.0.0            
          inet6 addr: fe80::5484:7aff:fefe:9799/64 Scope:Link            
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1            
          RX packets:12 errors:0 dropped:0 overruns:0 frame:0            
          TX packets:6 errors:0 dropped:0 overruns:0 carrier:0            
          collisions:0 txqueuelen:0            
          RX bytes:768 (768.0 b)  TX bytes:468 (468.0 b)

 

 

在容器內查看IP:

root@yh:/# ifconfig -a            
eth0      Link encap:Ethernet  HWaddr 22:e1:72:17:b6:dd 
          inet addr:172.17.0.2  Bcast:0.0.0.0  Mask:255.255.0.0            
          inet6 addr: fe80::20e1:72ff:fe17:b6dd/64 Scope:Link            
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1            
          RX packets:3 errors:0 dropped:0 overruns:0 frame:0            
          TX packets:3 errors:0 dropped:0 overruns:0 carrier:0            
          collisions:0 txqueuelen:1000            
          RX bytes:238 (238.0 B)  TX bytes:238 (238.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)

附錄

Docker關於網絡這塊的文檔詳見:https://docs.docker.com/articles/networking/

另外一個工具pipework也可以設置靜態IP:https://github.com/jpetazzo/pipework

 

遺留問題

問題:docker容器重啓之後,eth0消失,IP失效。

描述:docker文檔中描述:容器stop的時候,docker自動清理網卡配置,所以重啓之後容器內的eth0消失,靜態IP也就失效了。

解決方法:1. run一個docker容器之後,再次執行文中的腳本或者pipework重新設置IP即可。 2. 可能還有更好的辦法,待研究。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章