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 ~]#
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 ~]#
https://jenkins.io/zh/
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
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/
# 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 ~]#
# 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"
解决插件安装慢的解决方式,通过 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
default
https://updates.jenkins.io/update-center.json
# 插件下载地址
http://updates.jenkins-ci.org/download/plugins/
插件安装过程中。。,如果因为某种原因导致有有安装失败的插件,没有关系,可以后期再单独安装,
# 插件下载地址
http://updates.jenkins-ci.org/download/plugins/
ll .jenkins/plugins/
搜索需要 gitlab 的插件并安装 gitlab 和 Blue Ocean
新建一个任务
提示无法连接仓库,此时需要将 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
V111111111111111
V222222222222222
V333333333333333
V444444444444444
V555555555555555
V666666666666666
[root@jenkins-master /var/lib/jenkins/workspace]# cat baichuan_web_app2/index.html
v1111111111111-app2
[root@jenkins-master /var/lib/jenkins/workspace]#
[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 [email protected]: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
V111111111111111
V222222222222222
V333333333333333
V444444444444444
V555555555555555
V666666666666666
V777777777777777
[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 [email protected]
[root@jenkins-master ~]# ssh-copy-id [email protected]
[root@jenkins-master ~]# ssh-copy-id [email protected]
# 将 Jenkins Master 的公钥拷贝到两台Haproxy机器
[root@jenkins-master ~]# ssh-copy-id [email protected]
[root@jenkins-master ~]# ssh-copy-id [email protected]
# 验证免密登录是否可用
[root@jenkins-master ~]# ssh [email protected] "whoami;pwd"
song
/home/song
[root@jenkins-master ~]# ssh [email protected] "whoami;pwd"
song
/home/song
[root@jenkins-master ~]# ssh [email protected] "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 [email protected]: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 [email protected]
[root@jenkins-slave1 ~]# ssh-copy-id [email protected]
[root@jenkins-slave1 ~]# ssh-copy-id [email protected]
# 将 Jenkins Slave1 的公钥拷贝到两台Haproxy机器
[root@jenkins-slave1 ~]# ssh-copy-id [email protected]
[root@jenkins-slave1 ~]# ssh-copy-id [email protected]
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 [email protected]
[root@jenkins-slave2 ~]# ssh-copy-id [email protected]
[root@jenkins-slave2 ~]# ssh-copy-id [email protected]
# 将 Jenkins Slave2 的公钥拷贝到两台Haproxy机器
[root@jenkins-slave2 ~]# ssh-copy-id [email protected]
[root@jenkins-slave2 ~]# ssh-copy-id [email protected]
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
生成 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: '[email protected]: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: '[email protected]: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'''
}
}
}
}