#
创建脚本,可以在
java
环境中运行任何的
jar
包或者
war
包
#!/bin/bash
/usr/local/jdk/bin/java
-jar
/java/src/*.?ar
一、思路分析:
(
1
)
nginx
1
、下载镜像,将本地的
dist
项目的目录挂载在容器的
/usr/share/nginx/html/
2
、启动容器
3
、该项目是一个前后端分离的项目,并非所有的请求都是来自同一个位置,设置请求的时候还是需要在hosts
文件中挟持域名
4
、域名是固定的,但是,域名可以绑定不同的
ip
5
、所以我们设置前端请求发送给一个
bu.yuanyu.zhangmin
的域名,然后在本机中将域名劫持给我
们的
docker
服务器,当我们发送请求给该域名时,
docker
服务器将给我们作出回应
(
2
)
mysql
1
、各个版本都有官方的镜像,直接下载
docker pull mysql:5.7.44
2
、启动容器的时候,挂载
data
目录
docker run
-itd -p3306
:3306
-v
/root/pes/data:/var/lib/mysql
-e
MYSQL_ROOT_PASSWORD
=
root mysql:5.7.44
3
、制作
data
的时候,顺⼿删除
auto.cnf
(
3
)
tomcat
1
、被
war
包集成,所以我们直接启动
war
2
、
springboot
可以直接集成
tomcat
,
build
一个
jar
包或者是
war
包
3
、我们没有安装
tomcat
4
、
application.properties
(将
tomcat
连接数据库)
1
、可以配置端口
2
、配置数据库的访问
5
、使用
/usr/local/jdk/bin/java -jar Project_ExamSystem-V1.0.0.war
启动,但是在启动这个任务时
一定要跳转到
application.properties
文件所在目录,因为在启动的同时,还需要加载
applicaiton.properties
二、制作
docker-compose.yml
文件,实现一键部署
1
、基础准备(保证基础镜像无问题)
1
)下载基础镜像
[root@docker ~]
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 5ef79149e0ec
2
weeks ago 188MB
mysql
5
.7.44 5107333e08a8
8
months ago 501MB
centos latest 5d0da3dc9764
2
years ago 231MB
2
)创建
nginx:v0
镜像
[root@docker project_exam_system]
# tree web/
web/
├── dist
│?? ├── assets
│?? │?? ├── AdminView-yX0Ltz_1.js
│?? │?? ├── CategoryView-Ca4t3JNT.js
│?? │?? ├── CityView-0ESlUfo8.css
│?? │?? ├── CityView-BJTl06GN.js
......
│ ├── TeacherView-Cogr4CCq.js
│ │ ├── TopicView-DFXgxSyC.js
│ │ └── TopicView-Is6fJS__.css
│ ├── favicon.ico
│ └── index.html
└── Dockerfile
[root@docker web]
# docker build -t nginx:v0 .
[
+
] Building
0
.2s (7/7) FINISHED
docker:default
[root@docker ~]
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx v0 13ccc573fe05
9
seconds ago 189MB
3
)创建
java:v0
镜像
[root@docker project_exam_system]
# tree java
java
├── application.properties
├── Dockerfile
├── java.sh
├── jdk
│ ├── bin
│ │ ├── jar
│ │ ├── jarsigner
│ │ ├── java
│ │ ├── javac
......
│ │ ├── rmiregistry.1
│ │ └── serialver.1
│ ├── README
│ └── release
├── jdk-17_linux-x64_bin.tar.gz
└── Project_ExamSystem-V1.0.0.war
[root@docker java]
# docker build -t java:v0 .
[
+
] Building
7
.2s (9/9) FINISHED
docker:default
[root@docker java]
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
java v0 7fae85c42cbe
36
seconds ago 591MB
4
)创建
mysql:v0
镜像
[root@docker project_exam_system]
# tree mysql/
mysql/
├── Dockerfile
└── project_exam_system.sql
0
directories,
2
files
[root@docker mysql]
# docker build -t mysql:v0 .
[
+
] Building
0
.3s (7/7) FINISHED
docker:default
[root@docker mysql]
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql v0 258d8ed442ac
16
seconds ago 512MB
5
)启动
nginx
容器
[root@docker ~]
# docker run -itd -p80:80 nginx:v0
6
)启动
java
容器
[root@docker ~]
# docker run -itd --name java -p8080:8080 java:v0
7
)启动
mysql
容器
[root@docker ~]
# docker run -itd --name mysql -p3306:3306 mysql:v0
8
)查看容器状态
[root@docker ~]
# docker ps
CONTAINER ID IMAGE COMMAND CREATED
STATUS PORTS
NAMES
79cc8614e967 mysql:v0
"docker-entrypoint.s…"
9
seconds ago
Up
6
seconds
0
.0.0.0:3306->3306/tcp, :::3306->3306/tcp,
33060
/tcp
mysql
29d3ce68ceca java:v0
"/usr/local/jdk/bin/…"
36
seconds ago
Up
34
seconds
0
.0.0.0:8080->8080/tcp, :::8080->8080/tcp java
fed2633b259b haproxy:latest
"docker-entrypoint.s…"
12
minutes ago
Up
11
minutes
0
.0.0.0:5000->5000/tcp, :::5000->5000/tcp
wizardly_edison
#
之后这些容器可以全部直接删除
[root@docker ~]
# docker rm -f 79
(容器编号)
29
(容器编号)
fe
(容器编号)
![](https://i-blog.csdnimg.cn/direct/666e4da34aa74a3e9f8c0a3c7f176304.png)
2
、配置高可用的项目(使用
haproxy
调用
web
和
java
容器)
1
)先创建三个前端项目(
nginx
容器)
容器在不映射端口时,在远程是无法访问
nginx
服务,而且现在也不希望外部直接访问
nginx
,希望
创建
nginx
服务的集群,这个集群被
haproxy
代理,创建
3
个
nginx
容器,创建一个
haproxy
服务器,而且nginx容器还需要指定名称,
web0
、
web1
、
web2
。因为如果没有名称,那么容器就无法被
haproxy link到。
#
端口不能映射
80
,并指定名称
[root@docker ~]
# docker run -itd --name web0 nginx:v0
45d83cda5bef619b937d25e581db31401841b955f9367dbffbc79a236e632612
[root@docker ~]
# docker run -itd --name web1 nginx:v0
b9c80deb9f08a4c2327c1784c8fdb3ab8044c48160ee29102f27e2b52495b9f4
[root@docker ~]
# docker run -itd --name web2 nginx:v0
5fbd082f529cdab21b3a2eb74ae6d9560c694fe2a5368e1ad63affe1ad1c93e8
#
查看容器状态
[root@docker ~]
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS
PORTS NAMES
5fbd082f529c nginx:v0
"/docker-entrypoint.…"
4
seconds ago Up
3
seconds
80
/tcp web2
b9c80deb9f08 nginx:v0
"/docker-entrypoint.…"
8
seconds ago Up
6
seconds
80
/tcp web1
45d83cda5bef nginx:v0
"/docker-entrypoint.…"
12
seconds ago Up
11
seconds
80
/tcp web0
2
)
haproxy
容器外部测试
在宿主机上安装了
haproxy
,编辑配置文件,代理三个
nginx
容器中的
web
服务,是直接添加容器的
ip
地址
#
安装
haproxy
[root@docker ~]
# yum -y install haproxy
#
查看三个
nginx
容器的
IP
地址
[root@docker ~]
# docker inspect 45 | grep IPA
"SecondaryIPAddresses"
: null,
"IPAddress"
:
"172.17.0.2"
,
"IPAMConfig"
: null,
"IPAddress"
:
"172.17.0.2"
,
[root@docker ~]
# docker inspect b9 | grep IPA
"SecondaryIPAddresses"
: null,
"IPAddress"
:
"172.17.0.3"
,
"IPAMConfig"
: null,
"IPAddress"
:
"172.17.0.3"
,
[root@docker ~]
# docker inspect 5f | grep IPA
"SecondaryIPAddresses"
: null,
"IPAddress"
:
"172.17.0.4"
,
"IPAMConfig"
: null,
"IPAddress"
:
"172.17.0.4"
,
#
修改配置文件
[root@docker ~]
# vim /etc/haproxy/haproxy.cfg
#
注释静态资源
# use_backend static if url_static
#
修改轮询模块
backend app
balance roundrobin
server app1
172
.17.0.2:80 check
server app2
172
.17.0.3:80 check
server app3
172
.17.0.4:80 check
#
启动服务
[root@docker ~]
# haproxy -f /etc/haproxy/haproxy.cfg
[root@docker ~]
# netstat -lntup | grep 5000
tcp
0 0 0
.0.0.0:5000
0
.0.0.0:* LISTEN
1985
/haproxy
#
测试访问过后可以直接删除进程来停止服务
[root@docker ~]
# kill -9 1985
(进程号)
[root@docker ~]
# netstat -lntup | grep 5000
![](https://i-blog.csdnimg.cn/direct/e48e9b34301e4a81b019f3d5c662bdc6.png)
3
)创建
haproxy
容器
创建一个
haproxy
容器,将配置文件导入到容器,在容器中启动
haproxy
,也是可以的
#
拉取
haprxy
镜像
[root@docker ~]
# docker pull haproxy
[root@docker ~]
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
haproxy latest 4e5bebb0fd91
7
weeks ago 103MB
#
该
haproxy.cfg
文件是在官方网站里下载下来的
[root@docker ~]
# vim haproxy.cfg
#
只修改
web
的轮询模块即可,默认是
5000
端口,也可修改
listen proxy-web
bind
0
.0.0.0:5000
......
server web0
172
.17.0.2:80 check weight
1
maxconn
2000
server web1
172
.17.0.3:80 check weight
1
maxconn
2000
server web2
172
.17.0.4:80 check weight
1
maxconn
2000
[root@docker ~]
# docker run -itd -p5000:5000 haproxy:latest /bin/bash
fed2633b259b96d3c0ed5e9ca51c031c36b1e21361cb3cf9d57b9d49a9ea1710
[root@docker ~]
# docker cp haproxy.cfg fed:/usr/local/etc/haproxy
Successfully copied
5
.12kB to fed:/usr/local/etc/haproxy
[root@docker ~]
# docker attach fed
haproxy@fed2633b259b:~
$ ls
/usr/local/etc/haproxy/haproxy.cfg
/usr/local/etc/haproxy/haproxy.cfg
#
启动服务
haproxy@fed2633b259b:~
$ haproxy
-f
/usr/local/etc/haproxy/haproxy.cfg
![](https://i-blog.csdnimg.cn/direct/92026a38cc2c4d6eb728c28e58e051de.png)
4
)使用
haproxy
容器调用
web
容器
# -itd
交互 终端 后台
# link
锚定
web
容器
# v
将配置文件挂载到容器中
[root@docker ~]
# docker run -itd --link web0 --link web1 --link web2 -
p5000:5000 -v /root/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg
haproxy:latest
65466d3aef2566512b63690c02e4497ddb7594b8268a26136ec040e2cb858b3b
[root@docker ~]
# docker ps
CONTAINER ID IMAGE COMMAND CREATED
STATUS PORTS NAMES
65466d3aef25 haproxy:latest
"docker-entrypoint.s…"
6
seconds ago Up
4
seconds
0
.0.0.0:5000->5000/tcp, :::5000->5000/tcp
romantic_curie
79cc8614e967 mysql:v0
"docker-entrypoint.s…"
2
hours ago Up
2
hours
0
.0.0.0:3306->3306/tcp, :::3306->3306/tcp,
33060
/tcp mysql
29d3ce68ceca java:v0
"/usr/local/jdk/bin/…"
3
hours ago Up
3
hours
0
.0.0.0:8080->8080/tcp, :::8080->8080/tcp java
5fbd082f529c nginx:v0
"/docker-entrypoint.…"
4
hours ago Up
4
hours
80
/tcp web2
b9c80deb9f08 nginx:v0
"/docker-entrypoint.…"
4
hours ago Up
4
hours
80
/tcp web1
45d83cda5bef nginx:v0
"/docker-entrypoint.…"
4
hours ago Up
4
hours
80
/tcp web0
#
删除该
haproxy
容器(使用的是
ip
地址,要改为使用域名才可以进行
link
)
[root@docker ~]
# docker rm -f 65
![](https://i-blog.csdnimg.cn/direct/804f945b3e0c4ecf961e7940ba53b0a7.png)
5
)调用
haproxy
的监控界面(并改
ip
轮询为域名轮询)
#
只查看即可,创建
haproxy
服务器容器时将相应端口映射出去就行,记得初始的账户和密码
[root@docker ~]
# vim haproxy.cfg
########
监控界面配置
#################
listen admin_status
#
监控界面访问信息
bind
0
.0.0.0:8888
mode http
# URI
相对地址
stats uri /dbs
#
统计报告格式
stats realm Global\ statistics
#
登录账户信息
stats auth admin:123456
[root@docker ~]
# vim haproxy.cfg
server web0 web0:80 check weight
1
maxconn
2000
server web1 web1:80 check weight
1
maxconn
2000
server web2 web2:80 check weight
1
maxconn
2000
[root@docker ~]
# docker run -itd --link web0 --link web1 --link web2 -
p5000:5000 -p8888:8888 -v
/root/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg haproxy:latest
#
删除该
haproxy
容器
[root@docker ~]
# docker rm -f 8af
![](https://i-blog.csdnimg.cn/direct/8df56af7c0124a7a9b98583b8929bcbf.png)
![](https://i-blog.csdnimg.cn/direct/8c85f95412664e75bab4fd96eec05d54.png)
6
)使用
haproxy
容器调用
java
容器
[root@docker ~]
# docker run -itd --name java0 java:v0
283c87bcaea166b017160aa84ce5424dd9baf3ec8fd168a1406f7dc11be3e694
[root@docker ~]
# docker run -itd --name java1 java:v0
e851f31c87f39c2d028e42b36bd77bcf818a8eb97c9a6cc002656fd1501c0ea6
[root@docker ~]
# docker run -itd --name java2 java:v0
fb94c87c4fa87d08b0d621157aa33ed74ca459beb32a3c950a7703227ab2f031
[root@docker ~]
# vim haproxy.cfg
#
复制一份
proxy-web
的模块,修改为
java
模块
listen proxy-java
#
修改端口为
8080
bind
0
.0.0.0:8080
mode http
#
负载均衡算法
# static-rr
权重
, leastconn
最少连接
, source
请求
IP,
轮询
roundrobin
balance roundrobin
#
日志格式
option tcplog
#
在
mysql
创建一个没有权限的
haproxy
用户,密码为空。
haproxy
用户
# create user 'haproxy'@'%' identified by ''; FLUSH PRIVILEGES;
#option mysql-check user haproxy
#
这里是容器中的
IP
地址,由于配置的是轮询
roundrobin
,
weight
权重其实没有生效
#
修改域名为
java
域名,端口为
java
端口
server java0 java0:8080 check weight
1
maxconn
2000
server java1 java1:8080 check weight
1
maxconn
2000
server java2 java2:8080 check weight
1
maxconn
2000
#server MYSQL_3 192.168.130.102:3306 check weight 1 maxconn 2000
#
使用
keepalive
检测死链
# option tcpka
[root@docker ~]
# docker run -itd --link web0 --link web1 --link web2 --link
java0 --link java1 --link java2 -p8080:8080 -p5000:5000 -p8888:8888 -v
/root/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg haproxy:latest
e5ea894c0f30988604cfd6fe5a48b9a69f14c2c3f2e373fe775c0554f37f19db
![](https://i-blog.csdnimg.cn/direct/38e6380672a64065b1849e7c3faee199.png)
#
下载
http-tools
,使用
ab
测试承载量
[root@docker ~]
# yum -y install http-tools
[root@docker ~]
# ab -n 100 -c 10 http://10.0.0.7:5000/
![](https://i-blog.csdnimg.cn/direct/e8cae77b902342c0ba401594f8f73a7f.png)
![](https://i-blog.csdnimg.cn/direct/e07984a12f0c4f9ea3272c3d7aff33b7.png)
3
、编辑
docker-compose.yml
文件,一键部署完整项目
新机子:
1
)
docker-compose
环境准备
#
配置
docker
环境
source
docker.sh
scp
10
.0.0.7:/etc/docker/daemon.json /etc/docker/daemon.json
vim
/etc/docker/daemon.json
{
"registry-mirrors"
: [
"https://do.nark.eu.org"
,
"https://dc.j8.work"
,
"https://docker.m.daocloud.io"
,
"https://dockerproxy.com"
,
"https://docker.mirrors.ustc.edu.cn"
,
"https://docker.nju.edu.cn"
]
}
systemctl
start
docker
#
安装
pip
(
python
包管理器)
yum
-y
install python2-pip
#
升级
pip
pip install
--upgrade
pip
==
20
.3
-i
https://mirrors.aliyun.com/pypi/simple
#
安装
docker-compose
[root@compose ~]
# pip install docker-compose --ignore-installed requests -i
https://mirrors.aliyun.com/pypi/simple
2
)使用
compose
构建自动化部署文件
#
创建项目目录
[root@compose ~]
# mkdir -p pes/{java,mysql,web}
[root@compose ~]
# tree pes/
pes/
├── java
├── mysql
└── web
3
directories,
0
files
[root@compose ~]
# cd pes
#
在项目目录中创建
docker-compoce.yml
文件
[root@compose pes]
# vim docker-compose.yml
version:
"3"
services:
web:
container_name: web0
image: nginx:latest
ports:
-
"80:80"
volumes:
-
./web/src/dist/:/usr/share/nginx/html/
expose:
-
80
restart
:
"always"
# mysql:
# container_name: mysql0
# image: mysql:5.7.44
# java:
# container_name: java0
# image: java:v0
[root@compose pes]
# mkdir -p web/src/
[root@compose pes]
# scp -r 10.0.0.7:/root/project_exam_system/web/dist
web/src/
[root@compose pes]
# ls web/src/
dist
#
拉取
nginx
镜像
[root@compose pes]
# docker pull nginx
Using default tag: latest
#
执行
docker compose
命令创建指定容器
[root@compose pes]
# docker compose up -d
WARN[0000] /root/pes/docker-compose.yml:
`version`
is obsolete
[
+
] Running
1
/1
✔
Container web0 Started
0
.9s
#
查看容器是否正常创建启动
[root@compose pes]
# docker ps
CONTAINER ID IMAGE COMMAND CREATED
STATUS PORTS NAMES
5a007ca2fdbe nginx:latest
"/docker-entrypoint.…"
About a minute ago
Up About a minute
0
.0.0.0:80->80/tcp, :::80->80/tcp web0
![](https://i-blog.csdnimg.cn/direct/fc36cf0855364c7c9b55ab1ab8a2a6d3.png)
3
)使用
docker compose
一次性创建多台完全一样的容器
#
查看
docker-compose
的帮助文档
[root@compose ~]
# docker-compose --help
scale Set number of containers
for
a
service
#
查看
docker-compose scale
的帮助文档
[root@compose ~]
# docker-compose scale --help
Usage: scale [options]
[SERVICE
=
NUM...]
#
一次性创建多台相同容器时,不能为容器启相同的名称,映射相同的端口,所以需要在
yml
文件中将这
两行注释掉
[root@compose pes]
# vim docker-compose.yml
version:
"3"
services:
web:
#container_name: web0
image: nginx:latest
#ports:
#- "80:80"
volumes:
-
./web/src/dist/:/usr/share/nginx/html/
expose:
-
80
restart
:
"always"
#
使用
scale
选项创建
3
台相同的
web
容器
[root@compose pes]
# docker compose up --scale web=3 -d
WARN[0000] /root/pes/docker-compose.yml:
`version`
is obsolete
[
+
] Running
3
/3
✔
Container pes-web-3 Started
0
.8s
✔
Container pes-web-1 Started
0
.5s
✔
Container pes-web-2 Started
1
.1s
#
查看容器列表
[root@compose ~]
# docker ps
CONTAINER ID IMAGE COMMAND CREATED
STATUS PORTS NAMES
dd63d63e1ced nginx:latest
"/docker-entrypoint.…"
29
minutes ago Up
29
minutes
80
/tcp pes_web_2
12edb14dfae7 nginx:latest
"/docker-entrypoint.…"
29
minutes ago Up
29
minutes
80
/tcp pes_web_1
89fa62180f85 nginx:latest
"/docker-entrypoint.…"
29
minutes ago Up
29
minutes
80
/tcp pes_web_3
# docker-compose
暂停集群
[root@compose ~]
# cd pes/
[root@compose pes]
# docker-compose stop
/usr/lib/python2.7/site-packages/paramiko/transport.py:33:
CryptographyDeprecationWarning: Python
2
is no longer supported by the Python
core team. Support
for
it is now deprecated
in
cryptography, and will be
removed
in
the next release.
from cryptography.hazmat.backends import default_backend
Stopping pes-web-1 ...
done
Stopping pes-web-3 ...
done
Stopping pes-web-2 ...
done
[root@compose pes]
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
# docker-compose
移除集群
[root@compose pes]
# docker-compose down
/usr/lib/python2.7/site-packages/paramiko/transport.py:33:
CryptographyDeprecationWarning: Python
2
is no longer supported by the Python
core team. Support
for
it is now deprecated
in
cryptography, and will be
removed
in
the next release.
from cryptography.hazmat.backends import default_backend
Removing pes-web-1 ...
done
Removing pes-web-3 ...
done
Removing pes-web-2 ...
done
Removing network pes_default
[root@compose pes]
# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES