Docker使用自定义网桥_docker

摘要: 本文讲的是Docker使用自定义网桥docker, Docker 服务默认会创建一个 docker0 网桥,它在内核层连通了其他的物理或虚拟网卡,这就将所有容器和本地主机都放到同一个物理网络。 用户也可以指定网桥来连接各个容器。


网桥和docker配置


1.配置Br0网桥

[chenxk@localhost ~]$ vim /etc/sysconfig/network-scripts/ifcfg-br0

DEVICE="br0"

ONBOOT="yes"

TYPE="Bridge"

BOOTPROTO=static

IPADDR=192.168.15.49

NETMASK=255.255.255.0

GATEWAY=192.168.15.1

DNS1=192.168.15.1

DEFROUTE=yes


2.设置物理网卡ifcfg-eno16777736;


DEVICE=ifcfg-eno16777736

HWADDR=00:0C:29:DB:B2:28

TYPE=Ethernet

UUID=b2268aab-fa2e-49e9-bd67-2572f29e5790

ONBOOT=yes

NM_CONTROLLED=yes

BOOTPROTO=static

#IPADDR=192.168.15.49

#NETMASK=255.255.255.0

#GATEWAY=192.168.15.1

DNS1=192.168.15.1

BRIDGE=br0  --这个很重要


3.安装bridge-utils工具包

yum  install bridge-utils

4.查看网桥信息

brctl show


5.设置docker服务启动参数

这里要注意的是,不同的Linux操作系统docker的配置文件所在不同

centos 在/etc/sysconfig/docker

其他操作系统请前往下面的网址

https://docs.docker.com/installation/#installation

centos 6:

~#:vim /etc/sysconfig/docker #在OPTIONS='--selinux-enabled'这行中修改为OPTIONS='--selinux-enabled -b=br0'即让docker服务启动时使用br0网卡进行桥接

centos 7:

Configure Docker to use the new bridge by setting the option in the daemon.json file, which is located in /etc/docker/ on Linux or C:\ProgramData\docker\config\ on Windows Server. On Docker for Mac or Docker for Windows, click the Docker icon, choose Preferences, and go to Daemon.

If the daemon.json file does not exist, create it. Assuming there are no other settings in the file, it should have the following contents:

{

"bridge": "br0"

}

6.启动docker服务

~#:service docker start

7.安装pipework

~#:git clone https://github.com/jpetazzo/pipework

~#:cp ~/pipework/pipework /usr/local/bin/

8.启动一个手动设置网络的容器

这里最好不要让docker自动获取ip,下次启动会有变化而且自动获取的ip可能会和物理网段中的ip冲突

~#:docker run -itd --net=none --name=test centos7 /bin/bash

9.为test容器设置一个与桥接物理网络同地址段的ip@网关

~#:pipework br0 test 192.168.184.11/[email protected]

10.进入容器查看ip

~#:docker attach test




PipeWork设置Docker网络

由于要设置Docker 容器网络地址,下面把自己的配置过程记录以下,和大家一起分享。

首先,本次配置采用PipeWork 固定容器IP,需要用到上一篇讲的Centos7网桥设置:http://blog.csdn.NET/wd_boy/article/details/60959431

好了,接下里讲PipeWork的设置

1.下载PipeWoke

下载地址:https://github.com/jpetazzo/pipework.git

2.启动两个容器

docker run -itd --name test1 --net=none centos /bin/bash

docker run -itd --name test2 --net=none centos /bin/bash

3.设置容器IP

pipework br0 test2 192.168.15.244/[email protected]

pipework br0 test1 192.168.15.243/[email protected]

其中@后面的ip为Docker容器宿主机的网关

4.为了解决每次重启宿主机都需要配置网桥和绑定容器ip,在rc.local中配置

vim /etc/rc.d/init.d/rc.local

具体内容:

service docker start;

docker start test1;

docker start test2;

pipework br0 test2 192.168.15.244/[email protected];

pipework br0 test1 192.168.15.243/[email protected];

当然要给rc.local可执行权限: chmod +x rc.local

另外需要注意的是,在执行rc.local的时候系统里面的path变量没有初始化完成,所以需要用命令的完全路径

如果不知道命令路径位置,使用which,比如 which docker  就会显示docker所在的路径

本文参考:http://blog.csdn.net/chinagissoft/article/details/51250839

http://blog.csdn.Net/chinagissoft/article/details/51251982

-------------------------------------------------------------------------------------------------------------------------

报错处理:

在通过pipework 给docker容器分配IP的时候, 系统报出以下错误:

Object "netns" is unknown, try "ip help".

该错误是由于系统版本暂时不支持namespaces, 可通过更新内核或者iproute的方法获取对namespaces的支持.更新方法如下:

添加yum源:

[cce]

cat /etc/yum.repos.d/rdo.repo

[openstack-kilo]

name=OpenStack Kilo Repository

baseurl=https://repos.fedorapeople.org/repos/openstack/EOL/openstack-icehouse/epel-6/

skip_if_unavailable=0

enabled=1

gpgcheck=0

gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-RDO-kilo

## 接下来更新iproute即可:

# yum update iproute -y

-------------------------------------------------------------------------------------------------------------

你可能感兴趣的:(Docker使用自定义网桥_docker)