DockerFile build run 单个容器
Docker Compose 来轻松高效的管理容器->批量运行编排多个容器
Compose 是Docker 官方开源项目,需要安装
官方介绍
定义、运行多个容器
yaml file 配置文件
Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration. To learn more about all the features of Compose, see the list of features.
所有环境都可使用Compose
Compose works in all environments: production, staging, development, testing, as well as CI workflows. You can learn more about each case in Common Use Cases.
三步骤:
Using Compose is basically a three-step process:
- Define your app’s environment with a
Dockerfile
so it can be reproduced anywhere.
- Dockerfile 保证我们的项目可以在任何地方运行
- Define the services that make up your app in
docker-compose.yml
so they can be run together in an isolated environment.
- services: 容器、应用
- docker-compose.yml
- Run
docker compose up
and the Docker compose command starts and runs your entire app. You can alternatively rundocker-compose up
using the docker-compose binary.
- 启动项目
A docker-compose.yml
looks like this:
version: "3.9" # optional since v1.27.0
services:
web:
build: .
ports:
- "5000:5000"
volumes:
- .:/code
- logvolume01:/var/log
links:
- redis
redis:
image: redis
volumes:
logvolume01: {}
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
# 速度快一点
curl -L https://get.daocloud.io/docker/compose/releases/download/1.25.5/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
[root@h1 bin]# docker-compose version
docker-compose version 1.25.5, build 8a1c60f6
docker-py version: 4.1.0
CPython version: 3.7.5
OpenSSL version: OpenSSL 1.1.0l 10 Sep 2019
官网体验:https://docs.docker.com/compose/gettingstarted/ python-计数器应用
[root@h1 composetest]# cat docker-compose.yml
version: "3"
services:
web:
build: .
ports:
- "5000:5000"
redis:
image: "redis:alpine"
多个服务 = 项目(项目中的内容都在同个网络下,可域名访问)
如果在同一个网络下,可以直接通过域名访问
docker-compose down | ctrl c
[root@h1 composetest]# docker-compose down
Stopping composetest_redis_1 ... done
Stopping composetest_web_1 ... done
Removing composetest_redis_1 ... done
Removing composetest_web_1 ... done
Removing network composetest_default
官网介绍
# 总共三层
version: '' # 版本
services: #服务
服务1: web
# 服务配置
images:
port:
network:
depends_on: # 启动依赖(顺序)
...
服务2: redis
...
# 其他配置 网络、卷、全局规则
volumes:
network:
官网
集群方式的部署
准备:四台1核2g服务器并安装docker
官网
Docker Engine 1.12 introduces swarm mode that enables you to create a cluster of one or more Docker Engines called a swarm. A swarm consists of one or more nodes: physical or virtual machines running Docker Engine 1.12 or later in swarm mode.
There are two types of nodes: managers and workers.
manager:
worker node:
只能在节点运行,提供相同的内容
For a replicated service, you specify the number of identical tasks you want to run. For example, you decide to deploy an HTTP service with three replicas, each serving the same content.
可在任意位置运行
A global service is a service that runs one task on every node. There is no pre-specified number of tasks. Each time you add a node to the swarm, the orchestrator creates a task and the scheduler assigns the task to the new node. Good candidates for global services are monitoring agents, an anti-virus scanners or other types of containers that you want to run on every node in the swarm.
--mode
docker service create --mode replicated 默认的 --mode global
场景:
日志收集:每一个节点有自己的日志收集器,过滤,再把所有日志最终传给日志中心。
服务监控
# 第一台机器 初始化为manager节点
[root@h1 composetest]# docker swarm init --advertise-addr 41.93.183.43
Swarm initialized: current node (vh7dnxzwb4jmne7dwhyq269l2) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-13ys18ser2q6c8378rm3plir24o843ty1zzbvmbpgweub8ah37-bjszycylt9a25tn8ar3dedtaa 119.3.166.71:2377
# 作为manger加入
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
# 第二台、第三台机器加入,作为一个worker加入
[root@aliyun-h1 ~]# docker swarm join --token SWMTKN-1-13ys18ser2q6c8378rm3plir24o843tylzzbvmbpg1eub8ah37-bjszycylt9a25tn8ar3dedtaa 119.3.166.71:2377
This node joined a swarm as a worker.
# 第四台机器作为 manager 加入
# 先在第一台机器执行 docker swarm join-token manager
# 将得到的token复制到第四台机器
[root@tencent-h1 ~]# docker swarm join --token SWMTKN-1-13ys18ser2q6c8378rm3plir24o843tylzzbvmbpgweub8a137-881545bl3sn18mzlpcvkxdj9h 119.3.166.71:2377
This node joined a swarm as a manager.
# worker 升级为 manager节点
docker node promote
# 查看集群信息
[root@h1 composetest]# docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
4af3f51p0nuxp35viyi602sj9 aliyun-h1 Ready Active 20.10.3
vh7dnxzwb4jmne7dwhyq269l2 * h1 Ready Active Leader 19.03.12
pdfgx55nybgk853couyg3arvm iZ2zeimmhbueqxfu45ploqZ Ready Active 20.10.12
nxj4f120ahxewvxxg0ulgv6rs tencent-h1 Ready Active Reachable 20.10.7
双主双从->假设一个节点挂了,其他节点是否可用? 不可用
Raft协议:保证大多数节点存活才可用
N
管理器集群最多可以容忍管理器的丢失 (N-1)/2
。**ps:**manager 节点不是越多越好
Adding more managers does NOT mean increased scalability or higher performance. In general, the opposite is true.
实验:
[root@tencent-h1 ~]# docker node ls
Error response from daemon: rpc error: code = Unknown desc = The swarm does not have a leader. It's possible that too few managers are online. Make sure more than half of the managers are online.
[root@aliyun-h1 ~]# docker node ls
Error response from daemon: This node is not a swarm manager. Worker nodes can't be used to view or modify cluster state. Please run this command on a manager node or promote the current node to a manager.
目的:创建 nginx 服务、动态扩展服务、动态更新服务
docker run 容器启动、不具备扩缩容器
docker service 集群启动、具备扩缩能力
# 启动 nginx
[root@aliyun-h1 ~]# docker service create -p 8000:80 --name rm-nginx nginx:1.10
usk7csg8k8vjizrxpvdnoniru
overall progress: 1 out of 1 tasks
1/1: running [==================================================>]
verify: Service converged
# 查看name=rm-nginx 服务的状态
[root@h1 composetest]# docker service ps rm-nginx
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
whjq3qdgi2sv rm-nginx.1 nginx:1.10 aliyun-h1 Running Running 6 minutes ago
# 查看服务 PS:在非leader节点上执行会出错
[root@h1 composetest]# docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
usk7csg8k8vj rm-nginx replicated 1/1 nginx:1.10 *:8000->80/tcp
# 扩容到3个副本 集群中的任意节点都可访问,实现高可用 == docker service scale rm-nginx=3
# PS:如果使用的是公网创造的集群,无法实现任意节点都可访问,原因未知
[root@h1 composetest]# docker service update --replicas 3 rm-nginx
rm-nginx
overall progress: 3 out of 3 tasks
1/3: running [==================================================>]
2/3: running [==================================================>]
3/3: running [==================================================>]
[root@aliyun-h1 ~]# docker swarm join --token SWMTKN-1-6ahwo24msdqflwtqwzf44yfn5ger4vd414kp492vwn9qw6kupg-dk0z5b9b3z8yaxjx5ly5w4q8k 119.3.166.71:2377Error response from daemon: manager stopped: can't initialize raft node: rpc error: code = Unknown desc = could not connect to prospective new cluster member using its advertised address: rpc error: code = DeadlineExceeded desc = context deadline exceeded
集群的管理和编排。docker可初始化一个 swarm 集群,其他节点可加入(manager、worker)
就是一个docker节点。多个节点组成了一个网络集群(manager、worker)
服务、可管理节点或者工作节点运行
容器内的命令
官网
网络模式:PublishMode:ingress
Overlay:加一层,由中间这层去统一调度到任意机器
ingress:特殊的 Overlay 网络,具有负载均衡功能 IPVS VIP
[root@tencent-h1 ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
2fea5acac9d3 bridge bridge local
6802ef6d4b01 docker_gwbridge bridge local
9ca88c1eec4e host host local
31x3e4t51obc ingress overlay swarm
5c8625209093 none null local
[root@tencent-h1 ~]# docker network inspect 31x3e4t51obc
# 感觉功能有点类似交换机,由它去统一调度
# 初始化为manager节点
docker swarm init --advertise-addr ip地址(内网直接可联调,公网未成功联调)
# 作为manager加入(在manager执行)
docker swarm join-token manager #将得到的命令复制到要加入的节点中
# 作为worker 加入(在manager执行)
docker swarm join-token worker #将得到的命令复制到要加入的节点中
# worker 升级为 manager节点
docker node promote
# 查看集群信息 leader为manager节点,Reachable 为后备manager节点(挂掉之后会选举出新的)
docker node ls
# 离开集群
docker swarm leave
# 启动服务 eg:nginx
# 之前 docker 的参数都可用
docker service create -p 8000:80 --name rm-nginx nginx:1.10
# 查看服务状态
docker service ps 服务名称
# 查看服务列表(只能在manager执行)
docker service ls
# 扩缩容,两种方式
docker service update --replicas 数量 名字|服务id
docker service scale 名字|服务id=数量
一个stack 是一组相互关联的 service,这组 service共享依赖,可被安排在一起运行和扩展
# docker-compose 单机部署
docker-compose up -d wordpress.yaml
# docker stack 集群部署
docker stack deploy wordpress.yaml
# 查看所有stack
docker stack ls
安全、配置密码、证书
配置