一、回顾
1、使用harbao仓库
1. Python -- version
2. yum -y update
3. yum -y install python2-pip
4. pip install --upgrade pip==20.3 -i https://mirrors.aliyun.com/pypi/simple
5. pip install docker-compose -i https://mirrors.aliyun.com/pypi/simple
6. source docker.sh
7. 解压harbar
8. vim harbor.yml
8.1 hostname
8.2 https 注释
9. ./prepare
10. ./installed
10.1 使用nginx发布web服务
10.2 使用registry管理
2、docker-compose的编排
1. 在学习kubernetes之前的热身
2. 通过运行一个yum管理docker容器
3. docker-compose.yml
vim docker-compose.yml
version: "2"
service:
nginx:
contanner_name: name0
image: nginx:latest
ports:
- "80:80"
volumns:
- /opt/web/:/usr/share/nginx/html/
1.容器的角色
2.容器的位置
3.容器的启动
4.容器之间的关系
playbook role 编排
二、使用docker部署project-exam-system
1、背景
在一台主机之内,实现容器的编排,发布考试系统
2、 环境准备
1. docker
1.1 vim /etc/docker/daemon.json
[root@docker ~]# 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@docker ~]# systemctl start docker
3、普通部署
1. 镜像
1.1 前端:nginx服务
(1)拉取nginx:latest镜像
[root@docker ~]# docker search nginx
[root@docker ~]# docker pull nginx
(2)创建nginx容器
[root@docker ~]# docker run -itd -p80:80 nginx:latest
[root@docker ~]# docker ps --all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 243f7f9a0470 nginx:latest "/docker-entrypoint.…" 12 seconds ago Up 12 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp ecstatic_johnson
[root@docker ~]# docker exec -it 243 ls /usr/share/nginx/html
50x.html index.html
(3)创建一个目录,不单独放置前端的内容,将后端的所有资料也放在这里
[root@docker ~]# mkdir project_exam_system
[root@docker ~]# cd project_exam_system/
[root@docker project_exam_system]# mkdir web[root@docker project_exam_system]# ls web/
dist
[root@docker project_exam_system]# ls web/dist/
assets favicon.ico index.html
(4)上传项目 dist
C:\Users\think book>scp -r E:\xxxx\dist\ root@192.168.8.168:~/project_exam_system/web/
[root@docker project_exam_system]# docker stop 243
243
[root@docker project_exam_system]# docker rm 243
(5)再创建一个容器,不映射端口
[root@docker web]# docker run -itd -p80:80 -v /root/project_exam_system/web/dist/:/usr/share/nginx/html/ nginx:latest
46ffd2bf7face9ce7866af7d440f43f299c4c7d8d6c51444a0db602e162a1353
[root@docker web]# docker exec -it 46f ls /usr/share/nginx/html
assets favicon.ico index.html
(6)测试
登录异常,原因是后端没有部署
登陆的时候请求域名,我们是hosts域名劫持来实现
修改/etc/hosts
192.168.8.168 bu.yuanyu.zhangmin
1.2 后端业务
springboot 优点 不用单独部署tomcat,已经在springboot中部署tomcat
(1)准备拉取基础镜像
[root@docker project_exam_system]# docker pull centos
(2)创建容器
[root@docker ~]# docker run -it centos:latest /bin/bash
[root@5fd07aadd6a6 /]# lsbin etc lib lost+found mnt proc run srv tmp var dev home lib64 media opt root sbin sys usr
(3)将在服务器上的java文件上传到容器中
资源项目.war .properties
[root@docker ~]# docker cp jdk-17_linux-x64_bin.tar.gz 5fd:/
Successfully copied 183MB to 5fd:/
[root@docker ~]# docker cp application.properties 5fd:/
Successfully copied 4.1kB to 5fd:/
[root@docker ~]# docker cp Project_ExamSystem-V1.0.0.war 5fd:/
Successfully copied 41MB to 5fd:/
(4)对已经传到容器的文件进行部署
[root@docker ~]# docker attach 5fd
[root@5fd07aadd6a6 /]# lsProject_ExamSystem-V1.0.0.war lib64 sbin application.properties lost+found srv bin media sys dev mnt tmp etc opt usr home proc var jdk-17_linux-x64_bin.tar.gz root lib run
(4)创建jdk解压目录
[root@5fd07aadd6a6 /]# mkdir /usr/local/jdk
[root@5fd07aadd6a6 /]# tar -zxvf jdk-17_linux-x64_bin.tar.gz -C /usr/local/jdk/ --strip-components=1 //=1 :跳过一层目录[root@5fd07aadd6a6 /]# ls /usr/local/jdk/
LICENSE bin include legal man
README conf jmods lib release
(5)修改applation.proerties文件
[root@5fd07aadd6a6 /]# vi application.properties
spring.datasource.url=jdbc:mysql://192.168.8.168:3306/project_exam_system spring.datasource.username=blt spring.datasource.password=blt spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
(6)启动
[root@5fd07aadd6a6 /]# /usr/local/jdk/bin/java -jar Project_ExamSystem-V1.0.0.war ctrl+p+q
[root@docker ~]# curl 172.17.0.3:8080
{"code":20002,"msg":"账号不存在或密码错误"}
(7)根据容器生成镜像
[root@docker ~]# docker commit 5fd java:v0
[root@docker ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZE java v0 21a2b56dffe3 21 seconds ago 774MB centos yum 1bd5d7b13eed 2 days ago 272MB nginx latest 5ef79149e0ec 2 weeks ago 188MB
[root@docker ~]# docker stop df
df
[root@docker ~]# docker rm 5f
df
(8)根据新建的镜像创建容器
[root@docker ~]# docker run -itd -p8080:8080 java:v0
[root@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f68d82c68dfe java:v0 "/bin/bash" 5 minutes ago Up 5 minutes 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp fervent_ganguly
[root@docker ~]# docker attach f6
[root@f68d82c68dfe /]# /usr/local/jdk/bin/java -jar Project_ExamSystem-V1.0.0.war
1.3 数据持久化业务 mysql 5.7.44
(1)拉取mysql镜像
[root@docker ~]# docker pull mysql:5.7.44
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE mysql 5.7.44 5107333e08a8 8 months ago 501MB
(2)根据镜像制作容器,并映射端口
[root@docker ~]# docker run -itd -e MYSQL_ROOT_PASSWORD=root -p3306:3306 mysql:5.7.44
7dcf9b329a1abef12c82dbcf1d1eac9ce2b908ac84909490088a09e2611dadc8
[root@docker ~]# docker ps --allCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 7dcf9b329a1a mysql:5.7.44 "docker-entrypoint.s…" 5 seconds ago Up 4 seconds 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp nice_gauss
(3)导出数据库文件
[root@docker ~]# docker cp 7dc:/var/lib/mysql project_exam_system/
C:\Users\think book>scp E:\xxx\project_exam_system.sql root@192.168.8.168:~/project_exam_system
[root@docker ~]# cd project_exam_system/
[root@docker project_exam_system]# ls
mysql project_exam_system.sql web[root@docker project_exam_system]# mkdir mysql
[root@docker project_exam_system]# mv project_exam_system.sql mysql/[root@docker project_exam_system]# head mysql/project_exam_system.sql
create database if not exists project_exam_system charset utf8mb4; use project_exam_system; /* Navicat MySQL Data Transfer Source Server : MySQL84 Source Server Type : MySQL Source Server Version : 80400 Source Host : localhost:3308 Source Schema : project_exam_system
(4)将sql上传到MySQL容器
[root@docker ~]# docker cp project_exam_system/mysql/project_exam_system.sql 7dc:/
Successfully copied 11MB to 7dc:/
(5)执行mysql文件
(6)使用exec指令访问数据库管理界面
[root@docker ~]# docker exec -it 7dc mysql -uroot -proot
mysql> show databases; +---------------------+ | Database | +---------------------+ | information_schema | | mysql | | performance_schema | | project_exam_system | | sys | +---------------------+ 5 rows in set (0.01 sec) mysql> create user 'blt'@'%' identified by 'blt'; Query OK, 0 rows affected (0.00 sec) mysql> grant all on *.* to 'blt'@'%'; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
(7)登录测试
(8)Dockerfile优化
创建Java资源存放目录,并把所有资源文件移动到Java目录下
[root@docker ~]# mkdir project_exam_system/java
[root@docker ~]# mv jdk-17_linux-x64_bin.tar.gz project_exam_system/java/
[root@docker ~]# mv application.properties project_exam_system/java/
[root@docker ~]# mv Project_ExamSystem-V1.0.0.war project_exam_system/java/[root@docker ~]# tree -L 3 project_exam_system/
project_exam_system/ ├── java │ ├── application.properties │ ├── jdk-17_linux-x64_bin.tar.gz │ └── Project_ExamSystem-V1.0.0.war ├── mysql │ └── project_exam_system.sql └── web └── dist ├── assets ├── favicon.ico └── index.html 5 directories, 6 files
进入java目录解压jdk并改名
[root@docker ~]# cd project_exam_system/java/
[root@docker java]# tar -axvf jdk-17_linux-x64_bin.tar.gz[root@docker java]# mv jdk-17.0.12/ jdk
编写.sh文件和Dockerfile文件
[root@docker java]# vim start.sh
#!/bin/bash /usr/local/jdk/bin/java -jar Project_ExamSystem-V1.0.0.war
[root@docker java]# vim Dockerfile
FROM centos:latest ADD application.properties /application.properties ADD jdk/ /usr/local/jdk ADD Project_ExamSystem-V1.0.0.war /Project_ExamSystem-V1.0.0.war WORKDIR / EXPOSE 8080 ADD start.sh /start.sh RUN chmod -v +x /start.sh CMD [ "start.sh","/bin/bash" ]
使用当前目录下的Dockerfile来构建一个名为java:v1的镜像
[root@docker java]# docker build -t java:v1 .
[root@docker java]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE java v1 5d3dda7a16e7 21 seconds ago 591MB
根据java:v1镜像创建容器
[root@docker java]# docker run -it -p8081:8080 java:v1 /bin/bash
[root@a3ab4be45e47 /]# /usr/local/jdk/bin/java -jar Project_ExamSystem-V1.0.0.war
[root@docker java]# docker exec a3a ss -anut | grep 8080tcp LISTEN 0 128 *:8080 *:*