使用Jenkins結合Gogs和SonarQube對項目代碼進行測試、部署、回滾,以及使用keepalived+haproxy調度至後端tomcat

0 環境說明

主tomcat:192.168.0.112
備tomcat:192.168.0.183javascript

haproxy+keepalived-1:192.168.0.156
haproxy+keepalived-2:192.168.0.157php

git: 還沒有部署
sonar-scanner:還沒有部署css

軟件:
jdk-8u144-linux-x64.tar.gz
apache-tomcat-8.5.43.tar.gz
haproxy-1.5.18-8.el7.x86_64.rpm
keepalived-1.3.5-8.el7_6.5.x86_64.rpmhtml

1、分別配置兩臺tomcat後端服務的java環境

1. 準備jdk8壓縮包

[root@bogon src]# pwd
/usr/local/src
[root@bogon src]# ls
jdk-8u144-linux-x64.tar.gz

2.解壓jdk壓縮包到指定目錄下

[root@bogon src]# tar -zxv -f jdk-8u144-linux-x64.tar.gz -C /usr/local/
[root@bogon src]# cd /usr/local/
[root@bogon local]# ls
bin  etc  games  include  jdk1.8.0_144  lib  lib64  libexec  sbin  share  src

3. 配置java的環境變量並生效

[root@bogon local]# cd /etc/profile.d/
[root@bogon profile.d]# vim java.sh
export JAVA_HOME=/usr/local/jdk1.8.0_144
export JRE_HOME=$JAVA_HOME/jre
export CLASSPATH=$JAVA_HOME/lib/:$JRE_HOME/lib
export TOMCAT_HOME=/usr/local/apache-tomcat-8.5.43
export PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin:$TOMCAT_HOME/bin
[root@bogon profile.d]# source java.sh

4. 測試java環境

[root@bogon profile.d]# echo ${JAVA_HOME}
/usr/local/jdk1.8.0_144
[root@bogon profile.d]# echo ${CLASSPATH}
/usr/local/jdk1.8.0_144/lib/:/usr/local/jdk1.8.0_144/jre/lib
[root@bogon profile.d]# echo ${PATH}
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/jdk1.8.0_144/bin:/usr/local/jdk1.8.0_144/jre/bin:/usr/local/apache-tomcat-8.5.43/bin
[root@bogon profile.d]# java -version
java version "1.8.0_144"
Java(TM) SE Runtime Environment (build 1.8.0_144-b01)
Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode)

2、分別安裝配置tomcat服務

1. 準備tomcat二進制壓縮包

[root@bogon src]# pwd
/usr/local/src
[root@bogon src]# ls
apache-tomcat-8.5.43.tar.gz  jdk-8u144-linux-x64.tar.gz

2.解壓jtomcat壓縮包到指定目錄下

[root@bogon src]# tar -zxv -f apache-tomcat-8.5.43.tar.gz -C /usr/local/
[root@bogon src]# cd /usr/local/
[root@bogon local]# ls
bin  etc  games  include  jdk1.8.0_144  lib  lib64  libexec  sbin  share  src

3.啓動tomcat服務

[root@bogon apache-tomcat-8.5.43]# /usr/local/apache-tomcat-8.5.43/bin/startup.sh 
Using CATALINA_BASE:   /usr/local/apache-tomcat-8.5.43
Using CATALINA_HOME:   /usr/local/apache-tomcat-8.5.43
Using CATALINA_TMPDIR: /usr/local/apache-tomcat-8.5.43/temp
Using JRE_HOME:        /usr/local/jdk1.8.0_144/jre
Using CLASSPATH:       /usr/local/apache-tomcat-8.5.43/bin/bootstrap.jar:/usr/local/apache-tomcat-8.5.43/bin/tomcat-juli.jar
Tomcat started.

4. 查看啓動端口

