docker 学习之-swarm集群搭建及测试

swarm is a simple tool which controls a cluster of Docker hosts and exposes it as a single "virtual" host.
就是一个docker集群管理工具。swarm的管理对象不是服务器层面而是集群层面的,指令只能是发送到集群,不能指定某个节点主机。

实验环境为3台虚拟机:
安装系统为centos7 ;docker社区版,版本为version 18.09.3, build 774a1f4

HostnameIP
node1192.168.56.100
node2192.168.56.101
node3192.168.56.102

--------------------------------------------------docker-ce安装--------------------------------------------
1.安装必要的系统工具:
yum install -y yum-utils device-mapper-persistent-data lvm2

2.添加软件源信息:
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

3.更新 yum 缓存:
yum makecache fast

4.安装 Docker-ce:
yum -y install docker-ce

5.启动 Docker 后台服务
systemctl start docker
systemctl enable docker

其他:
在内网代理环境配置代理;存储目录配置略(非重点,不必须)


--------------------------------------------------swarm集群---------------------------------------------
1.拉取swarm镜像(在node1上):
docker pull swarm
2.初始化swarm-manage(在node1上,node1成为Leader)
docker swarm init --advertise-addr 192.168.56.100

参数说明:
init:初始化swarm集群;
--advertise-addr:通告地址,告知其它swarm节点通过192.168.56.100:加入swarm集群;

配置后提示信息包含如下内容用来加入集群:
docker swarm join --token SWMTKN-1-23togywdok3bslzcgk3nkuyinq392nmd0rvs54qyr69y0ji04r-2hzjt4zuvffw8kh5krpwkpx9g 192.168.56.100:2377

3.agent节点加入集群(在node2,node3上):
docker swarm join --token SWMTKN-1-23togywdok3bslzcgk3nkuyinq392nmd0rvs54qyr69y0ji04r-2hzjt4zuvffw8kh5krpwkpx9g 192.168.56.100:2377
(node1防火墙要开放2377端口;agent节点需要开放7946端口)

4.在manager节点(node1)上查看集群:
docker node ls

ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
0ik4sb8zu3hx1eor81gw1fm8j * node1 Ready Active Leader 18.09.3
rqsmge57mx6vp18jpkkie0drg node2 Ready Active 18.09.3
5dhhc93d00yigrv0n915nhiup node3 Ready Active 18.09.3


--------------------------------------------------实验测试---------------------------------------------

现在一个swarm集群已经建好,非常简单。测试在swarm集群中启动nginx服务,测试其容错能力。

只需要在集群的manager节点配置:(在node1上)
1.拉取nginx镜像
docker pull nginx

2.创建服务,服务名为test1,副本数3(副本数不能超过集群中节点数)
docker service create --replicas 3 --name test1 nginx
gf3869720b1o91l4w6gwkcnxt
overall progress: 3 out of 3 tasks
1/3: running [==================================================>]
2/3: running [==================================================>]
3/3: running [==================================================>]
verify: Service converged ]

3.查看副本运行情况:
docker service ps test1
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
tqvaaszfieqk test1.1 nginx:latest node2 Running Running about an hour ago
9d2mmi1537a8 test1.2 nginx:latest node3 Running Running about an hour ago
oicoadweql0z test1.3 nginx:latest node1 Running Running about an hour ago

此时nginx已经启动,在每个节点上docker ps -l 都能看到nginx的容器在运行。

4.将nginx容器的80端口映射到虚拟机8001端口上:
docker service update --publish-add 8001:80 test1
test1
overall progress: 3 out of 3 tasks
1/3: running [==================================================>]
2/3: running [==================================================>]
3/3: running [==================================================>]
verify: Service converged

查看端口,发现每个节点的8001端口都被映射:
netstat -tnl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:22 0.0.0.0: LISTEN
tcp 0 0 127.0.0.1:25 0.0.0.0:
LISTEN
tcp6 0 0 :::22 ::: LISTEN
tcp6 0 0 ::1:25 :::
LISTEN
tcp6 0 0 :::8001 ::: LISTEN
tcp6 0 0 :::7946 :::
LISTEN

在每个节点防火墙开放8001端口后,通过web访问:
通过浏览器访问任意节点:
http://192.168.56.100:8001/
http://192.168.56.101:8001/
http://192.168.56.102:8001/

docker 学习之-swarm集群搭建及测试

已经可以访问到index.html

5.将容器数据持久化:
默认容器的数据是保存在容器的可读写层,当容器被删除时其上的数据将会丢失,所以为了实现数据的持久性则需要选择一种数据持久技术来保存数据,当前有以下几种方式:

  • Volumes
  • Bind mounts
  • tmpfs

Volume 是 docker 容器生成持久化数据的首选机制。bind mounts 依赖主机机器的目录机构,volume 完全由 docker 管理。
volume 较 bind mounts 有几个优势:

  • volume 比 bind mounts 更易备份和迁移。
  • 可以使用 docker cli 命令和 API 管理 volume。
  • volume 工作在 linux 和 windows 容器上。
  • volume 在多个容器间共享可以更安全。
  • volume 驱动允许存储 volume 到主机和云,加密 volume 内容,或者添加其它功能。
  • 新的 volume 可以含有容器预放置的内容。

在node1上:
docker service update --mount-add type=volume,src=test1-nginx,dst=/usr/share/nginx/html test1
test1
overall progress: 3 out of 3 tasks
1/3: running [==================================================>]
2/3: running [==================================================>]
3/3: running [==================================================>]
verify: Service converged

通过docker inspect 9f52976aa6de 查看到容器volume映射到的宿主机目录为:
/var/lib/docker/volumes/test1-nginx/_data
在该目录下替换index.html为测试页面:
docker 学习之-swarm集群搭建及测试

修改后发现只有node1的页面修改了,集群中其他2个node的数据并没有同步。
问题:在访问时页面打开页比较慢,不知什么原因。
swarm暂时记录至此,还有很多细节待日后补充。

转载于:https://blog.51cto.com/14263938/2371030

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值