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。
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.
Define the services that make up your app in docker-compose.yml so they can be run together in an isolated environment.
Run 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
2.授权
sudo chmod +x /usr/local/bin/docker-compose
python应用。 计数器。redis!
跟着官网做https://docs.docker.com/compose/gettingstarted/
在Dockerfile文件第二行加入国内镜像
RUN echo -e http://mirrors.ustc.edu.cn/alpine/v3.7/main/ > /etc/apk/repositories
删掉Dockerfile文件中,“RUN apk add --no-cache gcc musl-dev linux-headers”
参考链接:https://github.com/docker/compose/issues/6292:
如果docker-compose up报错
sudo gpasswd -a ${USER} docker
感谢提功解决
[]: https://blog.csdn.net/yubo_725/article/details/89885429?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.channel_param&depth_1
启动结果
启动的容器
没访问一次 会加一
总结
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
version: '3.8'
services:
web:
build: .
ports:
- "5000:5000"
volumes:
- .:/code
redis:
image: "redis:alpine"
流程:
10个服务=》项目(项目中的内容都在同个网络下,域名访问)
docker network inspect [networkid]
如果在同一网络下,我们可以直接通过域名访问。
docker-compose.yaml 核心!
https://docs.docker.com/compose/compose-file/#compose-file-structure-and-examples
https://docs.docker.com/compose/wordpress/
下载程序、安装数据库、配置…
compose应用 => 一键启动
[root@localhost home]# mkdir my_wordpress
[root@localhost home]# cd my_wordpress/
[root@localhost my_wordpress]# vi docker-compose.yml
docker-compose up #启动
version: '3.3'
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
volumes:
db_data: {}
~
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: kuangapp
depends_on:
- redis
ports:
- "8080:8080"
redis:
image: "library/redis:alpine"
丢到服务器运行 docker-compose up
docker-compose down # 关闭容器
docker-compose up
docker-compose up --build # 重新构建
总结:
工程、服务、容器
项目 compose: 三层
到此,我们的服务器购买成功!
和我们单机安装一样
https://blog.csdn.net/fanjianhai/article/details/107860159
技巧: xshell直接同步操作,省时间!
工作机制:https://blog.csdn.net/qq_21197507/article/details/115071715
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 加入一个结点!
双主双从:假设一个结点挂了!其他结点是否可以用!
Raft协议:保证大多数结点存活才可以使用,只要>1, 集群至少大于3台!
实验:
1、将docker1机器停止。宕机!双主,另外一个结点也不能使用了!
2.可以将其他结点离开docker swarm leave
worker就是工作的,管理结点操作! 3台结点设置为了管理结点。
Docker swarm集群增加节点
十分简单:集群,可用! 3个主节点。 > 1台管理结点存活!
Raft协议:保证大多数结点存活,才可以使用,高可用!
弹性、扩缩容!集群!
以后告别 docker run!
docker-compose up!启动一个项目。单机!
集群: swarm docker-service
k8s service pods
容器 => 服务!
容器 => 服务! => 副本!
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!
虽然docker在4台机器上,实际网络是同一个!ingress网络,是一个特殊的Coverlay网络
[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创始人 V8
–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
- Docker Config
```sh
配置!
[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创始人 V8
go指针