Docker Swarm环境搭建02

1、本节介绍一下stack的操作

2、环境介绍

主机名 IP地址 节点类别
kub01 172.16.172.71 manager
kub02 172.16.172.72 worker
kub03 172.16.172.73 worker

3、启动环境

#在kub01初始化swarm,会输出一个token
sudo docker swarm init --advertise-addr 172.16.172.71

#kub02和kub03加入该swarm
sudo docker swarm join \
    --token SWMTKN-1-249jjodetz6acnl0mrvotp3ifl4jnd2s53buweoasfedx695jm-cdjp3v2jjq2ndfxlv8o2g49n9 \
    172.16.172.71:2377

#查看节点信息
sudo docker node ls

4、新建docker-compose.yml文件

version: "3"
services:
  web:
    image: myserver:1.0.0
    deploy:
      replicas: 10
      restart_policy:
        condition: on-failure
      resources:
        limits:
          cpus: "0.1"
          memory: 64M
    ports:
      - "8080:8080"
    networks:
      - webnet
  visualizer:
    image: dockersamples/visualizer:stable
    ports:
      - "8090:8080"
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock"
    deploy:
      placement:
        constraints: [node.role == manager]
    networks:
      - webnet
  redis:
    image: redis:3.2
    ports:
      - "6379:6379"
    volumes:
      - /home/hiup/dockervisual/data:/data
    deploy:
      placement:
        constraints: [node.role == manager]
    command: redis-server --appendonly yes
    networks:
      - webnet
networks:
  webnet:

5、需要确认的信息
要保证三个镜像都是可用的
要保证redis的数据文件夹是可用的
三个服务的networks是一样的

6、启用stack

#启动stack
sudo docker stack deploy -c docker-compose.yml mystack01

#查看stack
sudo docker stack ls

#查看服务
sudo docker service ls

7、用浏览器打开本地8080端口,可以在visualizer看到各容器的状态

8、停止stack

停止
sudo docker stack rm mystack01

9、节点退出swarm

#kub02和kub03退出swarm
sudo docker swarm leave

#kub01退出swarm,由于最后一个管理node也退出了,swarm结束
sudo docker swarm leave --force

Comments are closed.