Docker Machine项目

负责在多种平台上快速安装Docker环境 基于go语言实现

支持多种后端驱动,包括虚拟机、本地主机和云平台

先要安装docker-machine

按着教程的写 总提示/usr/local/bin权限不足

只要曲线救国

$ curl -L https://github.com/docker/machine/releases/download/v0.13.0/docker-machine-`uname -s`-`uname -m` >/tmp/docker-machine && \
sudo install /tmp/docker-machine /usr/local/bin/docker-machine

想要创建一个docker主机作为管理节点

发现virtualbox没安装 一搜全是图形界面的

sudo sh -c 'echo "deb http://download.virtualbox.org/virtualbox/debian xenial contrib" >> /etc/apt/sources.list.d/virtualbox.list'

wget -q https://www.virtualbox.org/download/oracle_vbox_2016.asc -O- | sudo apt-key add -

sudo apt update

sudo apt install virtualbox-5.0

首先创建一个Docker主机作为管理节点

docker-machine create -d virtualbox manager
docker-machine ssh manager

Docker Swarm

提供Docker容器集群服务,是Docker官方对容器云平台生态进行支持的核心方案,使用它可以将多个Docker主机封装为单个大型虚拟Docker主机,快速打造一套容器云平台。

Swarm mode内置key-value存储功能,提供了众多的新特性:具有容错能力的去中心化设计内置服务发现、负载平衡、路由网络、动态伸缩、滚动更新、安全传输……

节点

运行Docker主机可以主动初始化一个Swarm集群或者加入一个已经存在的Swarm集群,这样这个运行Docker的主机就成为一个Swarm集群的节点。

节点分为管理节点和工作节点,

管理节点用于Swarm集群的管理,docker swarm命令基本只能在管理节点执行。一个Swarm集群可以有多个管理节点,但只能有一个管理节点可以成为leader,leader通过raft协议实现。

工作节点是任务执行节点,管理节点将服务(service)下发给工作节点执行、管理节点默认也作为工作节点。

 

服务和任务

任务是Swarm中最小的调度单位,目前来说是一个单一的容器

服务是指一组任务的集合,服务定义了任务的属性。有两种模式:1.replicated services按照一定规则在各个工作节点上运行指定个数的任务 2.global services 每个工作节点上运行一个任务  两种模式通过docker service create --mode参数指定

在管理节点初始化一个Swarm集群

 docker-machine ssh manager
                        ##         .
                  ## ## ##        ==
               ## ## ## ## ##    ===
           /"""""""""""""""""\___/ ===
      ~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ /  ===- ~~~
           \______ o           __/
             \    \         __/
              \____\_______/
 _                 _   ____     _            _
| |__   ___   ___ | |_|___ \ __| | ___   ___| | _____ _ __
| '_ \ / _ \ / _ \| __| __) / _` |/ _ \ / __| |/ / _ \ '__|
| |_) | (_) | (_) | |_ / __/ (_| | (_) | (__|   <  __/ |
|_.__/ \___/ \___/ \__|_____\__,_|\___/ \___|_|\_\___|_|
Boot2Docker version 18.06.1-ce, build HEAD : c7e5c3e - Wed Aug 22 16:27:42 UTC 2018
Docker version 18.06.1-ce, build e68fc7a
docker@manager:~$ docker swarm init
Error response from daemon: could not choose an IP address to advertise since this system has multiple addresses on different interfaces (10.0.2.15 on eth0 and 192.168.99.100 on eth1) - specify one with --advertise-addr
docker@manager:~$ docker swarm init --advertise-addr 192.168.99.100
Swarm initialized: current node (3d3p8b4wikno2cidf9lvq4ah9) is now a manager.

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

    docker swarm join --token SWMTKN-1-09ig4rpyxwrodtlyn5b6vduz1wen0y8nlxnq7qn04oupvpzjku-auh2e29vpikkq6kdk0klpxczt 192.168.99.100:2377

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

docker@manager:~$ docker-machine create -d virtualbox worker1
-sh: docker-machine: not found
docker@manager:~$ exit
exit status 127

增加工作节点

zyj@zyj:~$ docker-machine create -d virtualbox worker1
Running pre-create checks...
Creating machine...
(worker1) Copying /home/zyj/.docker/machine/cache/boot2docker.iso to /home/zyj/.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...

This machine has been allocated an IP address, but Docker Machine could not
reach it successfully.

SSH for the machine should still work, but connecting to exposed ports, such as
the Docker daemon port (usually <ip>:2376), may not work properly.

You may need to add the route manually, or use another related workaround.

This could be due to a VPN, proxy, or host file configuration issue.

You also might want to clear any VirtualBox host only interfaces you are not using.
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
zyj@zyj:~$ docker-machine ssh worker1
                        ##         .
                  ## ## ##        ==
               ## ## ## ## ##    ===
           /"""""""""""""""""\___/ ===
      ~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ /  ===- ~~~
           \______ o           __/
             \    \         __/
              \____\_______/
 _                 _   ____     _            _
