手动部署Java项目至后端Web集群
自动部署Java项目至Web集群
Jenkins -192.168.1.30
Gitlab -192.168.1.31
LB -192.168.1.32
Web1 -192.168.1.33
Web2 -192.168.1.34
-后端Web集群组安装Tomcat与Maven
--永久关闭防火墙和Selinux
[root@Jenkins/Gitlab/LB/Web1/Web2 ~]# systemctl disable firewalld
[root@Jenkins/Gitlab/LB/Web1/Web2 ~]# systemctl stop firewalld
[root@Jenkins/Gitlab/LB/Web1/Web2 ~]# setenforce 0
[root@Jenkins/Gitlab/LB/Web1/Web2 ~]# sed -i 's/enforcing/disabled/' /etc/selinux/config
[root@Jenkins/Gitlab/LB/Web1/Web2 ~]# getenforce
[root@Web1 ~]# yum -y install java #需要安装jdk
[root@Web1 ~]# java -version
openjdk version "1.8.0_322"
OpenJDK Runtime Environment (build 1.8.0_322-b06)
OpenJDK 64-Bit Server VM (build 25.322-b06, mixed mode)
[root@Web1/2 ~]# wget https://mirror.tuna.tsinghua.edu.cn/apache/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz
[root@Web1/2 ~]# wget https://mirror.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-9/v9.0.59/bin/apache-tomcat-9.0.59.tar.gz
[root@Web1/2 ~]# ll
-rw-r--r-- 1 root root 9506321 3月 10 07:08 apache-maven-3.6.3-bin.tar.gz
-rw-r--r-- 1 root root 11543067 3月 10 07:08 apache-tomcat-9.0.59.tar.gz
[root@Web1/2 ~]# tar -xf apache-maven-3.6.3-bin.tar.gz -C /usr/local/
[root@Web1/2 ~]# ls /usr/local/
apache-maven-3.6.3 bin etc games include lib lib64 libexec nginx sbin share src
-后端Web集群组Maven镜像仓库与环境变量配置
[root@Web1/2 ~]# cd /usr/local/apache-maven-3.6.3/
[root@Web1/2 apache-maven-3.6.3]# ls
bin boot conf lib LICENSE NOTICE README.txt
[root@Web1/2 apache-maven-3.6.3]# vim conf/settings.xml #编辑settins.xml文件,在159行下加入配置
...
<url>http://my.repository.com/repo/path</url>
</mirror>
-->
<mirror> #复制一个阿里云镜像仓库配置进去
<id>aliyunmaven</id>
<mirrorOf>repositoryId</mirrorOf>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</mirror> #到这里为完整的一个阿里云镜像仓库配置
</mirrors>
...
[root@Web1/2 apache-maven-3.6.3]# vim /etc/profile
...
export MAVEN_HOME=/usr/local/apache-maven-3.6.3 #最后一行加上配置
export PATH=$PATH:$MAVEN_HOME/bin
[root@Web1/2 apache-maven-3.6.3]# source /etc/profile
[root@Web1/2 ~]# mvn -version
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /usr/local/apache-maven-3.6.3
Java version: 1.8.0_322, vendor: Red Hat, Inc., runtime: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.322.b06-1.el7_9.x86_64/jre
Default locale: zh_CN, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-862.el7.x86_64", arch: "amd64", family: "unix"
后端集群组解压并启动Tomcat
[root@Web1/2 ~]# tar -xf apache-tomcat-9.0.59.tar.gz
[root@Web1/2 ~]# cd apache-tomcat-9.0.59
[root@Web1/2 apache-tomcat-9.0.59]# ./bin/startup.sh
-后端负载均衡LB配置
[root@LB ~]# vim /usr/local/nginx/conf/nginx.conf
...
upstream java {
server 192.168.1.33:8080;
server 192.168.1.34:8080;
}
server {
listen 80;
server_name java.fzmyw.com;
location / {
proxy_pass http://java;
proxy_set_header Host $http_host;
}
}
...
[root@LB ~]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@LB ~]# /usr/local/nginx/sbin/nginx -s reoload
Windows访问需对域名做解析
-创建Java-demo项目并推送代码至Gitlab远程项目仓库
[root@Gitlab ~]# ll
java-demo.zip
[root@Gitlab ~]# unzip java-demo.zip #解压java项目代码
[root@Gitlab ~]# cd company/
[root@Gitlab company]# ls
company.iml company.sql lib mvnw mvnw.cmd pom.xml src
[root@Gitlab java-demo]# git init #初始化git仓库
[root@Gitlab java-demo]# git remote add origin git@gitlab.fzmyw.com:devops/java-demo.git #添加远程版本仓库
[root@Gitlab java-demo]# git add . #提交代码至暂存区
[root@Gitlab java-demo]# git commit -m "java-demo" #提交至本地仓库
[root@Gitlab java-demo]# git push origin -u master #推送代码到远程仓库
-安装maven编译工具,手动获取源代码,然后使用maven进行编译
Linux下Maven的安装与配置:https://editor.csdn.net/md/?articleId=123277283
由于Maven编译工具需要依赖Java,所以需要先安装JDK,不能是Jre。
[root@Jenkins ~]# ls
apache-maven-3.6.3-bin.tar.gz
[root@Jenkins ~]# tar -xf apache-maven-3.6.3-bin.tar.gz -C /usr/local/
[root@Jenkins ~]# ls /usr/local/
apache-maven-3.6.3 bin etc games include lib lib64 libexec sbin share src
[root@Jenkins ~]# vim /etc/profile #配置环境变量
...
export MAVEN_HOME=/usr/local/apache-maven-3.6.3 #最后一行加上配置
export PATH=$PATH:$MAVEN_HOME/bin
[root@Jenkins ~]# source /etc/profile
[root@Jenkins ~]# mvn -v
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /usr/local/apache-maven-3.6.3
Java version: 1.8.0_322, vendor: Red Hat, Inc., runtime: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.322.b06-1.el7_9.x86_64/jre
Default locale: zh_CN, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-862.el7.x86_64", arch: "amd64", family: "unix"
-配置maven阿里云镜像仓库
[root@Jenkins ~]# cd /usr/local/apache-maven-3.6.3/
[root@Jenkins apache-maven-3.6.3]# ls
bin boot conf lib LICENSE NOTICE README.txt
[root@Jenkins apache-maven-3.6.3]# vim conf/settings.xml #编辑settins.xml文件,在159行下加入配置
...
<url>http://my.repository.com/repo/path</url>
</mirror>
-->
<mirror> #复制一个镜像仓库配置进去
<id>aliyunmaven</id>
<mirrorOf>repositoryId</mirrorOf>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</mirror> #到这里为完整的一个镜像仓库配置
</mirrors>
...
-拉取Gitlab项目源代码,使用maven手动编译成war包
Maven在编译项目(pom.xml文件)的时候,会根据当前项目源代码自动去下载各种运行项目所需要的jar包,使打成的jar/war包项目能正常运行。
[root@Jenkins ~]# git clone git@gitlab.fzmyw.com:devops/java-demo.git #免密克隆Gitlab项目,需要将ssh公钥内容配置到Gitlab用户的ssh密钥配置中
正克隆到 'java-demo'...
remote: Enumerating objects: 181, done.
remote: Counting objects: 100% (181/181), done.
remote: Compressing objects: 100% (164/164), done.
remote: Total 181 (delta 5), reused 179 (delta 4)
接收对象中: 100% (181/181), 1.12 MiB | 0 bytes/s, done.
处理 delta 中: 100% (5/5), done.
[root@Jenkins ~]# cd company/
[root@Jenkins company]# ls
company.iml company.sql lib mvnw mvnw.cmd pom.xml src
[root@Jenkins company]# mvn package -Dmaven.test.skip=true #maven手动编译成war包,并且跳过所有的单元测试
[INFO] Scanning for projects...
Downloading from central: https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-starter-parent/1.5.6.RELEASE/spring-boot-starter-parent-1.5.6.RELEASE.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-starter-parent/1.5.6.RELEASE/spring-boot-starter-parent-1.5.6.RELEASE.pom (7.5 kB at 3.2 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-dependencies/1.5.6.RELEASE/spring-boot-dependencies-1.5.6.RELEASE.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-dependencies/1.5.6.RELEASE/spring-boot-dependencies-1.5.6.RELEASE.pom (93 kB at 110 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/com/fasterxml/jackson/jackson-bom/2.8.9/jackson-bom-2.8.9.pom
Downloaded from central: https://repo.maven.apache.org/maven2/com/fasterxml/jackson/jackson-bom/2.8.9/jackson-bom-2.8.9.pom (10 kB at 22 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/com/fasterxml/jackson/jackson-parent/2.8/jackson-parent-2.8.pom
...
...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 02:49 min
[INFO] Finished at: 2022-03-10T09:10:38+08:00
[INFO] ------------------------------------------------------------------------
[root@Jenkins company]# ls target/ #可以看到已经编译完成的dbSun-1.0.war包
classes dbSun-1.0 dbSun-1.0.war dbSun-1.0.war.original generated-sources maven-archiver maven-status
-将编译后的war包批量推送至Tomcat集群,然后重启Tomcat
[root@Jenkins company]# for i in {33..34}; do scp target/dbSun-1.0.war root@192.168.1.$i:/root/apache-tomcat-9.0.59/webapps/; done #将编译好的项目war包发送至后端Tomcat集群服务器中tomcat的webapps目录下
dbSun-1.0.war 100% 77MB 97.3MB/s 00:00
dbSun-1.0.war 100% 77MB 5.3MB/s 00:14
[root@Web1/2 ~]# cd apache-tomcat-9.0.59/webapps/
[root@Web1/2 webapps]# ls
dbSun-1.0 dbSun-1.0.war docs examples host-manager manager ROOT
[root@Web1/2 webapps]# rm -rf ROOT/ #删除原有的Tomcat下ROOT目录
[root@Web1/2 webapps]# mv dbSun-1.0.war ROOT.war
[root@Web1/2 webapps]# ls
dbSun-1.0 docs examples host-manager manager ROOT.war
[root@Web1/2 webapps]# ../bin/shutdown.sh && ../bin/startup.sh #重启Tomcat
-升级Jenkins版本
清华大写开源镜像站升级包下载:https://mirror.tuna.tsinghua.edu.cn/jenkins/war-stable/
[root@Jenkins ~]# ll jenkins.war
-rw-r--r-- 1 root root 94952903 3月 10 12:23 jenkins.war
[root@Jenkins ~]# ps aux |grep jenkins
root 20861 0.0 0.0 112724 984 pts/1 R+ 12:24 0:00 grep --color=auto jenkins
root 44189 0.2 24.7 2727080 507424 ? Ssl 3月09 4:50 /etc/alternatives/java -Djava.awt.headless=true -DJENKINS_HOME=/var/lib/jenkins -jar /usr/lib/jenkins/jenkins.war --logfile=/var/log/jenkins/jenkins.log --webroot=/var/cache/jenkins/war --httpPort=8080 --debug=5 --handlerCountMax=100 --handlerCountMaxIdle=20
[root@Jenkins ~]# cd /usr/lib/jenkins/ #进入jenkins运行目录
[root@Jenkins jenkins]# ls
jenkins.war
[root@Jenkins jenkins]# mv jenkins.war jenkins.war.2.263.1 #将旧版本的jenkins.war备份
[root@Jenkins jenkins]# ls
jenkins.war.2.263.1
[root@Jenkins jenkins]# mv /root/jenkins.war . #将新版本的jenkins.war程序拷贝至jenkins运行目录
[root@Jenkins jenkins]# ls
jenkins.war jenkins.war.2.263.1
[root@Jenkins jenkins]# systemctl restart jenkins #重启jenkins
-安装Maven Integration插件
在这里插入图片描述
[root@Jenkins ~]# mvn -v #可以查看到maven跟jdk的安装路径
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /usr/local/apache-maven-3.6.3
Java version: 1.8.0_322, vendor: Temurin, runtime: /root/jdk8u322-b06/jre
Default locale: zh_CN, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-862.el7.x86_64", arch: "amd64", family: "unix"
[root@Jenkins ~]# cd /var/lib/jenkins/workspace/ #进入jenkins工作目录
[root@Jenkins workspace]# ll
总用量 0
drwxr-xr-x 7 jenkins jenkins 271 3月 9 20:36 freestyle-nongye
drwxr-xr-x 2 jenkins jenkins 6 3月 9 20:36 freestyle-nongye@tmp
drwxr-xr-x 6 root root 152 3月 10 13:40 maven-java-demo
drwxr-xr-x 2 root root 6 3月 10 13:39 maven-java-demo@tmp
[root@Jenkins workspace]# cd maven-java-demo
[root@Jenkins maven-java-demo]# ll
总用量 52
-rw-r--r-- 1 root root 20549 3月 10 13:39 company.sql
drwxr-xr-x 2 root root 47 3月 10 13:39 lib
-rw-r--r-- 1 root root 6468 3月 10 13:39 mvnw
-rw-r--r-- 1 root root 4994 3月 10 13:39 mvnw.cmd
-rw-r--r-- 1 root root 8258 3月 10 13:39 pom.xml
drwxr-xr-x 3 root root 18 3月 10 13:39 src
drwxr-xr-x 7 root root 156 3月 10 13:41 target
[root@Jenkins maven-java-demo]# ll target/ #查看编译成功的war包
总用量 152924
drwxr-xr-x 4 root root 88 3月 10 13:41 classes
drwxr-xr-x 4 root root 37 3月 10 13:41 dbSun-1.0
-rw-r--r-- 1 root root 81282311 3月 10 13:41 dbSun-1.0.war
-rw-r--r-- 1 root root 75307785 3月 10 13:41 dbSun-1.0.war.original
drwxr-xr-x 3 root root 25 3月 10 13:41 generated-sources
drwxr-xr-x 2 root root 28 3月 10 13:41 maven-archiver
drwxr-xr-x 3 root root 35 3月 10 13:41 maven-status
-编写自动发布脚本
[root@Jenkins shell]# cp html_depoly_tag.sh java_deploy_tag.sh
[root@Jenkins shell]# vim java_deploy_tag.sh
#!/sbin/bin/bash
DATE=$(date +%Y-%m-%d-%H-%M-%S)
WEB="192.168.1.33 192.168.1.34"
CODE="/root/apache-tomcat-9.0.59/webapps"
Sdir="/opt"
Name=${DATE}-${git_version} #git_version是在Jenkins项目配置中Git Parameter那里设置的变量名字,将时间戳变量跟tag版本变量组合成一个,看着精简一点
scp_web_server() {
for i in ${WEB}
do
scp $WORKSPACE/target/*.war root@$i:$Sdir/ROOT-$Name.war
ssh root@$i "mkdir -p $CODE/ROOT-$Name && \
unzip $Sdir/ROOT-$Name.war -d $CODE/ROOT-$Name && \ #这一步也可以修改下,直接将war包拷贝过去也会自动解压(前提是有unzip命令),但是会出现一些.war文件,所以就这样也可以
rm -rf $CODE/ROOT && \
ln -s $CODE/ROOT-$Name $CODE/ROOT && \
sleep 3 && \
bash /root/apache-tomcat-9.0.59/bin/shutdown.sh && /root/apache-tomcat-9.0.59/bin/startup.sh"
done
}
deploy() {
scp_web_server
}
deploy
-后端web集群组安装unzip,用于解压编译好的war包
[root@Web1/2 bin]# yum -y install unzip
-Jenkins上项目配置
-开发修改项目源代码,并提交一些不同的tag版本至Gitlab仓库中
模拟开发修改项目源代码,提交不同的代码tag版本至Gitlab仓库,自动部署脚本根据Jenkins项目构建时选择的git_version版本去拉取该版本的项目代码进行编译打包并部署。
[root@Gitlab company]# ls
company.iml company.sql lib mvnw mvnw.cmd pom.xml src
[root@Gitlab company]# touch README.md
[root@Gitlab company]# git add .
[root@Gitlab company]# git commit -m "v1.0"
[root@Gitlab company]# git tag -a "v1.0" -m "v1.0版本"
[root@Gitlab company]# git push origin v1.0
...
To git@gitlab.fzmyw.com:devops/java-demo.git
* [new tag] v1.0 -> v1.0
[root@Gitlab company]# echo "Java-demo" >> README.md
[root@Gitlab company]# git add .
[root@Gitlab company]# git commit -m "v2.0"
[root@Gitlab company]# git tag -a "v2.0" -m "v2.0版本"
[root@Gitlab company]# git push origin v2.0
...
To git@gitlab.fzmyw.com:devops/java-demo.git
* [new tag] v2.0 -> v2.0
-构建项目测试自动上线
-修改自动发布脚本,让其支持tag版本回退与判断是否重复构建
[root@Jenkins shell]# cp java_deploy_tag.sh java_deploy_tag_rollback.sh
[root@Jenkins shell]# vim java_deploy_tag_rollback.sh
#!/sbin/bin/bash
DATE=$(date +%Y-%m-%d-%H-%M-%S)
WEB="192.168.1.33 192.168.1.34"
CODE="/root/apache-tomcat-9.0.59/webapps"
Sdir="/opt"
Name=${DATE}-${git_version} #git_version是在Jenkins项目配置中Git Parameter那里设置的变量名字,将时间戳变>量跟tag版本变量组合成一个,看着精简一点
scp_web_server() {
for i in ${WEB}
do
scp $WORKSPACE/target/*.war root@$i:$Sdir/ROOT-$Name.war
ssh root@$i "mkdir -p $CODE/ROOT-$Name && \
unzip $Sdir/ROOT-$Name.war -d $CODE/ROOT-$Name && \
rm -rf $CODE/ROOT && \
ln -s $CODE/ROOT-$Name $CODE/ROOT && \
sleep 3 && \
bash /root/apache-tomcat-9.0.59/bin/shutdown.sh && /root/apache-tomcat-9.0.59/bin/startup.sh"
done
}
deploy() {
scp_web_server
}
rollback() {
back_file=$(ssh root@192.168.1.33 "find ${CODE} -maxdepth 1 -type d -name "ROOT-*-${git_version}"")
#由于后端集群部署回退时间一致,所以这里就只需要到一台上查找我们在Jenkins构建时选择的git_version变量的值,即tag版本相对应的项目版本文件夹
for i in ${WEB}
do
ssh root@$i "rm -rf $CODE/ROOT && \ #删除当前版本的项目软链接文件夹
ln -s ${back_file} $CODE/ROOT" #重新将要回退的项目版本文件夹软链接
done
}
#根据Jenkins项目中配置的选项参数变量deploy_env的值来判断执行哪一个脚本函数(部署脚本还是回退脚本)---解决重复构建问题
if [ $deploy_env == "deploy" ]; then
if [ "$GIT_COMMIT" == "$GIT_PREVIOUS_SUCCESSFUL_COMMIT" ];then
echo "你已经部署过该${git_version}版本"
exit 1
else
deploy
fi
elif [ $deploy_env == "rollback" ]; then
rollback
fi
-Jenkins配置,添加选项参数(支持选择部署或回退)
-Jenkins构建项目,测试回退项目版本
[root@Web1/2 ~]# ll /root/apache-tomcat-9.0.59/webapps/
总用量 79364
drwxr-x--- 15 root root 4096 3月 10 07:09 docs
drwxr-x--- 7 root root 99 3月 10 07:09 examples
drwxr-x--- 6 root root 79 3月 10 07:09 host-manager
drwxr-x--- 6 root root 114 3月 10 07:09 manager
lrwxrwxrwx 1 root root 64 3月 12 09:20 ROOT -> /root/apache-tomcat-9.0.59/webapps/ROOT-2022-03-11-16-18-40-v2.0 #可以看出当前在运行的项目tag版本是v2.0
drwxr-xr-x 5 root root 48 3月 11 09:24 ROOT-2022-03-10-16-22-20-v1.0
drwxr-xr-x 5 root root 48 3月 12 09:20 ROOT-2022-03-11-16-18-40-v2.0
[root@Web1/2 ~]# ll /root/apache-tomcat-9.0.59/webapps/
总用量 79364
drwxr-x--- 15 root root 4096 3月 10 08:18 docs
drwxr-x--- 7 root root 99 3月 10 08:18 examples
drwxr-x--- 6 root root 79 3月 10 08:18 host-manager
drwxr-x--- 6 root root 114 3月 10 08:18 manager
lrwxrwxrwx 1 root root 64 3月 12 02:01 ROOT -> /root/apache-tomcat-9.0.59/webapps/ROOT-2022-03-10-16-22-20-v1.0
drwxr-xr-x 5 root root 48 3月 11 09:24 ROOT-2022-03-10-16-22-20-v1.0
drwxr-xr-x 5 root root 48 3月 12 09:20 ROOT-2022-03-11-16-18-40-v2.0
-Jenkins构建项目,部署已发布过的v2.0版本,测试解决重复构建的问题
-开发修改代码发布v3.0新版本tag提交至Gitlab上,测试Jenkins部署新版本项目
[root@Gitlab ~]# cd company/
[root@Gitlab company]# ls
company.iml company.sql lib mvnw mvnw.cmd pom.xml README.md src
[root@Gitlab company]# echo "第三次提交代码v3.0" >> README.md
[root@Gitlab company]# git add .
[root@Gitlab company]# git commit -m "v3.0"
[root@Gitlab company]# git tag -a "v3.0" -m "v3.0"
[root@Gitlab company]# git push origin v3.0
-Web集群组上查看当前运行的项目版本
[root@Web1 ~]# ll /root/apache-tomcat-9.0.59/webapps/
总用量 79364
drwxr-x--- 15 root root 4096 3月 10 07:09 docs
drwxr-x--- 7 root root 99 3月 10 07:09 examples
drwxr-x--- 6 root root 79 3月 10 07:09 host-manager
drwxr-x--- 6 root root 114 3月 10 07:09 manager
lrwxrwxrwx 1 root root 64 3月 12 11:33 ROOT -> /root/apache-tomcat-9.0.59/webapps/ROOT-2022-03-11-18-31-24-v3.0 #可以看到现在运行的项目版本是V3.0版本
drwxr-xr-x 5 root root 48 3月 11 09:24 ROOT-2022-03-10-16-22-20-v1.0
drwxr-xr-x 5 root root 48 3月 12 09:20 ROOT-2022-03-11-16-18-40-v2.0
drwxr-xr-x 5 root root 48 3月 12 11:33 ROOT-2022-03-11-18-31-24-v3.0