為Docker容器設定靜態IP

來源:互聯網
上載者:User

標籤:log   ott   tns   odi   roc   ast   juno   ati   如何   

此文已由作者袁歡授權網易雲社區發布。

歡迎訪問網易雲社區,瞭解更多網易技術產品運營經驗。


建立docker容器

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

docker ps

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


[email protected]:/# 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

宿主機是CentOS6.6,為了支援ip netns命令,需要進行升級:

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

建立一個修改靜態IP的指令碼modify_docker_ip.sh,內容如下:


#/bin/bashif [ -z $1 ] || [ -z $2 ] || [ -z $3 ] || [ -z $4 ] || [ -z $5 ];thenecho "Usage: $0 CONTAINERID/CONTAINER_NAME 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"        echo "       Call the script like: sh manual_con_static_ip.sh  my_container 192.168.5.123 24 192.168.5.1 deth0"        exitfi  CONTAINERID_NAME=$1SETIP=$2SETMASK=$3GATEWAY=$4ETHNAME=$5 #判斷宿主機網卡是否存在ifconfig $ETHNAME > /dev/null 2>&1if [ $? -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    fifi#pid=`docker inspect -f ‘{{.State.Pid}}‘ $CONTAINERID_NAME`echo pid=$pidmkdir -p /var/run/netnsfind -L /var/run/netns -type l -delete if [ -f /var/run/netns/$pid ]; then    rm -f /var/run/netns/$pidfiln -s /proc/$pid/ns/net /var/run/netns/$pid#ip link add $ETHNAME type veth peer name Bbrctl addif docker0 $ETHNAMEip link set $ETHNAME upip link set B netns $pid#先刪除容器內已存在的eth0ip netns exec $pid ip link del eth0 > /dev/null 2>&1#設定容器新的網卡eth0ip netns exec $pid ip link set dev B name eth0ip netns exec $pid ip link set eth0 upip netns exec $pid ip addr add $SETIP/$SETMASK dev eth0ip 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:

[[email protected] ~]# 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:

[email protected]:/# 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. 可能還有更好的辦法,待研究。


網易雲Container Service為使用者提供了無伺服器容器,讓企業能夠快速部署業務,輕鬆營運服務。Container Service支援Auto Scaling、垂直擴容、灰階升級、服務發現、服務編排、錯誤恢複及效能監測等功能。


免費體驗雲安全(易盾)Alibaba Content Security Service、驗證碼等服務

更多網易技術、產品、運營經驗分享請點擊。




相關文章:
【推薦】 分布式儲存系統可靠性系列一:如何估算

為Docker容器設定靜態IP

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.