Docker swarm集群入门简单使用(1)

2023年 7月 16日 75.2k 0

当你没有使用swarm时候,使用是docker的命令,如果使用swarm这使用的是swarm提供的一些命令服务编排进行操作,可以但节点使用或者集群服务。尽管这些服务大多数是无状态的,但是,我们应该明白,一旦使用集群那么很多东西都需要进行考虑,比如存储方案,使用本地存储还是跨主机存储,如何进行选型等,镜像参考,编排工具等。总而言之,swarm也是很好的选择,我们可以进行简单安装和配置。

Swarm守护进程是使用Go编写,发展的速度非常快,在使用中但swarm Leader(Manager)节点创建后,其他工作节点加入后,集中管理在Leader节点,工作节点接收并执行从管理器节点分派的任务。默认情况下,管理器节点也会以服务器节点运行服务,它可能是这样的:Screenshot.png

了解searm:https://docs.docker.com/engine/swarm/key-concepts/#what-is-a-node

       https://docs.docker.com/engine/swarm/#feature-highlights
       

在开始之前,参与的机器需要能够互通,并且安装docker

swarm节点

[root@DS-VM-Node117-LinuxEA ~]# docker swarm init --advertise-addr 10.10.240.117 --listen-addr 0.0.0.0
Swarm initialized: current node (as4u4yh1h5h84y06h2etad4yb) is now a manager.

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

    docker swarm join 
    --token SWMTKN-1-4s87dp8n3mlraw9vd1amq6b2x3007it5n609t6ls82n3lj0mm8-agkh99gkc1ezyk8jnlev0kckz 
    10.10.240.117:2377

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

[root@DS-VM-Node117-LinuxEA ~]# 

添加防火墙规则:iptables -I INPUT 4 -p tcp --dport 2377 -j ACCEPT

worker节点:

[root@DS-VM-Nod114-LinuxEA ~]# docker swarm join --token SWMTKN-1-4s87dp8n3mlraw9vd1amq6b2x3007it5n609t6ls82n3lj0mm8-agkh99gkc1ezyk8jnlev0kckz 10.10.240.117:2377
This node joined a swarm as a worker.
[root@DS-VM-Nod114-LinuxEA ~]# 

swarm查看

[root@DS-VM-Node117-LinuxEA ~]# docker node ls
ID                           HOSTNAME                   STATUS  AVAILABILITY  MANAGER STATUS
34lehmydkl4sji5o799y813nr    DS-VM-Nod114-LinuxEA.cluster.com  Ready   Active        
as4u4yh1h5h84y06h2etad4yb *  DS-VM-NodeLinuxEA.cluster.com  Ready   Active        Leader
[root@DS-VM-Node117-LinuxEA ~]# 

在加一台

[root@DS-VM-Node98-LinuxEA ~]# docker swarm join --token SWMTKN-1-4s87dp8n3mlraw9vd1amq6b2x3007it5n609t6ls82n3lj0mm8-agkh99gkc1ezyk8jnlev0kckz 10.10.240.117:2377
This node joined a swarm as a worker.
[root@DS-VM-Node98-LinuxEA ~]#

此时有两台workd加入到集群中

[root@DS-VM-Node117-LinuxEA ~]# docker node ls
ID                           HOSTNAME                   STATUS  AVAILABILITY  MANAGER STATUS
34lehmydkl4sji5o799y813nr    DS-VM-Nod114-LinuxEA.cluster.com  Ready   Active        
as4u4yh1h5h84y06h2etad4yb *  DS-VM-Node117-LinuxEA.cluster.com  Ready   Active        Leader
d464utrj8hgseauht11zddy2i    DS-VM-Node98-LinuxEA.cluster.com   Ready   Active  

我们运行一个服务来大致了解下简单的使用方法:

创建一个服务

[root@DS-VM-Node117-LinuxEA ~]# docker service create --replicas 1 --name helloworld alpine ping docker.com
82q0h1jwpudm91g4fkgr0l3v4
查看
[root@DS-VM-Node117-LinuxEA ~]# docker service ls
ID            NAME        REPLICAS  IMAGE   COMMAND
82q0h1jwpudm  helloworld  0/1       alpine  ping docker.com

查看状态

[root@DS-VM-Node117-LinuxEA ~]# docker service inspect --pretty helloworld
ID:        82q0h1jwpudm91g4fkgr0l3v4
Name:        helloworld
Mode:        Replicated
 Replicas:    1
