首页 > 其他分享 >docker swarm快速入门篇

docker swarm快速入门篇

时间:2023-02-24 08:13:38浏览次数:36  
标签:入门篇 root swarm yinzhengjie docker com ubuntu13

目录


一.docker swarm集群部署

主机名 操作系统 配置
centos10.yinzhengjie.com CentOS 7.9.2009 CPU: 2core,Memory: 4G,Disk: 50G+
ubuntu13.yinzhengjie.com Ubuntu 20.04.05 CPU: 2core,Memory: 4G,Disk: 50G+
ubuntu14.yinzhengjie.com Ubuntu 20.04.05 CPU: 2core,Memory: 4G,Disk: 50G+

1. 初始化manager节点

[email protected]:~# docker swarm init --advertise-addr 10.0.0.13
Swarm initialized: current node (ophsn9oh8xzih7ofvbp0fkyet) is now a manager.

To add a worker to this swarm, run the following command:

    docker swarm join --token SWMTKN-1-65beby1eo5o689xgpez7q5b5i597hknifwizaoqwhxvttbjkaf-3zq9eue9el0m0m6bhq1yh0tem 10.0.0.13:2377

To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.

[email protected]:~# 
[email protected]:~# docker swarm join-token worker  # 查看worker节点加入到集群的token,其实上面的命令已经自带啦~
To add a worker to this swarm, run the following command:

    docker swarm join --token SWMTKN-1-65beby1eo5o689xgpez7q5b5i597hknifwizaoqwhxvttbjkaf-3zq9eue9el0m0m6bhq1yh0tem 10.0.0.13:2377

[email protected]:~# 

2.worker节点加入集群

[email protected]:~# docker swarm join --token SWMTKN-1-65beby1eo5o689xgpez7q5b5i597hknifwizaoqwhxvttbjkaf-3zq9eue9el0m0m6bhq1yh0tem 10.0.0.13:2377
This node joined a swarm as a worker.
[email protected]:~# 

[[email protected] ~]# docker swarm join --token SWMTKN-1-65beby1eo5o689xgpez7q5b5i597hknifwizaoqwhxvttbjkaf-3zq9eue9el0m0m6bhq1yh0tem 10.0.0.13:2377
This node joined a swarm as a worker.
[[email protected] ~]# 

3.查看集群节点列表

[email protected]:~# docker node ls
ID                            HOSTNAME                   STATUS    AVAILABILITY   MANAGER STATUS   ENGINE VERSION
ezyn4audw09ulmoy9nn0q1r55     centos10.yinzhengjie.com   Ready     Active                          23.0.1
ophsn9oh8xzih7ofvbp0fkyet *   ubuntu13.yinzhengjie.com   Ready     Active         Leader           23.0.1
wcox22cuu4nftf1raty4upieb     web14.yinzhengjie.com      Ready     Active                          23.0.1
[email protected]:~# 

4.给节点添加label

[email protected]:~# docker node update --label-add name=swarm01 centos10.yinzhengjie.com
centos10.yinzhengjie.com
[email protected]:~# 
[email protected]:~# docker node update --label-add name=swarm02 ubuntu13.yinzhengjie.com
ubuntu13.yinzhengjie.com
[email protected]:~# 
[email protected]:~# docker node update --label-add name=swarm03 web14.yinzhengjie.com
web14.yinzhengjie.com
[email protected]:~# 

5.将work节点提升为manager角色以实现高可用

[email protected]:~# docker node ls
ID                            HOSTNAME                   STATUS    AVAILABILITY   MANAGER STATUS   ENGINE VERSION
ezyn4audw09ulmoy9nn0q1r55     centos10.yinzhengjie.com   Ready     Active                          23.0.1
ophsn9oh8xzih7ofvbp0fkyet *   ubuntu13.yinzhengjie.com   Ready     Active         Leader           23.0.1
wcox22cuu4nftf1raty4upieb     web14.yinzhengjie.com      Ready     Active                          23.0.1
[email protected]:~# 
[email protected]:~# 
[email protected]:~# docker node promote centos10.yinzhengjie.com
Node centos10.yinzhengjie.com promoted to a manager in the swarm.
[email protected]:~# 
[email protected]:~# 
[email protected]:~# docker node promote web14.yinzhengjie.com
Node web14.yinzhengjie.com promoted to a manager in the swarm.
[email protected]:~# 
[email protected]:~# docker node ls
ID                            HOSTNAME                   STATUS    AVAILABILITY   MANAGER STATUS   ENGINE VERSION
ezyn4audw09ulmoy9nn0q1r55     centos10.yinzhengjie.com   Ready     Active         Reachable        23.0.1
ophsn9oh8xzih7ofvbp0fkyet *   ubuntu13.yinzhengjie.com   Ready     Active         Leader           23.0.1
wcox22cuu4nftf1raty4upieb     web14.yinzhengjie.com      Ready     Active         Reachable        23.0.1
[email protected]:~# 

