mirror of
https://github.com/yeasy/docker_practice.git
synced 2024-12-25 14:38:54 +00:00
Update Swarm mode
This commit is contained in:
parent
486e9fb7d0
commit
e531f6a36b
@ -37,7 +37,7 @@
|
||||
* [HEALTHCHECK 健康检查](image/dockerfile/healthcheck.md)
|
||||
* [ONBUILD 为他人作嫁衣裳](image/dockerfile/onbuild.md)
|
||||
* [参考文档](image/dockerfile/references.md)
|
||||
* [Dockerfile 多阶段构建](image/multistage-builds.md)
|
||||
* [Dockerfile 多阶段构建](image/multistage-builds.md)
|
||||
* [其它制作镜像的方式](image/other.md)
|
||||
* [删除本地镜像](image/rmi.md)
|
||||
* [实现原理](image/internal.md)
|
||||
@ -94,6 +94,11 @@
|
||||
* [安装](machine/install.md)
|
||||
* [使用](machine/usage.md)
|
||||
* [Docker 三剑客之 Docker Swarm](swarm/README.md)
|
||||
* [Swarm mode](swarm_mode/README.md)
|
||||
* [基本概念](swarm_mode/overview.md)
|
||||
* [创建 Swarm 集群](swarm_mode/create.md)
|
||||
* [在 Swarm 集群部署服务](swarm_mode/deploy.md)
|
||||
* [在 Swarm 集群中使用 docker-compose.yml](swarm_mode/stack.md)
|
||||
* [Etcd 项目](etcd/README.md)
|
||||
* [简介](etcd/intro.md)
|
||||
* [安装](etcd/install.md)
|
||||
|
47
swarm_mode/demo/docker-compose.yml
Normal file
47
swarm_mode/demo/docker-compose.yml
Normal file
@ -0,0 +1,47 @@
|
||||
version: "3"
|
||||
|
||||
services:
|
||||
wordpress:
|
||||
image: wordpress
|
||||
ports:
|
||||
- 80:80
|
||||
networks:
|
||||
- overlay
|
||||
environment:
|
||||
WORDPRESS_DB_HOST: db:3306
|
||||
WORDPRESS_DB_USER: wordpress
|
||||
WORDPRESS_DB_PASSWORD: wordpress
|
||||
deploy:
|
||||
mode: replicated
|
||||
replicas: 3
|
||||
|
||||
db:
|
||||
image: mysql
|
||||
networks:
|
||||
- overlay
|
||||
volumes:
|
||||
- db-data:/var/lib/mysql
|
||||
environment:
|
||||
MYSQL_ROOT_PASSWORD: somewordpress
|
||||
MYSQL_DATABASE: wordpress
|
||||
MYSQL_USER: wordpress
|
||||
MYSQL_PASSWORD: wordpress
|
||||
deploy:
|
||||
placement:
|
||||
constraints: [node.role == manager]
|
||||
|
||||
visualizer:
|
||||
image: dockersamples/visualizer:stable
|
||||
ports:
|
||||
- "8080:8080"
|
||||
stop_grace_period: 1m30s
|
||||
volumes:
|
||||
- "/var/run/docker.sock:/var/run/docker.sock"
|
||||
deploy:
|
||||
placement:
|
||||
constraints: [node.role == manager]
|
||||
|
||||
volumes:
|
||||
db-data:
|
||||
networks:
|
||||
overlay:
|
BIN
swarm_mode/image/wordpress.png
Normal file
BIN
swarm_mode/image/wordpress.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 136 KiB |
98
swarm_mode/stack.md
Normal file
98
swarm_mode/stack.md
Normal file
@ -0,0 +1,98 @@
|
||||
## 在 Swarm 集群中使用 docker-compose.yml
|
||||
|
||||
正如之前使用 `docker-compose.yml` 来一次启动、管理多个容器,在 `Swarm` 集群中也可以使用 `docker-compose.yml` 来配置多个服务。
|
||||
|
||||
上一节中,我们使用 `docker service create` 一次只能部署一个服务,使用 `docker-compose.yml` 我们可以一次启动多个关联的服务。
|
||||
|
||||
我们以在 `Swarm` 集群中部署 `WordPress` 为例进行说明。
|
||||
|
||||
```yaml
|
||||
version: "3"
|
||||
|
||||
services:
|
||||
wordpress:
|
||||
image: wordpress
|
||||
ports:
|
||||
- 80:80
|
||||
networks:
|
||||
- overlay
|
||||
environment:
|
||||
WORDPRESS_DB_HOST: db:3306
|
||||
WORDPRESS_DB_USER: wordpress
|
||||
WORDPRESS_DB_PASSWORD: wordpress
|
||||
deploy:
|
||||
mode: replicated
|
||||
replicas: 3
|
||||
|
||||
db:
|
||||
image: mysql
|
||||
networks:
|
||||
- overlay
|
||||
volumes:
|
||||
- db-data:/var/lib/mysql
|
||||
environment:
|
||||
MYSQL_ROOT_PASSWORD: somewordpress
|
||||
MYSQL_DATABASE: wordpress
|
||||
MYSQL_USER: wordpress
|
||||
MYSQL_PASSWORD: wordpress
|
||||
deploy:
|
||||
placement:
|
||||
constraints: [node.role == manager]
|
||||
|
||||
visualizer:
|
||||
image: dockersamples/visualizer:stable
|
||||
ports:
|
||||
- "8080:8080"
|
||||
stop_grace_period: 1m30s
|
||||
volumes:
|
||||
- "/var/run/docker.sock:/var/run/docker.sock"
|
||||
deploy:
|
||||
placement:
|
||||
constraints: [node.role == manager]
|
||||
|
||||
volumes:
|
||||
db-data:
|
||||
networks:
|
||||
overlay:
|
||||
```
|
||||
|
||||
在 `Swarm` 集群管理节点新建该文件,其中的 `visualizer` 服务提供一个可视化页面,我们可以从浏览器中很直观的查看集群中各个服务的运行节点。
|
||||
|
||||
在 `Swarm` 集群中使用 `docker-compose.yml` 我们用 `docker stack` 命令,下面我们对该命令进行详细讲解。
|
||||
|
||||
### 部署服务
|
||||
|
||||
部署服务使用 `docker stack deploy`,其中 `-c` 参数指定 Compose 文件名。
|
||||
|
||||
```bash
|
||||
$ docker stack deploy -c docker-compose.yml wordpress
|
||||
```
|
||||
|
||||
现在我们打开浏览器输入 `任一节点IP:8080` 即可看到各节点运行状态。如下图所示:
|
||||
|
||||
![](image/wordpress.png)
|
||||
|
||||
在浏览器新的标签页输入 `任一节点IP` 即可看到 `WordPress` 安装界面,安装完成之后,输入 `任一节点IP` 即可看到 `WordPress` 页面。
|
||||
|
||||
### 查看服务
|
||||
|
||||
```bash
|
||||
$ docker stack ls
|
||||
NAME SERVICES
|
||||
wordpress 3
|
||||
```
|
||||
|
||||
### 移除服务
|
||||
|
||||
要移除服务,使用 `docker stack down`
|
||||
|
||||
```bash
|
||||
$ docker stack down wordpress
|
||||
Removing service wordpress_db
|
||||
Removing service wordpress_visualizer
|
||||
Removing service wordpress_wordpress
|
||||
Removing network wordpress_overlay
|
||||
Removing network wordpress_default
|
||||
```
|
||||
|
||||
该命令不会移除服务所使用的 `数据卷`,如果你想移除数据卷请使用 `docker volume rm`
|
Loading…
Reference in New Issue
Block a user