docker(18):用PIPEWORK为docker容器配置独立IP

docker(18):用PIPEWORK为docker容器配置独立IP

参考文档:http://blog.csdn.net/design321/article/details/48264825

官方网站:https://github.com/jpetazzo/pipework

宿主环境:centos7.6

1  安装pipework

git clone https://github.com/jpetazzo/pipework.git
cd pipework/
cp pipework /usr/local/bin/
chmod +x /usr/local/bin/pipework

 

2  配置网桥连接

2.1  安装工具

yum install bridge-utils.x86_64 -y

 

2.2 修改网卡配置,实现桥接

给宿主机创建网桥

[root@docker-136 ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33 
DEVICE=ens33
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=static
BROADCAST=192.168.0.255 #(前三位要和主机的ip地址一致,后一位为255)
DNS1=114.114.114.114
IPADDR=192.168.0.136 #(虚拟机的IP地址,前三位与主机的一致)
NETMASK=255.255.255.0
GATEWAY=192.168.0.1 #(主机的默认网关地址)
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=yes
IPV6INIT=yes
BRIDGE=br0  #增加这个

 

设置物理网卡桥接到网桥

[root@docker-136 ~]# cat /etc/sysconfig/network-scripts/ifcfg-br0 
TYPE=Bridge
BOOTPROTO=static
NAME=br0
DEVICE=br0
ONBOOT=yes
IPADDR=192.168.0.136
NETMASK=255.255.255.0
GATEWAY=192.168.0.1
DNS1=114.114.114.114

 

重启网络

systemctl restart network

 

3 运行容器

[root@docker-136 ~]# pipework br0 $(docker run -d -it -p 80:80  --name http-199 httpd:2.2) 192.168.0.199/24@192.168.0.1
WARNING: IPv4 forwarding is disabled. Networking will not work.
[root@docker-136 ~]# ping 192.168.0.199
PING 192.168.0.199 (192.168.0.199) 56(84) bytes of data.
64 bytes from 192.168.0.199: icmp_seq=1 ttl=64 time=0.557 ms
64 bytes from 192.168.0.199: icmp_seq=2 ttl=64 time=0.099 ms
^C
--- 192.168.0.199 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 0.099/0.328/0.557/0.229 ms
[root@docker-136 ~]# curl 192.168.0.199
<html><body><h1>It works!</h1></body></html>[root@docker-136 ~]# 
[root@docker-136 ~]#

 

 

 

 

4 重启容器需要重新指定IP

[root@docker-136 ~]# docker stop 30a129173f2d
30a129173f2d
[root@docker-136 ~]# docker start 30a129173f2d
30a129173f2d
[root@docker-136 ~]# curl 192.168.0.199          
^C
[root@docker-136 ~]# ping 192.168.0.199
PING 192.168.0.199 (192.168.0.199) 56(84) bytes of data.
^C
--- 192.168.0.199 ping statistics ---
5 packets transmitted, 0 received, 100% packet loss, time 4002ms

[root@docker-136 ~]# docker ps -a
CONTAINER ID        IMAGE                                      COMMAND                  CREATED             STATUS              PORTS                                                                            NAMES
30a129173f2d        httpd:2.2                                  "httpd-foreground"       6 minutes ago       Up About a minute   0.0.0.0:80->80/tcp                                                               http-199
6ee8323663bd        192.168.0.136:5000/centos-7-ssh-nginx:v1   "/bin/sh"                54 minutes ago      Up 52 minutes       80/tcp                                                                           web-1
77fe3eb86e34        progrium/consul                            "/bin/start -server …"   2 hours ago         Up About an hour    53/tcp, 53/udp, 8300-8302/tcp, 8400/tcp, 8301-8302/udp, 0.0.0.0:8500->8500/tcp   consul
[root@docker-136 ~]# pipework br0 http-199  192.168.0.199/24@192.168.0.1
[root@docker-136 ~]# ping 192.168.0.199
PING 192.168.0.199 (192.168.0.199) 56(84) bytes of data.
64 bytes from 192.168.0.199: icmp_seq=1 ttl=64 time=0.627 ms
64 bytes from 192.168.0.199: icmp_seq=2 ttl=64 time=0.125 ms
^C
--- 192.168.0.199 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 0.125/0.376/0.627/0.251 ms
[root@docker-136 ~]# curl 192.168.0.199    
<html><body><h1>It works!</h1></body></html>[root@docker-136 ~]#

 

posted on 2019-08-16 15:58  光阴8023  阅读(742)  评论(0编辑  收藏  举报