经典示例: Example Voting App 示例投票App
GitHub地址:https://github.com/dockersamples/example-voting-app
网络不好的同学,我已经把源码下载,地址:https://download.csdn.net/download/weixin_48447848/77354937
详细讲解地址请看:Docker Compose 练习:投票 App
Example Voting App 示例投票App
跨多个Docker容器运行的简单分布式应用程序。
Linux 容器 示例
Linux 堆栈使用 Python、Node.js、.NET Core(或可选的 Java),使用 Redis 进行消息传递,使用 Postgres 进行存储。
如果您在 Windows 上使用 Docker Desktop,则可以通过切换到 Linux 容器来运行 Linux 版本,或者运行 Windows 容器版本。
在这个目录下运行:
docker-compose up
该应用程序将在http://localhost:5000 运行,结果将在http://localhost:5001 。
或者,如果你想在Docker Swarm上运行它,首先确保你有一个 swarm。如果没有,请运行:
docker swarm init
一旦你有了你的 swarm,在这个目录中运行:
docker stack deploy --compose-file docker-stack.yml vote
上手操作 Swarm 多节点集群
前期准备
请看之前文章Docker Swarm 多节点集群,找到一种适合你的搭建多节点的方法。
代码讲解
大部分内容与Docker Compose 练习:投票 App相同,只对Swarm部署的yaml文件讲解。
example-voting-app/docker-stack.yml
deploy :设置部署的参数
replicas 容器个数update_config 更新的参数,几次,间隔时间restart_policy 重启的策略
version: "3"
services:
redis:
image: redis:alpine
networks:
- frontend
deploy:
replicas: 1
update_config:
parallelism: 2
delay: 10s
restart_policy:
condition: on-failure
db:
image: postgres:9.4
environment:
POSTGRES_USER: "postgres"
POSTGRES_PASSWORD: "postgres"
POSTGRES_HOST_AUTH_METHOD: "trust"
volumes:
- db-data:/var/lib/postgresql/data
networks:
- backend
deploy:
placement:
constraints: [node.role == manager]
vote:
image: dockersamples/examplevotingapp_vote:before
ports:
- 5000:80
networks:
- frontend
depends_on:
- redis
deploy:
replicas: 2
update_config:
parallelism: 2
restart_policy:
condition: on-failure
result:
image: dockersamples/examplevotingapp_result:before
ports:
- 5001:80
networks:
- backend
depends_on:
- db
deploy:
replicas: 1
update_config:
parallelism: 2
delay: 10s
restart_policy:
condition: on-failure
worker:
image: dockersamples/examplevotingapp_worker
networks:
- frontend
- backend
depends_on:
- db
- redis
deploy:
mode: replicated
replicas: 1
labels: [APP=VOTING]
restart_policy:
condition: on-failure
delay: 10s
max_attempts: 3
window: 120s
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]
networks:
frontend:
backend:
volumes:
db-data:
上手操作
新安装的docker是没有启动swarm的,需要初始化
node1节点192.168.0.18为manger管理节点,主要敲命令的:
$ docker swarm init
Error response from daemon: could not choose an IP address to advertise since this system has multiple addresses on different interfaces (172.18.0.4 on eth1 and 192.168.0.18 on eth0) - specify one with --advertise-addr
[node1] (local) root@192.168.0.18 ~
$ docker swarm init --advertise-addr 192.168.0.18
Swarm initialized: current node (y8431tcl51sx54riya31rbvvg) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-1qrsj4vfuv69hxoizdf21bswxvio4jdjdpvizi42n5g4vicwmv-9rh0c9o6h6iq6mtuge0i3yws2 192.168.0.18:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
[node1] (local) root@192.168.0.18 ~
$
node2节点192.168.0.17:
$ docker swarm join --token SWMTKN-1-1qrsj4vfuv69hxoizdf21bswxvio4jdjdpvizi42n5g4vicwmv-9rh0c9o6h6iq6mtuge0i3yws2 192.168.0.18:2377
This node joined a swarm as a worker.
[node2] (local) root@192.168.0.17 ~
node3节点192.168.0.16:
$ docker swarm join --token SWMTKN-1-1qrsj4vfuv69hxoizdf21bswxvio4jdjdpvizi42n5g4vicwmv-9rh0c9o6h6iq6mtuge0i3yws2 192.168.0.18:2377
This node joined a swarm as a worker.
[node3] (local) root@192.168.0.16 ~
创建服务
$ git clone https://github.com/dockersamples/example-voting-app
Cloning into 'example-voting-app'...
remote: Enumerating objects: 971, done.
remote: Total 971 (delta 0), reused 0 (delta 0), pack-reused 971
Receiving objects: 100% (971/971), 982.39 KiB | 9.54 MiB/s, done.
Resolving deltas: 100% (359/359), done.
$ ls
example-voting-app
$ cd example-voting-app/
$ docker stack deploy --compose-file docker-stack.yml vote
Creating network vote_default
Creating network vote_frontend
Creating network vote_backend
Creating service vote_visualizer
Creating service vote_redis
Creating service vote_db
Creating service vote_vote
Creating service vote_result
Creating service vote_worker
查看启动状态
$ docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
bzdbgncx4aie vote_db replicated 1/1 postgres:9.4
midnvgi8pb25 vote_redis replicated 1/1 redis:alpine
fcyggosob44h vote_result replicated 1/1 dockersamples/examplevotingapp_result:before *:5001->80/tcp
ci0278kb2erd vote_visualizer replicated 1/1 dockersamples/visualizer:stable *:8080->8080/tcp
flh8ecex2q28 vote_vote replicated 2/2 dockersamples/examplevotingapp_vote:before *:5000->80/tcp
8f9kvakcrta8 vote_worker replicated 1/1 dockersamples/examplevotingapp_worker:latest
查看db数据的日志,发现出现密码错误
docker vervice logs vote_db
vote_db.1.zg0dbopxmklu@node1 | FATAL: password authentication failed for user "postgres"
vote_db.1.zg0dbopxmklu@node1 | DETAIL: Connection matched pg_hba.conf line 95: "host all all all md5
example-voting-app/docker-stack.yml 文件需要修改:
db:
image: postgres:9.4
environment:
POSTGRES_USER: "postgres"
POSTGRES_PASSWORD: "postgres"
POSTGRES_HOST_AUTH_METHOD: "trust"
volumes:
- db-data:/var/lib/postgresql/data
重新启动,直接执行命令,自动更新
$ docker stack deploy --compose-file docker-stack.yml vote
Updating service vote_worker (id: 8f9kvakcrta8ml0ahbltbyrfu)
Updating service vote_visualizer (id: ci0278kb2erdm55y133pg85nj)
Updating service vote_redis (id: midnvgi8pb25o3oz3ptcawhhz)
Updating service vote_db (id: bzdbgncx4aieb0mgepgoxjl9s)
Updating service vote_vote (id: flh8ecex2q28gcifs53fg2odu)
Updating service vote_result (id: fcyggosob44hs0lzw1q8yzf3q)
|