| |__   ___   ___ | |_|___ \ __| | ___   ___| | _____ _ __
| '_ \ / _ \ / _ \| __| __) / _` |/ _ \ / __| |/ / _ \ '__|
| |_) | (_) | (_) | |_ / __/ (_| | (_) | (__|   <  __/ |
|_.__/ \___/ \___/ \__|_____\__,_|\___/ \___|_|\_\___|_|
Boot2Docker version 18.06.1-ce, build HEAD : c7e5c3e - Wed Aug 22 16:27:42 UTC 2018
Docker version 18.06.1-ce, build e68fc7a
docker@worker1:~$ docker swarm join --token SWMTKN-1-09ig4rpyxwrodtlyn5b6vduz1wen0y8nlxnq7qn04oupvpzjku-auh2e29vpikkq6kdk0kl
pxczt 192.168.99.100:2377
This node joined a swarm as a worker.
docker@worker1:~$ exit
zyj@zyj:~$ docker-machine create -d virtualbox worker2
Running pre-create checks...
Creating machine...
(worker2) Copying /home/zyj/.docker/machine/cache/boot2docker.iso to /home/zyj/.docker/machine/machines/worker2/boot2docker.iso...
(worker2) Creating VirtualBox VM...
(worker2) Creating SSH key...
(worker2) Starting the VM...
(worker2) Check network to re-create if needed...
(worker2) 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...

This machine has been allocated an IP address, but Docker Machine could not
reach it successfully.

SSH for the machine should still work, but connecting to exposed ports, such as
the Docker daemon port (usually <ip>:2376), may not work properly.

You may need to add the route manually, or use another related workaround.

This could be due to a VPN, proxy, or host file configuration issue.

You also might want to clear any VirtualBox host only interfaces you are not using.
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 worker2
zyj@zyj:~$ docker-machine ssh worker2
                        ##         .
                  ## ## ##        ==
               ## ## ## ## ##    ===
           /"""""""""""""""""\___/ ===
      ~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ /  ===- ~~~
           \______ o           __/
             \    \         __/
              \____\_______/
 _                 _   ____     _            _
| |__   ___   ___ | |_|___ \ __| | ___   ___| | _____ _ __
| '_ \ / _ \ / _ \| __| __) / _` |/ _ \ / __| |/ / _ \ '__|
| |_) | (_) | (_) | |_ / __/ (_| | (_) | (__|   <  __/ |
|_.__/ \___/ \___/ \__|_____\__,_|\___/ \___|_|\_\___|_|
Boot2Docker version 18.06.1-ce, build HEAD : c7e5c3e - Wed Aug 22 16:27:42 UTC 2018
Docker version 18.06.1-ce, build e68fc7a
docker@worker2:~$ docker swarm join --token SWMTKN-1-09ig4rpyxwrodtlyn5b6vduz1wen0y8nlxnq7qn04oupvpzjku-auh2e29vpikkq6kdk0kl
"docker swarm join" requires exactly 1 argument.
See 'docker swarm join --help'.

Usage:  docker swarm join [OPTIONS] HOST:PORT

Join a swarm as a node and/or manager
docker@worker2:~$ exit
exit status 1
zyj@zyj:~$ docker node ls
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.38/nodes: dial unix /var/run/docker.sock: connect: permission denied

查看集群

zyj@zyj:~$ docker-machine ssh manager
                        ##         .
                  ## ## ##        ==
               ## ## ## ## ##    ===
           /"""""""""""""""""\___/ ===
      ~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ /  ===- ~~~
           \______ o           __/
             \    \         __/
              \____\_______/
 _                 _   ____     _            _
| |__   ___   ___ | |_|___ \ __| | ___   ___| | _____ _ __
| '_ \ / _ \ / _ \| __| __) / _` |/ _ \ / __| |/ / _ \ '__|
| |_) | (_) | (_) | |_ / __/ (_| | (_) | (__|   <  __/ |
|_.__/ \___/ \___/ \__|_____\__,_|\___/ \___|_|\_\___|_|
Boot2Docker version 18.06.1-ce, build HEAD : c7e5c3e - Wed Aug 22 16:27:42 UTC 2018
Docker version 18.06.1-ce, build e68fc7a
docker@manager:~$ docker node ls
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
3d3p8b4wikno2cidf9lvq4ah9 *   manager             Ready               Active              Leader              18.06.1-ce
kf3cbod1jtjljtpq3wmih1nnd     worker1             Ready               Active                                  18.06.1-ce
docker@manager:~$ exit

部署服务

发现好卡啊 增加服务伸缩的时候不能跨度太大

docker@manager:~$  docker service create --replicas 3 -p 80:80 --name nginx nginx:1.13.7-alpine
Error response from daemon: rpc error: code = InvalidArgument desc = port '80' is already in use by service 'nginx' (25kpadjw7naf5i7pvath4gckq) as an ingress port
docker@manager:~$ docker service ls
ID                  NAME                MODE                REPLICAS            IMAGE                 PORTS
25kpadjw7naf        nginx               replicated          1/3                 nginx:1.13.7-alpine   *:80->80/tcp
docker@manager:~$ docker service ps nginx
ID                  NAME                IMAGE                 NODE                DESIRED STATE       CURRENT STATE              ERROR               PORTS
rpcb6k3om44j        nginx.1             nginx:1.13.7-alpine   worker1             Running             Running 20 minutes ago                         
x2g9q3slk7cj        nginx.2             nginx:1.13.7-alpine   worker2             Running             Preparing 20 minutes ago                       
qmqr63hg83hm        nginx.3             nginx:1.13.7-alpine   manager             Running             Preparing 20 minutes ago                       
docker@manager:~$ docker service logs nginx
nginx.1.rpcb6k3om44j@worker1    | 10.255.0.2 - - [27/Oct/2018:13:09:27 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:63.0) Gecko/20100101 Firefox/63.0" "-"
nginx.1.rpcb6k3om44j@worker1    | 2018/10/27 13:09:27 [error] 6#6: *1 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 10.255.0.2, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "192.168.99.100"
nginx.1.rpcb6k3om44j@worker1    | 10.255.0.2 - - [27/Oct/2018:13:09:27 +0000] "GET /favicon.ico HTTP/1.1" 404 169 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:63.0) Gecko/20100101 Firefox/63.0" "-"
nginx.1.rpcb6k3om44j@worker1    | 10.255.0.3 - - [27/Oct/2018:13:09:53 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:63.0) Gecko/20100101 Firefox/63.0" "-"
nginx.1.rpcb6k3om44j@worker1    | 2018/10/27 13:09:53 [error] 6#6: *2 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 10.255.0.3, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "192.168.99.101"
nginx.1.rpcb6k3om44j@worker1    | 10.255.0.3 - - [27/Oct/2018:13:09:53 +0000] "GET /favicon.ico HTTP/1.1" 404 169 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:63.0) Gecko/20100101 Firefox/63.0" "-"
nginx.1.rpcb6k3om44j@worker1    | 10.255.0.4 - - [27/Oct/2018:13:09:56 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:63.0) Gecko/20100101 Firefox/63.0" "-"
nginx.1.rpcb6k3om44j@worker1    | 2018/10/27 13:09:56 [error] 6#6: *3 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 10.255.0.4, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "192.168.99.102"
nginx.1.rpcb6k3om44j@worker1    | 10.255.0.4 - - [27/Oct/2018:13:09:56 +0000] "GET /favicon.ico HTTP/1.1" 404 169 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:63.0) Gecko/20100101 Firefox/63.0" "-"
nginx.1.rpcb6k3om44j@worker1    | 2018/10/27 13:10:02 [error] 6#6: *2 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 10.255.0.3, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "192.168.99.101"
nginx.1.rpcb6k3om44j@worker1    | 10.255.0.3 - - [27/Oct/2018:13:10:02 +0000] "GET /favicon.ico HTTP/1.1" 404 169 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:63.0) Gecko/20100101 Firefox/63.0" "-"
docker@manager:~$ docker service scale nginx
Invalid scale specifier 'nginx'.
See 'docker service scale --help'.

