docker虚拟网桥实现固定IP,容器互通,外网可用
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了docker虚拟网桥实现固定IP,容器互通,外网可用相关的知识,希望对你有一定的参考价值。
提示:Docker不再兼容CentOS6,在使用docker的时候请将系统进行升级
CentOS最低版本7.0 kernel最低3.10.0
注意关闭SElinux和iptables等防火墙
一、更新阿里云CentOS源,安装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镜像并启动,安装网络工具后重新进行封装为镜像
1、下载centos镜像:
[[email protected] 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] ... [[email protected] 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 [[email protected] home]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE docker.io/centos latest 3fa822599e10 6 days ago 203.5 MB
2、进入容器安装网络工具:
[[email protected] home]# docker run -itd --name=centos_net 3fa /bin/bash c7bc119c8387e133b37aa5ff713074529b51f2d6303780310b6ba520eeef6a79 [[email protected] home]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c7bc119c8387 3fa "/bin/bash" 4 seconds ago Up 4 seconds centos_net [[email protected] home]# docker attach c7b [[email protected] /]# yum -y install net-tools iproute ... 等待yum安装完成(Ctrl + P + Q)快捷键回到宿主主机,容器后台继续运行
3、重新封装为镜像:
[[email protected] home]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c7bc119c8387 3fa "/bin/bash" 3 minutes ago Up 3 minutes centos_net [[email protected] home]# docker commit c7b centos_net sha256:7a04c40fc8f8a727d2d3467fc3a75d8a5c3639cf0ad27f35b53c0f67f3783afc [[email protected] 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 [[email protected] home]# docker stop c7b && docker rm c7b c7b c7b
三、创建虚拟网桥并设置为默认网桥
1、安装pipework
[[email protected] home]# mkdir -p /home/pipework && cd /home/pipework [[email protected] pipework]# yum -y install git ... [[email protected] 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. [[email protected] pipework]# ls pipework [[email protected] pipework]# ls ./pipework/ docker-compose.yml doctoc LICENSE pipework pipework.spec README.md [[email protected] pipework]# cp -rp pipework/pipework /usr/local/bin/
参考 http://blog.csdn.net/xu470438000/article/details/50512442
2、创建虚拟网桥
安装bridge管理工具 [[email protected] pipework]# yum -y install bridge-utils 停止docker服务 [[email protected] pipework]# systemctl stop docker 查看网卡状态 [[email protected] 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网桥 [[email protected] pipework]# ip link set dev docker0 down 删除该docker0网桥 [[email protected] pipework]# brctl delbr docker0 查看,已不存在了,删除成功 [[email protected] 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虚拟网桥 [[email protected] pipework]# brctl addbr br0 启动br0虚拟网桥 [[email protected] pipework]# ip link set dev br0 up 为该虚拟网桥设置ip和掩码 [[email protected] pipework]# ip addr add 192.168.1.1/24 dev br0 查看,设置成功 [[email protected] 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
以上是关于docker虚拟网桥实现固定IP,容器互通,外网可用的主要内容,如果未能解决你的问题,请参考以下文章