如何部署Docker多物理机集群(Swarm)?

Docker

1. 部署基础环境

学习本章,请先按照如下教程部署Docker环境,
https://www.cmdschool.org/archives/2183
另外,我们还建议你先学习以下章节,
https://www.cmdschool.org/archives/2276
https://www.cmdschool.org/archives/2288

2 搭建Swarm集群

2.1 Swarm集群的基础知识

– Swarm集群是一组运行Docker并加入集群中的机器
– 加入集群后,运行的Dckcek将在集群管理器中执行
– 群体中的机器可以是物理节点也可以是虚拟节点
– Swarm集群支持多种策略运行容器(例如应用运行在负载最低节点)
– 集群管理者是集群中唯一可以执行命令的机器或授权的其他机器

2.2 配置公钥认证(可选)

In docker-m

su - docker
ssh-keygen -t rsa

向导如下:

Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
[…]

注:一路回车

ssh-copy-id -i ~/.ssh/id_rsa.pub docker@docker-n1
ssh-copy-id -i ~/.ssh/id_rsa.pub docker@docker-n2

测试公钥认证:

ssh docker@docker-n1
ssh docker@docker-n2

2.3 建立你的集群

2.3.1 执行的步骤

运行“docker swarm init”启用集群模式
运行“docker swarm join”将新的节点加入集群

2.3.2 管理主机启用集群

In docker-m

su - docker
docker swarm init

提示如下:

Swarm initialized: current node (a6rzys411891zfqd5pjmifv9u) is now a manager.

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

    docker swarm join --token SWMTKN-1-2o1l0zx4ow0av23rvffgi8827t6dk8zobmscph7ypscgplm61p-ejp2a1v7665uyw2yr0yoft3zv 10.168.0.60:2377

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

2.3.3 配置管理主机防火墙

In docker-m & docker-n[1-2]

su -
firewall-cmd --permanent --add-port 2377/tcp
firewall-cmd --reload 
firewall-cmd --list-all

2.3.4 将成员主机加入集群

In docker-n[1-2]

su - docker
docker swarm join --token SWMTKN-1-2o1l0zx4ow0av23rvffgi8827t6dk8zobmscph7ypscgplm61p-ejp2a1v7665uyw2yr0yoft3zv 10.168.0.60:2377

2.3.5 查看群集成员

In docker-m

docker node ls

显示如下:

ID                            HOSTNAME                STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
i54968m5j6lh4oph9ua66436p     docker-n1.cmdschool.org   Ready               Active                                  18.03.1-ce
ozije5a3sfj3akyzocasscqny     docker-n2.cmdschool.org   Ready               Active                                  18.03.1-ce
a6rzys411891zfqd5pjmifv9u *   docker-m.cmdschool.org   Ready               Active              Leader              18.03.1-ce

2.4 部署群集应用

2.4.1 集群中部署应用

In docker-m

su – docker
docker stack rm getstartedlab
docker stack deploy --with-registry-auth -c /data/docker/app-python/docker-compose.yml getstartedlab

注:需要使用参数“with-registry-auth”向群集中的其他成员主机宣告公共镜像的源,否则运行会出错

2.4.2 检查应用部署

In docker-m

2.4.3 查看服务

docker service ls

显示如下:

ID                  NAME                MODE                REPLICAS            IMAGE                          PORTS
j02kso7vyfcd        getstartedlab_web   replicated          5/5                 userxxx/app-python:part1   *:80->80/tcp

注:留意服务的名称“getstartedlab_web”,因为下面要用到

2.4.4 查看服务的节点

docker service ps getstartedlab_web

显示如下:

ID                  NAME                  IMAGE                          NODE                      DESIRED STATE       CURRENT STATE            ERROR               PORTS
p8k6ylglw8vm        getstartedlab_web.1  userxxx/app-python:part1   docker-n1.cmdschool.org   Running             Running 26 minutes ago               
ybfuq9v9pha5        getstartedlab_web.2  userxxx/app-python:part1   docker-n2.cmdschool.org   Running             Running 26 minutes ago               
khcb473osyvz        getstartedlab_web.3  userxxx/app-python:part1   docker-m.cmdschool.org    Running             Running 26 minutes ago               
x9ofvzj3c7oh        getstartedlab_web.4  userxxx/app-python:part1   docker-n1.cmdschool.org   Running             Running 26 minutes ago               
eqx6fphyfcf3        getstartedlab_web.5  userxxx/app-python:part1   docker-n2.cmdschool.org   Running             Running 26 minutes ago     

注:以上可知,应用已经自动分布到集群的不同位置

2.4.5 测试应用运行

curl http://127.0.0.1:80
ssh docker-n1 curl http://127.0.0.1:80
ssh docker-n2 curl http://127.0.0.1:80

参阅文档:
=============================

基本概念:
—————————–
https://docs.docker.com/get-started/#test-docker-version

安装Docker:
—————————–
https://docs.docker.com/install/

官方源地址:
—————————–
https://download.docker.com/linux/centos/7/x86_64/stable/Packages/

没有评论

发表评论

Docker
如何打包Docker MAC-Telnet?

1 基础知识 一款可以使用MikroTik RouterOS MAC-Telnet协议连接Mikro …

Docker
如何部署Docker Unifi-WiFi?

1 前言 一个问题,一篇文章,一出故事。 笔者Unifi-WiFi想要迁移至docker环境,于是产 …

Docker
如何打包Docker Unifi-WiFi?

1 基础知识 正式安装前,我们建议你先熟悉以下章节, 如何基于Debian部署UBNT Unifi控 …