docker集群操作-swarm

环境准备

  1. centos7设置主机名

    1
    2
    # hostnamectl set-hostname swarm-node01
    # bash
  2. 安装docker-ce
    vim /etc/yum.repo.d/docker-ce.repo

    1
    2
    3
    4
    5
    6
    [docker-ce-stable]
    name=Docker CE Stable - $basearch
    baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/$basearch/stable
    enabled=1
    gpgcheck=1
    gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg

yum -y install docker-ce
systemctl start docker.service
systemctl enable docker.service

vim /etc/docker/daemon.json

1
2
3
{
"insecure-registries": ["192.168.18.144:5000" ]
}

systemctl restart docker.service

  1. 集群信息
    1
    2
    3
    4
    ip              hostname
    192.168.18.144 swarm-manager
    192.168.18.143 swarm-node01
    192.168.18.139 swarm-node02

创建集群

  1. manager节点创建集群

    1
    2
    3
    4
    5
    6
    7
    8
    [root@swarm-manager ~]# docker swarm init --advertise-addr 192.168.18.144
    Swarm initialized: current node (kr2eg2qxh4osz5oimhvdp2ge2) is now a manager.

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

    docker swarm join --token SWMTKN-1-122qbqs2mtppjkv8i8xyel5ozcupuzlwcbln1regjfr8qhm7k4-6w8o15u0868kouuz5jxkhf1vq 192.168.18.144:2377

    To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
  2. 查看节点信息

    1
    2
    3
    4
    5
    [root@swarm-manager ~]# docker node ls
    ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
    kr2eg2qxh4osz5oimhvdp2ge2 * swarm-manager Ready Active Leader 19.03.8
    pyzo42dqj7fwdhzhu8ffmod0n swarm-node01 Ready Active 19.03.8
    q3gx53izmcwelw08e6kiz2s2j swarm-node02 Ready Active 19.03.8
  3. 向集群添加节点

    1
    docker swarm join --token SWMTKN-1-122qbqs2mtppjkv8i8xyel5ozcupuzlwcbln1regjfr8qhm7k4-6w8o15u0868kouuz5jxkhf1vq 192.168.18.144:2377

执行任务

  1. 创建任务
    1
    2
    3
    4
    5
    6
    docker service create \
    --replicas 3 \
    -p 80:80 \
    --with-registry-auth \
    --name nginx \
    192.168.18.144:5000/nginx:1.17.9
1
2
3
4
5
6
docker service create \
--replicas 2 \
-p 1521:1521 \
--with-registry-auth \
--name oracle11g \
192.168.18.144:5000/oracle/database:11.2.0.4.0
  1. 查看任务

    1
    docker service ls
  2. 查看任务详情

    1
    docker service ps nginx
  3. 删除任务

    1
    docker service rm nginx
  4. 服务参数更新

    1
    docker service update --replicas=3 nginx

集群查看工具

  1. 部署docker-visualizer
    单机部署visualizer
    1
    docker run -d -p 8080:8080 -v /var/run/docker.sock:/var/run/docker.sock --name visualizer 192.168.18.144:5000/dockersamples/visualizer

集群部署

1
2
3
4
5
6
docker service create \
--name=viz \
--publish=8080:8080/tcp \
--constraint=node.role==manager \
--mount=type=bind,src=/var/run/docker.sock,dst=/var/run/docker.sock \
192.168.18.144:5000/dockersamples/visualizer
  1. 搭建Portainer
    Portainer是一个管理swarm的UI工具
    1
    2
    3
    4
    5
    6
    7
    docker service create \
    --name portainer \
    --publish 9000:9000 \
    --constraint 'node.role == manager' \
    --mount type=bind,src=/var/run/docker.sock,dst=/var/run/docker.sock \
    192.168.18.144:5000/portainer/portainer:1.23.2 \
    -H unix:///var/run/docker.sock

注意点

1.有新版本镜像时如何自动更新到各个服务?

  1. 创建服务
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    docker service create \
    --replicas 3 \
    -p 80:80 \
    --with-registry-auth \
    --name nginx \
    192.168.18.144:5000/nginx:1.17.9
    ```
    使用`--with-registry-auth`参数
    使用已登录的认证信息,不配置会报no such image
    2. 滚动更新

docker service update nginx
–image 192.168.18.144:5000/nginx:1.17.10
```