6.查看node信息

[email protected]:~# docker node inspect centos10.yinzhengjie.com
[
    {
		....
        "Status": {
            "State": "ready",
            "Addr": "10.0.0.10"
        },
        "ManagerStatus": {
            "Reachability": "reachable",
            "Addr": "10.0.0.10:2377"
        }
    }
]
[email protected]:~# 

7.创建overlay网络

	(1)部署overlay网络环境
[email protected]:~# docker run -dp 8500:8500 --restart always --name=dev-consul -e CONSUL_BIND_INTERFACE=ens33 consul

		# 以下配置"cluster-store"和"cluster-advertise"参数并不支持docker23.0.1哟,建议更换20.10系列的docker版本

cat > /etc/docker/daemon.json <<'EOF'
{
  "registry-mirrors": ["https://tuv7rqqq.mirror.aliyuncs.com"],
   "cluster-store": "consul://10.0.0.14:8500",
   "cluster-advertise": "10.0.0.14:6666"
}
EOF
systemctl restart docker


	(2)使用ovarlay创建
[email protected]:~# docker network create -d overlay --subnet=172.31.0.0/21 --gateway=172.31.7.254 --attachable yinzhengjie-network
nkndkvgns1uzoqbk60flc8308
[email protected]:~# 
[email protected]:~# docker network ls
NETWORK ID     NAME                               DRIVER    SCOPE
...
28kxwkcn4ho3   ingress                            overlay   swarm
nkndkvgns1uz   yinzhengjie-network                overlay   swarm
[email protected]:~# 




温馨提示:
	(1)docker 23.0.1版本貌似不支持 "cluster-store"和 "cluster-advertise"的配置,如果非要使用overlay网络,可以考虑使用20.10系列的docker哟。
	(2)当然,我们也可以使用macvlan,Flannel,Calico等技术哟。

二.验证docker swarm集群

1.创建容器测试

[email protected]:~# docker service create --replicas 2 -p 9999:80 --network yinzhengjie-network --name myweb2023 nginx:1.22.1-alpine 
2b3d7adaws22am7u32y38zd0p
overall progress: 2 out of 2 tasks 
1/2: running   [==================================================>] 
2/2: running   [==================================================>] 
verify: Service converged 
[email protected]:~# 

2.查看service信息

[email protected]:~# docker service ls   # 注意哈,它会监听所有worker节点的9999端口哟~
ID             NAME        MODE         REPLICAS   IMAGE                 PORTS
2b3d7adaws22   myweb2023   replicated   2/2        nginx:1.22.1-alpine   *:9999->80/tcp
[email protected]:~# 
[email protected]:~# ss -ntl
State       Recv-Q      Send-Q           Local Address:Port             Peer Address:Port      Process      
...                     
LISTEN      0           4096                         *:9999                          *:*                                          
[email protected]:~# 
[email protected]:~# docker service ps myweb2023  # 查看容器在哪个worker节点上。 
ID             NAME          IMAGE                 NODE                       DESIRED STATE   CURRENT STATE                    ERROR     PORTS
z9qohpe9j0qs   myweb2023.1   nginx:1.22.1-alpine   centos10.yinzhengjie.com   Running         Running less than a second ago             
wh7jgc61amsz   myweb2023.2   nginx:1.22.1-alpine   web14.yinzhengjie.com      Running         Running 3 minutes ago                      
[email protected]:~# 

3.访问服务

[email protected]:~# curl -I 10.0.0.13:9999
HTTP/1.1 200 OK
Server: nginx/1.22.1
Date: Fri, 24 Feb 2023 07:28:44 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Wed, 19 Oct 2022 10:49:37 GMT
Connection: keep-alive
ETag: "634fd641-267"
Accept-Ranges: bytes

[email protected]:~# 



温馨提示:
	如果非要

三.验证服务的高可用性

1.查看服务

[email protected]:~# docker service ps 2b3d7adaws22
ID             NAME          IMAGE                 NODE                       DESIRED STATE   CURRENT STATE                    ERROR     PORTS
z9qohpe9j0qs   myweb2023.1   nginx:1.22.1-alpine   centos10.yinzhengjie.com   Running         Running less than a second ago             
wh7jgc61amsz   myweb2023.2   nginx:1.22.1-alpine   web14.yinzhengjie.com      Running         Running 8 minutes ago                      
[email protected]:~# 