[root@bogon apache-tomcat-8.5.43]# ss -tlnp
State       Recv-Q Send-Q                   Local Address:Port                                  Peer Address:Port              
LISTEN      0      128                                  *:22                                               *:*                   users:(("sshd",pid=965,fd=3))
LISTEN      0      100                          127.0.0.1:25                                               *:*                   users:(("master",pid=1048,fd=13))
LISTEN      0      1                     ::ffff:127.0.0.1:8005                                            :::*                   users:(("java",pid=1349,fd=70))
LISTEN      0      100                                 :::8009                                            :::*                   users:(("java",pid=1349,fd=55))
LISTEN      0      100                                 :::8080                                            :::*                   users:(("java",pid=1349,fd=50))
LISTEN      0      128                                 :::22                                              :::*                   users:(("sshd",pid=965,fd=4))
LISTEN      0      100                                ::1:25                                              :::*                   users:(("master",pid=1048,fd=14))

5. 瀏覽器訪問測試"主tomcat服務"

瀏覽器輸入地址:http://192.168.0.112:8080/ 進行訪問,
爲了方便區分,在文件/usr/local/apache-tomcat-8.5.43/webapps/ROOT/index.jsp下面增長以下內容,在</body>上面前端

<h2>主</h2>

6. 瀏覽器訪問測試"從tomcat服務"

瀏覽器輸入地址:http://192.168.0.183:8080/ 進行訪問
爲了方便區分,在文件/usr/local/apache-tomcat-8.5.43/webapps/ROOT/index.jsp下面增長以下內容,在</body>上面java

<h2>備</h2>

3、分別配置兩臺keepalived+haproxy高可用分離調度服務

1. 安裝高可用服務keepalived

yum -y install keepalived

2. 修改keepalived配置文件

! Configuration File for keepalived

global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id haproxy # 在備份服務中的路由id設置爲 "haproxy-1",不可相同
   vrrp_skip_check_adv_addr
   # vrrp_strict #禁用掉vrrp,不然只支持組播不支持單播模式
   vrrp_iptables #開啓不自動添加防火牆規則,避免沒法訪問此主機
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_instance VI_1 {
    state MASTER  #設置爲主服務,在備份服務中設置爲"BACKUP",備份服務
    interface ens33 #綁定的網卡
    virtual_router_id 51 # 實例路由id號,在同一網段內virtual_router_id 值不能相同,備份的能夠是50
    priority 100 #優先級,備份服務優先級必須小於100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        #192.168.200.16
        #192.168.200.17
        #192.168.200.18
        192.168.0.220/ dev ens33 label ens33:0 #將虛擬vip綁定到本地ens33網卡並取名爲ens33:0,備份的也須要設置
    }
    unicast_src_ip 192.168.0.156 #單播源地址ip,這個是填寫自身的IP,在備份服務中設置源ip爲192.168.1.11
    unicast_peer{
    192.168.0.157 # 單播目標地址ip這個填寫另外一臺的IP,在備份服務中設置目標ip爲192.168.1.10
    }

}

3.分別啓動keepalived服務

# 主keepalivd:
[root@bogon keepalived]# systemctl start keepalived.service

[root@bogon keepalived]# systemctl status keepalived.service
● keepalived.service - LVS and VRRP High Availability Monitor
   Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled)
   Active: active (running) since Tue 2019-08-13 15:51:22 CST; 6min ago
  Process: 1452 ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 1453 (keepalived)
   CGroup: /system.slice/keepalived.service
           ├─1453 /usr/sbin/keepalived -D
           ├─1454 /usr/sbin/keepalived -D
           └─1455 /usr/sbin/keepalived -D

Aug 13 15:51:48 bogon Keepalived_healthcheckers[1454]: Adding sorry server [192.168.200.200]:1358 to VS [10.10.10.2]:1358
Aug 13 15:51:48 bogon Keepalived_healthcheckers[1454]: Removing alive servers from the pool for VS [10.10.10.2]:1358
Aug 13 15:51:48 bogon Keepalived_healthcheckers[1454]: Remote SMTP server [192.168.200.1]:25 connected.
Aug 13 15:51:48 bogon Keepalived_healthcheckers[1454]: Error reading data from remote SMTP server [192.168.200.1]:25.
Aug 13 15:51:49 bogon Keepalived_healthcheckers[1454]: Timeout connecting server [192.168.201.100]:443.
Aug 13 15:51:49 bogon Keepalived_healthcheckers[1454]: Check on service [192.168.201.100]:443 failed after 3 retry.
Aug 13 15:51:49 bogon Keepalived_healthcheckers[1454]: Removing service [192.168.201.100]:443 from VS [192.168.200.100]:443
Aug 13 15:51:49 bogon Keepalived_healthcheckers[1454]: Lost quorum 1-0=1 > 0 for VS [192.168.200.100]:443
Aug 13 15:51:49 bogon Keepalived_healthcheckers[1454]: Remote SMTP server [192.168.200.1]:25 connected.
Aug 13 15:51:49 bogon Keepalived_healthcheckers[1454]: Error reading data from remote SMTP server [192.168.200.1]:25.

