当前位置:网站首页>7.Swarm搭建集群
7.Swarm搭建集群
2020-11-06 22:38:37 【太猪-YJ】
使用dokcer会遇到的问题:
- 怎么去管理这么多容器?
- 怎么能方便的横向扩展
- 如果容器down了,怎么能恢复启动?
- 如何去更新容器而不影响业务?
- 如何去监控追踪这些容器?
- 怎么去调度容器的创建?
- 保护隐私数据?
Docker自带了容器编排工具:Swarm Mode
Manager节点作为管理者,为了维持高可用,不能只部署一个,如果部署多个又存在状态如何同步的问题。docker内置了分布式数据库,通过raft协议确保Manager信息是同步的,不会出现脑裂这种现象。
Worker节点是工作节点,它也需要和其他Worker节点数据同步,使用Gossip协议进行信息的同步。
我们通过swarm manager节点去部署一个service,我们是不知道这个service最终会运行在哪些容器节点上的。swarm manager会根据一些策略,比如内存,硬盘资源等,将service部署在某些container node中。
3 nodes swarm cluster setup
1.在第一台机器上,创建docker swarm manager命令
docker swarm init --advertise-addr=192.168.8.117 本机IP
创建成功,会返回 docker swarm join --token SWMTKN-1-1aud6ztpiwgujisqn20gb9lfldziidj0dz7lww3puod36t5j5d-c6azyl8d0rhhzd8c25u9laxxp 192.168.8.117:2377 用于其他docker swarm join本机,建立集群。
2.在第二台机器上,创建docker swarm并加入第一台机器,命令
docker swarm join --token SWMTKN-1-1aud6ztpiwgujisqn20gb9lfldziidj0dz7lww3puod36t5j5d-c6azyl8d0rhhzd8c25u9laxxp 192.168.8.117:2377
发生异常
Error response from daemon: rpc error: code = Unavailable desc = all SubConns are in TransientFailure, latest connection error: connection error: desc = "transport: Error while dialing dial tcp 192.168.8.117:2377: connect: no route to host"
原因
如果要使用swarm功能,需要在所有manager node节点上开启2377端口。
解决办法
开放指定端口 firewall-cmd --zone=public --add-port=2377/tcp --permanent
重启防火墙才生效 systemctl restart firewalld
查看swarm node
docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
5xeakkex0klrezwb1p5y0pcja localhost.localdomain Ready Active 19.03.13
rk1pu6zgxkh278262d5yprzqi localhost.localdomain Ready Active 19.03.13
ulms2qb8gvnehsmgx7d7dcx9a * localhost.localdomain Ready Active Leader 19.03.13
搭建步骤:
1、环境准备:
1.1、准备三台已近安装docker engine的centos/Ubuntu系统主机(docker版本必须在
1.12以上的版本,老版本不支持swarm)
1.2、docker容器主机的ip地址固定,集群中所有工作节点必须能访问该管理节点
1.3、集群管理节点必须使用相应的协议并且保证端口可用
集群管理通信:TCP,端口2377
节点通信:TCP和UDP,端口7946
覆盖型网络(docker网络):UDP,端口4789 overlay驱动
说明:三台容器主机的ip地址分别为:
192.168.200.162(管理节点)
192.168.200.163(工作节点)
192.168.200.158(工作节点)
主机名称分别为:manager1、work1以及work2
vim /etc/hostname (修改完成后需要重启)
2、创建docker swarm
2.1、在manager1机器上创建docker swarm集群
docker swarm init ‐‐advertise‐addr 192.168.200.162
(‐‐advertise‐addr将该IP地址的机器设置为集群管理节点;如果是单节点,无需该参
数)
2.2、查看管理节点集群信息:
docker node ls
3、向docker swarm中添加工作节点:在两个工作节点中分别执行如下命令,ip地址是
manager节点的
3.1、添加两个work节点
docker swarm join ‐‐token xxx 192.168.200.138:2377 (worker1)
docker swarm join ‐‐token xxx 192.168.200.138:2377 (worker2)
(‐‐token xxx:向指定集群中加入工作节点的认证信息,xxx认证信息是在创建docker
swarm时产生的)
3.2、继续查看管理节点集群信息与之前的区别
docker node ls
4、在docker swarm中部署服务
在Docker Swarm集群中部署服务时,既可以使用Docker Hub上自带的镜像来启动服务,也
可以使用自己通Dockerfile构建的镜像来启动服务。如果使用自己通过Dockerfile构建的
镜像来启动服务那么必须先将镜像推送到Docker Hub中心仓库。为了方便读者的学习,这里
以使用Docker Hub上自带的alpine镜像为例来部署集群服务
4.1、部署服务
docker service create ‐‐replicas 1 ‐‐name helloworld alpine ping
docker.com
docker service create指令:用于在Swarm集群中创建一个基于alpine镜像的服务
‐‐replicas参数:指定了该服务只有一个副本实例
‐‐name参数:指定创建成功后的服务名称为helloworld
ping docker.com指令:表示服务启动后执行的命令
5.查看docker swarm集群中的服务
查看服务列表:docker service ls
查看部署具体服务的详细信息:docker service inspect 服务名称
查看服务在集群节点上的分配以及运行情况:docker service ps 服务名称
6、修改副本数量
在manager1上,更改服务副本的数量(创建的副本会随机分配到不同的节点)
docker service scale helloworld=5
7、删除服务(在管理节点)
docker service rm 服务名称
8、访问服务
8.1、查看集群环境下的网络列表:docker network ls
8.2、在manager1上创建一overlay为驱动的网络(默认使用的网络连接ingress)
docker network create ‐d=overlay my‐multi‐host‐network
8.3、在集群管理节点manager1上部署一个nginx服务
docker service create \
‐‐network my‐multi‐host‐network \
‐‐name my‐web \
‐p 8080:80 \
‐‐replicas 2 \
nginx
8.3、在管理节点查看服务的运行情况:
docker service ps my‐web
8.4、访问测试
版权声明
本文为[太猪-YJ]所创,转载请带上原文链接,感谢
https://my.oschina.net/xiaoyoung/blog/4699871
边栏推荐
- C++ 数字、string和char*的转换
- C++学习——centos7上部署C++开发环境
- C++学习——一步步学会写Makefile
- C++学习——临时对象的产生与优化
- C++学习——对象的引用的用法
- C++编程经验(6):使用C++风格的类型转换
- Won the CKA + CKS certificate with the highest gold content in kubernetes in 31 days!
- C + + number, string and char * conversion
- C + + Learning -- capacity() and resize() in C + +
- C + + Learning -- about code performance optimization
猜你喜欢
-
C + + programming experience (6): using C + + style type conversion
-
Latest party and government work report ppt - Park ppt
-
在线身份证号码提取生日工具
-
Online ID number extraction birthday tool
-
️野指针?悬空指针?️ 一文带你搞懂!
-
Field pointer? Dangling pointer? This article will help you understand!
-
HCNA Routing&Switching之GVRP
-
GVRP of hcna Routing & Switching
-
Seq2Seq实现闲聊机器人
-
【闲聊机器人】seq2seq模型的原理
随机推荐
- LeetCode 91. 解码方法
- Seq2seq implements chat robot
- [chat robot] principle of seq2seq model
- Leetcode 91. Decoding method
- HCNA Routing&Switching之GVRP
- GVRP of hcna Routing & Switching
- HDU7016 Random Walk 2
- [Code+#1]Yazid 的新生舞会
- CF1548C The Three Little Pigs
- HDU7033 Typing Contest
- HDU7016 Random Walk 2
- [code + 1] Yazid's freshman ball
- CF1548C The Three Little Pigs
- HDU7033 Typing Contest
- Qt Creator 自动补齐变慢的解决
- HALCON 20.11:如何处理标定助手品质问题
- HALCON 20.11:标定助手使用注意事项
- Solution of QT creator's automatic replenishment slowing down
- Halcon 20.11: how to deal with the quality problem of calibration assistant
- Halcon 20.11: precautions for use of calibration assistant
- “十大科学技术问题”揭晓!|青年科学家50²论坛
- "Top ten scientific and technological issues" announced| Young scientists 50 ² forum
- 求反转链表
- Reverse linked list
- js的数据类型
- JS data type
- 记一次文件读写遇到的bug
- Remember the bug encountered in reading and writing a file
- 单例模式
- Singleton mode
- 在这个 N 多编程语言争霸的世界,C++ 究竟还有没有未来?
- In this world of N programming languages, is there a future for C + +?
- es6模板字符
- js Promise
- js 数组方法 回顾
- ES6 template characters
- js Promise
- JS array method review
- 【Golang】️走进 Go 语言️ 第一课 Hello World
- [golang] go into go language lesson 1 Hello World