云计算41——摆设project_exam_system项目(续)
# 创建脚本,可以在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目次
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)下载基础镜像
# 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镜像
# 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
# docker build -t nginx:v0 .
[+] Building 0.2s (7/7) FINISHED
docker:default
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx v0 13ccc573fe05 9 seconds ago 189MB
2)创建nginx:v0镜像
# 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
# docker build -t nginx:v0 .
[+] Building 0.2s (7/7) FINISHED
docker:default
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx v0 13ccc573fe05 9 seconds ago 189MB
3)创建java:v0镜像
# 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
# docker build -t java:v0 .
[+] Building 7.2s (9/9) FINISHED
docker:default
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
java v0 7fae85c42cbe 36 seconds ago 591MB
4)创建mysql:v0镜像
# tree mysql/
mysql/
├── Dockerfile
└── project_exam_system.sql
0 directories, 2 files
# docker build -t mysql:v0 .
[+] Building 0.3s (7/7) FINISHED
docker:default
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql v0 258d8ed442ac 16 seconds ago 512MB
5)启动nginx容器
# docker run -itd -p80:80 nginx:v0
6)启动java容器
# docker run -itd --name java -p8080:8080 java:v0
7)启动mysql容器
# docker run -itd --name mysql -p3306:3306 mysql:v0
8)查察容器状态
# 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
# 之后这些容器可以全部直接删除
# docker rm -f 79(容器编号) 29(容器编号) fe(容器编号)
https://i-blog.csdnimg.cn/direct/46e4d8100b5e405bbce144e6b0ccf84e.png
2、配置高可用的项目(使用haproxy调用web和java容器)
1)先创建三个前端项目(nginx容器)
容器在不映射端口时,在远程是无法访问nginx服务,而且现在也不希望外部直接访问nginx,希望 创建nginx服务的集群,这个集群被haproxy署理,创建3个nginx容器,创建一个haproxy服务器,而且
nginx容器还需要指定名称,web0、web1、web2。因为如果没闻名称,那么容器就无法被haproxy link到。
# 端口不能映射80,并指定名称
# docker run -itd --name web0 nginx:v0
45d83cda5bef619b937d25e581db31401841b955f9367dbffbc79a236e632612
# docker run -itd --name web1 nginx:v0
b9c80deb9f08a4c2327c1784c8fdb3ab8044c48160ee29102f27e2b52495b9f4
# docker run -itd --name web2 nginx:v0
5fbd082f529cdab21b3a2eb74ae6d9560c694fe2a5368e1ad63affe1ad1c93e8
# 查察容器状态
# 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
# yum -y install haproxy
# 查察三个nginx容器的IP地点
# docker inspect 45 | grep IPA
"SecondaryIPAddresses": null,
"IPAddress": "172.17.0.2",
"IPAMConfig": null,
"IPAddress": "172.17.0.2",
# docker inspect b9 | grep IPA
"SecondaryIPAddresses": null,
"IPAddress": "172.17.0.3",
"IPAMConfig": null,
"IPAddress": "172.17.0.3",
# docker inspect 5f | grep IPA
"SecondaryIPAddresses": null,
"IPAddress": "172.17.0.4",
"IPAMConfig": null,
"IPAddress": "172.17.0.4",
# 修改配置文件
# 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
# 启动服务
# haproxy -f /etc/haproxy/haproxy.cfg
# netstat -lntup | grep 5000
tcp 0 0 0.0.0.0:5000 0.0.0.0:* LISTEN
1985/haproxy
# 测试访问过后可以直接删除历程来停止服务
# kill -9 1985(历程号)
# netstat -lntup | grep 5000
https://i-blog.csdnimg.cn/direct/13c8882ca425450a9092910b7d149f2c.png
3)创建haproxy容器
创建一个haproxy容器,将配置文件导入到容器,在容器中启动haproxy,也是可以的
# 拉取haprxy镜像
# docker pull haproxy
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
haproxy latest 4e5bebb0fd91 7 weeks ago 103MB
# 该haproxy.cfg文件是在官方网站里下载下来的
# 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
# docker run -itd -p5000:5000 haproxy:latest /bin/bash
fed2633b259b96d3c0ed5e9ca51c031c36b1e21361cb3cf9d57b9d49a9ea1710
# docker cp haproxy.cfg fed:/usr/local/etc/haproxy
Successfully copied 5.12kB to fed:/usr/local/etc/haproxy
# 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/7fd1f002aa5f42e3a47524d96d315a44.png
4)使用haproxy容器调用web容器
# -itd 交互 终端 后台
# link 锚定web容器
# v 将配置文件挂载到容器中
# docker run -itd --link web0 --link web1 --link web2 -
p5000:5000 -v /root/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg
haproxy:latest
65466d3aef2566512b63690c02e4497ddb7594b8268a26136ec040e2cb858b3b
# 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)
# docker rm -f 65
https://i-blog.csdnimg.cn/direct/f78e391e09844226bd7ded1f3d181803.png
5)调用haproxy的监控界面(并改ip轮询为域名轮询)
# 只查察即可,创建haproxy服务器容器时将相应端口映射出去就行,记得初始的账户和密码
# 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
# 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
# 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容器
# docker rm -f 8af
https://i-blog.csdnimg.cn/direct/20c0011d30bd4fa9bdc63fae75c92cad.png https://i-blog.csdnimg.cn/direct/d19dbc0ebfcc4b69bb21a283bba611ca.png
6)使用haproxy容器调用java容器
# docker run -itd --name java0 java:v0
283c87bcaea166b017160aa84ce5424dd9baf3ec8fd168a1406f7dc11be3e694
# docker run -itd --name java1 java:v0
e851f31c87f39c2d028e42b36bd77bcf818a8eb97c9a6cc002656fd1501c0ea6
# docker run -itd --name java2 java:v0
fb94c87c4fa87d08b0d621157aa33ed74ca459beb32a3c950a7703227ab2f031
# 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
# 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/f93bb2a2d9c940768de8af6ba136d2e0.png # 下载http-tools,使用ab测试承载量
# yum -y install http-tools
# ab -n 100 -c 10 http://10.0.0.7:5000/
https://i-blog.csdnimg.cn/direct/6b345d221e03417c876f1a6b9a9d49e1.png https://i-blog.csdnimg.cn/direct/d14efb771c804439889cbfd044f44cae.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
# pip install docker-compose --ignore-installed requests -i
https://mirrors.aliyun.com/pypi/simple
2)使用compose构建自动化摆设文件
# 创建项目目次
# mkdir -p pes/{java,mysql,web}
# tree pes/
pes/
├── java
├── mysql
└── web
3 directories, 0 files
# cd pes
# 在项目目次中创建docker-compoce.yml文件
# 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
# mkdir -p web/src/
# scp -r 10.0.0.7:/root/project_exam_system/web/dist
web/src/
# ls web/src/
dist
# 拉取nginx镜像
# docker pull nginx
Using default tag: latest
# 实行docker compose命令创建指定容器
# docker compose up -d
WARN /root/pes/docker-compose.yml: `version` is obsolete
[+] Running 1/1
✔ Container web0 Started
0.9s
# 查察容器是否正常创建启动
# 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/2bf87f4a4f6841e1b057f267dc89d2f8.png
3)使用docker compose一次性创建多台完全一样的容器
# 查察docker-compose的帮助文档
# docker-compose --help
scale Set number of containers for a service
# 查察docker-compose scale的帮助文档
# docker-compose scale --help
Usage: scale
# 一次性创建多台相同容器时,不能为容器启相同的名称,映射相同的端口,所以需要在yml文件中将这
两行注释掉
# 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容器
# docker compose up --scale web=3 -d
WARN /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
# 查察容器列表
# 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暂停集群
# cd 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
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
# docker-compose移除集群
# 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
# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页:
[1]