摒弃世俗浮躁
追求技术精湛

docker虚拟网桥实现固定IP,容器互通,外网可用

提示:
Docker不再兼容CentOS6,在使用docker的时候请将系统进行升级
CentOS最低版本7.0   kernel最低3.10.0
注意关闭SElinux、iptables、firewalld等防护措施

一、更新阿里云yum源,安装Docker

备份yum源
# mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup

下载阿里云yum源
# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

更新缓存
# yum clean all && yum makecache

下载epel
# yum -y install epel-release

更新缓存
# yum clean all && yum makecache

安装docker
# yum -y install docker-io

启动docker服务
# systemctl start docker

测试docker状态
# docker info

参考 http://mirrors.aliyun.com/help/centos
参考 https://help.aliyun.com/document_detail/51853.html


二、下载centos镜像并启动,安装网络工具后重新进行封装为镜像

2.1、下载centos镜像:

[root@aliyun home]# docker search centos
INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED
docker.io docker.io/centos The official build of CentOS. 3857 [OK]
docker.io docker.io/ansible/centos7-ansible Ansible on Centos7 103 [OK]
docker.io docker.io/jdeathe/centos-ssh CentOS-6 6.9 x86_64 / CentOS-7 7.4.1708 x8... 90 [OK]
docker.io docker.io/consol/centos-xfce-vnc Centos container with "headless" VNC sessi... 35 [OK]
docker.io docker.io/tutum/centos Simple CentOS docker image with SSH access 34
docker.io docker.io/imagine10255/centos6-lnmp-php56 centos6-lnmp-php56 33 [OK]
...

[root@aliyun home]# docker pull docker.io/centos
Using default tag: latest
Trying to pull repository docker.io/library/centos ...
latest: Pulling from docker.io/library/centos
85432449fd0f: Pull complete
Digest: sha256:3b1a65e9a05f0a77b5e8a698d3359459904c2a354dc3b25ae2e2f5c95f0b3667

[root@aliyun home]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/centos latest 3fa822599e10 6 days ago 203.5 MB

2.2、进入容器安装网络工具:

[root@aliyun home]# docker run -itd --name=centos_net 3fa /bin/bash
c7bc119c8387e133b37aa5ff713074529b51f2d6303780310b6ba520eeef6a79

[root@aliyun home]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c7bc119c8387 3fa "/bin/bash" 4 seconds ago Up 4 seconds centos_net

[root@aliyun home]# docker attach c7b
[root@c7bc119c8387 /]# yum -y install net-tools iproute
...

等待yum安装完成(Ctrl + P + Q)快捷键回到宿主主机,容器后台继续运行

2.3、重新封装为镜像:

[root@aliyun home]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c7bc119c8387 3fa "/bin/bash" 3 minutes ago Up 3 minutes centos_net

[root@aliyun home]# docker commit c7b centos_net
sha256:7a04c40fc8f8a727d2d3467fc3a75d8a5c3639cf0ad27f35b53c0f67f3783afc

[root@aliyun home]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos_net latest 7a04c40fc8f8 10 seconds ago 288.6 MB
docker.io/centos latest 3fa822599e10 6 days ago 203.5 MB
[root@aliyun home]# docker stop c7b && docker rm c7b
c7b
c7b

三、创建虚拟网桥并设置为默认网桥

3.1、安装pipework

[root@aliyun home]# mkdir -p /home/pipework && cd /home/pipework

[root@aliyun pipework]# yum -y install git
...

[root@aliyun pipework]# git clone https://github.com/jpetazzo/pipework.git
Cloning into 'pipework'...
remote: Counting objects: 501, done.
remote: Total 501 (delta 0), reused 0 (delta 0), pack-reused 501
Receiving objects: 100% (501/501), 172.97 KiB | 0 bytes/s, done.
Resolving deltas: 100% (264/264), done.

[root@aliyun pipework]# ls
pipework

[root@aliyun pipework]# ls ./pipework/
docker-compose.yml doctoc LICENSE pipework pipework.spec README.md

[root@aliyun pipework]# cp -rp pipework/pipework /usr/local/bin/

参考 http://blog.csdn.net/xu470438000/article/details/50512442

3.2、创建虚拟网桥

安装bridge管理工具
[root@aliyun pipework]# yum -y install bridge-utils

停止docker服务
[root@aliyun pipework]# systemctl stop docker

查看网卡状态
[root@aliyun pipework]# ip a s
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 1000
    link/ether 00:16:3e:04:3b:69 brd ff:ff:ff:ff:ff:ff
    inet 172.31.43.125/20 brd 172.31.47.255 scope global eth0
       valid_lft forever preferred_lft forever
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN 
    link/ether 02:42:2f:aa:d4:c5 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 scope global docker0
       valid_lft forever preferred_lft forever
       
停止自带的docker0网桥
[root@aliyun pipework]# ip link set dev docker0 down

删除该docker0网桥
[root@aliyun pipework]# brctl delbr docker0

查看,已不存在了,删除成功
[root@aliyun pipework]# ip a s
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 1000
    link/ether 00:16:3e:04:3b:69 brd ff:ff:ff:ff:ff:ff
    inet 172.31.43.125/20 brd 172.31.47.255 scope global eth0
       valid_lft forever preferred_lft forever
       
创建br0虚拟网桥
[root@aliyun pipework]# brctl addbr br0

启动br0虚拟网桥
[root@aliyun pipework]# ip link set dev br0 up

为该虚拟网桥设置ip和掩码
[root@aliyun pipework]# ip addr add 192.168.1.1/24 dev br0

查看,设置成功
[root@aliyun pipework]# ip a s
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 1000
    link/ether 00:16:3e:04:3b:69 brd ff:ff:ff:ff:ff:ff
    inet 172.31.43.125/20 brd 172.31.47.255 scope global eth0
       valid_lft forever preferred_lft forever
6: br0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN qlen 1000
    link/ether 22:e9:84:47:82:c8 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.1/24 scope global br0
       valid_lft forever preferred_lft forever

3.3、设置br为默认网桥

[root@aliyun pipework]# vi /etc/sysconfig/docker-network

[root@aliyun pipework]# cat /etc/sysconfig/docker-network
# /etc/sysconfig/docker-network
DOCKER_NETWORK_OPTIONS="-b=br0"

启动docker服务
[root@aliyun pipework]# systemctl start docker

四、启动容器并分配IP

[root@aliyun pipework]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos_net          latest              7a04c40fc8f8        2 hours ago         288.6 MB
docker.io/centos    latest              3fa822599e10        6 days ago          203.5 MB

[root@aliyun pipework]# docker run --name=centos --net=none -itd 3fa /bin/bash
7eb78b6b4e103f0f8b0ce70559b20925890ab8b2f2b30e2a848f45e21873c01e

[root@aliyun pipework]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
7eb78b6b4e10        3fa                 "/bin/bash"         4 seconds ago       Up 3 seconds                            centos

[root@aliyun pipework]# brctl show
bridge name	bridge id		STP enabled	interfaces
br0		8000.000000000000	no		
docker0		8000.0242b580373f	no		

[root@aliyun pipework]# pipework br0 -i eth0 7eb 192.168.1.50/24@192.168.1.1
[root@aliyun pipework]# brctl show
bridge name	bridge id		STP enabled	interfaces
br0		8000.12056d403390	no		veth0pl15082
docker0		8000.0242b580373f	no		

[root@aliyun pipework]# docker attach 7eb

[root@7eb78b6b4e10 /]# ping www.baidu.com
PING www.a.shifen.com (220.181.111.188) 56(84) bytes of data.
64 bytes from 220.181.111.188 (220.181.111.188): icmp_seq=1 ttl=50 time=17.4 ms
64 bytes from 220.181.111.188 (220.181.111.188): icmp_seq=2 ttl=50 time=17.4 ms
64 bytes from 220.181.111.188 (220.181.111.188): icmp_seq=3 ttl=50 time=17.4 ms
^C
--- www.a.shifen.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2002ms
rtt min/avg/max/mdev = 17.440/17.462/17.481/0.016 ms

[root@7eb78b6b4e10 /]# ping 192.168.1.1
PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.
64 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=0.064 ms
64 bytes from 192.168.1.1: icmp_seq=2 ttl=64 time=0.057 ms
64 bytes from 192.168.1.1: icmp_seq=3 ttl=64 time=0.057 ms
^C
--- 192.168.1.1 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 1999ms
rtt min/avg/max/mdev = 0.057/0.059/0.064/0.007 ms

[root@7eb78b6b4e10 /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.50  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::7c7c:4aff:fe15:e0df  prefixlen 64  scopeid 0x20<link>
        ether 7e:7c:4a:15:e0:df  txqueuelen 1000  (Ethernet)
        RX packets 2124  bytes 10826617 (10.3 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1717  bytes 116914 (114.1 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

参考 https://www.linuxea.com/1466.html
参考 http://blog.csdn.net/xu470438000/article/details/50512442

赞(3) 打赏
未经允许不得转载:时光日记 » docker虚拟网桥实现固定IP,容器互通,外网可用

评论 抢沙发

评论前必须登录!

 

觉得文章有用就打赏一下文章作者

非常感谢你的打赏,我们将继续给力更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫

微信扫一扫

登录

找回密码

注册