Docker - 用Flannel跨主机
试了下比较流行的几种SDN,感觉flannel还是比较好用,这里简单记录一下。
用的是virtualbox,3个机器,分别为:
- genesis : inet 192.168.99.103/24 brd 192.168.99.255 scope global dynamic enp0s3
- exodus : inet 192.168.99.105/24 brd 192.168.99.255 scope global dynamic enp0s3
- leviticus : inet 192.168.99.106/24 brd 192.168.99.255 scope global dynamic enp0s3
虚机信息如下
[root@localhost yum.repos.d]# uname -mars
Linux localhost.localdomain 3.10.0-229.el7.x86_64 #1 SMP Fri Mar 6 11:36:42 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
[root@localhost yum.repos.d]# cat /etc/*-release
CentOS Linux release 7.3.1611 (Core)
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"
CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"
CentOS Linux release 7.3.1611 (Core)
CentOS Linux release 7.3.1611 (Core)
[root@localhost yum.repos.d]# docker version
Client:
Version: 1.12.5
API version: 1.24
Go version: go1.6.4
Git commit: 7392c3b
Built: Fri Dec 16 02:23:59 2016
OS/Arch: linux/amd64
随便选择两台机器run一下,在容器中ifconfig:
[root@localhost ~]# docker run -it busybox
/ # ifconfig
eth0 Link encap:Ethernet HWaddr 02:42:AC:11:00:02
inet addr:172.17.0.2 Bcast:0.0.0.0 Mask:255.255.0.0
inet6 addr: fe80::42:acff:fe11:2/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:12 errors:0 dropped:0 overruns:0 frame:0
TX packets:6 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:1016 (1016.0 B) TX bytes:508 (508.0 B)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
发现参数完全相同,单纯bridge模式下并没有跨主机互通,而host模式是并不建议使用的。
install
先yum install -y etcd flannel,如果没问题是再好不过了。
etcd 3.x支持--config-file参数,需要的话可以从源代码install(需要golang 1.6+)。
先从etcd开始,简单说就是"distributed key value store"。
etcd集群的3种方式:
- static
- etcd discovery
- DNS discovery
DNS discovery主要是用srv record,这里先不搞DNS服务,下面对static和etcd discovery两种方式简单说明一下。
static
参数可以在启动时置顶,或者写到配置文件中,默认配置文件为/etc/etcd/etcd.conf。
genesis的配置如下:
ETCD_NAME=genesis
ETCD_DATA_DIR="/var/lib/etcd/genesis"
ETCD_LISTEN_PEER_URLS="http://192.168.99.103:2380"
ETCD_LISTEN_CLIENT_URLS="http://192.168.99.103:2379,http://127.0.0.1:2379"
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.99.103:2380"
ETCD_ADVERTISE_CLIENT_URLS="http://192.168.99.103:2379"
ETCD_INITIAL_CLUSTER_STATE="new"
ETCD_INITIAL_CLUSTER_TOKEN="etct-fantasy"
ETCD_INITIAL_CLUSTER="exodus=http://192.168.99.105:2380,genesis=http://192.168.99.103:2380"
exodus的配置如下:
ETCD_NAME=exodus
ETCD_DATA_DIR="/var/lib/etcd/exodus"
ETCD_LISTEN_PEER_URLS="http://192.168.99.105:2380"
ETCD_LISTEN_CLIENT_URLS="http://192.168.99.105:2379,http://127.0.0.1:2379"
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.99.105:2380"
ETCD_ADVERTISE_CLIENT_URLS="http://192.168.99.105:2379"
ETCD_INITIAL_CLUSTER_STATE="new"
ETCD_INITIAL_CLUSTER_TOKEN="etct-fantasy"
ETCD_INITIAL_CLUSTER="exodus=http://192.168.99.105:2380,genesis=http://192.168.99.103:2380"
启动方式看自己喜好,如果打算用systemctl启动的话,注意/usr/lib/systemd/system/etcd.service中的内容可能不会如你所愿。
启动后,检查一下集群状态,顺便也看看有哪些成员:
[root@localhost etcd]# etcdctl cluster-health
member 7a4f27f78a05e755 is healthy: got healthy result from http://192.168.99.103:2379
failed to check the health of member 8e8718b335c6c9a2 on http://192.168.99.105:2379: Get http://192.168.99.105:2379/health: dial tcp 192.168.99.105:2379: i/o timeout
member 8e8718b335c6c9a2 is unreachable: [http://192.168.99.105:2379] are all unreachable
cluster is healthy
提示"member unreachable",看来是被exodus的防火墙拦住了,我们先粗暴一点。
[root@localhost etcd]# systemctl stop firewalld
[root@localhost etcd]# etcdctl cluster-health
member 7a4f27f78a05e755 is healthy: got healthy result from http://192.168.99.103:2379
member 8e8718b335c6c9a2 is healthy: got healthy result from http://192.168.99.105:2379
cluster is healthy
etcd discovery
当然,这样配置的前提是已经知道各个节点的信息。
但实际场景中可能无法预知各个member,所以我们需要让etcd自己去发现(discovery)。
首先,etcd提供了一个public discovery service - discovery.etcd.io,我们用它来生成一个discovery token,并在genesis创建目录:
[root@localhost etcd]# curl https://discovery.etcd.io/new?size=3
https://discovery.etcd.io/6321c0706046c91f2b2598206ffa3272
[root@localhost etcd]# etcdctl set /discovery/6321c0706046c91f2b2598206ffa3272/_config/size 3
修改exodus的配置,用discovery代替之前的cluster:
ETCD_NAME=exodus
ETCD_DATA_DIR="/var/lib/etcd/exodus"
ETCD_LISTEN_PEER_URLS="http://192.168.99.105:2380"
ETCD_LISTEN_CLIENT_URLS="http://192.168.99.105:2379,http://127.0.0.1:2379"
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.99.105:2380"
ETCD_ADVERTISE_CLIENT_URLS="http://192.168.99.105:2379"
ETCD_DISCOVERY=http://192.168.99.103:2379/v2/keys/discovery/98a976dac265a218f1a1959eb8dde57f
如果启动后一直显示如下错误(参考: raft election):
rafthttp: the clock difference against peer ?????? is too high [??????s > 1s]
简单的解决方法是通过ntp:
[root@localhost etcd]yum install ntp -y
[root@localhost etcd]# systemctl enable ntpd
Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.
[root@localhost etcd]# systemctl start ntpd
flannel
set一个路径给flannel用:
etcdctl set /coreos.com/network/config '{ "Network": "10.1.0.0/16" }'
以systemctl start flanneld方式启动时,如果出现以下错误
network.go:53] Failed to retrieve network config: 100: Key not found (/coreos.net) [9]
注意/etc/sysconfig/flanneld中的内容,FLANNEL_ETCD_PREFIX很可能是/atomic.io/network,将其改为/coreos.com/network。
或者也可以通过-etcd-prefix指定。
启动成功后,查看subnet:
[root@localhost etcd]# etcdctl ls /coreos.com/network/subnets
/coreos.com/network/subnets/10.1.90.0-24
/coreos.com/network/subnets/10.1.30.0-24
/coreos.com/network/subnets/10.1.18.0-24
flannel启动成功后会生成/run/flannel/docker,内容如下:
DOCKER_OPT_BIP="--bip=10.1.30.1/24"
DOCKER_OPT_IPMASQ="--ip-masq=true"
DOCKER_OPT_MTU="--mtu=1450"
DOCKER_NETWORK_OPTIONS=" --bip=10.1.30.1/24 --ip-masq=true --mtu=1450 "
用以下方式启动docker:
[root@localhost etcd]# source /run/flannel/docker
[root@localhost etcd]# docker daemon ${DOCKER_NETWORK_OPTIONS} >> /dev/null 2>&1 &
/run/flannel/docker是怎么来的?
参考flanneld的两个启动参数,-subnet-dir和-subnet-file。
在genesis进入容器看看效果:
[root@localhost etcd]# docker run -it busybox
/ # ifconfig
eth0 Link encap:Ethernet HWaddr 02:42:0A:01:5A:02
inet addr:10.1.90.2 Bcast:0.0.0.0 Mask:255.255.255.0
inet6 addr: fe80::42:aff:fe01:5a02/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1450 Metric:1
RX packets:6 errors:0 dropped:0 overruns:0 frame:0
TX packets:6 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:508 (508.0 B) TX bytes:508 (508.0 B)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
在exodus也做类似操作,在exodus中ping一下10.1.90.2,发现是通的。
并在各自容器中也ping一下,检查是否跨主机互通。
Docker - 用Flannel跨主机的更多相关文章
- Docker容器的跨主机连接
Docker容器的跨主机连接 Docker容器的跨主机连接 使用网桥实现跨主机容器连接 网络拓扑 网络连接使用的是Bridge 操作 修改/etc/network/interfaces文件,分配静态I ...
- Docker学习(15) Docker容器的跨主机连接
Docker容器的跨主机连接 Docker使用网桥跨主机容器连接 Docker使用Open cSwitch实现跨主机容器连接 Docker使用weave实现跨主机容器连接
- Docker系列04—跨主机网络方案(overlay/weave)
在前面详细讲解了几种网络模式:none,host,bridge,container.他们解决了单个主机间的容器的通信问题,并不能实现多个主机容器之间的通信. 跨主机网络方案包括两大类: 1,docke ...
- 利用虚拟网桥实现Docker容器的跨主机访问
最近在研究Docker,Docker的网络配置是比较令人头疼的部分,尤其是跨主机的容器间通信,很多解决方案都比较复杂,这里,我只用虚拟网桥来实现Docker的跨主机访问,分享出来,希望对Docker学 ...
- Docker 容器的跨主机连接
使用网桥实现跨主枳容器连接 不推荐 使用OpenvSwitch实现跨主机容器连接 OpenvSwitch: OpenvSwitch是一个高质量的.多层虚拟交换枳,使用开源Apache2.0许可协议,由 ...
- docker swarm模式跨主机连接
一.前言 当我们开发好微服务之后,考虑到灵活快速持续部署的需要,通常会考虑将其Docker镜像化并在Docker环境下运行.由于微服务个数通常会较多,把所有微服务部署在一台docker主机上是不现实的 ...
- docker容器间跨主机通信
http://jnzg905.iteye.com/blog/2269583 https://blog.csdn.net/pingpangbing0902/article/details/7823889 ...
- Docker容器跨主机通信
默认情况下Docker容器需要跨主机通信两个主机节点都需要在同一个网段下,这时只要两个Docker容器的宿主机能相互通信并且该容器使用net网络模式,改实现方式为网桥模式通信: 除此之外我们还可以通过 ...
- docker 跨主机网络:overlay 简介
简介 docker 在早前的时候没有考虑跨主机的容器通信,这个特性直到 docker 1.9 才出现.在此之前,如果希望位于不同主机的容器能够通信,一般有几种方法: 使用端口映射:直接把容器的服务端口 ...
随机推荐
- fork,exec,source
http://alsww.blog.51cto.com/2001924/1113112 http://stackoverflow.com/questions/1127502/start-stop-da ...
- AOP 在javascript 中的使用
AOP(Aspect Oriented Programming) 意为面向切面编程 可以在不修改原有代码的情况下增加新功能,利用AOP可以对业务逻辑各个部分进行隔离,从而使得业务逻辑各部分的耦合度降低 ...
- 计算机学院大学生程序设计竞赛(2015’12) 1009 The Magic Tower
#include<cmath> #include<cstdio> #include<cstring> #include<algorithm> using ...
- Linux的iptables常用配置范例(3)
编辑/etc/rc.local,加入iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -o eth1 -j MASQUERADE,外网口eth1为dhc ...
- spring boot + neo4j restful
整整折腾了三天,终于把spring boot + neo4j的路走通了. 这里介绍3个部分,pom,entity,repository 1)pom <?xml version="1.0 ...
- IOS中集合视图UICollectionView中DecorationView的简易使用方法
转载自: http://www.it165.net/pro/html/201312/8575.html Decoration View是UICollectionView的装饰视图.苹果官方给的案例 ...
- nginx keepalived 主从切换
注: LVS + Keepalived 不知道为什么出现一个很郁闷的问题....... ------------------------------------------------------ ...
- javascript alert,confirm,prompt弹框用法
1. alert是弹出警告框,在文本里面加入\n就可以换行. 2. confirm弹出确认框,会返回布尔值,通过这个值可以判断点击时确认还是取消.true表示点击了确认,false表示点击了取消. 3 ...
- Instruments使用实战
http://www.cocoachina.com/ios/20150225/11163.html 最近采用Instruments 来分析整个应用程序的性能.发现很多有意思的点,以及性能优化和一些分析 ...
- VR元年,VR虚拟现实这只风口上的猪有怎样的变化?
走过了2016年,无论我们承认不承认,这一年到底是不是VR元年,我们都很难否定,在这一年,VR虚拟现实生态圈有很大的变化,那么,这一年VR虚拟现实到底有怎样的改变呢?我们的VR虚拟现实生态圈,发生了什 ...