目录
一、系统准备
二、准备应用
三、准备镜像
四、创建虚拟主机
五、配置集群环境
六、部署服务
1、直接部署镜像
2、通过yml文件部署
七、状态查询
1、Windows必须都是64位的。
2、只有Windows 10中的Pro, Enterprise or Education版本,才能安装Docker Desktop for Windows。Windows 7~Windows 10 Home,只能安装Docker Toolbox on Windows。
3、必须在BIOS中开启虚拟化。
本文以Windows 10 Home版为例进行讲解,所以安装Docker Toolbox on Windows
本文将直接使用Docker官网上的例子。
app.py
from flask import Flask
from redis import Redis, RedisError
import os
import socket
# Connect to Redis
redis = Redis(host="redis", db=0, socket_connect_timeout=2, socket_timeout=2)
app = Flask(__name__)
@app.route("/")
def hello():
try:
visits = redis.incr("counter")
except RedisError:
visits = "cannot connect to Redis, counter disabled"
html = "Hello {name}!
" \
"Hostname: {hostname}
" \
"Visits: {visits}"
return html.format(name=os.getenv("NAME", "world"), hostname=socket.gethostname(), visits=visits)
if __name__ == "__main__":
app.run(host='0.0.0.0', port=80)
requirements.txt
Flask
Redis
Dockerfile
# Use an official Python runtime as a parent image
FROM python:2.7-slim# Set the working directory to /app
WORKDIR /app# Copy the current directory contents into the container at /app
COPY . /app# Install any needed packages specified in requirements.txt
RUN pip install --trusted-host pypi.python.org -r requirements.txt# Make port 80 available to the world outside this container
EXPOSE 80# Define environment variable
ENV NAME World# Run app.py when the container launches
CMD ["python", "app.py"]
docker-compose.yml。testname必须是当前登录的用户名,如果没有登录,请先使用docker login登录。另外,可以通过docker info查看当前登录的用户。
version: "3"
services:
web:
# replace username/repo:tag with your name and image details
image: testname/friendlyhello:1.0
deploy:
replicas: 5
resources:
limits:
cpus: "0.1"
memory: 50M
restart_policy:
condition: on-failure
ports:
- "4000:80"
networks:
- webnet
networks:
webnet:
创建一个镜像
$ docker build --tag=friendlyhello .
查看已创建的镜像
$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
friendlyhello latest 186850645063 13 seconds ago 131MB
由于需要其它虚拟主机访问此镜像,所以需要将镜像共享,而共享时需要设置用户名和版本,所以作如下修改:
$ docker image tag friendlyhello:latest testname/friendlyhello:1.0
查看已创建的镜像
$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
friendlyhello latest 186850645063 17 hours ago 131MB
testname/friendlyhello 1.0 186850645063 17 hours ago 131MB
然后将生成的镜像上传到Docker Hub上,便于其它虚拟主机获取
$ docker push testname/friendlyhello:1.0
为了测试方便,我们在同一个物理机器上创建虚拟主机。创建三个虚拟主机:default、myvm1、myvm2
$ docker-machine create --driver virtualbox default
$ docker-machine create --driver virtualbox myvm1
$ docker-machine create --driver virtualbox myvm2
查询创建结果,其中URL表示此主机的管理URL,当构建集群时,使用此URL进行通讯
$ docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
default * virtualbox Running tcp://192.168.99.100:2376 v18.09.4
myvm1 - virtualbox Running tcp://192.168.99.101:2376 v18.09.4
myvm2 - virtualbox Running tcp://192.168.99.102:2376 v18.09.4
如果有虚拟主机没有启动,则启动它们
$ docker-machine start myvm1
$ docker-machine start myvm2
如果想看虚拟主机中设置有哪些环境变量
$ docker-machine env myvm1
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.99.101:2376"
export DOCKER_CERT_PATH="C:\Users\yeaxi\.docker\machine\machines\myvm1"
export DOCKER_MACHINE_NAME="myvm1"
export COMPOSE_CONVERT_WINDOWS_PATHS="true"
# Run this command to configure your shell:
# eval $("C:\Docker Toolbox\docker-machine.exe" env myvm1)
让default主机作为管理主机,--advertise-addr表示管理主机对外提供的访问地址,集群节点可以通过此地址访问此管理主机
$ docker-machine ssh default "docker swarm init --advertise-addr 192.168.99.100"
此命令执行完成后,会开放2377端口,并生成一个Token,用于将集群节点添加到此集群
$ docker-machine ssh myvm1 "docker swarm join --token XXXXXXXXXXXXXXXXXXXXXXX 192.168.99.100:2377"
$ docker-machine ssh myvm2 "docker swarm join --token XXXXXXXXXXXXXXXXXXXXXXX 192.168.99.100:2377"
添加完成后,可以查看状态,其中default已成为manager了
$ docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
camlgebkgkccn8lvw8nixk0y4 * default Ready Active Leader 18.09.4
r388lxf03s6srijzfwixls8wa myvm1 Ready Active 18.09.4
s04pr40q6kbes7g74yz9qcz3g myvm2 Ready Active 18.09.4
可以直接通过镜像文件部署服务,其中--replicas表示要启动的任务数
docker service create --replicas 4 --name hello hello-world
可以查看启动了哪些任务
$ docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
itsnxckj2s9d hello replicated 0/4 hello-world:latest
查看每个任务在各主机上的运行情况,可以看到总会有4 个任务处理Running或Ready状态
$ docker service ps hello
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
9ffq1vtkqx44 hello.1 hello-world:latest myvm1 Running Running less than a second ago
nmrqns91n43m \_ hello.1 hello-world:latest myvm1 Shutdown Complete 5 seconds ago
khoh8ild8e7y \_ hello.1 hello-world:latest myvm1 Shutdown Complete 11 seconds ago
arwamygnz5qr \_ hello.1 hello-world:latest myvm1 Shutdown Complete 17 seconds ago
70lzxy1ia7ub \_ hello.1 hello-world:latest myvm1 Shutdown Complete 22 seconds ago
wwcgmiesje4c hello.2 hello-world:latest myvm2 Ready Ready 2 seconds ago
dtn09krre6h5 \_ hello.2 hello-world:latest default Shutdown Complete 2 seconds ago
viue6faeszdi \_ hello.2 hello-world:latest default Shutdown Complete 8 seconds ago
fpf5wk39cfxq \_ hello.2 hello-world:latest default Shutdown Complete 13 seconds ago
mhzl8i6blw03 \_ hello.2 hello-world:latest default Shutdown Complete 19 seconds ago
j0avr2abdoyy hello.3 hello-world:latest default Ready Ready 2 seconds ago
wl0wwian4fik \_ hello.3 hello-world:latest myvm2 Shutdown Complete 2 seconds ago
flfu4wr59zo4 \_ hello.3 hello-world:latest myvm2 Shutdown Complete 8 seconds ago
d6jhfug0rrwm \_ hello.3 hello-world:latest myvm2 Shutdown Complete 13 seconds ago
t1a7k7b17aab \_ hello.3 hello-world:latest myvm1 Shutdown Complete 19 seconds ago
qo665v71uwz1 hello.4 hello-world:latest myvm1 Running Running less than a second ago
retmj0eq4utg \_ hello.4 hello-world:latest myvm1 Shutdown Complete 5 seconds ago
yai5xhft63cg \_ hello.4 hello-world:latest myvm1 Shutdown Complete 11 seconds ago
jnzq9acysnkv \_ hello.4 hello-world:latest myvm2 Shutdown Complete 17 seconds ago
6j91c772fqa3 \_ hello.4 hello-world:latest myvm2 Shutdown Complete 23 seconds ago
如果要结束任务,则可以使用
$ docker service rm hello
在以前,通过yml文件部署都是使用docker-compose,现在都改用docker stack了。
部署前面定义好的docker-compose.yml文件,会看到栈getstartedlab中有一个服务getstartedlab_web
$ docker stack deploy -c docker-compose.yml getstartedlab
Creating network getstartedlab_webnet
Creating service getstartedlab_web
查看启动的任务,需要登录到manager上部署
$ docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
tihb1efymx2l getstartedlab_web replicated 5/5 testname/friendlyhello:1.0 *:4000->80/tcp
查看任务栈中服务的信息,服务命名=栈名+服务名
$ docker stack services getstartedlab
ID NAME MODE REPLICAS IMAGE PORTS
eamabrjgw8bj getstartedlab_web replicated 5/5 testname/friendlyhello:1.0 *:4000->80/tcp
也可以直接查栈中某一个服务的运行情况
$ docker service ps getstartedlab_web
ID NAME IMAGE NODE STATE CURRENT STATE ERROR PORTS
ju4irl04p3sk getstartedlab_web.1 netyeaxi/friendlyhello:1.0 myvm2 Running Running 2 minutes ago
tv3v71jx4xcw getstartedlab_web.2 netyeaxi/friendlyhello:1.0 myvm1 Running Running 2 minutes ago
pcv1bdm3x2bw getstartedlab_web.3 netyeaxi/friendlyhello:1.0 myvm2 Running Running 2 minutes ago
vv7dd3hds5se getstartedlab_web.4 netyeaxi/friendlyhello:1.0 default Running Running 2 minutes ago
gty3exduox5a getstartedlab_web.5 netyeaxi/friendlyhello:1.0 myvm1 Running Running 2 minutes ago
可以通过如下方式停止服务
$ docker service rm getstartedlab_web
也可以通过如下方式直接删除整个栈
$ docker stack rm getstartedlab
Removing service getstartedlab_web
Removing network getstartedlab_webnet
可以分别登录各虚拟主机,查询容器状态
$ docker-machine ssh myvm1 "docker container ls"
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4dd25545021c testname/friendlyhello:1.0 "python app.py" About an hour ago Up About an hour 80/tcp getstartedlab_web.5.9yglf5r6hzl96feshdqxcehxh
708c8fae12e4 testname/friendlyhello:1.0 "python app.py" About an hour ago Up About an hour 80/tcp getstartedlab_web.2.mbsy6yljrqb3osdbuhnbdq2y0
$ docker-machine ssh myvm2 "docker container ls"
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ecf52eb72161 testname/friendlyhello:1.0 "python app.py" About an hour ago Up About an hour 80/tcp getstartedlab_web.4.bso06tt6jqcuyrr2p2i4h4em9
229b19d13b48 testname/friendlyhello:1.0 "python app.py" About an hour ago Up About an hour 80/tcp getstartedlab_web.3.veodufz2tftet9l4tas2jkvvk
$ docker-machine ssh default "docker container ls"
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c8133ce22adc testname/friendlyhello:1.0 "python app.py" About an hour ago Up About an hour 80/tcp getstartedlab_web.1.a06t1z41in1samu9xs5mho5x6
参考文档
Get Started
docker container