5 Jenkins 部署与基础配置
IP地址 | 角色 |
---|---|
172.18.8.19 | jenkins-master |
172.18.8.29 | jenkins-node1 |
172.18.8.39 | jenkins-node2 |
[root@jenkins-master ~]# tail -n1 .bashrc
PS1='\[\e[1;32m\][\[\e[0m\]\[\e[1;32m\]\[\e[1;33m\]\u\[\e[34m\]@\h\[\e[1;31m\] \w\[\e[1;32m\]]\[\e[0m\]# '
[root@jenkins-master ~]#
5.1 配置 java 环境并部署 jenkins
5.1.1 java 环境配置
tar zxvf jdk-8u291-linux-x64.tar.gz -C /usr/local/src
ln -sv /usr/local/src/jdk1.8.0_291/ /usr/local/jdk
ln -sv /usr/local/jdk/bin/java /usr/bin/
vim /etc/profile.d/tomcat.sh
export JAVA_HOME=/usr/local/jdk
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
export CLASSPATH=.$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar
[root@jenkins-master ~]# source /etc/profile.d/tomcat.sh
[root@jenkins-master ~]# java -version
java version "1.8.0_291"
Java(TM) SE Runtime Environment (build 1.8.0_291-b10)
Java HotSpot(TM) 64-Bit Server VM (build 25.291-b10, mixed mode)
[root@jenkins-master ~]#
5.1.2 启动 jenkins
https://jenkins.io/zh/
5.1.2.1 通过 jar 包直接启动 jenkins
wget https://mirrors.tuna.tsinghua.edu.cn/jenkins/war-stable/2.289.3/jenkins.war
java \
-Dcom.sun.management.jmxremote \
-Dcom.sun.management.jmxremote.port=12345 \
-Dcom.sun.management.jmxremote.authenticate=false \
-Dcom.sun.management.jmxremote.ssl=false \
-Djava.rmi.server.hostname="172.18.8.216" \
-jar jenkins.war &
# 访问
http://172.18.8.216:8080
5.1.2.2 把 jenkins 放到 tomcat 启动
mkdir /apps
cd /apps
tar zxvf apache-tomcat-8.5.69.tar.gz
mv jenkins.war apache-tomcat-8.5.69/webapps
cd /apps/apache-tomcat-8.5.69
./bin/catalina.sh run
# 访问
http://172.18.8.216:8080/jenkins/
5.1.2.3 deb 包安装 jenkins 配置
# Ubuntu
https://mirrors.tuna.tsinghua.edu.cn/jenkins/debian-stable/
# 下载 jenkins 安装包
wget https://mirrors.tuna.tsinghua.edu.cn/jenkins/debian-stable/jenkins_2.289.3_all.deb
# 安装 jenkins
apt-cache madison daemon
apt install daemon
dpkg -c jenkins_2.289.3_all.deb
dpkg -i jenkins_2.289.3_all.deb
# Ubuntu 配置文件
[root@jenkins-master ~]# grep -Ev "#|^$" /etc/default/jenkins
NAME=jenkins
JAVA_ARGS="-Djava.awt.headless=true"
PIDFILE=/var/run/$NAME/$NAME.pid
JENKINS_USER=root
JENKINS_GROUP=root
JENKINS_WAR=/usr/share/$NAME/$NAME.war
JENKINS_HOME=/var/lib/$NAME
RUN_STANDALONE=true
JENKINS_LOG=/var/log/$NAME/$NAME.log
JENKINS_ENABLE_ACCESS_LOG="no"
MAXOPENFILES=8192
HTTP_PORT=8080
PREFIX=/$NAME
JENKINS_ARGS="--webroot=/var/cache/$NAME/war --httpPort=$HTTP_PORT"
[root@jenkins-master ~]#
[root@jenkins-master ~]# systemctl restart jenkins.service
[root@jenkins-master ~]# systemctl status jenkins.service
● jenkins.service - LSB: Start Jenkins at boot time
Loaded: loaded (/etc/init.d/jenkins; generated)
Active: active (exited) since Sun 2021-08-08 17:06:26 CST; 7s ago
Docs: man:systemd-sysv-generator(8)
Process: 3887 ExecStart=/etc/init.d/jenkins start (code=exited, status=0/SUCCESS)
8月 08 17:06:25 jenkins systemd[1]: jenkins.service: Succeeded.
8月 08 17:06:25 jenkins systemd[1]: Stopped LSB: Start Jenkins at boot time.
8月 08 17:06:25 jenkins systemd[1]: Starting LSB: Start Jenkins at boot time...
8月 08 17:06:25 jenkins jenkins[3887]: Correct java version found
8月 08 17:06:25 jenkins jenkins[3887]: * Starting Jenkins Automation Server jenkins
8月 08 17:06:25 jenkins su[3920]: (to root) root on none
8月 08 17:06:25 jenkins su[3920]: pam_unix(su-l:session): session opened for user root by (uid=0)
8月 08 17:06:25 jenkins su[3920]: pam_unix(su-l:session): session closed for user root
8月 08 17:06:26 jenkins jenkins[3887]: ...done.
8月 08 17:06:26 jenkins systemd[1]: Started LSB: Start Jenkins at boot time.
[root@jenkins ~]#
# 访问
http://172.18.8.19:8080
# 登录密码
[root@jenkins-master ~]# cat /var/lib/jenkins/secrets/initialAdminPassword
5efd0ff49fb144f0a163bd568a47d833
[root@jenkins-master ~]#
5.1.2.4 rpm 包安装 jenkins 配置
# Centos
https://mirrors.tuna.tsinghua.edu.cn/jenkins/redhat-stable/
grep -Ev "#|^$" /etc/sysconfig/jenkins
JENKINS_HOME="/var/lib/jenkins"
JENKINS_JAVA_CMD=""
JENKINS_USER="jenkins"
JENKINS_JAVA_OPTIONS="-Djava.awt.headless=true \
-Dcom.sun.management.jmxremote \
-Dcom.sun.management.jmxremote.port=12345 \
-Dcom.sun.management.jmxremote.authenticate=false \
-Dcom.sun.management.jmxremote.ssl=false \
-Djava.rmi.server.hostname="172.18.8.216" \
"
JENKINS_PORT="8080"
JENKINS_LISTEN_ADDRESS=""
JENKINS_HTTPS_PORT=""
JENKINS_HTTPS_KEYSTORE=""
JENKINS_HTTPS_KEYSTORE_PASSWORD=""
JENKINS_HTTPS_LISTEN_ADDRESS=""
JENKINS_DEBUG_LEVEL="5"
JENKINS_ENABLE_ACCESS_LOG="no"
JENKINS_HANDLER_MAX="100"
JENKINS_HANDLER_IDLE="20"
JENKINS_ARGS=""
# 可选启动参数
JENKINS_JAVA_OPTIONS="--server -Xms1g -Xmx1g -Xss512k -Xmn1g
-XX:CMSInitiatingOccupancyFraction=65
-XX:+UseFastAccessorMethods
-XX:+AggressiveOpts -XX:+UseBiasedLocking
-XX:+DisableExplicitGC -XX:MaxTenuringThreshold=10
-XX:NewSize=2048M -XX:MaxNewSize=2048M -XX:NewRatio=2
-XX:PermSize=128m -XX:MaxPermSize=512m -XX:CMSFullGCsBeforeCompaction=5
-XX:+ExplicitGCInvokesConcurrent -XX:+UseConcMarkSweepGC -XX:+UseParNewGC
-XX:+CMSParallelRemarkEnabled -Djava.awt.headless=true
-Dcom.sun.management.jmxremote
-Dcom.sun.management.jmxremote.port=12345
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.ssl=false
-Djava.rmi.server.hostname="172.18.8.216"
5.1.3 访问 Jenkins 页面:
5.1.4 选择安装 jenkins 插件
解决插件安装慢的解决方式,通过 Nginx 进行 rewrite 或者反向代理,如下:
127.0.0.1 updates.jenkins-ci.org
location /download/plugins {
proxy_set_header Host mirrors.tuna.tsinghua.edu.cn;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
rewrite /download/plugins(.*) /jenkins/plugins/$1 break;
proxy_pass http://mirrors.tuna.tsinghua.edu.cn;
}
如果现实 jenkins 已离线,将以下文件中的更新检查地址改成国内清华大学地址,然后重启 jenkins 即可:
https://mirrors.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json
# cat /var/lib/jenkins/hudson.model.UpdateCenter.xml
<?xml version='1.1' encoding='UTF-8'?>
<sites>
<site>
<id>default</id>
<url>https://updates.jenkins.io/update-center.json</url>
</site>
5.1.5 插件安装过程中
# 插件下载地址
http://updates.jenkins-ci.org/download/plugins/
插件安装过程中。。,如果因为某种原因导致有有安装失败的插件,没有关系,可以后期再单独安装,
5.1.6 创建 jenkins 管理员
5.1.7 配置 jenkins URL
5.1.8 配置完成并登陆 jenkins
5.1.9 jenkins 插件管理及安装
5.1.9.1 插件安装目录
# 插件下载地址
http://updates.jenkins-ci.org/download/plugins/
ll .jenkins/plugins/
5.1.9.2 安装插件
搜索需要 gitlab 的插件并安装 gitlab 和 Blue Ocean
将 jenkins 启动用户(jenkins-master、jenkins-slave1、jenkins-slave2)的公钥放到 gitlab 上,这样 jenkins 就可以登录 gitlab 已非交互方式进行克隆代码了,克隆要用 ssh 协议,而不是用 http 协议
新建一个任务
方法1:在源码管理中选择 git
提示无法连接仓库,此时需要将 jenkins 的私钥添加到 jenkins 的配置中
选择添加的私钥,没有提示无法连接仓库,表示正常
构建后登录 Jenkins Master 查看
[root@jenkins-master /var/lib/jenkins/workspace]# ll
total 24
drwxr-xr-x 6 root root 4096 Aug 12 01:57 ./
drwxr-xr-x 14 jenkins jenkins 4096 Aug 12 01:58 ../
drwxr-xr-x 3 root root 4096 Aug 10 12:35 baichuan_web_app1/
drwxr-xr-x 2 root root 4096 Aug 12 01:43 'baichuan_web_app1@tmp'/
drwxr-xr-x 3 root root 4096 Aug 12 01:57 baichuan_web_app2/
drwxr-xr-x 2 root root 4096 Aug 12 01:57 'baichuan_web_app2@tmp'/
[root@jenkins-master /var/lib/jenkins/workspace]# cat baichuan_web_app1/index.html
<h1>V111111111111111</h1>
<h1>V222222222222222</h1>
<h1>V333333333333333</h1>
<h1>V444444444444444</h1>
<h1>V555555555555555</h1>
<h1>V666666666666666</h1>
[root@jenkins-master /var/lib/jenkins/workspace]# cat baichuan_web_app2/index.html
<h1>v1111111111111-app2</h1>
[root@jenkins-master /var/lib/jenkins/workspace]#
方法2:自定义脚本
[root@jenkins-master ~]# mkdir -p /data/gitdata/baichuan
[root@jenkins-master ~]# cat /data/script/baichuan-app1-deploy.sh
#!/bin/bash
#
cd /data/gitdata/baichuan && rm -rf app1
git clone git@172.18.8.9:baichuan/app1.git
[root@jenkins-master ~]#
构建后登录 Jenkins Master 查看结果
[root@jenkins-master ~]# ls /data/gitdata/baichuan/
app1
[root@jenkins-master ~]# cat /data/gitdata/baichuan/app1/index.html
<h1>V111111111111111</h1>
<h1>V222222222222222</h1>
<h1>V333333333333333</h1>
<h1>V444444444444444</h1>
<h1>V555555555555555</h1>
<h1>V666666666666666</h1>
<h1>V777777777777777</h1>
[root@jenkins-master ~]#
发布项目
首先做免密登录
# 将 Jenkins Master 的公钥拷贝到三台web机器 172.18.8.49 172.18.8.59 172.18.8.69 的 song 目录下
# Jenkins-master 上操作
[root@jenkins-master ~]# ssh-copy-id song@172.18.8.49
[root@jenkins-master ~]# ssh-copy-id song@172.18.8.59
[root@jenkins-master ~]# ssh-copy-id song@172.18.8.69
# 将 Jenkins Master 的公钥拷贝到两台Haproxy机器
[root@jenkins-master ~]# ssh-copy-id root@172.18.8.79
[root@jenkins-master ~]# ssh-copy-id root@172.18.8.89
# 验证免密登录是否可用
[root@jenkins-master ~]# ssh song@172.18.8.49 "whoami;pwd"
song
/home/song
[root@jenkins-master ~]# ssh song@172.18.8.59 "whoami;pwd"
song
/home/song
[root@jenkins-master ~]# ssh song@172.18.8.69 "whoami;pwd"
song
/home/song
[root@jenkins-master ~]#
# 普通用户使用 sudo 的权限
root@web1:~# grep song /etc/sudoers
song ALL=(ALL:ALL) NOPASSWD:ALL
root@web1:~#
root@web2:~# grep song /etc/sudoers
song ALL=(ALL:ALL) NOPASSWD:ALL
root@web2:~#
root@web3:~# grep song /etc/sudoers
song ALL=(ALL:ALL) NOPASSWD:ALL
root@web3:~#
编写脚本
[root@jenkins-master ~]# cat /data/script/baichuan-app1-deploy.sh
#!/bin/bash
#
ipList="172.18.8.49 172.18.8.59 172.18.8.69"
fileName="app1.tar.gz"
remoteDir="/data/tomcat/tomcat_appdir/"
remoteApp="/data/tomcat/tomcat_webapps/myapp"
cd /data/gitdata/baichuan && rm -rf app1
git clone git@172.18.8.9:baichuan/app1.git
cd /data/gitdata/baichuan/app1
tar czvf $fileName ./index.html
for ip in $ipList
do
rsync -aP $fileName song@$ip:$remoteDir
if [ $? -ne 0 ];then
exit 1
fi
ssh song@$ip "sudo systemctl stop tomcat"
ssh song@$ip "rm -rf $remoteApp && mkdir -p $remoteApp && cd $remoteDir && tar zxvf $fileName -C $remoteApp"
ssh song@$ip "sudo systemctl start tomcat"
done
[root@jenkins-master ~]#
构建结果
分布式构建
Jenkins-node1
# apt -y install openjdk-8-jdk 或者编译安装
tar zxvf jdk-8u291-linux-x64.tar.gz -C /usr/local/src
ln -sv /usr/local/src/jdk1.8.0_291/ /usr/local/jdk
ln -sv /usr/local/jdk/bin/java /usr/bin/
vim /etc/profile.d/tomcat.sh
export JAVA_HOME=/usr/local/jdk
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
export CLASSPATH=.$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar
[root@jenkins-slave1 ~]# source /etc/profile.d/tomcat.sh
[root@jenkins-slave1 ~]# java -version
java version "1.8.0_291"
Java(TM) SE Runtime Environment (build 1.8.0_291-b10)
Java HotSpot(TM) 64-Bit Server VM (build 25.291-b10, mixed mode)
[root@jenkins-slave1 ~]# mkdir -p /var/lib/jenkins/
[root@jenkins-slave1 ~]# ssh-keygen
[root@jenkins-slave1 ~]# ssh-copy-id song@172.18.8.49
[root@jenkins-slave1 ~]# ssh-copy-id song@172.18.8.59
[root@jenkins-slave1 ~]# ssh-copy-id song@172.18.8.69
# 将 Jenkins Slave1 的公钥拷贝到两台Haproxy机器
[root@jenkins-slave1 ~]# ssh-copy-id root@172.18.8.79
[root@jenkins-slave1 ~]# ssh-copy-id root@172.18.8.89
Jenkins-node2
tar zxvf jdk-8u291-linux-x64.tar.gz -C /usr/local/src
ln -sv /usr/local/src/jdk1.8.0_291/ /usr/local/jdk
ln -sv /usr/local/jdk/bin/java /usr/bin/
vim /etc/profile.d/tomcat.sh
export JAVA_HOME=/usr/local/jdk
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
export CLASSPATH=.$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar
[root@jenkins-slave2 ~]# source /etc/profile.d/tomcat.sh
[root@jenkins-slave2 ~]# java -version
java version "1.8.0_291"
Java(TM) SE Runtime Environment (build 1.8.0_291-b10)
Java HotSpot(TM) 64-Bit Server VM (build 25.291-b10, mixed mode)
[root@jenkins-slave2 ~]# mkdir -p /var/lib/jenkins/
[root@jenkins-slave2 ~]# mkdir -p /var/lib/jenkins/
[root@jenkins-slave2 ~]# ssh-keygen
[root@jenkins-slave2 ~]# ssh-copy-id song@172.18.8.49
[root@jenkins-slave2 ~]# ssh-copy-id song@172.18.8.59
[root@jenkins-slave2 ~]# ssh-copy-id song@172.18.8.69
# 将 Jenkins Slave2 的公钥拷贝到两台Haproxy机器
[root@jenkins-slave2 ~]# ssh-copy-id root@172.18.8.79
[root@jenkins-slave2 ~]# ssh-copy-id root@172.18.8.89
Dashboard--->Manage Jenkins--->Manage Nodes and Clouds
查看 Jenkins slave1 日志
java version "1.8.0_291"
Java(TM) SE Runtime Environment (build 1.8.0_291-b10)
Java HotSpot(TM) 64-Bit Server VM (build 25.291-b10, mixed mode)
[08/12/21 07:27:12] [SSH] Checking java version of /var/lib/jenkins/jdk/bin/java
Couldn't figure out the Java version of /var/lib/jenkins/jdk/bin/java
bash: /var/lib/jenkins/jdk/bin/java: No such file or directory
[08/12/21 07:27:12] [SSH] Checking java version of java
[08/12/21 07:27:12] [SSH] java -version returned 1.8.0_291.
[08/12/21 07:27:12] [SSH] Starting sftp client.
[08/12/21 07:27:13] [SSH] Copying latest remoting.jar...
[08/12/21 07:27:13] [SSH] Copied 1,502,119 bytes.
Expanded the channel window size to 4MB
# 从 jenkins master 向 jenkins slave 拷贝 jar 包
[08/12/21 07:27:13] [SSH] Starting agent process: cd "/var/lib/jenkins" && java -jar remoting.jar -workDir /var/lib/jenkins -jar-cache /var/lib/jenkins/remoting/jarCache
Aug 12, 2021 7:27:13 AM org.jenkinsci.remoting.engine.WorkDirManager initializeWorkDir
INFO: Using /var/lib/jenkins/remoting as a remoting work directory
Aug 12, 2021 7:27:13 AM org.jenkinsci.remoting.engine.WorkDirManager setupLogging
INFO: Both error and output logs will be printed to /var/lib/jenkins/remoting
<===[JENKINS REMOTING CAPACITY]===>channel started
Remoting version: 4.7
This is a Unix agent
Evacuated stdout
Agent successfully connected and online
pipline
生成 token
脚本式
node("jenkins-slave1") {
stage("clone 代码"){
//sh 'rm -rf /var/lib/jenkins/workspace/baichuan_web_pipline_app1_The_script_style/*'
echo 'clone 代码'
deleteDir()
git branch: 'main', credentialsId: '57e7aa52-d7d4-46f1-bd2f-687f5b94323f', url: 'git@172.18.8.9:baichuan/app1.git'
echo 'clone 代码完成'
}
stage("代码构建"){
sh 'cd /var/lib/jenkins/workspace/baichuan_web_pipline_app1_The_script_style/ && tar czvf app1.tar.gz ./index.html'
}
stage("代码复制"){
sh '''localDir="/var/lib/jenkins/workspace/baichuan_web_pipline_app1_The_script_style/"
remoteDir="/data/tomcat/tomcat_appdir/"
IPLIST="172.18.8.49 172.18.8.59 172.18.8.69"
for ip in $IPLIST
do
cd $localDir && scp app1.tar.gz song@$ip:$remoteDir
done'''
}
stage("停止 tomcat 服务"){
sh '''IPLIST="172.18.8.49 172.18.8.59 172.18.8.69"
for ip in $IPLIST
do
ssh song@$ip "sudo systemctl stop tomcat"
done'''
}
stage("代码部署"){
sh '''remoteAppDir="/data/tomcat/tomcat_webdir/myapp"
remoteDir="/data/tomcat/tomcat_appdir/"
IPLIST="172.18.8.49 172.18.8.59 172.18.8.69"
for ip in $IPLIST
do
ssh song@$ip "rm -rf $remoteAppDir && mkdir -p $remoteAppDir && cd $remoteDir && tar xvf app1.tar.gz -C $remoteAppDir"
done'''
}
stage("启动 tomcat 服务"){
sh '''IPLIST="172.18.8.49 172.18.8.59 172.18.8.69"
for ip in $IPLIST
do
ssh song@$ip "sudo systemctl start tomcat"
done'''
}
}
更新到 V11 版本测试
声明式
https://www.jenkins.io/zh/doc/book/pipeline/
pipeline {
//agent any
agent { label 'jenkins-slave1' }
stages {
stage('clone 代码') {
steps {
echo 'clone 代码'
deleteDir()
git branch: 'main', credentialsId: '57e7aa52-d7d4-46f1-bd2f-687f5b94323f', url: 'git@172.18.8.9:baichuan/app1.git'
echo 'clone 代码完成'
}
}
stage('代码构建'){
steps {
sh 'cd /var/lib/jenkins/workspace/baichuan_web_pipline_app1_The_statement_style/ && tar czvf app1.tar.gz ./index.html'
}
}
stage('代码复制') {
steps {
sh '''localDir="/var/lib/jenkins/workspace/baichuan_web_pipline_app1_The_statement_style/"
remoteDir="/data/tomcat/tomcat_appdir/"
IPLIST="172.18.8.49 172.18.8.59 172.18.8.69"
for ip in $IPLIST
do
cd $localDir && scp app1.tar.gz song@$ip:$remoteDir
done'''
}
}
stage("停止 tomcat 服务"){
steps {
sh '''IPLIST="172.18.8.49 172.18.8.59 172.18.8.69"
for ip in $IPLIST
do
ssh song@$ip "sudo systemctl stop tomcat"
done'''
}
}
stage("代码部署") {
steps {
sh '''remoteAppDir="/data/tomcat/tomcat_webdir/myapp"
remoteDir="/data/tomcat/tomcat_appdir/"
IPLIST="172.18.8.49 172.18.8.59 172.18.8.69"
for ip in $IPLIST
do
ssh song@$ip "rm -rf $remoteAppDir && mkdir -p $remoteAppDir && cd $remoteDir && tar xvf app1.tar.gz -C $remoteAppDir"
done'''
}
}
stage("启动 tomcat 服务") {
agent { label 'jenkins-slave2' }
steps {
sh '''IPLIST="172.18.8.49 172.18.8.59 172.18.8.69"
for ip in $IPLIST
do
ssh song@$ip "sudo systemctl start tomcat"
done'''
}
}
}
}