jenkins

jenkins

https://jenkins.io/zh/download/
https://pkg.jenkins.io/redhat-stable/
咱們下載軟件包看小版本號,數字越大,說明越穩定
插件下載
https://plugins.jenkins.io/
http://updates.jenkins-ci.org/download/plugins/html

history 命令顯示執行時間和執行者
export HISTTIMEFORMAT="%Y-%m-%d:%H-%M-%S:whoami: "java

準備環境

jenkins
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
<Host name="localhost"  appBase="/data/tomcat/webapps"
            unpackWARs="flase" autoDeploy="flase">

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啓動腳本node

#!/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

node1linux

[root@node1 ~]# vim /etc/keepalived/keepalived.conf 

! Configuration File for keepalived

global_defs {
   notification_email {
     root@localhost
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   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

node2git

[root@node2 ~]# vim /etc/keepalived/keepalived.conf 

! Configuration File for keepalived

global_defs {
   notification_email {
     root@localhost
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   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 jenkinsweb

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
77df77ee737c4451a6098463c57aeed7redis

用瀏覽器訪問192.168.10.253:8080
密碼就是上面一串
jenkinsshell

點推薦,這個過程取決於你的網速
jenkinsapache

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

用戶名爲jenkins
jenkins

jenkins
jenkins

jenkins
jenkins

jenkins
jenkins

一、安裝插件gitlab+blue ocean
jenkins

jenkins
jenkins

安裝過程
jenkins

systemctl restart jenkins

建立一個任務
jenkins

jenkins
jenkins

jeknins
jeknins

安裝role-based 使用這個插件作受權管理
jenkins

jenkins
jenkins

設置完成後不要退出 不要退出 不要退出
jenkins

建立一個用戶
jenkins

jenkins
jenkins

jenkins
jenkins

看到所建立的用戶沒有任務權限
jenkins

設置權限 在系統管理
jenkins

jenkins
jenkins

jenkins
jenkins

只給read一個權限
jenkins

jenkins
jenkins

jenkins
jenkins

jenkins
jenkins

最好在開一個瀏覽器作下測試,使用剛建立的用戶登錄下

關聯任務
jenkins

配置郵件
jenkins

jenkins
jenkins

獲取郵箱的受權碼
jenkins

jenkins
jenkins

用USER1用戶登錄構建一個
jenkins

jenkins
jenkins

jenkins
jenkins

jenkins
jenkins

jenkins
jenkins

jenkins
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

上傳jenkins服務器的公鑰
jenkins

jenkins
jenkins

jenkins
jenkins

測試git結果
git clone git@192.168.10.254:web233/web1.git

獲取私鑰
cat /root/.ssh/id_rsa
在jenkins添加私鑰
jenkins

jenkins
jenkins

jenkins
jenkins

jenkins
jenkins

jenkins
jenkins

jenkins
jenkins

jenkins
jenkins

jenkins
jenkins

jenkins
jenkins

jenkins
jenkins

構建開始前刪除工做區(開始前刪除目錄)
jenkins

jenkins
jenkins

ll -i /var/lib/jenkins/workspace

jenkins與tomcat1 2作免密鑰登錄
ssh-copy-id -p22 www@192.168.10.230
ssh-copy-id -p 22 www@192.168.10.231

代碼以下

whoami
#打包過程
cd /var/lib/jenkins/workspace/test && zip -r web233.zip ./*
 #中止tomcat服務
ssh -p22 www@192.168.10.230 "/etc/init.d/tomcat stop"
ssh -p22 www@192.168.10.231 "/etc/init.d/tomcat stop"
 #傳送文件到web服務器
scp -P22 web233.zip www@192.168.10.230:/data/tomcat/webapps/testapp
scp -P22 web233.zip www@192.168.10.231:/data/tomcat/webapps/testapp
 #代碼替換
ssh -p22 www@192.168.10.230 "cd /data/tomcat/webapps/testapp/ && rm -rf ./index.* && unzip web233.zip && rm -rf web233.zip"
ssh -p22 www@192.168.10.231 "cd /data/tomcat/webapps/testapp/ && rm -rf ./index.* && unzip web233.zip && rm -rf web233.zip"
 #啓動tomcat服務
ssh -p22 www@192.168.10.230 "/etc/init.d/tomcat start"
ssh -p22 www@192.168.10.231 "/etc/init.d/tomcat start"

jenkins
jenkins

暫時沒有找到解決方法,因此
必定要把jenkins用戶改成root
必定要把jenkins用戶改成root
必定要把jenkins用戶改成root
[root@jenkins ]# grep root /etc/sysconfig/jenkins
JENKINS_USER="root"

能夠在控制檯輸出能夠看到
jenkins

測試上傳一個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
jenkins

jenkins
jenkins

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

jenkins添加節點

jenkins
jenkins

jenkins
jenkins

jenkins
jenkins

jenkins
jenkins

jenkins
jenkins

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

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

jenkins
jenkins

名字、描述和標籤儘可能保持一致

jenkins
jenkins

jenkins
jenkins

能夠經過日誌查看部署jenkins slave進度
jenkins

jenkins
jenkins

jenkins
jenkins

在創建一個新的節點jenkins-slave2

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

jenkins
jenkins

jenkins
jenkins

查看日誌
jenkins
有這些提示,說明JDK沒安裝,或者環境變量未設置好

jenkins
jenkins

安裝JDK完成後就行了,java環境儘可能放在/usr/local/java

jenkins
jenkins
相關文章
相關標籤/搜索