[root@bogon keepalived]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:ae:fb:8c brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.156/24 brd 192.168.0.255 scope global dynamic ens33
       valid_lft 5299sec preferred_lft 5299sec
    inet 192.168.0.220/0 scope global ens33:0 #綁定的虛擬vip
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:feae:fb8c/64 scope link 
       valid_lft forever preferred_lft forever
#備keepalivd:
[root@bogon keepalived]# systemctl start keepalived.service

[root@bogon keepalived]# systemctl status keepalived.service
● keepalived.service - LVS and VRRP High Availability Monitor
   Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled)
   Active: active (running) since Tue 2019-08-13 16:14:20 CST; 8min ago
  Process: 1386 ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 1387 (keepalived)
   CGroup: /system.slice/keepalived.service
           ├─1387 /usr/sbin/keepalived -D
           ├─1388 /usr/sbin/keepalived -D
           └─1389 /usr/sbin/keepalived -D

Aug 13 16:14:47 bogon Keepalived_healthcheckers[1388]: Adding sorry server [192.168.200.200]:1358 to VS [10.10.10.2]:1358
Aug 13 16:14:47 bogon Keepalived_healthcheckers[1388]: Removing alive servers from the pool for VS [10.10.10.2]:1358
Aug 13 16:14:47 bogon Keepalived_healthcheckers[1388]: Remote SMTP server [192.168.200.1]:25 connected.
Aug 13 16:14:47 bogon Keepalived_healthcheckers[1388]: Error reading data from remote SMTP server [192.168.200.1]:25.
Aug 13 16:14:47 bogon Keepalived_healthcheckers[1388]: Timeout connecting server [192.168.201.100]:443.
Aug 13 16:14:47 bogon Keepalived_healthcheckers[1388]: Check on service [192.168.201.100]:443 failed after 3 retry.
Aug 13 16:14:47 bogon Keepalived_healthcheckers[1388]: Removing service [192.168.201.100]:443 from VS [192.168.200.100]:443
Aug 13 16:14:47 bogon Keepalived_healthcheckers[1388]: Lost quorum 1-0=1 > 0 for VS [192.168.200.100]:443
Aug 13 16:14:47 bogon Keepalived_healthcheckers[1388]: Remote SMTP server [192.168.200.1]:25 connected.
Aug 13 16:14:47 bogon Keepalived_healthcheckers[1388]: Error reading data from remote SMTP server [192.168.200.1]:25.

[root@bogon keepalived]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:c5:6b:34 brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.157/24 brd 192.168.0.255 scope global dynamic ens33
       valid_lft 6058sec preferred_lft 6058sec
    inet 192.168.0.220/0 scope global ens33:0 # 綁定的虛擬IP,這個跟文檔說的不同,有待進一步研究
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fec5:6b34/64 scope link 
       valid_lft forever preferred_lft forever

4.分別配置兩臺調度服務內核參數

[root@bogon keepalived]# vim /etc/sysctl.conf 
net.ipv4.ip_nonlocal_bind = 1   #開啓非本地ip綁定,避免haproxy沒法綁定非本機ip
net.ipv4.ip_forward = 1  #開啓路由轉發功能

[root@bogon keepalived]# sysctl -p
net.ipv4.ip_nonlocal_bind = 1   #開啓非本地ip綁定,避免haproxy沒法綁定非本機ip
net.ipv4.ip_forward = 1  #開啓路由轉發功能

5. 分別編譯安裝好haproxy

[root@bogon ~]# yum -y install haproxy
[root@bogon ~]# cd /etc/haproxy
[root@bogon haproxy]# cp haproxy.cfg haproxy.cfg.bak
[root@bogon haproxy]# vim haproxy.cfg
#---------------------------------------------------------------------
# Example configuration for a possible web application.  See the
# full configuration options online.
#
#   http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
#
#---------------------------------------------------------------------

