程序员Docker容器

Docker Swarm部署(1)

2018-06-15  本文已影响21人  知足常乐晨

Docker Machine 创建 Docker 主机

Docker Machine 创建三台主机

docker-machine create --driver virtualbox default
docker-machine create --driver virtualbox worker1
docker-machine create --driver virtualbox worker2

docker-machine常用命令 官网命令详情

Docker Swarm 配置集群节点

创建一个 Docker Swarm 管理节点

初始化集群的时候,会自动把当前节点设置为管理节点

docker swarm init --advertise-addr 192.168.99.100(manager节点IP)

查看节点 join --token

docker@default:~$ docker swarm join-token manager
To add a manager to this swarm, run the following command:

    docker swarm join --token SWMTKN-1-34aqnqamrm71s2dqtoyk6jnddkslbhf7jrwnvb7sh1xvc7dv3a-ebtqa2s1us3kaav7vui2wf4nb 192.168.99.100:2377

docker@default:~$ docker info | grep -A5 Swarm
Swarm: active
 NodeID: yx2548i6b8sr1a7pa9fm04ki5
 Is Manager: true
 ClusterID: pn54eo31jedgf09wi44my835h
 Managers: 1
 Nodes: 1
docker@default:~$ 

加入集群

如果其他节点关闭,使用以下方法启动节点虚拟机

# 启动worker1,worker2
$ docker-machine start worker1
$ docker-machine start worker2

在worker1 ,worker2上运行以下命令,加入default主机上创建的swarm集群

docker@default:~$ docker swarm join --token SWMTKN-1-14samnq4vi3brud74f3erylzx8ngx08bo4m8amk579i6y1yif7-b60g94a6r3d9qtl60mlva22yp 192.168.99.100:2377

结点查看

docker@default:~$ docker node ls
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
yx2548i6b8sr1a7pa9fm04ki5 *   default             Ready               Active              Leader              18.05.0-ce
kn5d0vim6zw2a4kvfvkq627ec     worker1             Ready               Active              Reachable           18.05.0-ce
2hd6p5acvo0xd3appptq9io9p     worker2             Ready               Active              Reachable           18.05.0-ce
# 删除结点
docker@default:~$ docker node rm worker1(结点主机名)

在Swarm中部署服务

部署一个nginx服务

# 先创建一个名为network_swarm的网络
docker network create --driver overlay network_swarm
docker network ls
# 创建一个名为service_test的服务,并制定网络名network_swarm,使用nginx镜像
docker@default:~$ docker service create --replicas 2 -p 8888:80 --name nginx nginx:latest
sw8j3fy3tzow7pjujj2wqnu3g
overall progress: 2 out of 2 tasks 
1/2: running   [==================================================>] 
2/2: running   [==================================================>] 
verify: Service converged 
# 查看服务状况
docker@default:~$ docker service ls
ID                  NAME                MODE                REPLICAS            IMAGE               PORTS
sw8j3fy3tzow        nginx 
docker@default:~$ docker service ps nginx
ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE            ERROR               PORTS
x4wp2ii0umu9        nginx.1             nginx:latest        worker1             Running             Running 25 seconds ago                       
3u2zzr1a4kjc        nginx.2             nginx:latest        default             Running             Running 25 seconds ago          

---replicas 2 表示创建服务的实例个数(默认是一个),即在2其中两个docker主机上分别创建一个nginx服务。

从图中可以看出nginx服务启动在了default和worker1主机上

手动设置实例个数

由上可以看出,有一台集群中worker1的主机上没有运行nginx服务,通过以下命令在worker1主机上运行一个nginx服务,并通过docker service ps nginx查看

docker@default:~$ 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@default:~$ docker service ps nginx
ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE           ERROR               PORTS
x4wp2ii0umu9        nginx.1             nginx:latest        worker1             Running             Running 5 minutes ago                       
3u2zzr1a4kjc        nginx.2             nginx:latest        default             Running             Running 5 minutes ago                       
i252oxks2kib        nginx.3             nginx:latest        worker2             Running             Running 7 seconds ago                

测试

从上面可以看到三台主机上nginx服务均已启动,使用任意的主机IP进行访问

image.png
image.png
image.png

测试成功!!

上一篇下一篇

猜你喜欢

热点阅读