jenkins

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: "

准备环境

jenkins_第1张图片
jenkins

两台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
密码就是上面一串
jenkins_第2张图片

点推荐,这个过程取决于你的网速
jenkins_第3张图片

显示插件数
[root@jenkins ~]# ls /var/lib/jenkins/plugins/|wc -l
146

用户名为jenkins
jenkins_第4张图片

jenkins_第5张图片
jenkins

jenkins_第6张图片
jenkins

jenkins_第7张图片
jenkins

1、安装插件gitlab+blue ocean
jenkins_第8张图片

jenkins_第9张图片
jenkins

安装过程
jenkins_第10张图片

systemctl restart jenkins

创建一个任务
jenkins_第11张图片

jenkins_第12张图片
jenkins

jenkins_第13张图片
jeknins

安装role-based 使用这个插件做授权管理
jenkins_第14张图片

jenkins_第15张图片
jenkins

设置完成后不要退出 不要退出 不要退出
jenkins_第16张图片

创建一个用户
jenkins_第17张图片

jenkins_第18张图片
jenkins

jenkins_第19张图片
jenkins

看到所创建的用户没有任务权限
jenkins_第20张图片

设置权限 在系统管理
jenkins_第21张图片

jenkins_第22张图片
jenkins

jenkins_第23张图片
jenkins

只给read一个权限
jenkins_第24张图片

jenkins_第25张图片
jenkins

jenkins_第26张图片
jenkins

jenkins_第27张图片
jenkins

最好在开一个浏览器做下测试,使用刚创建的用户登陆下

关联任务
jenkins_第28张图片

配置邮件
jenkins_第29张图片

jenkins
jenkins

获取邮箱的授权码
jenkins_第30张图片

jenkins_第31张图片
jenkins

用USER1用户登陆构建一个
jenkins_第32张图片

jenkins_第33张图片
jenkins

jenkins_第34张图片
jenkins

jenkins_第35张图片
jenkins

jenkins_第36张图片
jenkins

jenkins_第37张图片
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

用浏览器访问gitlab服务器
jenkins_第38张图片

上传jenkins服务器的公钥
jenkins_第39张图片

jenkins_第40张图片
jenkins

jenkins_第41张图片
jenkins

测试git结果
git clone [email protected]:web233/web1.git

获取私钥
cat /root/.ssh/id_rsa
在jenkins添加私钥
jenkins_第42张图片

jenkins_第43张图片
jenkins

jenkins_第44张图片
jenkins

jenkins_第45张图片
jenkins

jenkins_第46张图片
jenkins

jenkins_第47张图片
jenkins

jenkins_第48张图片
jenkins

jenkins_第49张图片
jenkins

jenkins_第50张图片
jenkins

jenkins_第51张图片
jenkins

构建开始前删除工作区(开始前删除目录)
jenkins_第52张图片

jenkins_第53张图片
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_第54张图片
jenkins

暂时没有找到解决方法,所以
一定要把jenkins用户改为root
一定要把jenkins用户改为root
一定要把jenkins用户改为root
[root@jenkins ]# grep root /etc/sysconfig/jenkins
JENKINS_USER="root"

可以在控制台输出可以看到
jenkins_第55张图片

测试上传一个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

jenkins_第56张图片
jenkins

jenkins_第57张图片
jenkins

http://192.168.10.230:8080/testapp/
http://192.168.10.231:8080/testapp/
http://192.168.10.233/testapp/

jenkins添加节点

jenkins_第58张图片
jenkins

jenkins_第59张图片
jenkins

jenkins_第60张图片
jenkins

jenkins_第61张图片
jenkins

jenkins_第62张图片
jenkins

[root@jenkins1 ~]# mkdir -pv /var/lib/jenkins/workspace

注意jenkins-slave java环境放在/usr/local/java

jenkins_第63张图片
jenkins

名字、描述和标签尽量保持一致

jenkins_第64张图片
jenkins

jenkins_第65张图片
jenkins

可以通过日志查看部署jenkins slave进度
jenkins_第66张图片

jenkins_第67张图片
jenkins

jenkins_第68张图片
jenkins

在建立一个新的节点jenkins-slave2

[root@jenkins2 ~]# mkdir -pv /var/lib/jenkins/workspace

jenkins_第69张图片
jenkins

jenkins_第70张图片
jenkins

查看日志
jenkins_第71张图片
有这些提示,说明JDK没安装,或者环境变量未设置好

jenkins_第72张图片
jenkins

安装JDK完成后就好了,java环境尽量放在/usr/local/java

jenkins_第73张图片
jenkins

转载于:https://www.cnblogs.com/fina/p/11213564.html

你可能感兴趣的:(jenkins)