#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
    # to have these messages end up in /var/log/haproxy.log you will
    # need to:
    #
    # 1) configure syslog to accept network log events.  This is done
    #    by adding the '-r' option to the SYSLOGD_OPTIONS in
    #    /etc/sysconfig/syslog
    #
    # 2) configure local2 events to go to the /var/log/haproxy.log
    #   file. A line like the following can be added to
    #   /etc/sysconfig/syslog
    #
    #    local2.*                       /var/log/haproxy.log
    #
    log         127.0.0.1 local2

    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     100000
    user        haproxy
    group       haproxy
    daemon

    # turn on stats unix socket
    stats socket /var/lib/haproxy/stats
    
    #nbproc 2  #開啓的線程數
   # cpu-map 1 0  #綁定到cup的第0號核心
   # cpu-map 2 1  #綁定到cup的第1號核心



#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
#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                 100000

defaults     #默認設置,爲前端、後端及listen默認設置
    option http-keep-alive
    option  forwardfor  #ip透傳
    maxconn 100000
    mode http
    timeout connect 300000ms
    timeout client  300000ms
    timeout server  300000ms


#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
#frontend  main *:5000
#    acl url_static       path_beg       -i /static /images /javascript /stylesheets
#    acl url_static       path_end       -i .jpg .gif .png .css .js

#    use_backend static          if url_static
#    default_backend             app

#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
#backend static
#    balance     roundrobin
#    server      static 127.0.0.1:4331 check

#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
#backend app
#    balance     roundrobin
#    server  app1 127.0.0.1:5001 check
#    server  app2 127.0.0.1:5002 check
#    server  app3 127.0.0.1:5003 check
#    server  app4 127.0.0.1:5004 check

listen stats   #開啓監聽狀態頁
    mode http   #http協議
    bind 0.0.0.0:8000   #狀態頁訪綁定的端口
    stats enable   #開啓狀態頁
    log global    #全局日誌
    stats uri     /haproxy-status   #狀態也路徑
    stats auth    admin:123456   #狀態頁登陸的用戶名及密碼

listen  web_port      #監聽的服務
    bind 192.168.0.220:80  #綁定的虛擬vip及端口,當外網訪問此虛擬vip時會自動調度到後端服務
    mode http    #http協議
    balance roundrobin  #調度算法 roundrobin動態輪詢
    log global   #全局日誌
    server 192.168.0.112  192.168.0.112:8080  check inter 3000 fall 2 rise 5     #調度的後端服務
    server 192.168.0.183  192.168.0.183:8080  check inter 3000 fall 2 rise 5     #調度的後端服務

[root@bogon haproxy]# systemctl start haproxy.service
[root@bogon haproxy]# systemctl status haproxy.service
# 如果狀態沒有啓動,經過查看/var/log/messages日誌中出現以下錯誤信息:haproxy-systemd-wrapper: [ALERT] 224/170040 (15627) :Starting proxy stats: cannot bind socket
# 解決辦法,執行以下命令,而後重啓服務便可:
[root@bogon haproxy]# setsebool -P haproxy_connect_any=1

6. 查看haproxy狀態

使用瀏覽器訪問:http://192.168.0.156:8000/haproxy-status , 或者 http://192.168.0.157:8000/haproxy-status
帳號是admin,密碼是123456python

7. 瀏覽器訪問調度服務,成功調度到後端服務

使用瀏覽器訪問:http://192.168.0.220 結果是先調度到備的那臺上面,可是由於採用的是輪詢算法,強制刷新會發現調度到主的上面linux

4、建立Jenkins的執行腳本,用以實現經過Jenkins的選項參數來自動測試、部署、回滾代碼

注意:事先搭建好jenkins、gitlab、sonaqube等服務,其中jenkins要安裝scanner掃描器git

1. 自定義建立指定的jenkins服務工做目錄

mkdir -pv /data/jenkins/worker

2. jenkins服務器腳本的保存路徑

# pwd
/data/jenkins

3. jenkins服務器編輯腳本

注意:裏面的參數須要修改,還沒有部署的服務須要部署web

# vim project.sh
#!/bin/bash

#jenkins參數選項
time=`date +%Y-%m-%d_%H-%M-%S`
# 2019-08-14_00-36-41
method=$1
group=$2
branch=$3

