part-5 stacks
简介
stack就是栈,栈的结构是什么样的呢?一层一层是紧挨着的,然后互相依赖,不能说中间少了一个。这样说就很明白了,栈实际上在docker中就相当于多个互相依赖的组件,共同组合成一个完整的项目,比方说你实现了登陆,我实现了登录后的操作。compose也很直白,协奏,乐团~
增加一个服务,然后重新部署应用
- 更新一下docker-compose.yml
version: "3"services: web: # replace username/repo:tag with your name and image details image: username/repo:tag deploy: replicas: 5 restart_policy: condition: on-failure resources: limits: cpus: "0.1" memory: 50M ports: - "80:80" networks: - webnet visualizer: image: dockersamples/visualizer:stable ports: - "8080:8080" volumes: - "/var/run/docker.sock:/var/run/docker.sock" deploy: placement: constraints: [node.role == manager] networks: - webnetnetworks: webnet:
- attach到myvm1,部署应用
eval $(docker-machine env myvm1)docker stack deploy -c docker-compose.yml getstartedlab
- 打开浏览器如何访问http://192.168.99.101:8080/ 就可以看见图形化的界面了
- 查看进程
➜ docker_file docker stack ps getstartedlabID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS3hec43yn4z5j getstartedlab_visualizer.1 dockersamples/visualizer:stable myvm1 Running Running 6 minutes agoxcucj08tfn72 getstartedlab_web.1 btchild/get-started:part-2 myvm2 Running Running 5 minutes agorifl2rqahv30 getstartedlab_web.2 btchild/get-started:part-2 myvm1 Running Running 7 minutes agoj75tnimk4r46 getstartedlab_web.3 btchild/get-started:part-2 myvm2 Running Running 5 minutes agoccgcgw2ahkrp getstartedlab_web.4 btchild/get-started:part-2 myvm1 Running Running 7 minutes agoirftco5od5hx getstartedlab_web.5 btchild/get-started:part-2 myvm2 Running Running 5 minutes ago
数据持久化
- 更新配置文件
version: "3"services: web: # replace username/repo:tag with your name and image details image: username/repo:tag deploy: replicas: 5 restart_policy: condition: on-failure resources: limits: cpus: "0.1" memory: 50M ports: - "80:80" networks: - webnet visualizer: image: dockersamples/visualizer:stable ports: - "8080:8080" volumes: - "/var/run/docker.sock:/var/run/docker.sock" deploy: placement: constraints: [node.role == manager] networks: - webnet redis: image: redis ports: - "6379:6379" volumes: - "/home/docker/data:/data" deploy: placement: constraints: [node.role == manager] command: redis-server --appendonly yes networks: - webnetnetworks: webnet:
- 可以看出是增加了redis,需要增加一个本地目录,用来持久化数据
docker-machine ssh myvm1 "mkdir ./data"
- 启动应用
➜ docker_file docker stack deploy -c docker-compose.yml getstartedlabCreating service getstartedlab_redisUpdating service getstartedlab_web (id: rw916xx9vy7pl89v9nln22yz8)Updating service getstartedlab_visualizer (id: gvoe98n4fjizj3k88jn6tdr3z)
- 可以通过查看http://192.168.99.101:8080/和http://192.168.99.101来查看相应页面
- 其实遇到了一个问题,然后完全按照步骤来做的,就发现完全不知道问题在哪里,最终想办法找到了日志,发现是app.py最早有个地方写错了,打包的时候就是错的,后期就出现了问题,于是重新打包上传,更新就好了;这里说一下查看log的命令,如下:
docker logs container_name