jenkins
https://jenkins.io/zh/download/
https://pkg.jenkins.io/redhat-stable/
我们下载软件包看小版本号,数字越大,说明越稳定
插件下载
https://plugins.jenkins.io/
http://updates.jenkins-ci.org/download/plugins/
history 命令显示执行时间和执行者
export HISTTIMEFORMAT="%Y-%m-%d:%H-%M-%S:whoami
: "
准备环境
两台TOMCAT
tar xf jdk-8u221-linux-x64.tar.gz
ln -sv /apps/jdk1.8.0_221 /apps/jdk
tar xf apache-tomcat-8.5.43.tar.gz
ln -sv /apps/apache-tomcat-8.5.43 /apps/tomcat
ps -ef |grep tomcat|grep -v grep |awk '{print $2}'
因为是做实验,我们关掉一些功能
vim /apps/tomcat/conf/server.xml
useradd www -u 2000
mkdir -p /data/tomcat/webapp
chown www.www /apps/tomcat/ -R
chown www.wwww /data/tomcat/webapp -R
[root@tomcat1 ~]# cat /data/tomcat/webapps/testapp/index.jsp
tomcat1 192.168.10.230
[root@tomcat2 ~]# cat /data/tomcat/webapps/testapp/index.jsp
tomcat2 192.168.10.231
tomcat启动脚本
#!/bin/bash
##
###
####
JDK_HOME=/apps/jdk
CATALINA_HOME=/apps/tomcat
export JDK_HOME CATALINA_HOME
source /etc/profile
#PID=`ps -ef | grep -v grep | grep java | awk '{print $2}'`
#NUM=`ps -ef | grep -v grep | grep java | awk '{print $2}' | wc -l`
#case $1 in
start(){
echo "正在判断服务状态,请稍等!"
echo "请稍等3秒钟"
echo "3";sleep 1;echo "2";sleep 1; echo "1";sleep 1
if netstat -an | grep 8080 | grep LISTEN > /dev/null;then
echo "tomcat 已经正在运行了"
else
echo “tomcat没有运行,1秒后启动”
echo 1;sleep 1
$CATALINA_HOME/bin/catalina.sh start
echo “tomcat 已经成功启动完成,5秒后判断是否启动成功”
echo "5";sleep 1;echo "4";sleep 1
echo "3";sleep 1;echo "2";sleep 1; echo "1";sleep 1
if netstat -an | grep 8080 | grep LISTEN > /dev/null;then
PID=`ps -ef | grep -v grep | grep java | awk '{print $2}'`
NUM=`ps -ef | grep -v grep | grep java | awk '{print $2}' | wc -l`
echo "tomcat 已经成功启动 ${NUM}个tomcat进程,PID为${PID}"
else
echo "tomcat启动失败,请重新启动"
echo 1
fi
fi
}
stop(){
NUM1=3
PID=`ps -ef | grep -v grep | grep java | awk '{print $2}'`
NUM=`ps -ef | grep -v grep | grep java | awk '{print $2}' | wc -l`
echo "3";sleep 1;echo "2";sleep 1; echo "1";sleep 1
if netstat -an | grep 8080 | grep LISTEN > /dev/null
then
echo "tomcat运行中,1秒关闭"
echo "1";sleep 1
echo "tomcat即将关闭,请稍等"
$CATALINA_HOME/bin/catalina.sh stop;echo "tomcat正在关闭"
sleep ${NUM1}
echo "3";sleep 1;echo "2";sleep 1; echo "1";sleep 1
pkill java && pkill tomcat
if netstat -an | grep 8080 | grep LISTEN > /dev/null;then
PID=`ps -ef | grep -v grep | grep java | awk '{print $2}'`
NUM=`ps -ef | grep -v grep | grep java | awk '{print $2}' | wc -l`
kill -9 $PID;echo "tomcat is fail ${NUM}"
else
echo "tomcat 已经关闭完成"
echo "3";sleep 1;echo "2";sleep 1; echo "1"
fi
else
echo "tomcat没有运行"
echo 1
fi
if netstat -an | grep 8080 | grep LISTEN > /dev/null; then
PID=`ps -ef | grep -v grep | grep java | awk '{print $2}'`
echo "tomcat is fail"
sleep 2
pkill tomcat;sleep 2
if netstat -an | grep 8080 | grep LISTEN > /dev/null;then
echo "tomcat is fail"
pkill java;sleep2
fi
fi
}
restart(){
stop
start
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
*)
echo $"USAGE: $0 {start|stop|restart|status}"
esac
keepalived+haproxy
node1
[root@node1 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from [email protected]
smtp_server 192.168.10.230
smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_iptables
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.10.233 dev eth0 label eth0:0
}
}
[root@node1 ~]# grep -v "#" /etc/haproxy/haproxy.cfg
global
log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
stats socket /var/lib/haproxy/stats
defaults
mode http
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000
listen webs
bind 192.168.10.233:80
mode http
log global
option httplog
server 192.168.10.230 192.168.10.230:8080 check inter 3000 fall 2 rise 5
server 192.168.10.231 192.168.10.231:8080 check inter 3000 fall 2 rise 5
node2
[root@node2 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from [email protected]
smtp_server 192.168.10.231
smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_iptables
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 80
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.10.233 dev eth0 label eth0:0
}
}
[root@node2 ~]# grep -v "#" /etc/haproxy/haproxy.cfg
global
log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
stats socket /var/lib/haproxy/stats
defaults
mode http
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000
listen webs
bind 192.168.10.233:80
mode http
log global
option httplog
server 192.168.10.230 192.168.10.230:8080 check inter 3000 fall 2 rise 5
server 192.168.10.231 192.168.10.231:8080 check inter 3000 fall 2 rise 5
cat /etc/sysctl.conf
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.ip_forward = 1
sysctl -p
jenkins
若发现jenkins起不来
yum install jenkins-2.138.4-1.1.noarch.rpm
systemctl start jenkins
candidates="
/apps/jdk/bin/java //目录加第一行
/etc/alternatives/java
/usr/lib/jvm/java-1.8.0/bin/java
/usr/lib/jvm/jre-1.8.0/bin/java
/usr/lib/jvm/java-1.7.0/bin/java
/usr/lib/jvm/jre-1.7.0/bin/java
/usr/bin/java
"
vim /etc/sysconfig/jenkins
JENKINS_JAVA_CMD="$candidate"
systemctl daemon-reload
systemctl start jenkins
tailf /var/log/jenkins/jenkins.log
[root@jenkins ~]# cat /var/lib/jenkins/secrets/initialAdminPassword
77df77ee737c4451a6098463c57aeed7
用浏览器访问192.168.10.253:8080
密码就是上面一串
显示插件数
[root@jenkins ~]# ls /var/lib/jenkins/plugins/|wc -l
146
systemctl restart jenkins
最好在开一个浏览器做下测试,使用刚创建的用户登陆下
jenkins生成sshkey
[root@jenkins ~]# ssh-keygen
[root@jenkins ~]# cat /root/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDepqIhyHOxr14B+vD4DwRN00fYxJ6ylbs4eiT/Cm+GFEh+A54ZNwPq48/lNQFD3Ue/7AAqxLztxqFiu+epuOcMwRYp8ZccY7M6XeMJHU+J8RKn0PnyvGiVjmIQrR/81OO6EYlmDd2qiBEhYa8j8n88aGu9jb0Fd9E6BbWgw6jVUNsUc4uTH9IfnZMhRAK7p2OneXqMEz9NGa157h/2JQZXIpYxTPWsXDVbaxWIyvjI5//6xBQzC9wgd2URKDNFPck5ZRcFs3uCSx6zir36kxpltqCAFet04MG72lZlsHrw8m+4YBZBjCIeXw+G8/Z+/QZP7ZACgGy16CtldGbV+yyD root@jenkins
测试git结果
git clone [email protected]:web233/web1.git
获取私钥
cat /root/.ssh/id_rsa
在jenkins添加私钥
ll -i /var/lib/jenkins/workspace
jenkins与tomcat1 2做免密钥登陆
ssh-copy-id -p22 [email protected]
ssh-copy-id -p 22 [email protected]
代码如下
whoami
#打包过程
cd /var/lib/jenkins/workspace/test && zip -r web233.zip ./*
#停止tomcat服务
ssh -p22 [email protected] "/etc/init.d/tomcat stop"
ssh -p22 [email protected] "/etc/init.d/tomcat stop"
#传送文件到web服务器
scp -P22 web233.zip [email protected]:/data/tomcat/webapps/testapp
scp -P22 web233.zip [email protected]:/data/tomcat/webapps/testapp
#代码替换
ssh -p22 [email protected] "cd /data/tomcat/webapps/testapp/ && rm -rf ./index.* && unzip web233.zip && rm -rf web233.zip"
ssh -p22 [email protected] "cd /data/tomcat/webapps/testapp/ && rm -rf ./index.* && unzip web233.zip && rm -rf web233.zip"
#启动tomcat服务
ssh -p22 [email protected] "/etc/init.d/tomcat start"
ssh -p22 [email protected] "/etc/init.d/tomcat start"
暂时没有找到解决方法,所以
一定要把jenkins用户改为root
一定要把jenkins用户改为root
一定要把jenkins用户改为root
[root@jenkins ]# grep root /etc/sysconfig/jenkins
JENKINS_USER="root"
测试上传一个GIT版本,然后构建
git clone http://192.168.10.254/web233/web1.git
lscd web1/
cat index.html
this is web233 test v1...
this is web233 test v2...
this is web233 test v3...
vim index.html
this is web233 test v1...
this is web233 test v2...
this is web233 test v3...
this is web233 test v4...
git add ./*
git commit -m "v4"
git push
http://192.168.10.230:8080/testapp/
http://192.168.10.231:8080/testapp/
http://192.168.10.233/testapp/
jenkins添加节点
[root@jenkins1 ~]# mkdir -pv /var/lib/jenkins/workspace
注意jenkins-slave java环境放在/usr/local/java
名字、描述和标签尽量保持一致
在建立一个新的节点jenkins-slave2
[root@jenkins2 ~]# mkdir -pv /var/lib/jenkins/workspace
查看日志
有这些提示,说明JDK没安装,或者环境变量未设置好
安装JDK完成后就好了,java环境尽量放在/usr/local/java