Usage:  docker service scale SERVICE=REPLICAS [SERVICE=REPLICAS...]

Scale one or multiple replicated services
docker@manager:~$ docker service scale nginx=4
nginx scaled to 4
overall progress: 2 out of 4 tasks 
1/4: running   [==================================================>] 
2/4: preparing [=================================>                 ] 
3/4: running   [==================================================>] 
4/4: preparing [=================================>                 ] 
^COperation continuing in background.
Use `docker service ps nginx` to check progress.
docker@manager:~$ docker service scale nginx=2
nginx scaled to 2
overall progress: 1 out of 2 tasks 
1/2: No such image: nginx:1.13.7-alpine@sha256:34aa80bb22c79235d466ccbbfa3659ff… 
2/2: preparing [=================================>                 ] 
verify: Detected task failure 
^COperation continuing in background.
Use `docker service ps nginx` to check progress.
docker@manager:~$ 
docker@manager:~$ docker service scale nginx=1
nginx scaled to 1
overall progress: 1 out of 1 tasks 
1/1: No such image: nginx:1.13.7-alpine@sha256:34aa80bb22c79235d466ccbbfa3659ff… 
verify: Service converged 
docker@manager:~$ docker service scale nginx=2
nginx scaled to 2
overall progress: 2 out of 2 tasks 
1/2: running   [==================================================>] 
2/2: running   [==================================================>] 
verify: Service converged 
docker@manager:~$ docker service scale nginx=3
nginx scaled to 3
overall progress: 3 out of 3 tasks 
1/3: running   [==================================================>] 
2/3: running   [==================================================>] 
3/3: running   [==================================================>] 
verify: Service converged 
docker@manager:~$ docker service scale nginx=4
nginx scaled to 4
overall progress: 4 out of 4 tasks 
1/4: running   [==================================================>] 
2/4: running   [==================================================>] 
3/4: running   [==================================================>] 
4/4: running   [==================================================>] 
verify: Service converged 
docker@manager:~$ docker service ps nginx
ID                  NAME                IMAGE                 NODE                DESIRED STATE       CURRENT STATE            ERROR                              PORTS
rpcb6k3om44j        nginx.1             nginx:1.13.7-alpine   worker1             Running             Running 35 minutes ago                                      
nmydx1pdczmz        nginx.2             nginx:1.13.7-alpine   worker1             Running             Running 4 minutes ago                                       
szs1czc5rl3b         \_ nginx.2         nginx:1.13.7-alpine   worker2             Shutdown            Rejected 5 minutes ago   "No such image: nginx:1.13.7-a…"   
nxiimbadqo5l         \_ nginx.2         nginx:1.13.7-alpine   worker2             Shutdown            Rejected 5 minutes ago   "No such image: nginx:1.13.7-a…"   
r467n114oprv         \_ nginx.2         nginx:1.13.7-alpine   worker2             Shutdown            Rejected 5 minutes ago   "No such image: nginx:1.13.7-a…"   
2olvgqrc8oho         \_ nginx.2         nginx:1.13.7-alpine   manager             Shutdown            Rejected 5 minutes ago   "No such image: nginx:1.13.7-a…"   
me25so8xq05m        nginx.3             nginx:1.13.7-alpine   worker1             Running             Running 3 minutes ago                                       
tfas8njxfdoi         \_ nginx.3         nginx:1.13.7-alpine   worker2             Shutdown            Rejected 3 minutes ago   "No such image: nginx:1.13.7-a…"   
nqvg5cmuhtq7         \_ nginx.3         nginx:1.13.7-alpine   worker2             Shutdown            Rejected 4 minutes ago   "No such image: nginx:1.13.7-a…"   
9j4t3hmsj1hj         \_ nginx.3         nginx:1.13.7-alpine   worker2             Shutdown            Rejected 4 minutes ago   "No such image: nginx:1.13.7-a…"   
ddfv6esx5rc0         \_ nginx.3         nginx:1.13.7-alpine   manager             Shutdown            Rejected 4 minutes ago   "No such image: nginx:1.13.7-a…"   
x69ojwjapiek        nginx.4             nginx:1.13.7-alpine   worker1             Running             Running 2 minutes ago                                       
q9qft98oe0pn         \_ nginx.4         nginx:1.13.7-alpine   worker2             Shutdown            Rejected 2 minutes ago   "No such image: nginx:1.13.7-a…"   
8tqtosdf7m6j         \_ nginx.4         nginx:1.13.7-alpine   worker2             Shutdown            Rejected 2 minutes ago   "No such image: nginx:1.13.7-a…"   
1tn3fn92r6sb         \_ nginx.4         nginx:1.13.7-alpine   worker2             Shutdown            Rejected 2 minutes ago   "No such image: nginx:1.13.7-a…"   
ugaz2553rmn7         \_ nginx.4         nginx:1.13.7-alpine   worker2             Shutdown            Rejected 2 minutes ago   "No such image: nginx:1.13.7-a…"