docker高级应用之单机持久化固定容器IP
过完年了,快1个月没有写博客,现在给大家分享一下我是如何在单机环境,实现持久化固定容器IP。 我是去年6月份开始玩docker,到现在已经9个月了,docker的最新版本也到了1.5.目前我这里对docker已经放到测试环境给研发使用,目前感觉最大的问题就是容器手动重启或者异常关闭后在启动容器,那么容器的ip就回变化(这个是使用默认的docker0做桥接),如果使用pipework手动配置ip,也得重新在来一遍,很是麻烦,所以我写了一个程序来自动化的解决这个问题。 一、程序介绍 使用到的程序有2个: 1、创建容器程序,名字是create_docker_container_use_static_ip.sh,使用shell脚本编写。 2、监控容器状态并自动给重启没有ip的容器设置ip,名字是auto_check_modify_container.py,使用python编写。 创建容器程序的逻辑: 1、运行创建脚本来创建docker container; 2、创建的信息会写入到etcd里进行保存; 3、根据配置信息来创建具体的container。 其中我程序使用openvswitch来坐桥接,并对不同vlan做不同的网段设置。 监控容器并给自动重启设置ip程序逻辑: 1、从etcd里获取容器的信息; 2、根据第一步的信息,进行nmap扫描ssh服务是否启动,未启动的转给下一步; 3、收到上一步的ip,然后根据etcd的容器信息进行重新pipework配置之前的固定ip。 下面是创建容器程序脚本内容 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 #!/bin/bash #author:DengLei #email:dl528888@gmail.com #thisscriptiscreatenewdockercontainerandusepipeworkaddstaticip,whencontainerisrestart,italsouselaststaticip container=$1 images=$2 daemon_program=$3 vlan_tag=$4 #ip_pool='172.16.10.1/24' bridge=ovs2 default_vlan=1 default_vlan_ip= '172.16.0.1/24' email= '244979152@qq.com' local_ip=` /sbin/ifconfig | grep 'inet' | grep -Ev '(127|117|211|172|::1|fe)' | awk '{print$2}' | head -n1` if [-z$1]||[-z$2]; then echo "Usage:container_namecontainer_imagevlan_tag(default:1)" echo "Example:Iwantcreatenewdockercontainertest1centos6:latestsupervisordvlan100" echo "Thecommandis:bash`basename$0`test1centos6:latestsupervisord100" exit 1 fi [`docker ps -a| grep - v "NAMES" | awk '{print$NF}' | grep "$container" &>> /dev/null && echo 0|| echo 1`- eq 0]&& echo "Thecontainer$containerisexist!" && exit 1 [` which pipework&> /dev/null && echo 0|| echo 1`- eq 1]&& echo "Ican'tfindpipework,pleaseinstallit!" && exit 1 if [-z$3]; then vlan_tag=${default_vlan} fi #createnewdockercontainer dockerrun--restartalways-d--net= "none" --name= "$container" $images$daemon_program&>> /dev/null #checkopenvswitchbridgeexist if [`ovs-vsctllist-br| grep ${bridge}&> /dev/null && echo 0|| echo 1`- eq 1]; then ovs-vsctladd-br${bridge} fi #setupdefaultvlaninfo check_default_vlan_exist=`etcdctlget /app/docker/ ${local_ip} /vlan ${default_vlan} /gateway 2> /dev/null || echo 1` if [${check_default_vlan_exist}= "1" ]; then ovs-vsctladd-port$bridgevlan${default_vlan}tag=${default_vlan}-- set interfacevlan${default_vlan} type =internal #ifconfigvlan${default_vlan}${default_vlan_ip} iplink set vlan${default_vlan}up ipaddradd$default_vlan_ipdevvlan${default_vlan} etcdctl set /app/docker/ ${local_ip} /vlan ${default_vlan} /nowip $default_vlan_ip&>> /dev/null etcdctl set /app/docker/ ${local_ip} /vlan ${default_vlan} /gateway $default_vlan_ip&>> /dev/null etcdctl set /app/docker/now_vlan_ip $default_vlan_ip&>> /dev/null fi #setupnewvlaninfo new_vlan_gateway_ip=`etcdctlget /app/docker/ ${local_ip} /vlan ${vlan_tag} /gateway 2> /dev/null || echo 1` if [${new_vlan_gateway_ip}= "1" ]; then sleep 1 now_vlan_ip=`etcdctlget /app/docker/now_vlan_ip ` new_vlan_ip=$( awk - v new_ip=` echo $now_vlan_ip| cut -d/-f1| awk -F '.' '{print$1"."$2"."$3+1"."$4}' `- v new_ip_netmask=` echo $now_vlan_ip| cut -d/-f2` 'BEGIN{printnew_ip"/"new_ip_netmask}' ) ovs-vsctladd-port$bridgevlan${vlan_tag}tag=${vlan_tag}-- set interfacevlan${vlan_tag} type =internal iplink set vlan${vlan_tag}up ipaddradd${new_vlan_ip}devvlan${vlan_tag} #ifconfigvlan${vlan_tag}${new_vlan_ip} etcdctl set /app/docker/now_vlan_ip ${new_vlan_ip}&>> /dev/null etcdctl set /app/docker/ ${local_ip} /vlan ${vlan_tag} /gateway ${new_vlan_ip}&>> /dev/null etcdctl set /app/docker/ ${local_ip} /vlan ${vlan_tag} /nowip ${new_vlan_ip}&>> /dev/null new_vlan_gateway_ip=${new_vlan_ip} fi #calculatenewip sleep 1 now_container_ip=`etcdctlget /app/docker/ ${local_ip} /vlan ${vlan_tag} /nowip ` echo "nowcontaineripis:$now_container_ip" new_container_ip=$( awk - v new_ip=` echo $now_container_ip| cut -d/-f1| awk -F '.' '{print$1"."$2"."$3"."$4+1}' `- v new_ip_netmask=` echo $now_container_ip| cut -d/-f2` 'BEGIN{printnew_ip"/"new_ip_netmask}' ) etcdctl set /app/docker/ ${local_ip} /vlan ${vlan_tag} /nowip $new_container_ip&> /dev/null echo "finishcalculatenewip!" create_time=` date + "%Y-%m-%d%T" ` ` which pipework`$bridge$container${new_container_ip}@` echo ${new_vlan_gateway_ip}| awk -F '/' '{print$1}' `@${vlan_tag} etcdctl set /app/docker/ ${local_ip} /vlan ${vlan_tag} /container/ ${container} "{'Physics_ip':'${local_ip}','Container_name':'${container}','Container_ip':'${new_container_ip}','Container_vlan':'${vlan_tag}','Container_vlan_gateway':'${new_vlan_gateway_ip}','Container_create':'${create_time}','Container_status':'running'}" 注意:如果想使用本程序,客户端必须安装openvswitch、etcd与etcdctl、pipework。 监控容器并给自动重启设置ip程序脚本内容: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 #!/usr/bin/envpython #author:DengLei #email:dl528888@gmail.com import sys import nmap import multiprocessing import time import socket,struct,fcntl import etcd import re fromdocker import Client import subprocess defget_local_ip(iface= 'em1' ): sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM) sockfd=sock.fileno() SIOCGIFADDR=0x8915 ifreq=struct.pack( '16sH14s' ,iface,socket.AF_INET, '\x00' *14) try: res=fcntl.ioctl(sockfd,SIOCGIFADDR,ifreq) except: return None ip=struct.unpack( '16sH2x4s8x' ,res)[2] return socket.inet_ntoa(ip) defget_etcd_info(key): etcd_result=[] r=etcd_client. read ( '%s%s' %(docker_etcd_key,local_ip),recursive=True,sorted=True) for child in r.children: if child. dir isnotTrueand "Container_name" in child.value: etcd_result.append(child.value) return etcd_result defcheck_by_nmap(ip,port): check_result=len(nm.scan( '%s' %ip, '%s' %port)[ 'scan' ]) if check_result==0: msg= '%s' %(ip) return msg defdocker_container_stop(): docker_container=docker_client.containers(all=True) container_name=[] container_stop_name=[] for i in docker_container: if re.match( 'Exited' ,i[ 'Status' ]): container_name.append(i[ 'Names' ]) for b in container_name: for c in b: container_stop_name.append(c) return container_stop_name defdocker_container_all(): docker_container=docker_client.containers(all=True) container_name=[] container_stop_name=[] for i in docker_container: container_name.append(i[ 'Names' ]) for b in container_name: for c in b: container_stop_name.append(c) return container_stop_name if __name__== "__main__" : nm=nmap.PortScanner() etcd_client=etcd.Client(host= '127.0.0.1' ,port=4001) docker_client=Client(base_url= 'unix://var/run/docker.sock' ,version= '1.15' ,timeout=10) local_ip=get_local_ip( 'ovs1' ) bridge= 'ovs2' docker_etcd_key= '/app/docker/' port= '22' ip_list=[] now_etcd_result=get_etcd_info(docker_etcd_key) docker_container_stop_name=docker_container_stop() docker_container_all_name=docker_container_all() for i in range(len(now_etcd_result)): now_info= eval (now_etcd_result[i]) key= '%s%s/vlan%s/container/%s' %(docker_etcd_key,local_ip,now_info[ 'Container_vlan' ],now_info[ 'Container_name' ]) if '/' +now_info[ 'Container_name' ]not in docker_container_stop_nameandnow_info[ 'Container_status' ]!= 'delete' : ip_list.append( eval (now_etcd_result[i])[ 'Container_ip' ]. split ( '/' )[0]) if now_info[ 'Container_status' ]== 'stop' : now_info[ 'Container_status' ]= 'running' etcd_client.write(key,now_info) else : now_info[ 'Container_status' ]= 'stop' etcd_client.write(key,now_info) if '/' +now_info[ 'Container_name' ]not in docker_container_all_name: print '%s%swilldelete,becausedockerclienthaddeleted!' %( time .strftime( '%Y-%m-%d%T' ),key) now_info[ 'Container_status' ]= 'delete' etcd_client.write(key,now_info) ip_list=list( set (ip_list)) if len(ip_list)==1: num=1 elif len(ip_list)>=4andlen(ip_list)<=8: num=4 elif len(ip_list)>8andlen(ip_list)<=20: num=8 else : num=15 pool=multiprocessing.Pool(processes=num) scan_result=[] for i in ip_list: pool.apply_async(check_by_nmap,(i,port,)) scan_result.append(pool.apply_async(check_by_nmap,(i,port,))) pool.close() pool. join () result=[] for res in scan_result: if res.get()isnotNone: result.append(res.get()) result=list( set (result)) if len(result)<=0: print '%sEverythingisallright!' % time .strftime( '%Y-%m-%d%T' ) sys. exit (1) else : print '%scontainerneedmodifycontainerstaticipis:%s' %( time .strftime( '%Y-%m-%d%T' ),result) for i in range(len(now_etcd_result)): now_info= eval (now_etcd_result[i]) if (now_info[ 'Container_ip' ]). split ( '/' )[0] in result: modify_container=subprocess.Popen( "`whichpipework`%s%s%s@%s@%s&>>/dev/null&&echo0||echo1" %(bridge,now_info[ 'Container_name' ],now_info[ 'Container_ip' ],now_info[ 'Container_vlan_gateway' ]. split ( '/' )[0],now_info[ 'Container_vlan' ]),shell=True,stdout=subprocess.PIPE) if (modify_container.stdout.readlines()[0]).strip( '\n' )==0: print 'container_name:%scontainer_ip:%smodifystaticipisfail!' %(now_info[ 'Container_name' ],now_info[ 'Container_ip' ]) else : print 'container_name:%scontainer_ip:%smodifystaticipissuccess!' %(now_info[ 'Container_name' ],now_info[ 'Container_ip' ]) 注意:使用本脚本必须安装nmap、etcd、docker模块。 其中python里nmap的安装方法是 1 easy_installpython-nmap 其中python里etcd的安装方法是 安装基础库 1 yum install libffilibffi-develpython-devel 安装程序 1 2 3 gitclonehttps: //github .com /jplana/python-etcd .git cd python-etcd pythonsetup.py install 其中python里docker的安装方法是 1 easy_installdocker-py 二、效果展示 需求: 创建主机test1、test2、test3、test4,需要test1与test2属于vlan100,test3与test4属于vlan300,架构图如下 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ovs2 | | ----------------- || || vlan100(tag100)vlan300(tag300) || || --------------------------------- |||| |||| test1test2test3test4 (tag100)(tag100)(tag300)(tag300) 下面是操作 1、创建ovs1、ovs2网桥,其中em1绑定到ovs1走内网,ovs2是容器的网络 1 2 3 4 5 6 ovs-vsctladd-brovs1 ovs-vsctladd-brovs2 ovs-vsctladd-portovs1em1 iplink set ovs1up ifconfig em10 ifconfig ovs110.10.17.3 2、创建test1、test2、test3、test4的容器 1 2 3 4 5 6 7 8 [root@docker-test3~] #shcreate_docker_container_use_static_ip.shtest1docker.ops-chukong.com:5000/centos6-http:new/usr/bin/supervisord100 { 'Physics_ip' : '10.10.17.3' , 'Container_name' : 'test1' , 'Container_ip' : '172.16.1.2/24' , 'Container_vlan' : '100' , 'Container_vlan_gateway' : '172.16.1.1/24' , 'Container_create' : '2015-03-0214:59:59' , 'Container_status' : 'running' } [root@docker-test3~] #shcreate_docker_container_use_static_ip.shtest2docker.ops-chukong.com:5000/centos6-http:new/usr/bin/supervisord100 { 'Physics_ip' : '10.10.17.3' , 'Container_name' : 'test2' , 'Container_ip' : '172.16.1.3/24' , 'Container_vlan' : '100' , 'Container_vlan_gateway' : '172.16.1.1/24' , 'Container_create' : '2015-03-0215:00:09' , 'Container_status' : 'running' } [root@docker-test3~] #shcreate_docker_container_use_static_ip.shtest3docker.ops-chukong.com:5000/centos6-http:new/usr/bin/supervisord300 { 'Physics_ip' : '10.10.17.3' , 'Container_name' : 'test3' , 'Container_ip' : '172.16.2.2/24' , 'Container_vlan' : '300' , 'Container_vlan_gateway' : '172.16.2.1/24' , 'Container_create' : '2015-03-0215:00:22' , 'Container_status' : 'running' } [root@docker-test3~] #shcreate_docker_container_use_static_ip.shtest4docker.ops-chukong.com:5000/centos6-http:new/usr/bin/supervisord300 { 'Physics_ip' : '10.10.17.3' , 'Container_name' : 'test4' , 'Container_ip' : '172.16.2.3/24' , 'Container_vlan' : '300' , 'Container_vlan_gateway' : '172.16.2.1/24' , 'Container_create' : '2015-03-0215:00:31' , 'Container_status' : 'running' } 这个脚本第一个参数是创建的容器名,第二个参数是docker images,第三个参数是运行命令,第四个参数是vlan。 下面是当前docker信息 1 2 3 4 5 6 [root@docker-test3~] #dockerps-a CONTAINERIDIMAGECOMMANDCREATEDSTATUSPORTSNAMES 386b4180b3d0docker.ops-chukong.com:5000 /centos6-http :new" /usr/bin/supervisor AboutaminuteagoUpAboutaminutetest4 92fee54ec498docker.ops-chukong.com:5000 /centos6-http :new" /usr/bin/supervisor AboutaminuteagoUpAboutaminutetest3 4dc84301894adocker.ops-chukong.com:5000 /centos6-http :new" /usr/bin/supervisor AboutaminuteagoUpAboutaminutetest2 45d4a73c6ed0docker.ops-chukong.com:5000 /centos6-http :new" /usr/bin/supervisor 2minutesagoUp2minutestest1 下面是openvswitch的信息 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 [root@docker-test3~] #ovs-vsctlshow d895d78b-8c89-49bc-b429-da6a4a2dcb3a Bridge "ovs2" Port "vlan300" tag:300 Interface "vlan300" type :internal Port "ovs2" Interface "ovs2" type :internal Port "veth1pl11491" tag:100 Interface "veth1pl11491" Port "veth1pl12003" tag:300 Interface "veth1pl12003" Port "vlan100" tag:100 Interface "vlan100" type :internal Port "veth1pl12251" tag:300 Interface "veth1pl12251" Port "veth1pl11788" tag:100 Interface "veth1pl11788" Port "vlan1" tag:1 Interface "vlan1" type :internal Bridge "ovs1" Port "em1" Interface "em1" Port "ovs1" Interface "ovs1" type :internal ovs_version: "2.3.1" 下面是etcd的信息 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 [root@docker-test3~] #etcdctlls/--recursive|grepdocker|grep"10.10.17.3/" /app/docker/ip/vlan/10 .10.17.3 /gate_way /app/docker/ip/vlan/10 .10.17.3 /now_ip /app/docker/10 .10.17.3 /vlan300 /app/docker/10 .10.17.3 /vlan300/container /app/docker/10 .10.17.3 /vlan300/container/test3 /app/docker/10 .10.17.3 /vlan300/container/test4 /app/docker/10 .10.17.3 /vlan300/gateway /app/docker/10 .10.17.3 /vlan300/nowip /app/docker/10 .10.17.3 /vlan1 /app/docker/10 .10.17.3 /vlan1/gateway /app/docker/10 .10.17.3 /vlan1/nowip /app/docker/10 .10.17.3 /vlan100 /app/docker/10 .10.17.3 /vlan100/gateway /app/docker/10 .10.17.3 /vlan100/nowip /app/docker/10 .10.17.3 /vlan100/container /app/docker/10 .10.17.3 /vlan100/container/test1 /app/docker/10 .10.17.3 /vlan100/container/test2 查看test1的信息 1 2 [root@docker-test3~] #etcdctlget/app/docker/10.10.17.3/vlan100/container/test1 { 'Physics_ip' : '10.10.17.3' , 'Container_name' : 'test1' , 'Container_ip' : '172.16.1.2/24' , 'Container_vlan' : '100' , 'Container_vlan_gateway' : '172.16.1.1/24' , 'Container_create' : '2015-03-0214:59:59' , 'Container_status' : 'running' } 跟创建容器的信息一样 进入容器,然后查看设置ip情况 1 2 3 4 5 6 7 8 [root@docker-test3~] #dockerexectest1ifconfigeth1|grep"inetaddr" inetaddr:172.16.1.2Bcast:0.0.0.0Mask:255.255.255.0 [root@docker-test3~] #dockerexectest2ifconfigeth1|grep"inetaddr" inetaddr:172.16.1.3Bcast:0.0.0.0Mask:255.255.255.0 [root@docker-test3~] #dockerexectest3ifconfigeth1|grep"inetaddr" inetaddr:172.16.2.2Bcast:0.0.0.0Mask:255.255.255.0 [root@docker-test3~] #dockerexectest4ifconfigeth1|grep"inetaddr" inetaddr:172.16.2.3Bcast:0.0.0.0Mask:255.255.255.0 可以看到ip都是之前设置的 然后在test1里ping外网与test2 1 2 3 4 5 6 7 8 9 10 11 12 13 14 [root@docker-test3~] #dockerexectest1ping-c1www.baidu.com PINGwww.a.shifen.com(180.149.131.205)56(84)bytesofdata. 64bytesfrom180.149.131.205:icmp_seq=1ttl=54 time =1.94ms ---www.a.shifen.com ping statistics--- 1packetstransmitted,1received,0%packetloss, time 12ms rttmin /avg/max/mdev =1.948 /1 .948 /1 .948 /0 .000ms [root@docker-test3~] #dockerexectest1ping-c1172.16.1.3 PING172.16.1.3(172.16.1.3)56(84)bytesofdata. 64bytesfrom172.16.1.3:icmp_seq=1ttl=64 time =0.431ms ---172.16.1.3 ping statistics--- 1packetstransmitted,1received,0%packetloss, time 0ms rttmin /avg/max/mdev =0.431 /0 .431 /0 .431 /0 .000ms 可以看到都是通的 在从test3里ping外网与test4 1 2 3 4 5 6 7 8 9 10 11 12 13 14 [root@docker-test3~] #dockerexectest3ping-c1www.baidu.com PINGwww.a.shifen.com(180.149.131.236)56(84)bytesofdata. 64bytesfrom180.149.131.236:icmp_seq=1ttl=54 time =2.19ms ---www.a.shifen.com ping statistics--- 1packetstransmitted,1received,0%packetloss, time 12ms rttmin /avg/max/mdev =2.197 /2 .197 /2 .197 /0 .000ms [root@docker-test3~] #dockerexectest3ping-c1172.16.2.3 PING172.16.2.3(172.16.2.3)56(84)bytesofdata. 64bytesfrom172.16.2.3:icmp_seq=1ttl=64 time =0.504ms ---172.16.2.3 ping statistics--- 1packetstransmitted,1received,0%packetloss, time 0ms rttmin /avg/max/mdev =0.504 /0 .504 /0 .504 /0 .000ms 也是通的 3、关闭test1并测试 1 2 3 4 5 6 7 8 9 10 11 12 13 [root@docker-test3~] #dockerstoptest1 test1 [root@docker-test3~] #dockerps-a CONTAINERIDIMAGECOMMANDCREATEDSTATUSPORTSNAMES 386b4180b3d0docker.ops-chukong.com:5000 /centos6-http :new" /usr/bin/supervisor 12minutesagoUp12minutestest4 92fee54ec498docker.ops-chukong.com:5000 /centos6-http :new" /usr/bin/supervisor 12minutesagoUp12minutestest3 4dc84301894adocker.ops-chukong.com:5000 /centos6-http :new" /usr/bin/supervisor 12minutesagoUp12minutestest2 45d4a73c6ed0docker.ops-chukong.com:5000 /centos6-http :new" /usr/bin/supervisor 12minutesagoExited(0)2secondsagotest1 [root@docker-test3~] #ping-c2172.16.1.2 PING172.16.1.2(172.16.1.2)56(84)bytesofdata. ---172.16.1.2 ping statistics--- 2packetstransmitted,0received,100%packetloss, time 999ms 可以看到test1关闭了,给予的ip也不能通了,下面启动容器,然后使用我自动检测与重新设置容器ip 1 2 3 4 5 6 7 8 9 10 11 [root@docker-test3~] #dockerstarttest1 test1 [root@docker-test3~] #dockerps-a CONTAINERIDIMAGECOMMANDCREATEDSTATUSPORTSNAMES 386b4180b3d0docker.ops-chukong.com:5000 /centos6-http :new" /usr/bin/supervisor 13minutesagoUp13minutestest4 92fee54ec498docker.ops-chukong.com:5000 /centos6-http :new" /usr/bin/supervisor 13minutesagoUp13minutestest3 4dc84301894adocker.ops-chukong.com:5000 /centos6-http :new" /usr/bin/supervisor 13minutesagoUp13minutestest2 45d4a73c6ed0docker.ops-chukong.com:5000 /centos6-http :new" /usr/bin/supervisor 13minutesagoUp2secondstest1 [root@docker-test3~] #pythonauto_check_modify_container.py 2015-03-0215:14:03containerneedmodifycontainerstaticipis:[ '172.16.1.2' ] container_name:test1container_ip:172.16.1.2 /24 modifystaticipissuccess! 可以看到启动容器后,程序自动搜集之前的ip信息,并重新配置了 在test1里测试 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 [root@docker-test3~] #dockerexectest1ifconfigeth1|grep"inetaddr" inetaddr:172.16.1.2Bcast:0.0.0.0Mask:255.255.255.0 [root@docker-test3~] #dockerexectest1ping-c1www.baidu.com PINGwww.a.shifen.com(180.149.131.236)56(84)bytesofdata. 64bytesfrom180.149.131.236:icmp_seq=1ttl=54 time =2.18ms ---www.a.shifen.com ping statistics--- 1packetstransmitted,1received,0%packetloss, time 12ms rttmin /avg/max/mdev =2.183 /2 .183 /2 .183 /0 .000ms [root@docker-test3~] #dockerexectest1ping-c1172.16.1.3 PING172.16.1.3(172.16.1.3)56(84)bytesofdata. 64bytesfrom172.16.1.3:icmp_seq=1ttl=64 time =0.382ms ---172.16.1.3 ping statistics--- 1packetstransmitted,1received,0%packetloss, time 0ms rttmin /avg/max/mdev =0.382 /0 .382 /0 .382 /0 .000ms 测试都没有问题 大家以后使用的时候,可以先使用我的程序创建容器,然后把检测程序放到crontab里,每分钟自动运行,这样就能实现持久化固定ip。 本文转自 reinxu 51CTO博客,原文链接:http://blog.51cto.com/dl528888/1616527,如需转载请自行联系原作者