Docker Swarm 集群与编排镜像

准备

  • 一台Ubuntu电脑,两台Windows电脑
    10.0.1.97 — Linux系统主机名studyserver (manager-node)
    10.0.0.95 — Windows系统虚拟主机名army-worker1和army-worker2 (worker-node)
    10.0.0.97 — Windows系统虚拟主机名rommy-worker1和rommy-worker2 (worker-node)
  • swarm主机间通信需要开放以下端口
    -TCP port 2377为集群管理通信
    -TCP and UDP port 7946 为节点间通信
    -UDP port 4789 为网络间流量
    方便起见,直接关闭两台主机的防火墙
    systemctl stop firewalld.service 停止firewall
    systemctl disable firewalld.service 禁止firewall开机启动

使用Swarm

  1. 创建一个swarm集群
    docker swarm init --advertise-addr 10.0.1.97
    --advertise-addr 指定IP,其他节点通过这个IP找到管理节点

    phoenix@studyserver:~$ docker swarm init --advertise-addr 10.0.1.97
    Swarm initialized: current node (k6rkdy8bk2rr141f0j7i824b4) is now a manager.
    
    To add a worker to this swarm, run the following command:
    
        docker swarm join --token SWMTKN-1-1h1edgale8r4833jm72f6p1iavp02z4jldeecdwd8r8jujalb7-9qymj7cgpcj8077wh9ljjwkui 10.0.1.97:2377
    
    To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
    
    phoenix@studyserver:~$
    

    这样就已经创建好了一个swarm集群,并且自动将当前主机加入集群,成为manager节点,大部分操作都必须在manager节点上执行

  1. 加入集群
    windows系统创建虚拟机, 依赖:

    1. VirtualBox-6.0.14-133895-Win.exe (https://www.virtualbox.org/wiki/Downloads)
    2. Docker Desktop
    C:\Users\rommy.yang>docker-machine create --driver virtualbox rommy-worker1

    我们在10.0.0.95上执行

    docker swarm join --token SWMTKN-1-1h1edgale8r4833jm72f6p1iavp02z4jldeecdwd8r8jujalb7-9qymj7cgpcj8077wh9ljjwkui 10.0.1.97:2377就可以加入该集群,并且成为一个工作节点

     C:\Users\vm> docker-machine ssh army-worker1
       ( '>')
      /) TC (\   Core is distributed with ABSOLUTELY NO WARRANTY.
     (/-_--_-\)           www.tinycorelinux.net
    
    docker@army-worker1:~$ docker swarm join --token SWMTKN-1-1h1edgale8r4833jm72f6p1iavp02z4jldeecdwd8r8jujalb7-9qymj7cgpcj8077wh9ljjwkui 10.0.1.97:2377
    docker@army-worker1:~$ exit
    
     C:\Users\vm> docker-machine ssh army-worker2
       ( '>')
      /) TC (\   Core is distributed with ABSOLUTELY NO WARRANTY.
     (/-_--_-\)           www.tinycorelinux.net
    
    docker@army-worker2:~$ docker swarm join --token SWMTKN-1-1h1edgale8r4833jm72f6p1iavp02z4jldeecdwd8r8jujalb7-9qymj7cgpcj8077wh9ljjwkui 10.0.1.97:2377
    docker@army-worker2:~$ exit 
    

    如果忘了命令,可以在管理节点执行docker swarm join-token worker 同样会显示这条命令

  2. 查看节点,在管理节点执行
    docker node ls 可以查看所有节点

    phoenix@studyserver:~$ docker node ls
    ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
    x1ryautlktq5whqpe3357nvmd     army-worker1        Ready               Active                                  19.03.5
    yjsmxcqi7pmot8v4htuafxls4     army-worker2        Ready               Active                                  19.03.5
    iwgswb71f4of13g12vd9m27dm     rommy-worker1       Ready               Active                                  19.03.5
    jsxqcobfzyewjaqhvds8tp3r0     rommy-worker2       Ready               Active                                  19.03.5
    k6rkdy8bk2rr141f0j7i824b4 *   studyserver         Ready               Active              Leader              18.09.7
    

    Leader就表明了哪个节点是管理节点

创建网络

为了在集群中实现跨主机通信,我们需要创建一个overlay网络,与创建bridge网络类似

docker network create \
 -d overlay my_overlay_network \
 --subnet 10.10.0.0/24 \
 --gateway 10.10.0.1

-d 指定网络模式为overlay
--subnet 可以指定网段
-gateway 指定网关

 

Stack部署

Stack就是Service的集合
类似docker-compose.yml我们也可以编写stack.yml统一创建服务

新建stack.yml

version: '3'
services:
  mssql:
    image: 10.0.1.97:5000/mssql:latest
    hostname: mssql
    container_name: v-mssql
    volumes:
     - ./data:/var/opt/mssql
    environment:
     - ACCEPT_EULA=Y
     - SA_PASSWORD=Guang@8620
    restart: always
    deploy:
      replicas:1
    networks:
      - my_overlay_network
    ports:
     - "1433:1433"

  dockerMssql:
    image: 10.0.1.97:5000/docker-mssql:latest
    hostname: docker-mssql
    container_name: v-docker-mssql
    volumes:
     - ./logs:/logs
    restart: always
    deploy:
      replicas: 2
    networks:
      - my_overlay_network
    ports:
     - "8821:8080"
    links:
     - mssql:mssql
    depends_on:
     - mssql

    command: java -jar /app.jar --server.port=8080 --spring.profiles.active=docker

networks:
  my_overlay_network:
    external: true

部署stack

phoenix@studyserver:/var/docker/swarm$ docker stack deploy my_stack --compose-file=stack.yml

部署完成

phoenix@studyserver:/var/docker/swarm$ docker stack services my_stack
ID                  NAME                   MODE                REPLICAS            IMAGE                                PORTS
um0gt8usr44p        my_stack_mssql         replicated          0/1                 10.0.1.97:5000/mssql:latest          *:1433->1433/tcp
ute5rkay84u3        my_stack_dockerMssql   replicated          1/2                 10.0.1.97:5000/docker-mssql:latest   *:8821->8080/tcp

执行后可以看到,成功创建了3个服务

phoenix@studyserver:/var/docker/swarm$ docker stack ps my_stack
ID                  NAME                         IMAGE                                NODE                DESIRED STATE       CURRENT STATE                 ERROR                              PORTS
lalcadx4qer6        my_stack_mssql.1             10.0.1.97:5000/mssql:latest          army-worker2        Running             Preparing 20 seconds ago
ffi229m6x0e3        my_stack_dockerMssql.1       10.0.1.97:5000/docker-mssql:latest   studyserver         Running             Running 15 seconds ago
95a75afwuuih        my_stack_mssql.1             10.0.1.97:5000/mssql:latest          army-worker1        Shutdown            Rejected 20 seconds ago       "invalid mount config for type…"
btkz7rsojnpn         \_ my_stack_mssql.1         10.0.1.97:5000/mssql:latest          army-worker2        Shutdown            Rejected 55 seconds ago       "invalid mount config for type…"
xu835bf2bji7         \_ my_stack_mssql.1         10.0.1.97:5000/mssql:latest          army-worker1        Shutdown            Rejected about a minute ago   "invalid mount config for type…"
h48dauv85dot        my_stack_dockerMssql.1       10.0.1.97:5000/docker-mssql:latest   studyserver         Shutdown            Failed 22 seconds ago         "task: non-zero exit (1)"
lrfdhwsgea07        my_stack_mssql.1             10.0.1.97:5000/mssql:latest          army-worker1        Shutdown            Rejected about a minute ago   "invalid mount config for type…"
qejbmpy7mrfp        my_stack_dockerMssql.1       10.0.1.97:5000/docker-mssql:latest   studyserver         Shutdown            Failed about a minute ago     "task: non-zero exit (1)"
o8dtqaxpbk6j         \_ my_stack_dockerMssql.1   10.0.1.97:5000/docker-mssql:latest   army-worker2        Shutdown            Rejected about a minute ago   "invalid mount config for type…"
kxgcc1tl75w3         \_ my_stack_dockerMssql.1   10.0.1.97:5000/docker-mssql:latest   army-worker2        Shutdown            Rejected about a minute ago   "invalid mount config for type…"
qiv5lxh1w16q        my_stack_dockerMssql.2       10.0.1.97:5000/docker-mssql:latest   rommy-worker1       Running             Preparing 54 seconds ago
wbymre3474wn         \_ my_stack_dockerMssql.2   10.0.1.97:5000/docker-mssql:latest   army-worker1        Shutdown            Rejected 58 seconds ago       "invalid mount config for type…"
ywnlwx91mi3t         \_ my_stack_dockerMssql.2   10.0.1.97:5000/docker-mssql:latest   rommy-worker1       Shutdown            Rejected about a minute ago   "invalid mount config for type…"
vx1neaov63q4         \_ my_stack_dockerMssql.2   10.0.1.97:5000/docker-mssql:latest   rommy-worker1       Shutdown            Rejected about a minute ago   "invalid mount config for type…"
mpn48dbl9tcr         \_ my_stack_dockerMssql.2   10.0.1.97:5000/docker-mssql:latest   rommy-worker1       Shutdown            Rejected about a minute ago   "invalid mount config for type…"

搭建Portainer

 

$ curl -L https://downloads.portainer.io/portainer-agent-stack.yml -o portainer-agent-stack.yml
$ docker stack deploy --compose-file=portainer-agent-stack.yml portainer

您只需要使用浏览器访问运行portainer的Docker引擎的9000端口。

http://10.0.1.97:9000

滚动至顶部