Centos7 Docker 多主机 容器互连--基于OVS
Posted Hey!Linux.
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Centos7 Docker 多主机 容器互连--基于OVS相关的知识,希望对你有一定的参考价值。
来一张自己画的图,mark:2016年6月27日17:09:14
自己理解,如有错误 多谢指教。
centos7, 部署OVS和docker、以及基于centos6.8的ssh images 命令。
#!/bin/bash #auther :V yum upgrade -y sed -i \'/SELINUX/s/enforcing/disabled/\' /etc/selinux/config systemctl stop firewalld.service && systemctl disable firewalld.service cat>>/etc/yum.repos.d/docker.repo<< V [docker-repo] name= docker repo baseurl=https://yum.dockerproject.org/repo/main/centos/7 enabled=1 gpgcheck=1 gpgkey=https://yum.dockerproject.org/gpg V yum install docker-engine -y chkconfig docker on systemctl enable docker.service systemctl preset-all systemctl start docker.service systemctl enable docker.service docker pull centos:centos6.8 mkdir ssh cd ssh yum -y install vim cat>> Dockerfile<<V #---------ssh! FROM centos:centos6.8 MAINTAINER \'shiyiwen@anjubao.com\' ENV TERM xterm RUN yum -y install openssh-server;yum clean all RUN mkdir /var/run/sshd RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key RUN ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key RUN /bin/echo \'root:anjubao.com\'|chpasswd RUN /bin/sed -i \'s/.*session.*required.*pam_loginuid.so.*/session optional pam_loginuid.so/g\' /etc/pam.d/sshd RUN /bin/echo -e "LANG=\\"en_US.UTF-8\\"" > /etc/default/local EXPOSE 22 CMD ["/usr/sbin/sshd","-D"] V docker build -t=\'syw/ssh:centos6.8\' . if [ $? -ne 0 ];then echo \'build error\' exit 0 fi docker run -d --name test1 syw/ssh yum install bridge-utils -y cd ~ yum install wget -y yum -y install openssl-devel kernel-devel yum groupinstall "Development Tools" wget http://openvswitch.org/releases/openvswitch-2.5.0.tar.gz yum install gcc make python-devel openssl-devel kernel-devel graphviz kernel-debug-devel autoconf automake rpm-build redhat-rpm-config libtool -y mkdir -p ~/rpmbuild/SOURCES tar zxf openvswitch-2.5.0.tar.gz cp openvswitch-2.5.0.tar.gz ~/rpmbuild/SOURCES/ ls /lib/modules/$(uname -r) -ln rpmbuild -bb --without check openvswitch-2.5.0/rhel/openvswitch.spec yum -y install tree cd rpmbuild/RPMS/x86_64/ yum localinstall openvswitch-2.5.0-1.x86_64.rpm -y systemctl start openvswitch.service
二、配置OVS
HOST1
#!/bin/bash #author:V ETHNAME=`ip addr|grep -w "2:"|awk \'{print $2}\'|awk -F: \'{print $1}\'` IPADDR=`ip addr|grep inet|grep -v "inet6"|awk \' NR==2 { print $2 }\'|awk -F"/" \'{print $1}\'` echo 1 > /proc/sys/net/ipv4/ip_forward ovs-vsctl add-br ovsbr0 ovs-vsctl add-port ovsbr0 gre0 -- set Interface gre0 type=gre options:remote_ip=192.168.17.129 brctl addbr kbr0 brctl addif kbr0 ovsbr0 ip link set dev docker0 down ip link del dev docker0 #=============configure kbr0 by:v=====网上有的版本会少参数,centos7 会报错哦===== cat >>/etc/sysconfig/network-scripts/ifcfg-kbr0 << V DEVICE=kbr0 ONBOOT=YES BOOTPROTO=static IPADDR=172.17.1.1 NETMASK=255.255.255.0 GATEWAY=172.17.1.254 USERCTL=no TYPE=Bridge IPV6INIT=no V cat >>/etc/sysconfig/network-scripts/route-ens32 <<V 172.17.2.0/24 via 192.168.17.129 dev eno16777736 V systemctl restart network.service systemctl stop docker.service #===============================configure docker network====by:v===== cat >>/etc/systemd/system/docker.service.d/docker.conf<< V [Service] ExecStart= ExecStart=/usr/bin/docker daemon -H fd:// -b kbr0 V chmod +x /etc/rc.d/rc.local cat >>/etc/rc.d/rc.local<< V ip route del default ip route add default via 192.168.17.2 dev $ETHNAME brctl addif kbr0 ovsbr0 V systemctl start docker.service
HOST2
#!/bin/bash #author:V ETHNAME=`ip addr|grep -w "2:"|awk \'{print $2}\'|awk -F: \'{print $1}\'` IPADDR=`ip addr|grep inet|grep -v "inet6"|awk \' NR==2 { print $2 }\'|awk -F"/" \'{print $1}\'` echo 1 > /proc/sys/net/ipv4/ip_forward ovs-vsctl add-br ovsbr0 ovs-vsctl add-port ovsbr0 gre0 -- set Interface gre0 type=gre options:remote_ip=192.168.17.128 brctl addbr kbr0 brctl addif kbr0 ovsbr0 ip link set dev docker0 down ip link del dev docker0 #=============configure kbr0 by:v=====网上有的版本会少参数,centos7 会报错哦===== cat >>/etc/sysconfig/network-scripts/ifcfg-kbr0 << V DEVICE=kbr0 ONBOOT=YES BOOTPROTO=static IPADDR=172.17.2.1 NETMASK=255.255.255.0 GATEWAY=172.17.2.254 USERCTL=no TYPE=Bridge IPV6INIT=no V cat >>/etc/sysconfig/network-scripts/route-ens32 <<V 172.17.2.0/24 via 192.168.17.128 dev eno16777736 V systemctl restart network.service systemctl stop docker.service #===============================configure docker network====by:v===== cat >>/etc/systemd/system/docker.service.d/docker.conf<< V [Service] ExecStart= ExecStart=/usr/bin/docker daemon -H fd:// -b kbr0 V chmod +x /etc/rc.d/rc.local cat >>/etc/rc.d/rc.local<< V ip route del default ip route add default via 192.168.17.2 dev $ETHNAME brctl addif kbr0 ovsbr0 V systemctl start docker.service
容器A 可以 ping通HOST2的容器A。HOST1 可以用SSH登录到 HOST2的容器中。so 只需要在外面将路由指向容器即可。
ps:
1、在docker run 的时候的cmd 是会覆盖dockerfile的cmd 即,如果需要覆盖可在容器/usr/sbin/sshd -D 启动sshd。
2、本博文中OVS为2.5 之前的bug 修复,所以不需要修改spec 文件。
3、centos7 或者说 是docker 1.11 版本的docker 绑定网卡 路径为/etc/systemd/system/docker.service.d/docker.conf。为system文件夹 说明是随系统启动。而里面的execstart可以看官方文档规定,必须要空定义一个。
4、这里的网关 (使用ip route 查看) 默认网关为kbr0 定义的网关,这里需要删除掉 更换网关,不然 宿主机和容器都无法通外网。
5、brctl addif 讲ovsbr0 加入到kbr0 这个网桥。重启network 服务会消失,所以不要重启服务,或者重启系统,这里我们添加到rc.local centos7的rc.local 本是没有执行权限 so 需要chmod +x 。
6、这里为gre0模式 ,如果想转换为vxlan 修改对应的type 即可,具体区别请百度。
7、以上脚本只适合centos7,但是过程是一样。
如有问题 请留言!
以上是关于Centos7 Docker 多主机 容器互连--基于OVS的主要内容,如果未能解决你的问题,请参考以下文章
centos7下安装docker(15.3跨主机网络-macvlan)
centos7下安装docker(15.6docker跨主机网络---Weave)