zz22 发表于 2019-2-21 12:14:08

docker overlay network 使用ZooKeeper

  概述
docker 通常情况下使用的是bridge网络,这种无法进行跨主机通讯,即,一台主机上的docker容器是不能直接访问另一台主机上的容器的内部IP。
对于通过docker netowkr create 创建的网络,docker 在内部维护了一个DNS服务,将容器名解析成容器内部的IP,这样就可以使用DNS进行容器间的通讯,但bridge模式,只能在同一台主机实现容器间的DNS通讯。
overlay模式通过vxlan可以将多台主机上的docker网络连接到一个大二层网络,这样多台主机上的docker网络就在一个网络平面中了,就可以使用docker内建的DNS服务实际容器间的DNS通讯,解决容器IP随机变化的问题。
docker的overlay网络需要一个数据库用于存储网络相关配置信息,这个数据库可以支持Consul、Etcd或ZooKeeper,docker官方文档给出了consul的配置例子,在本文中将给出zookeeper的配置。
  配置环境




主机
主机名
网上




192.168.100.141
zk0
eth1


192.168.100.138
zk1
eth1


192.168.100.142
zk2
eth1


  搭建zookeeper集群
由于docker 需要接入zookeeper,因此这里不将用docker安装zookeeper的方式,使用手动安装zookeeper


[*]下载zookeeper-3.4.12.tar.gz
[*]编辑zookeeper配置文件zookeeper-3.4.12/conf/zoo.cfg

# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
#dataDir=/tmp/zookeeper
dataDir=/opt/zookeeper/data
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
server.0=zk0:2888:3888
server.1=zk1:2888:3888
server.2=zk2:2888:3888

[*]创建zookeeper数据目录,并写入id
# 数据目录需要各节点上创建
mkdir -p /opt/zookeeper/data
# 写入id
ssh zk0 echo 0 > /opt/zookeeper/data/myid
ssh zk1 echo 1 > /opt/zookeeper/data/myid
ssh zk2 echo 2 > /opt/zookeeper/data/myid
[*]启动各节上的zookeeper
zookeeper-3.4.12/bin/zkServer.sh start
zookeeper-3.4.12/bin/zkServer.sh status
  配置docker

[*]编辑各节点上 /etc/docker/daemon.json ,实际测试,cluster-store只能配置一个zookeeper连接地址,这里配置各节点连接各自节点上的zookeeper
{
"hosts": ["unix:///var/run/docker.sock", "tcp://0.0.0.0:2376"],
"cluster-store": "zk://127.0.0.1:2181",
"cluster-advertise": "eth1:2376"
}
[*]配置eth1的mtu值,如果是centos7,可以在ifcfg-eth1中添加如下一行,eth1所接入的交换机需要开户巨型帧
...
MTU=9216
[*]重启docker
service docker restart
  创建overlay网络
在其中一个节点上创建网络就行,其余各节点,会自动同步创建网络
···bash
docker network create -d overlay example --subnet 192.168.200.0/24
docker network ls




页: [1]
查看完整版本: docker overlay network 使用ZooKeeper