Placement:
UpdateConfig:
 Parallelism:    1
 On failure:    pause
ContainerSpec:
 Image:        alpine
 Args:        ping docker.com
Resources:
[root@DS-VM-NodeLinuxEA ~]#

此时服务运行在集群的这台机器,我们扩展下,使他运行到work两台上

[root@DS-VM-Node117-LinuxEA ~]# docker service scale helloworld=5
helloworld scaled to 5

查看,有的在启动的状态

[root@DS-VM-Node117-LinuxEA ~]# docker service ps helloworld 
ID                         NAME          IMAGE   NODE                       DESIRED STATE  CURRENT STATE                    ERROR
8odhtpxb0fe4sqaq2t82jbzmg  helloworld.1  alpine  DS-VM-Node117-LinuxEA.cluster.com  Running        Running 7 minutes ago            
aphzf56gddnmznq0scgiec6js  helloworld.2  alpine  DS-VM-Nod114-LinuxEA.cluster.com  Running        Starting less than a second ago  
4hg4qhg9v40w0hc6yyt8oaqmz  helloworld.3  alpine  DS-VM-Nod114-LinuxEA.cluster.com  Running        Starting less than a second ago  
5s62m16vlvsvnu4zqdj6ffen9  helloworld.4  alpine  DS-VM-Node98-LinuxEA.cluster.com   Running        Running less than a second ago   
bgbjw088bb2meqbman2ubz54t  helloworld.5  alpine  DS-VM-Node98-LinuxEA.cluster.com   Running        Running less than a second ago

在查看都启动完成

[root@DS-VM-Node117-LinuxEA ~]# docker service ps helloworld 
ID                         NAME          IMAGE   NODE                       DESIRED STATE  CURRENT STATE                   ERROR
8odhtpxb0fe4sqaq2t82jbzmg  helloworld.1  alpine  DS-VM-Node117-LinuxEA.cluster.com  Running        Running 10 minutes ago          
aphzf56gddnmznq0scgiec6js  helloworld.2  alpine  DS-VM-Nod114-LinuxEA.cluster.com  Running        Running less than a second ago  
4hg4qhg9v40w0hc6yyt8oaqmz  helloworld.3  alpine  DS-VM-Nod114-LinuxEA.cluster.com  Running        Running less than a second ago  
5s62m16vlvsvnu4zqdj6ffen9  helloworld.4  alpine  DS-VM-Node98-LinuxEA.cluster.com   Running        Running less than a second ago  
bgbjw088bb2meqbman2ubz54t  helloworld.5  alpine  DS-VM-Node98-LinuxEA.cluster.com   Running        Running less than a second ago  
[root@DS-VM-Node117-LinuxEA ~]# 

到work机器查看:

[root@DS-VM-Nod114-LinuxEA ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                  PORTS    NAMES
4266875173af        alpine:latest       "ping docker.com"   40 seconds ago      Up Less than a second            helloworld.3.4hg4qhg9v40w0hc6yyt8oaqmz
2fa0d6351756        alpine:latest       "ping docker.com"   40 seconds ago      Up 3 seconds                     helloworld.2.aphzf56gddnmznq0scgiec6js
[root@DS-VM-Nod114-LinuxEA ~]# 
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                 PORTS     NAMES
d0690fd20f3d        alpine:latest       "ping docker.com"   4 minutes ago       Up 4 minutes                     helloworld.4.5s62m16vlvsvnu4zqdj6ffen9
f5e90f235064        alpine:latest       "ping docker.com"   4 minutes ago       Up 4 minutes                     helloworld.5.bgbjw088bb2meqbman2ubz54t
[root@DS-VM-Nod114-LinuxEA ~]# 

删除服务

[root@DS-VM-Node117-LinuxEA ~]# docker service rm helloworld 
helloworld
[root@DS-VM-Node117-LinuxEA ~]# docker service inspect helloworld
[]
Error: no such service: helloworld

即使服务不再存在,任务容器需要几秒钟的时间来清理。您可以使用docker ps节点来验证任务何时被删除。

相关文章

LeaferJS 1.0 重磅发布:强悍的前端 Canvas 渲染引擎
10分钟搞定支持通配符的永久有效免费HTTPS证书
300 多个 Microsoft Excel 快捷方式
一步步配置基于kubeadmin的kubevip高可用
istio全链路传递cookie和header灰度
REST Web 服务版本控制

发布评论