2.将服务"centos10.yinzhengjie.com"重启

[[email protected] ~]# reboot 

3.再次查看服务,容器会自动在其他节点被拉起

[email protected]:~# docker service ps 2b3d7adaws22
ID             NAME              IMAGE                 NODE                       DESIRED STATE   CURRENT STATE                    ERROR     PORTS
qmxthyaq4pra   myweb2023.1       nginx:1.22.1-alpine   ubuntu13.yinzhengjie.com   Ready           Ready 3 seconds ago                        
z9qohpe9j0qs    \_ myweb2023.1   nginx:1.22.1-alpine   centos10.yinzhengjie.com   Shutdown        Running less than a second ago             
wh7jgc61amsz   myweb2023.2       nginx:1.22.1-alpine   web14.yinzhengjie.com      Running         Running 9 minutes ago                      
[email protected]:~# 
[email protected]:~# docker service ps 2b3d7adaws22  # 过了一端时间后,注意观察"myweb2023.1"状态从"Ready"变更为"Running"
ID             NAME              IMAGE                 NODE                       DESIRED STATE   CURRENT STATE                     ERROR     PORTS
qmxthyaq4pra   myweb2023.1       nginx:1.22.1-alpine   ubuntu13.yinzhengjie.com   Running         Running 3 minutes ago                       
z9qohpe9j0qs    \_ myweb2023.1   nginx:1.22.1-alpine   centos10.yinzhengjie.com   Shutdown        Shutdown less than a second ago             
wh7jgc61amsz   myweb2023.2       nginx:1.22.1-alpine   web14.yinzhengjie.com      Running         Running 12 minutes ago                      
[email protected]:~#

标签:入门篇,root,swarm,yinzhengjie,docker,com,ubuntu13
From: https://www.cnblogs.com/yinzhengjie/p/17150043.html

相关文章

  • Python:Excel自动化实践入门篇 甲【留言点赞领图书门票】
    *以下内容为本人的学习笔记,如需要转载,请声明原文链接微信公众号「englyf」https://mp.weixin.qq.com/s?__biz=MzUxMTgxMzExNQ==&mid=2247485524&idx=1&sn=7af7790002e0e60......
  • Docker+Gunicorn+Flask部署项目
    (一)Flask应用1.创建一个目录并切换进去$mkdirgunicorn_demo$cdgunicorn_demo2.创建一个Flask应用构建一个最基本的Flask项目app.py文件fromflaskimp......
  • 力扣——《数据结构·入门篇》刷题笔记
    第一天-数组  1️⃣存在重复元素  题目:给你一个整数数组 nums 。如果任一值在数组中出现 至少两次 ,返回 true ;如果数组中每个元素互不相同,返回 false 。 ......
  • Docker部署RocketMQ集群
    一、概述ApacheRocketMQ是阿里开源的一款高性能、高吞吐量、队列模型的消息中间件的分布式消息中间件。关于RocketMQ集群架构的详细介绍,请参考链接:https://blog.csdn.ne......
  • docker 操作笔记
    1.Docker创建ubuntu系统更换apt-get源创建Dockerfile并且更新apt源在Dockerfile中添加如下两句代码:RUNsed-is@/archive.ubuntu.com/@/mirrors.aliyun.com/@g/et......
  • Windows 上 Docker 部署 MongoDb 并构建数据持久化
    拉取镜像老样子先拉取一个镜像。dockerpullmongo:latest运行容器dockerrun-p27017:27017--namemongo-v/d/mongo/data:/data/db-eMONGO_INITDB_ROOT_USERNA......
  • 在 code-server Docker Container 使用 host user/group 账户
      在code-serverDockerContainer使用hostuser/group账户(jgsun.github.io) Overview“工欲善其事,必先利其器!”此文为 定制code-serverdockerimage ......
  • 以docker方式部署的redis键值查询及清理
     1、首先使用 dockerps 命令来查看正在运行的容器。该命令会列出容器的ID、名称、端口号、状态等信息。也可以使用 dockerps-a 命令来查看所有容器,包括已经停止的......
  • 一次docker启动错误unable to configure the Docker daemon with file /etc/docker/da
    环境:三台服务器,一台daemon可以启动,另外两台不行解决原因:服务器内有两个docker.service,其中一个有配置--insecure-registry=127.0.0.1,重命名后启动docker可用报错如下:2月......
  • docker卸载提示Device or resource busy
    1. 停掉运行的容器,停掉docker先停掉全部运行的容器,并删除dockerkill$(dockerps-a-q)##删除所有的docker容器dockerrm$(dockerps-a-q)##删除所有的docker镜......