(六)Docker三剑客之Swarm

1. 创建swarm集群

  1. 创建一个管理节点
[root@Thor swarm]# docker swarm init
Swarm initialized: current node (x61xl0lp40736m963leujqypc) is now a manager.

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

    docker swarm join 
    --token SWMTKN-1-1i2q9napdouz8iimheo9ht9x9hsunffoaiwa2ixyhhcxyh3pu9-0t7xglalpspwop0q7hkfhlixp 
    192.168.99.1:2377

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

[root@Thor swarm]# 
  1. 创建两个工作节点:
  • 创建一个docker-machine
  • docker swarm join 到管理节点
[root@Thor swarm]# docker-machine create -d virtualbox worker1
Running pre-create checks...
Creating machine...
(worker1) Copying /root/.docker/machine/cache/boot2docker.iso to /root/.docker/machine/machines/worker1/boot2docker.iso...
(worker1) Creating VirtualBox VM...
(worker1) Creating SSH key...
(worker1) Starting the VM...
(worker1) Check network to re-create if needed...
(worker1) Waiting for an IP...
Waiting for machine to be running, this may take a few minutes...
Detecting operating system of created instance...
Waiting for SSH to be available...
Detecting the provisioner...
Provisioning with boot2docker...
Copying certs to the local machine directory...
Copying certs to the remote machine...
Setting Docker configuration on the remote daemon...
Checking connection to Docker...
Docker is up and running!
To see how to connect your Docker Client to the Docker Engine running on this virtual machine, run: docker-machine env worker1
[root@Thor swarm]# 
[root@Thor swarm]# docker-machine ssh worker1
                        ##         .
                  ## ## ##        ==
               ## ## ## ## ##    ===
           /"""""""""""""""""\___/ ===
      ~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ /  ===- ~~~
           \______ o           __/
                          __/
              \____\_______/
 _                 _   ____     _            _
| |__   ___   ___ | |_|___  __| | ___   ___| | _____ _ __
| '_  / _  / _ | __| __) / _` |/ _  / __| |/ / _  '__|
| |_) | (_) | (_) | |_ / __/ (_| | (_) | (__|   <  __/ |
|_.__/ \___/ \___/ \__|_____\__,_|\___/ \___|_|\_\___|_|
Boot2Docker version 18.06.0-ce, build HEAD : 1f40eb2 - Thu Jul 19 18:48:09 UTC 2018
Docker version 18.06.0-ce, build 0ffa825
docker@worker1:~$ 
docker@worker1:~$ docker swarm join 
>     --token SWMTKN-1-1i2q9napdouz8iimheo9ht9x9hsunffoaiwa2ixyhhcxyh3pu9-0t7xglalpspwop0q7hkfhlixp 
>     192.168.99.1:2377
This node joined a swarm as a worker.
docker@worker1:~$ 

2. 查看集群

[root@Thor swarm]# docker node ls
ID                           HOSTNAME  STATUS  AVAILABILITY  MANAGER STATUS
46fbdlksvu4w49lqqw9x9utgw    worker1   Ready   Active        
x61xl0lp40736m963leujqypc *  Thor      Ready   Active        Leader
xyscoh7b2s62cl7gdvol1t642    worker2   Ready   Active

3. 启动服务

[root@Thor swarm]# docker service create --replicas 3 --name httpd -p 777:80 httpd:latest
kyn022pzs4ap11wa5gz7qlbyq
[root@Thor swarm]#

在另一台设备上测试

[root@zbops ]$ curl 10.86.10.214:777
<html><body><h1>It works!</h1></body></html>

4. 查看服务

[root@Thor swarm]# docker service ps httpd
ID            NAME     IMAGE         NODE     DESIRED STATE  CURRENT STATE             ERROR  PORTS
2rswqj0s0r1b  httpd.1  httpd:latest  worker2  Running        Preparing 37 seconds ago         
tk1wem0defjv  httpd.2  httpd:latest  Thor     Running        Running 36 seconds ago           
wrydby42gw0d  httpd.3  httpd:latest  worker1  Running        Preparing 37 seconds ago 

5. 配置

配置文件的管理方式:

  1. 配置文件放入镜像;
  2. 设置环境变量;
  3. volume 动态挂载;
    这3中方式,都降低了镜像的通用性。

Docker 17.06 及以上版本,新增了docker config 子命令来管理集中中的配置信息,无需将配置文件放入镜像或挂载的方式即可实现对服务的配置。但 docker config 只能在 docker swarm集群中使用。

以前我们通过监听主机目录来配置 Redis,就需要在集群的每个节点放置该文件。
如果采用docker config 来管理服务的配置信息,我们只需在集群中的管理节点manager创建 config ,当部署服务时,集群会自动的将配置文件分发到运行服务的各个节点中,大大降低了配置信息的管理和分发难度。

帮助文档如下:

[root@PsqtestA10-211 ]# docker config --help

Usage:  docker config COMMAND

Manage Docker configs

Options:


Commands:
  create      Create a config from a file or STDIN
  inspect     Display detailed information on one or more configs
  ls          List configs
  rm          Remove one or more configs

Run 'docker config COMMAND --help' for more information on a command.

1)创建

[root@PsqtestA10-211 redis]# docker config create redis.conf redis.conf
knyrzmssepg5jr4kcod19fxrq

2)查看

[root@PsqtestA10-211 redis]# docker config ls
ID                          NAME                CREATED             UPDATED
knyrzmssepg5jr4kcod19fxrq   redis.conf          5 seconds ago       5 seconds ago

3)使用(docker swarm 为前提)

[root@PsqtestA10-211 redis]# docker service create --name redis --config redis.conf -p 6379:6379 redis redis-server /redis.conf
y05kisry81yarvau5c8etzg5l
overall progress: 1 out of 1 tasks 
1/1: running   [==================================================>] 
verify: Service converged 

[root@PsqtestA10-211 redis]# docker service ls
ID                  NAME                MODE                REPLICAS            IMAGE               PORTS
y05kisry81ya        redis               replicated          1/1                 redis:latest        *:6379->6379/tcp

4)redis-cli

[root@PsqtestA10-211 redis]# ./redis-cli -p 6379
127.0.0.1:6377> keys *
HISTORY: /root/.rediscli_history
(empty list or set)
127.0.0.1:6377> set age 26
HISTORY: /root/.rediscli_history
OK
127.0.0.1:6377> get age
HISTORY: /root/.rediscli_history
"26"
127.0.0.1:6377> 
原文地址:https://www.cnblogs.com/walkinginthesun/p/9432085.html