Docker
Dockerfile build run 手动操作,单个容器!
微服务,100个微服务,依赖关系。
Docker Compose 来轻松高效的管理容器,定义运行多个容器。
官方介绍
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 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:
Dockerfile
so it can be reproduced anywhere.
docker-compose.yml
so they can be run together in an isolated environment.
docker-compose up
and Compose starts and runs your entire app.
作用:批量容器编排
我自己的理解
Compose是Docker官方的开源项目,需要安装!
Dockerfile
让程序在任何地方运行。web服务、redis、mysql、nginx… 多个容器。 run
Compose
version: '2.0'
services:
web:
build: .
ports:
- "5000:5000"
volumes:
- .:/code
- logvolume01:/var/log
links:
- redis
redis:
image: redis
volumes:
logvolume01: {
}
docker-compose up 100个服务
Compose:重要概念
# 官网提供 (没有下载成功)
curl -L "https://github.com/docker/compose/releases/download/1.26.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
chmod +x /usr/local/bin/docker-compose
官方Demo地址
python应用。 计数器。redis!
应用app.py
Dockerfile 应用打包为镜像
FROM python:3.6-alpine
ADD . /code
WORKDIR /code
RUN pip install -r requirements.txt
CMD ["python", "app.py"]
# 官网的用来flask框架,我们这里不用它
# 这告诉Docker
# 从python3.7开始构建镜像
# 将当前目录添加到/code印像中的路径中
# 将工作目录设置为/code
# 安装Python依赖项
# 将容器的默认命令设置为python app.py
Docker-compose yaml文件(定义整个服务,需要的环境 web、redis) 完整的上线服务!
version: '3.8'
services:
web:
build: .
ports:
- "5000:5000"
volumes:
- .:/code
redis:
image: "redis:alpine"
启动compose 项目 (docker-compose up)
流程:
docker-compose.yaml 核心!
官方文档
官方文档
下载程序、安装数据库、配置…
compose应用 => 一键启动
编写项目微服务
Dockerfile构建镜像
FROM java:8
COPY *.jar /app.jar
CMD ["--server.port=8080"]
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "/app.jar"]
docker-compose.yml编排项目
version '3.8'
services:
xiaofanapp:
build: .
image: xiaofanapp
depends_on:
- redis
ports:
- "8080:8080"
redis:
image: "library/redis:alpine"
丢到服务器运行 docker-compose up
docker-compose down # 关闭容器
docker-compose up --build # 重新构建
总结:
工程、服务、容器
项目 compose: 三层
集群
到此,我们的服务器购买成功!
和我们单机安装一样
技巧: xshell直接同步操作,省时间!
docker swarm init --help
ip addr # 获取自己的ip(用内网的不要流量)
[root@iZ2ze58v8acnlxsnjoulk5Z ~]# docker swarm init --advertise-addr 172.16.250.97
Swarm initialized: current node (otdyxbk2ffbogdqq1kigysj1d) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-3vovnwb5pkkno2i3u2a42yrxc1dk51zxvto5hrm4asgn37syfn-0xkrprkuyyhrx7cidg381pdir 172.16.250.97:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
初始化结点docker swarm init
docker swarm join 加入一个结点!
# 获取令牌
docker swarm join-token manager
docker swarm join-token worker
[root@iZ2ze58v8acnlxsnjoulk6Z ~]# docker swarm join --token SWMTKN-1-3vovnwb5pkkno2i3u2a42yrxc1dk51zxvto5hrm4asgn37syfn-0xkrprkuyyhrx7cidg381pdir 172.16.250.97:2377
This node joined a swarm as a worker.
把后面的结点都搭建进去
100台!
双主双从:假设一个结点挂了!其他结点是否可以用!
Raft协议:保证大多数结点存活才可以使用,只要>1, 集群至少大于3台!
实验:
1、将docker1机器停止。宕机!双主,另外一个结点也不能使用了!
docker swarm leave
十分简单:集群,可用! 3个主节点。 > 1台管理结点存活!
Raft协议:保证大多数结点存活,才可以使用,高可用!
弹性、扩缩容!集群!
以后告别 docker run!
docker-compose up!启动一个项目。单机!
集群: swarm docker-service
k8s service
容器 => 服务!
容器 => 服务! => 副本!
redis => 10个副本!(同时开启10个redis容器)
体验:创建服务、动态扩容服务、动态更新服务
docker run 容器启动! 不具有扩缩容器
docker service 服务! 具有扩缩容器,滚动更新!
查看服务
动态扩缩容
[root@iZ2ze58v8acnlxsnjoulk5Z ~]# docker service update --replicas 3 my-nginx
1/3: running [==================================================>]
1/3: running [==================================================>]
2/3: running [==================================================>]
3/3: running [==================================================>]
verify: Service converged
[root@iZ2ze58v8acnlxsnjoulk5Z ~]# docker service scale my-nginx=5
my-nginx scaled to 5
overall progress: 3 out of 5 tasks
overall progress: 3 out of 5 tasks
overall progress: 3 out of 5 tasks
overall progress: 5 out of 5 tasks
1/5: running [==================================================>]
2/5: running [==================================================>]
3/5: running [==================================================>]
4/5: running [==================================================>]
5/5: running [==================================================>]
verify: Service converged
[root@iZ2ze58v8acnlxsnjoulk5Z ~]# docker service scale my-nginx=1
my-nginx scaled to 1
overall progress: 1 out of 1 tasks
1/1: running [==================================================>]
verify: Service converged
移出!docker service rm
docker swarm其实并不难
只要会搭建集群、会启动服务、动态管理容器就可以了!
swarm
集群的管理和编号,docker可以初始化一个swarm集群,其他结点可以加入。(管理,工作者)
Node
就是一个docker结点,多个结点就组成了一个网络集群(管理、工作者)
Service
任务,可以在管理结点或者工作结点来运行。核心,用户访问。
Task
容器内的命令、细节任务!
service
命令 -> 管理 -> api -> 调度 -> 工作结点(创建Task容器维护创建!)
服务副本和全局服务
调整service以什么方式运行
--mode string
Service mode (replicated or global) (default "replicated")
docker service create --mode replicated --name mytom tomcat:7 默认的
docker service create --mode global --name haha alpine ping www.baidu.com
拓展: 网络模式 “PublishMode”:“ingress”
Swarm:
Overlay:
ingress:特殊的Overlay网络!负载均衡的功能!ipvs vip!
[root@iZ2ze58v8acnlxsnjoulk5Z ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
74cecd37149f bridge bridge local
168d35c86dd5 docker_gwbridge bridge local
2b8f4eb9c2e5 host host local
dmddfc14n7r3 ingress overlay swarm
8e0f5f648e69 none null local
[root@iZ2ze58v8acnlxsnjoulk5Z ~]# docker network inspect ingress
[
{
"Name": "ingress",
"Id": "dmddfc14n7r3vms5vgw0k5eay",
"Created": "2020-08-17T10:29:07.002315919+08:00",
"Scope": "swarm",
"Driver": "overlay",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "10.0.0.0/24",
"Gateway": "10.0.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": true,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"ingress-sbox": {
"Name": "ingress-endpoint",
"EndpointID": "9d6ec47ec8309eb209f4ff714fbe728abe9d88f9f1cc7e96e9da5ebd95adb1c4",
"MacAddress": "02:42:0a:00:00:02",
"IPv4Address": "10.0.0.2/24",
"IPv6Address": ""
}
},
"Options": {
"com.docker.network.driver.overlay.vxlanid_list": "4096"
},
"Labels": {
},
"Peers": [
{
"Name": "cea454a89163",
"IP": "172.16.250.96"
},
{
"Name": "899a05b64e09",
"IP": "172.16.250.99"
},
{
"Name": "81d65a0e8c03",
"IP": "172.16.250.97"
},
{
"Name": "36b31096f7e2",
"IP": "172.16.250.98"
}
]
}
]
docker-compose 单机部署项目
docker stack 集群部署
# 单机
docker-compose up -d wordpress.yaml
# 集群
docker stack deploy wordpress.yaml
安全!配置密码!证书!
[root@iZ2ze58v8acnlxsnjoulk5Z ~]# docker secret --help
Usage: docker secret COMMAND
Manage Docker secrets
Commands:
create Create a secret from a file or STDIN as content
inspect Display detailed information on one or more secrets
ls List secrets
rm Remove one or more secrets
配置!
[root@iZ2ze58v8acnlxsnjoulk5Z ~]# docker config --help
Usage: docker config COMMAND
Manage Docker configs
Commands:
create Create a config from a file or STDIN
inspect Display detailed information on one or more configs
ls List configs
rm Remove one or more configs
云原生时代
Go语言!必须掌握! Java Go!
并发语言!
B语言,C语言的创始人。Unix创始人VB
go指针