#後端tomcat服務ip地址組
function ip_value(){
    if [[ "${group}" == "group1" ]];then
        ip_list="192.168.0.112"
        /usr/bin/echo ${ip_list}
    elif [[ "${group}" == "group2" ]];then
        ip_list="192.168.0.183"
        /usr/bin/echo ${ip_list}
    elif [[ "${group}" == "group3" ]];then
        ip_list="192.168.0.112 192.168.0.183"
        /usr/bin/echo ${ip_list}
    fi
}

#先從git上拉取代碼到Jenkins服務端
function code_deploy(){
    /usr/bin/cd /data/jenkins/worker
    /usr/bin/rm -rf ./*
    /usr/bin/git clone -b ${branch} git@192.168.0.168:3000/sandu/web-page.git
}

#代碼測試,使用sonar檢測代碼質量
function code_test(){
    /usr/bin/cd /data/jenkins/worker/web-page
    /usr/bin/cat > sonar-project.properties <<eof
        sonar.projectKey=one123456 
        sonar.projectName=code-test 
        sonar.projectVersion=1.0 
        sonar.sources=./ 
        sonar.language=python 
        sonar.sourceEncoding=UTF-8
eof
    /data/scanner/sonar-scanner/bin/sonar-scanner
}

#代碼打包壓縮
function code_compress(){
    /usr/bin/cd /data/jenkins/worker/
    /usr/bin/rm -f web-page/sonar-project.properties
    /usr/bin/tar -czv -f code.tar.gz web-page
}

#調度器剝離後端服務
function haproxy_down(){
    for ip in ${ip_list};do
        /usr/bin/echo ${ip}
        /usr/bin/ssh root@192.168.0.156 "echo "disable  server web_port/${ip}"|socat stdio /var/lib/haproxy/stats"
        /usr/bin/ssh root@192.168.0.157 "echo "disable  server web_port/${ip}"|socat stdio /var/lib/haproxy/stats"
    done
}

#後端服務下線
function backend_stop(){
    for ip in ${ip_list};do
        /usr/bin/echo ${ip}
        /usr/bin/ssh root@$ip "/usr/local/apache-tomcat-8.5.43/bin/shutdown.sh"
        # 備份後端代碼
        /usr/bin/ssh root@${ip} "tar -zcv -f /usr/local/apache-tomcat-8.5.43/back_code/${time}-backcode.tar.gz /usr/local/apache-tomcat-8.5.43/webapps"
    done
}

#部署代碼到後端服務站點
function scp_backend(){
    for ip in ${ip_list};do
        /usr/bin/echo ${ip}
        /usr/bin/scp /data/jenkins/worker/code.tar.gz root@${ip}:/usr/local/apache-tomcat-8.5.43/web_code/${time}-code.tar.gz
        /usr/bin/ssh root@${ip} "tar -xv -f /usr/local/apache-tomcat-8.5.43/web_code/${time}-code.tar.gz -C /usr/local/apache-tomcat-8.5.43/webapps"
    done
}

#啓動後端服務
function backend_start(){
    for ip in ${ip_list};do
        /usr/bin/echo ${ip}
        /usr/bin/ssh root@$ip "/usr/local/apache-tomcat-8.5.43/bin/startup.sh"
        /usr/bin/sleep 6
done
}

#測試訪問後端服務
function backend_test(){
for ip in ${ip_list};do
    /usr/bin/echo ${ip}
    status_code=`curl -I -s -m 6 -o /dev/null -w %{http_code} http://${ip}:8080`
    if [ ${status_code} -eq 200 ];then
        /usr/bin/echo "訪問測試成功,後端代碼部署成功"
        if [[ $ip == "192.168.0.183" ]];then
            /usr/bin/ssh root@192.168.0.156 "echo "enable server web_port/${ip}" | socat stdio /var/lib/haproxy/stats"
            /usr/bin/ssh root@192.168.0.157 "echo "enable server web_port/${ip}" | socat stdio /var/lib/haproxy/stats"
        fi
    else
        /usr/bin/echo "訪問測試失敗,請從新部署代碼至後端服務" 
    fi
done
}

#代碼回滾
function code_rollback(){
    for ip in ${ip_list};do
        /usr/bin/echo ${ip}
        /usr/bin/ssh root@${ip} "tar -zxv -f /usr/local/apache-tomcat-8.5.43/back_code/${time}-backcode.tar.gz -C /usr/local/apache-tomcat-8.5.43/webapps"
    done
    /usr/bin/echo "tomcat代碼回滾成功,回到上一版本,下一步進行訪問測試"
}

#主菜單命令
main(){
    case $1 in
        "deploy")
            ip_value;
            code_deploy;
            code_test;
            code_compress;
            haproxy_down;
            backend_stop;
            scp_backend;
            backend_start;
            backend_test;
        ;;
        "rollback")
            ip_value;
            haproxy_down;
            backend_stop;
            code_rollback;
            backend_start;
            backend_test;
        ;;
    esac
}
main $1 $2 $3

4. 在各後端建立好代碼壓縮文件和備份文件保存路徑

主tomcat:mkdir -p /usr/local/apache-tomcat-8.5.43/{web_code,back_code}
備tomcat:mkdir -p /usr/local/apache-tomcat-8.5.43/{web_code,back_code}

5. 在jenkins服務設置好免密祕鑰登陸各服務

jenkins所在的服務器分別向兩臺tomcat服務器和keepalived/haproxy服務器免密祕鑰登陸

ssh-copy-id 192.168.0.112
ssh-copy-id 192.168.0.183
ssh-copy-id 192.168.0.156
ssh-copy-id 192.168.0.157

5、在gitlab服務器克隆並推送代碼---須要修改

1)克隆指定的develop分支代碼

root@ubuntu1804:~# git clone -b develop http://192.168.1.30/jie/web-page.git
Cloning into 'web-page'...
Username for 'http://192.168.1.30': jie
Password for 'http://jie@192.168.1.30': 
remote: Enumerating objects: 39, done.
remote: Counting objects: 100% (39/39), done.
remote: Compressing objects: 100% (22/22), done.
remote: Total 39 (delta 4), reused 27 (delta 4)
Unpacking objects: 100% (39/39), done.

2)查看克隆的所包含的代碼文件

root@ubuntu1804:~# ls web-page/
index.html  Math.php

3)修改代文件

root@ubuntu1804:~/web-page# cat index.html 
<h1>welcome to tomcat page</h1>
<h3>simple-version v1</h3>

4)推送v1版代碼至gitlab代碼庫

root@ubuntu1804:~/web-page# git add ./*
root@ubuntu1804:~/web-page# git commit -m 'v1'
[develop d0dd713] v1
 1 file changed, 2 insertions(+), 2 deletions(-)

root@ubuntu1804:~/web-page# git push
Username for 'http://192.168.1.30': jie
Password for 'http://jie@192.168.1.30': 
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 316 bytes | 316.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: 
remote: To create a merge request for develop, visit:
remote:   http://192.168.1.30/jie/web-page/merge_requests/new?merge_request%5Bsource_branch%5D=develop
remote: 
To http://192.168.1.30/jie/web-page.git
     c10f5bf..d0dd713  develop -> develop

6、jenkins的配置文件修改及選項參數構建

1. 建立一個項目code-test

2. 配置此項目的configure文件,添加選項參數、字符參數且與腳本文件中的選項相對應

General,參數化構建過程,選項參數/字符參數

  1. method
  • deploy # 代碼部署
  • rollback # 代碼回滾
  1. group
  • group1
  • group2
  • group3
  1. branch
  • master # 主分支
  • develop # 開發分支

3. 配置jenkins的shell腳本命令,此腳本實現代碼的測試、部署以及 回滾

bulid(構建)--執行shell

cd /data/enkins
bash /project.sh $method $group $branch

4. 保存以上配置,而後部署第一組後端服務主tomcat

5. 控制檯輸出信息

6. 直接瀏覽器訪問主tomcat服務驗證是否部署成功

7. 再部署第二組後端服務備tomcat-1,控制檯輸出信息驗證是否部署成功

8. 分別查看後端服務部署的相關代碼文件,肯定代碼文件是否部署到後端服務

9. 直接瀏覽器訪問備tomcat1服務驗證是否部署成功

10. 最後經過瀏覽器haproxy調度器,成功調度到後端服務tomcat

11. 代碼測試結果(SonarQube)

12. 回滾測試

相關文章
相關標籤/搜索