1、在 10.4.7.14 和 10.4.7.15 上安装tomcat
[root@localhost ~]# wget 'https://apache.osuosl.org/tomcat/tomcat-9/v9.0.50/bin/apache-tomcat-9.0.50.tar.gz'
# 创建存放tomcat的目录
[root@localhost ~]# mkdir /root/soft
# 解压tomcat
[root@localhost ~]# tar zxvf apache-tomcat-9.0.50.tar.gz -C soft/
# 给tomcat创建软连接,便于管理
[root@localhost ~]# cd soft/
[root@localhost soft]# ln -s ~/soft/apache-tomcat-9.0.50 ~/soft/tomcat
# 修改tomcat端口,防止端口占用
[root@localhost soft]# cd tomcat
[root@localhost tomcat]# vim conf/server.xml
……省略部分
<Connector port="8881" protocol="HTTP/1.1" # 将这里的8080改掉,我这里就用8081
# 启动tomcat
[root@localhost tomcat]# sh bin/startup.sh
2、在 10.4.7.14 上配置负载均衡
[root@deploy tomcat]# cat /etc/nginx/conf.d/tomcat.prod.com.conf
upstream backend-tomcat-web {
server 10.4.7.14:8881;
server 10.4.7.15:8881;
}
server {
listen 80;
server_name tomcat.prod.com;
client_max_body_size 1000m;
location / {
proxy_pass http://backend-tomcat-web;
proxy_set_header Host $http_host;
proxy_set_header X-Real_IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
3、在 10.4.7.14 上配置dns解析
[root@deploy tomcat]# cat /var/named/prod.com.zone
$ORIGIN prod.com.
$TTL 600 ; 10 minutes
@ IN SOA dns.prod.com. dnsadmin.host.com. (
2019111001 ; serial
10800 ; refresh (3 hours)
900 ; retry (15 minutes)
604800 ; expire (1 week)
86400 ; minimum (1 day)
)
NS dns.prod.com.
$TTL 60 ; 1 minute
dns A 10.4.7.14
nginx A 10.4.7.14
gitlab A 10.4.7.15
jenkins A 10.4.7.14
tomcat A 10.4.7.14
[root@deploy src]# git remote remove origin
# 初始项目仓库
[root@deploy src]# git clone git@gitlab.prod.com:devops/tomcat-web.git
[root@deploy tomcat-web]# touch README.md
[root@deploy tomcat-web]# git add README.md
[root@deploy tomcat-web]# git commit -m "add README"
[master (root-commit) eb72404] add README
[root@deploy tomcat-web]# git push -u origin master
# 提交代码
[root@deploy src]# git add .
[root@deploy src]# git commit -m 'java'
[root@deploy tomcat-web]# git push -u origin master
java代码获取方式:git clone https://gitee.com/alivv/helloworld.git
第一种:直接使用yum安装
[root@deploy ~]# yum -y install maven
第二种: 使用二进制包安装
# 下载二进制包
[root@deploy ~]# wget 'https://archive.apache.org/dist/maven/maven-3/3.6.1/binaries/apache-maven-3.6.1-bin.tar.gz'
[root@deploy ~]# mkdir /data/nfs-volume/jenkins_home/maven-3.6.1-8u232
[root@deploy ~]# tar -zxf apache-maven-3.6.1-bin.tar.gz
[root@deploy ~]# mv apache-maven-3.6.1-bin /usr/local/
[root@deploy ~]# ln -s /usr/lcoal/apache-maven-3.6.1-bin /usr/local/maven
# 设置环境变量
[root@deploy ~]# export PATH=/usr/local/apache-maven-3.6.1-bin/bin:$PATH
# 查看maven版本
root@deploy ~]# mvn -v
# 在配置文件中添加下面内容,在159行上面,注意:不要加在注释内容中
[root@deploy ~]# vim /usr/share/maven/conf/settings.xml
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>*</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
[root@deploy ~]# git clone git@gitlab.prod.com:devops/tomcat-web.git
# 进入到项目仓库目录
[root@deploy ~]# cd tomcat-web/
# 使用mvn命令进行编译
[root@deploy tomcat-web]# mvn package -Dmaven.test.skip=true
# 删除tomcat原本的ROOT目录
[root@deploy helloworld]# for i in {14..15};do ssh 10.4.7.$i "rm -rf /root/soft/tomcat/webapps/ROOT";done
# 上传生成的war到tomcat节点,上传成ROOT.war后会自动解压到tomcat的webapp下的ROOT目录中
[root@deploy helloworld]# for i in {14..15};do scp /root/helloworld/target/helloworld.war 10.4.7.$i:/root/soft/tomcat/webapps/ROOT.war;done
# 重启tomcat
[root@deploy helloworld]# for i in {14..15};do ssh 10.4.7.$i "sh /root/soft/tomcat/bin/shutdown.sh && sh /root/soft/tomcat/bin/startup.sh";done
创建一个maven项目流水线
填写描述信息
填写丢弃策略
选择ssh的连接方式,创建 jenkins 的登入认证,并选择创建的用户
Build选项下,点击 “the tool configuration”,配置maven工具的路径信息(这时候,我们可以先将项目流水线保存,去配置maven,让jenkins知道maven位置)
到新弹出的 maven 路径配置页面进行配置
# 示例
[root@deploy ~]# mvn -version
Apache Maven 3.0.5 (Red Hat 3.0.5-17)
Maven home: /usr/share/maven
Java version: 1.8.0_292, vendor: Red Hat, Inc.
Java home: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.292.b10-1.el7_9.x86_64/jre # 这里将“/jre”去掉,就是java home地址
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-957.el7.x86_64", arch: "amd64", family: "unix"
添加参数化构建参数
[root@deploy scripts]# vim /root/scripts/tomcat-deploy.sh
#!/bin/bash
DATE=$(date +%Y-%m-%d-%H-%M-%S)
webserver="10.4.7.14 10.4.7.15"
Sdir=/opt
Ddir=/root/soft/tomcat/webapps
Postfix=${DATE}-${git_version}
# 到项目目录,将拉取下来的项目文件进行打包(打包的目的是为了方便传输给其他worker节点)
get_code(){
cd ${WORKSPACE}
}
# 将内容scp到各个节点
scp_webserver(){
for host in ${webserver};do
scp target/*.war root@${host}:${Sdir}/ROOT-${Postfix}.war
ssh ${host} "mkdir -p ${Ddir}/ROOT-${Postfix} &&\
unzip ${Sdir}/ROOT-${Postfix}.war -d ${Ddir}/ROOT-${Postfix} &&\
rm -rf ${Ddir}/ROOT &&\
ln -s ${Ddir}/ROOT-${Postfix} ${Ddir}/ROOT &\
sh /root/soft/tomcat/bin/shutdown.sh && sh /root/soft/tomcat/bin/startup.sh"
done
}
rollback(){
rollback_dir=$(find /root/soft/tomcat/webapps -maxdepth 1 -type d -name "*-${git_version}")
for host in ${webserver};do
ssh ${host} "
rm -rf ${Ddir}/ROOT &&\
ln -s ${rollback_dir} ${Ddir}/ROOT &\
sh /root/soft/tomcat/bin/shutdown.sh && sh /root/soft/tomcat/bin/startup.sh"
done
}
deploy(){
get_code
scp_webserver
}
if [[ ${action} == deploy ]];then
if [[ ${GIT_COMMIT} == ${GIT_PREVIOUS_SUCCESSFUL_COMMIT} ]];then
echo "这个版本已经部署过,若要使用旧版本,请选择rollback版本回退"
exit 1
else
deploy
fi
elif [[ ${action} == rollback ]];then
rollback
fi
# 修改 src/main/webapp/index.jsp 文件中内容,模拟v1.1版本代码
<h1><font color="blue">Hello World V1.1</font></h1>
# 提交上传代码
[root@deploy tomcat-web]# git add -A .
[root@deploy tomcat-web]# git commit -m 'new v1.1'
[root@deploy tomcat-web]# git tag -a 'v1.1' -m 'new'
[root@deploy tomcat-web]# git push -u origin master v1.1
[[ ${GIT_COMMIT} == ${GIT_PREVIOUS_SUCCESSFUL_COMMIT} ]]
判断语句解决这个问题if [[ ${action} == deploy ]];then
if [[ ${GIT_COMMIT} == ${GIT_PREVIOUS_SUCCESSFUL_COMMIT} ]];then
echo "这个版本已经部署过,若要使用旧版本,请选择rollback版本回退"
exit 1
else
deploy
fi
[root@deploy ~]# cd tomcat-web/
[root@deploy webapp]# vim src/main/webapp/index.jsp
[root@deploy tomcat-web]# git add .
[root@deploy tomcat-web]# git commit -m 'v1.2'
[root@deploy tomcat-web]# git tag -a 'v1.2' -m 'new'
[root@deploy tomcat-web]# git push -u origin v1.2
再次访问tomcat.prod.com,v1.2版本部署成功
[root@sonar ~]# yum -y install java wget unzip git
# 下载二进制包
[root@sonar ~]# wget https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql-5.6-community-el7-x86_64/mysql-community-client-5.6.45-2.el7.x86_64.rpm
[root@sonar ~]# wget https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql-5.6-community-el7-x86_64/mysql-community-common-5.6.45-2.el7.x86_64.rpm
[root@sonar ~]# wget https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql-5.6-community-el7-x86_64/mysql-community-libs-5.6.45-2.el7.x86_64.rpm
[root@sonar ~]# wget https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql-5.6-community-el7-x86_64/mysql-community-server-5.6.45-2.el7.x86_64.rpm
# 安装本地rpm包
[root@sonar ~]# yum -y localinstall mysql-community-*.rpm
# 查看rpm包
[root@sonar ~]# rpm -qa |grep mysql
mysql-community-server-5.6.45-2.el7.x86_64
mysql-community-common-5.6.45-2.el7.x86_64
mysql-community-libs-5.6.45-2.el7.x86_64
mysql-community-client-5.6.45-2.el7.x86_64
[root@sonar ~]# systemctl start mysqld
[root@sonar ~]# mysqladmin password abc123
# 创建sonar库
[root@sonar ~]# mysql -uroot -pabc123 -e "CREATE DATABASE sonar DEFAULT CHARACTER SET utf8;"
# 查看是否创建成功
[root@sonar ~]# mysql -uroot -pabc123 -e 'show databases;'
Warning: Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sonar |
| test |
+--------------------+
[root@sonar ~]# wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-7.0.zip
[root@sonar ~]# unzip sonarqube-7.0.zip -d /usr/local/
[root@sonar ~]# ln -s /usr/local/sonarqube-7.0 /usr/local/sonarqube
[root@sonar ~]# useradd sonar
[root@sonar ~]# chown -R sonar.sonar /usr/local/sonarqube-7.0
[root@sonar ~]# chown -R sonar.sonar /usr/local/sonarqube
[root@sonar ~]# vim /usr/local/sonarqube/conf/sonar.properties
……省略部分
# 取消掉下面几条参数的注释,并进行修改
sonar.jdbc.username=root # 配置上数据库的用户名和密码
sonar.jdbc.password=abc123
# 下面这个是连接的数据库信息,由于数据库在本地,所以不用改localhost:3306,而且默认是sonar普通系统用户启动
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false
# 使用sonar用户启动SonarQube
[root@sonar ~]# su - sonar -c "/usr/local/sonarqube/bin/linux-x86-64/sonar.sh start"
# 查看9000端口
[root@sonar ~]# netstat -anput |grep 9000
tcp6 0 0 :::9000 :::* LISTEN 20800/java
[root@deploy scripts]# vim /var/named/prod.com.zone
$ORIGIN prod.com.
$TTL 600 ; 10 minutes
@ IN SOA dns.prod.com. dnsadmin.host.com. (
2019111001 ; serial
10800 ; refresh (3 hours)
900 ; retry (15 minutes)
604800 ; expire (1 week)
86400 ; minimum (1 day)
)
NS dns.prod.com.
$TTL 60 ; 1 minute
harbor A 10.4.7.14
dns A 10.4.7.14
nginx A 10.4.7.14
gitlab A 10.4.7.15
jenkins A 10.4.7.14
tomcat A 10.4.7.14
sonar A 10.4.7.14
[root@deploy scripts]# systemctl restart named
server {
listen 80;
server_name sonar.prod.com;
client_max_body_size 1000m;
location / {
proxy_pass http://10.4.7.12:9000;
proxy_set_header Host $http_host;
proxy_set_header X-Real_IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
[root@deploy scripts]# systemctl reload named
mvn sonar:sonar \
-Dsonar.host.url=http://sonar.prod.com \
-Dsonar.login=aee542aa1e9e15578ffbc531bf8a2abe444530a4
# php代码检查的命令
sonar-scanner \
-Dsonar.projectKey=PHP \
-Dsonar.sources=. \
-Dsonar.host.url=http://sonar.prod.com \
-Dsonar.login=aee542aa1e9e15578ffbc531bf8a2abe444530a4
# html代码检查的命令
sonar-scanner \
-Dsonar.projectKey=html \
-Dsonar.sources=. \
-Dsonar.host.url=http://sonar.prod.com \
-Dsonar.login=aee542aa1e9e15578ffbc531bf8a2abe444530a4
如上面PHP的需要用到其他的sonar-scanner工具,可以在这下载:https://docs.sonarqube.org/latest/analysis/scan/sonarscanner/
1.安装汉化插件
方法一:直接在SonarQube页面上下载(这个下载的是最新版的,可能不匹配)
方法二:去官网下载,放到插件目录下,然后重启SonarQube
# 到插件目录下
[root@sonar ~]# cd /usr/local/sonarqube/extensions/plugins/
# 下载SonarQube-7.0的汉化插件
[root@sonar plugins]# wget 'https://github.com/xuhuisheng/sonar-l10n-zh/releases/download/sonar-l10n-zh-plugin-1.20/sonar-l10n-zh-plugin-1.20.jar'
# 重启SonarQube
[root@sonar linux-x86-64]# su - sonar -c "sh /usr/local/sonarqube/bin/linux-x86-64/sonar.sh restart"
2.安装代码质量扫描插件(之前都打包好了)
# 对原插件目录进行备份
[root@sonar ~]# mv /usr/local/sonarqube/extensions/plugins /usr/local/sonarqube/extensions/plugins-bak
# 将打包的本地插件解压到插件目录中
[root@sonar ~]# tar xf sonar_plugins.tar.gz -C /usr/local/sonarqube/extensions/plugins
# 改变目录文件属主和属组
[root@sonar ~]# chown sonar.sonar /usr/local/sonarqube/extensions/plugins
# 重启SonarQube
[root@sonar ~]# su - sonar -c "sh /usr/local/sonarqube/bin/linux-x86-64/sonar.sh restart"
FAQ常见问题
Q:
sh sonar.sh console
查看日志报错:WrapperSimpleApp: Encountered an error running main: java.nio.file.AccessDeniedException: /usr/local/sonarqube-7.0/temp/conf/es/elasticsearch.ymlA:
# 下载sonar-scanner(在jenkins机器上执行)
[root@deploy ~]# wget 'https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-4.0.0.1744-linux.zip4'
# 解压客户端工具
[root@deploy ~]# unzip sonar-scanner-cli-4.0.0.1744-linux.zip -d /usr/local/
[root@deploy ~]# ls /usr/local/
bin etc games include lib lib64 libexec sbin share sonar-scanner-4.0.0.1744-linux src
# 做软连接
[root@deploy ~]# ln -s /usr/local/sonar-scanner-4.0.0.1744-linux /usr/local/sonar-scanner
[root@deploy ~]# vim /usr/local/sonar-scanner/conf/sonar-scanner.properties
#----- Default SonarQube server
sonar.host.url=http://10.4.7.12:9000 # 配置SonarQube的地址
sonar.login=aee542aa1e9e15578ffbc531bf8a2abe444530a4 # 新增这条登入配置,等于号(=)后面是之间获取的token
#----- Default source code encoding
sonar.sourceEncoding=UTF-8 # 默认字符集是utf-8
# 由于SonarQube机器地址和token都在配置文件中写了,这里执行命令就可以省略
[root@deploy tomcat-test]# /usr/local/sonar-scanner/bin/sonar-scanner \
-Dsonar.projectKey=html \
-Dsonar.sources=.
执行效果如图
执行结束后,会将结果推送到SonarQube上,登入SonarQube,可以查看到执行结果
# 使用maven命令即可
[root@deploy tomcat-test]# mvn sonar:sonar \
-Dsonar.host.url=http://sonar.prod.com \
-Dsonar.login=aee542aa1e9e15578ffbc531bf8a2abe444530a4
效果如图
再次登入到sonar.prod,.com页面上查看,多了hello world的java项目
[root@deploy tomcat-test]# /usr/local/sonar-scanner/bin/sonar-scanner \
-Dsonar.projectKey=java \
-Dsonar.sources=.
示例参数:
sonar.projectName=${JOB_NAME}
# ${JOB_NAME}这个变量是插件内置变量,接收jenkins项目名称,会在SonarQube上显示的 jenkins 项目名称
sonar.projectKey=html
sonar.sources=.
# 扫描哪个项目的源码
# 进入到项目目录
[root@deploy ~]# cd web-test/
# 修改代码,提交v1.3版本
[root@deploy web-test]# vim index.html
[root@deploy web-test]# git add .
[root@deploy web-test]# git commit -m 'v1.3'
[root@deploy web-test]# git tag -a 'v1.3' -m 'sonar test'
[root@deploy web-test]# git push -u origin v1.3
示例参数:
sonar.projectName=${JOB_NAME}
sonar.projectKey=java
sonar.sources=.
sonar.java.binaries=target/sonar
# 进入到项目目录
[root@deploy ~]# cd web-test/
# 修改代码,提交v1.3版本
[root@deploy web-test]# vim index.html
[root@deploy tomcat-web]# git add .
[root@deploy tomcat-web]# git commit -m 'v1.4'
[root@deploy tomcat-web]# git tag -a 'v1.4' -m 'sonar java test'
[root@deploy tomcat-web]# git push -u origin v1.4
参考地址:https://www.cnblogs.com/lqh950422/p/13640027.html