docker1.9 network跨主机安装

 

背景:在跨host中,如果docker任何一个重启或者销毁,docker暴露的端口以及ip将可能重新配置,这个时候需要重新记录ip跟端口。在生产环境中往往需要一个固定的ip以及端口去跟容器通信。例如mysql跟php配置,如果在php配置里面写了mysql配置如10.0.0.1:3306如果mysql在不同host里面不能使用link,这个时候mysql需要重启,造成ip以及端口改变,现在只能修改用户php里面配置。

解决方案:通过docker network可以解决这个问题

架构设计:network需要一个key-value的存储系统来存储network里面各个节点信息以及状态,并且需要安装swarm来管理docker的各个容器。在慕课网生产中我们需要一个稳定的高可用的架构设计来满足这个需要

 

一些说明:

  1. 使用zookeeper集群作为key-value记录中心避免单点
  2. 在docker里面使用多个swarm-manger一般是三个,监控整个docker容器状况。
  3. 容器通过network进行通信。

Network的安装

一.ubuntu安装步骤

1.在ubuntu上安装docker(最新版本 1.9.1)


执行curl -Ssl https://get.docker.com/ | sh 


2.安装完成后
vi /etc/default/docker   (更新2375接收端口,修改docker_root目录,加入可信私有registry)
DOCKER_OPTS="-D -H 0.0.0.0:2375 -H unix:///var/run/docker.sock --graph /data/docker --insecure-registry imgstore.imooc.com"


3.保存并service docker restart 

4.安装设置程序开机自动启动的工具(ubuntu下没有chkconfig命令)
apt-get install sysv-rc-conf -y
sysv-rc-conf docker on    (使得docker随机器启动)

5.ubuntu14.04升级到3.16内核 (原为3.13)
apt-get install linux-generic-lts-utopic -y
然后直接重启即可

二.Docker的network安装

1.安装consul

命令如下

sudo apt-get install -y unzip curl
cd /tmp/
wget https://dl.bintray.com/mitchellh/consul/0.5.2_linux_amd64.zip -O consul.zip
unzip consul.zip
mv consul /usr/local/bin
chmod +x /usr/local/bin/consul
wget https://dl.bintray.com/mitchellh/consul/0.5.2_web_ui.zip -O dist.zip
unzip dist.zip
mkdir –p /home/consul
mv dist /home/consul/
mkdir –p /data/consul
2.启动consul
consul agent -server -bootstrap-expect 1 -data-dir /data/consul -ui-dir /home/consul/dist -client 0.0.0.0
UI默认地址是http://localhost:8500/ui
如果UI没有启动,需要添加额外的-client 0.0.0.0参数重启Consult代理。
 
3.设置docker使用consul作为network的配置
首先需要编辑docker配置文件
Service docker stop
Cd /etc/default/
Vi docker

DOCKER_OPTS="-D -H 0.0.0.0:2375 -H unix:///var/run/docker.sock --graph /data/docker --insecure-registry imgstore.imooc.com

-cluster-store=consul://$(CONUSL-IP):8500  //network存储地址 --cluster-advertise=$(HOSTIP):2376" //swarm通知地址

 

Service docker start
 
4.启动swarm程序
a.首先启动master
在host1上启动master
docker run -ti -d --restart=always --name consul-swarm-manager  
swarm:latest m --replication –addr $(HOSTIP):($SWARM-PORT) --host tcp://0.0.0.0:($SWARM-PORT) consul://(CONSUL-IP):8500 
注意:SWARM-PORT可以是任何一个没有使用的端口
CONSUL-IP是你最初安装consul的地址
 
b.在host2上启动swarm-agent
docker run -ti -d --restart=always --name consul-swarm-agent 
swarm:latest j  --addr (HOSTIP):2376  consul:// (CONSUL-IP):8500 
注意这里—addr需要监控端口是2376
 
5.创建overlay网络
docker network create --driver overlay my-net
 
6.创建容器
通过使用刚才创建的my-net网络可以做到跨主机通信于网络隔离
a.在host1上面创建ubuntu-1
docker run -itd --name=ubuntu-1 --net=my-net  ubuntu
b.在host2 上面创建ubuntu-2
docker run -itd --name=ubuntu-2 --net=my-net  ubuntu
 
7.测试case
a.在host1里面运行
docker attach ubuntu-1进入ubuntu-1
使用ping ubuntu-2能ping通
 
b.在host2里面运行
docker attach ubuntu-2 进入ubuntu-2
使用 netcat –l 1234监控1234端口
在host1里面的ubuntu-1里面运行
echo ”hello,ubuntu-2” | netcat ubuntu-2 1234
host2 里面能收到 hello,ubuntu-2
c.销毁ubuntu-2 
docker rm –f ubuntu-2
在ubuntu-1里面使用ping ubuntu-2 ping不通
重新创建ubuntu-2
docker run -itd --name=ubuntu-2 --net=my-net  ubuntu
在ubuntu-1里面能ping通
 
 

安装中出现的错误情况:

1. 这个在1.9里面已经进入bug,,下个版本可能会修复

错误:

Error response from daemon: Cannot start container 20b1998041019a5dc1e709a7814fd411d946a548f2f0644b1813d2f3c345c90a: subnet sandbox join failed for "10.10.10.0/24": error creating vxlan interface: file exists

解决方案:

# service docker stop

docker stop/waiting

node2:~# umount /var/run/docker/netns/*

node2:~# rm /var/run/docker/netns/*

node2:~# service docker start

docker start/running, process 17138

node2:~# docker run -it --name container2 --rm --net RED busybox

原文地址:https://www.cnblogs.com/breg/p/5075484.html