tomcat上部署jenkins | 主機名稱 | IP地址 | 需要的應用服務 | 工具包 | | | | | | | node1 | 192.168.111.141 | tomcat、jenkins | git、maven | | node2 | 192.168.111.142 | tomcat ...
tomcat上部署jenkins
目錄主機名稱 | IP地址 | 需要的應用服務 | 工具包 |
---|---|---|---|
node1 | 192.168.111.141 | tomcat、jenkins | git、maven |
node2 | 192.168.111.142 | tomcat | 無 |
部署tomcat
//安裝jdk
[root@node1 ~]# yum -y install java-17-openjdk
//下載9.0版本,10的版本部署有bug,無法實現
[root@node1 ~]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-9/v9.0.67/bin/apache-tomcat-9.0.67.tar.gz
//解壓tomcat,並部署安裝
[root@node1 ~]# tar -xzf apache-tomcat-9.0.67.tar.gz -C /usr/local/
[root@node1 ~]# cd /usr/local/
[root@node1 local]# ln -s apache-tomcat-9.0.67/ tomcat
[root@node1 local]# ls
apache-tomcat-9.0.67 bin etc games include lib lib64 libexec sbin share src tomcat
//啟動tomcat,測試能否訪問tomcat首頁
[root@node1 ~]# /usr/local/tomcat/bin/startup.sh
[root@node1 ~]# ss -anlt | grep 8080
LISTEN 0 100 *:8080 *:*
訪問首頁
部署jenkins
//下載jenkins到/usr/local/tomcat/webapps/目錄下
[root@node1 ~]# cd /usr/local/tomcat/webapps/
//下載下來後會自動解壓
[root@node1 webapps]# wget http://mirrors.jenkins.io/war-stable/2.346.3/jenkins.war
[root@node1 webapps]# ls
ROOT docs examples host-manager jenkins jenkins.war manager
//啟動服務
[root@node1 webapps]# cd ..
[root@node1 tomcat]# bin/catalina.sh stop
[root@node1 tomcat]# bin/catalina.sh start
瀏覽器訪問jenkins
//查看/root/.jenkins/secrets/initialAdminPassword
[root@node1 tomcat]# cat /root/.jenkins/secrets/initialAdminPassword
1af83134887b469fa049de1c9cbb84a7
將查看到的密碼複製
Jenkins創建流水線任務
在第一臺上部署服務tomcat和jenkins(上面已經部署),登錄web界面時選擇安裝推薦的插件,部署好後,設置國內源
等待下載
更改國內源
//先在node1主機上生成一個密鑰,傳給node2主機
[root@node1 ~]# ssh-keygen
[root@node1 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
在node2上部署tomcat
[root@node2 ~]# yum -y install java-11-openjdk
[root@node2 ~]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-9/v9.0.67/bin/apache-tomcat-9.0.67.tar.gz
[root@node2 ~]# tar -xzf apache-tomcat-9.0.67.tar.gz -C /usr/local/
[root@node2 ~]# cd /usr/local/
[root@node2 local]# ln -s apache-tomcat-9.0.67/ tomcat
[root@node2 local]# ls
apache-tomcat-9.0.67 bin etc games include lib lib64 libexec sbin share src tomcat
在node1中配置jenkins
創建流水線任務
首選都需要在jinkins所在伺服器,也就是node1中安裝git和maven
[root@node1 ~]# yum -y install git maven
添加git下載信息 http://github.com/lizhenliang/tomcat-java-demo.git
添加文本參數客戶端IP
添加字元參數定義tomcat路徑,ssh密碼
添加字元參數,打包
添加字元參數,定義tomcat路徑
寫流水腳本
pipeline {
agent any
stages {
stage('pull code') {
steps {
git '${git_url}'
}
}
stage('package') {
steps {
sh """
mvn clean
mvn package
"""
}
}
stage('deploy') {
steps {
sh """
sshpass -p "${ssh_pass}" ssh root@${client_ip} 'yum -y install java-11-openjdk > /dev/null'
sshpass -p "${ssh_pass}" ssh root@${client_ip} 'wget https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.67/bin/apache-tomcat-9.0.67.tar.gz -P /tmp'
sshpass -p "${ssh_pass}" ssh root@${client_ip} 'tar xf /tmp/apache-tomcat-9.0.67.tar.gz -C /usr/local'
sshpass -p "${ssh_pass}" ssh root@${client_ip} 'ln -s /usr/local/apache-tomcat-9.0.67 ${tomcat_path}'
sshpass -p "${ssh_pass}" ssh root@${client_ip} '${tomcat_path}/bin/catalina.sh start'
sshpass -p "${ssh_pass}" scp target/${maven_package}.war root@${client_ip}:${tomcat_path}/webapps/
sshpass -p "${ssh_pass}" ssh root@${client_ip} '${tomcat_path}/bin/catalina.sh stop && ${tomcat_path}/bin/catalina.sh start'
"""
}
}
}
}