8.30
回顾
1、使用harbor仓库
python --version
yum -y update
yum -y install python2-pip
部署考试系统
使用docker部署project-exam-system
1、在一台主机内,实现容器的编排看,发布考试系统
2、环境准备
docker
docker-compose
docker脚本
[root@docker001 ~]# vim docker.sh
cat << EOF | tee /etc/modules-load.d/k8s.conf
overlay
br_netfilter
EOF
modprobe overlay
modprobe br_netfilter
cat << EOF | tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.ipv4.ip_forward = 1
EOF
sysctl --system
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y
# 运行docker脚本
[root@docker001 ~]# source docker.sh
修改配置文件
[root@docker001 ~]# 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"
]
}
[root@docker001 ~]# vim /usr/lib/systemd/system/docker.service
13 ExecStart=/usr/bin/dockerd
[root@docker001 ~]# systemctl daemon-reload
重启服务
[root@docker001 ~]# systemctl restart docker
3、普通的部署
镜像
拉取nginx镜像
[root@docker001 ~]# docker pull nginx
查看镜像
[root@docker001 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 5ef79149e0ec 2 weeks ago 188MB
创建容器
# 创建容器
[root@docker001 ~]# docker run -itd -p80:80 nginx:latest
db8ffd17378657990222bb65accff54631c41e9251c844f1f95c8b66d44a0c7a
# 查看创建的容器
[root@docker001 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
db8ffd173786 nginx:latest "/docker-entrypoint...." 26 seconds ago Up 25 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp upbeat_stonebraker
# 访问测试
[root@docker001 ~]# curl localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
# 使用exec命令查看容器nginx中的/usr/share/nginx/html目录
[root@docker001 ~]# docker exec -it db8 ls /usr/share/nginx/html
50x.html index.html
--前端:nginx latest服务
创建一个目录,不单独放置前端的资料,将后端的所有资料也都放在这里
创建目录
[root@docker001 ~]# mkdir project_exam_system
[root@docker001 ~]# cd project_exam_system/
[root@docker001 project_exam_system]# mkdir web
[root@docker001 project_exam_system]# cd web/
[root@docker001 web]# rz -E
rz waiting to receive.
将dist文件放到web目录下
# 解压dist.zip包
[root@docker001 web]# unzip dist.zip
# 查看解压后的文件
[root@docker001 web]# ls
dist dist.zip
# 删除dist.zip包
[root@docker001 web]# rm -rf dist.zip
# 只需要留下解压后的dist文件
[root@docker001 web]# ls
dist
#查看内容
[root@docker001 web]# cd ..
[root@docker001 project_exam_system]# ls web/dist/
assets favicon.ico index.html
删除测试nginx页面所创建容器
# 停止db8容器的运行
[root@docker001 project_exam_system]# docker stop db8
db8
# 删除容器
[root@docker001 project_exam_system]# docker rm db8
db8
#创建容器,映射端口,挂载目录
[root@docker001 web]# docker run -itd -p80:80 -v /root/project_exam_system/web/dist/:/usr/share/nginx/html nginx:latest
11568e4a88646ea4b1cb9243678fe220ae263ea70591441f093dd72a7f4660a8
查看所创建的容器
[root@docker001 web]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
11568e4a8864 nginx:latest "/docker-entrypoint...." 29 seconds ago Up 28 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp vibrant_gates
修改hosts文件
-在物理机的C:\Windows\System32\drivers\etc中找到hosts文件,添加IP地址和域名
--测试
--上传前端项目dist
--创建启动容器
[root@d3 project_exam_system]# docker run -itd -p80:80 -v /root/project_exam_system/web/dist/:/usr/share/nginx/html nginx:latest5b4ca7741c21ecffe34e13e3b7c6da3e9fb7901bb68b3bab7c470ee83b0ca91b[root@d3 project_exam_system]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES5b4ca7741c21 nginx:latest "/docker-entrypoint...." 4 seconds ago Up 3 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp wonderful_hertz
浏览器访问测试
在实现登录的时候,出现问题,因为后端还没有部署
登录的时候
-后端的业务 springboot 不用单独部署tomcat,已经在springboot中内置
-数据持久化业务 mysql5.7
关于后端
#拉取cnetos镜像
[root@docker001 ~]# docker pull centos
#拖包
#将软件包jdk-17_linux-x64_bin.tar.gz 和软件包project_exam_system.zip拖到root下
[root@docker001 ~]# ls
anaconda-ks.cfg docker.sh jdk-17_linux-x64_bin.tar.gz project_exam_system.zip
#解压
解压project_exam_system.zip软件包
[root@docker001 ~]# unzip project_exam_system.zip
Archive: project_exam_system.zip
# 查看解压包后的文件
[root@docker001 ~]# ls
anaconda-ks.cfg docker.sh Project_ExamSystem-V1.0.0.war
application.properties jdk-17_linux-x64_bin.tar.gz project_exam_system.zip
dist project_exam_system.sql
#创建容器
# 使用centos镜像创建容器
[root@docker001 ~]# docker run -itd centos:latest /bin/bash
d107d28c1503012c6f9aa43d09c94d64f439c60de61d2c3c2d4d565690fdca0f
# 查看创建的容器
[root@docker001 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d107d28c1503 centos:latest "/bin/bash" 12 seconds ago Up 11 seconds quirky_chandrasekhar
11568e4a8864 nginx:latest "/docker-entrypoint...." 2 minutes ago Up 2 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp vibrant_gates
# 将宿主机的jdk包复制到容器中
[root@docker001 ~]# docker cp jdk-17_linux-x64_bin.tar.gz d107:/
Successfully copied 183MB to d107:/
# 将宿主机的Project包复制到容器中
[root@docker001 ~]# docker cp Project_ExamSystem-V1.0.0.war d107:/
Successfully copied 41MB to d107:/
# 将宿主机的application文件复制到容器中
[root@docker001 ~]# docker cp application.properties d107:/
Successfully copied 4.1kB to d107:/
#挂载容器终端
[root@docker001 ~]# docker attach d107
查看容器中是否有软件包
[root@d107d28c1503 /]# ls
Project_ExamSystem-V1.0.0.war home media run usr
application.properties jdk-17_linux-x64_bin.tar.gz mnt sbin var
bin lib opt srv
dev lib64 proc sys
etc lost+found root tmp
# 在容器中创建目录
[root@d107d28c1503 /]# mkdir /usr/local/jdk
# 解压软件包到指定目录,并且跳过第一层目录
[root@d107d28c1503 /]# tar -zxf jdk-17_linux-x64_bin.tar.gz -C /usr/local/jdk/ --strip-components=1
[root@d107d28c1503 /]# ls /usr/local/jdk/
LICENSE README bin conf include jmods legal lib man release
修改application.properties文件
[root@d107d28c1503 /]# vi application.properties
spring.datasource.url=jdbc:mysql://192.168.2.60:3306/project_exam_system
在容器中运行,且访问验证
[root@d107d28c1503 /]# /usr/local/jdk/bin/java -jar Project_ExamSystem-V1.0.0.war
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v3.3.2)
...
...
[root@docker001 ~]# curl 173.17.0.3:8080
{"code":20002,"msg":"账号不存在或密码错误"}[root@docker001 ~]#
创建java镜像
[root@docker001 ~]# docker commit d107 java:v0
sha256:64b4ea040bff2b7d3476273d62a5ae235fabe90e495ea81c6ac4efc6e33f2fd3
# 查看创建的java镜像
[root@docker001 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
java v0 64b4ea040bff 10 seconds ago 774MB
<none> <none> 22eb536c0d76 54 minutes ago 774MB
nginx latest 5ef79149e0ec 2 weeks ago 188MB
centos latest 5d0da3dc9764 2 years ago 231MB
[root@docker001 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d107d28c1503 centos:latest "/bin/bash" 14 minutes ago Up 14 minutes quirky_chandrasekhar
11568e4a8864 nginx:latest "/docker-entrypoint...." 17 minutes ago Up 17 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp vibrant_gates
删除centos创建的容器
# 停止容器
[root@docker001 ~]# docker stop d107
d107
# 删除容器
[root@docker001 ~]# docker rm d107
d107
用java镜像创建容器
[root@docker001 ~]# docker run -itd -p8080:8080 java:v0
464d9b2b5f0b4a0cb80ac134128130393f67f7d0bf37c84763cdbf5a29015c1f
运行
# 挂载创建的容器的终端
[root@docker001 ~]# docker attach 464d
# 运行
[root@464d9b2b5f0b /]# /usr/local/jdk/bin/java -jar Project_ExamSystem-V1.0.0.war
访问测试,出现200,500即可
数据库持久化-5.7.44
拉取mysql镜像
[root@docker001 ~]# docker pull mysql:5.7.44
创建
# 使用mysql镜像创建端口号为3306,用户和密码为root的容器
[root@docker001 ~]# docker run -itd -e MYSQL_ROOT_PASSWORD=root -p3306:3306 mysql:5.7.44
1a07feb046ee3038c2364c5b655ff4cfd31acf28db5e747810fc42b05195338d
# 查看创建的容器
[root@docker001 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1a07feb046ee mysql:5.7.44 "docker-entrypoint.s..." About a minute ago Up About a minute 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp optimistic_herschel
464d9b2b5f0b java:v0 "/bin/bash" 2 hours ago Up About an hour 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp epic_pascal
11568e4a8864 nginx:latest "/docker-entrypoint...." 3 hours ago Up About an hour 0.0.0.0:80->80/tcp, :::80->80/tcp vibrant_gates
在project_exam_system/目录下创建mysql目录
[root@docker001 ~]# mkdir mysql
[root@docker001 ~]# mv mysql project_exam_system/
[root@docker001 ~]# ls project_exam_system/
远程登录执行sql文件
完成之后,登录mysql
[root@docker001 ~]# docker exec -it 1a mysql -uroot -proot
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.44 MySQL Community Server (GPL)
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
# 查看数据库
mysql> show databases;
+---------------------+
| Database |
+---------------------+
| information_schema |
| mysql |
| performance_schema |
| project_exam_system |
| sys |
+---------------------+
5 rows in set (0.00 sec)
# 创建和java项目中的用户
mysql> create user 'zhangmin'@'%' identified by 'zhangmin';
Query OK, 0 rows affected (0.00 sec)
# 给创建的用户所有的权限
mysql> grant all on *.* to 'zhangmin'@'%';
Query OK, 0 rows affected (0.00 sec)
# 刷新权限
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql>
再登录,输入用户名和密码,可以进入考试系统