實驗準備:共用6臺主機:172.18.9.200:Gitlab,172.18.9.201:Jenkins,172.18.9.202:Haproxy+Keepalived(master),172.18.9.203:Haproxy+Keepalived(slave),172.18.9.204:Tomcat 1,172.18.9.205:Tomcat 2,java
[root@205/204 ~]#cd /apps
[root@205/204 apps]#ls
apache-tomcat-8.5.32 jdk-8u191-linux-x64.rpm apache-tomcat-8.5.32.tar.gz tomcat
[root@205/204 apps]#ln -sv /apps/apache-tomcat-8.5.37 /apps/tomcat
' /apps/tomcat' -> ' /apps/apache-tomcat-8.5.37'
[root@204/205 apps]#yum -y install jdk-8u191-linux-x64.rpm
[root@204/205 apps]#/apps/tomcat/bin/catalina.sh start
Using CATALINA_BASE: /apps/tomcat
Using CATALINA_HOME: /apps/tomcat
Using CATALINA_TMPDIR: /apps/tomcat/temp
Using JRE_HOME: /usr
Using CLASSPATH: /apps/tomcat/bin/bootstrap.jar:/apps/tomcat/bin/tomcat-juli.jar
Tomcat started.
[root@204/205 apps]#ss -ntl #先將端口開啓!
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 100 :::8080 :::*linux
[root@204 apps]#vim /etc/init.d/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
git
#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 tomcat | grep jdk | awk '{print $2}'
NUM=ps -ef | grep tomcat | grep jdk | awk '{print $2}' | wc -l
echo "Tomcat 已經成功啓動${NUM} 個Tomcat進程!,PID爲${PID}"
else
echo "Tomcat啓動失敗,請從新啓動!"
echo 1
fi
fi
}
stop() {
PID=ps -ef | grep -v grep | grep java | awk '{print $2}'
NUM=ps -ef | grep -v "color" | grep tomcat | awk '{print $2}' | wc -l
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運行中,1秒後關閉!"
echo 1;sleep 1
echo "即將關閉Tomcat服務,請稍等!"
$CATALINA_HOME/bin/catalina.sh stop ;echo "已經執行關閉命令,正在檢查關閉了多少Tomcat進程,請稍等30秒鐘!"
sleep 3
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 "color" | grep tomcat | awk '{print $2}' | wc -l
kill -9 $PID ;echo "已成功關閉${NUM} 個tomcat進程"
else
echo "Tomcat 已經關閉完成!"
echo "3";sleep 1;echo "2";sleep 1;echo "1";sleep 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}'
#NUM=ps -ef | grep -v "color" | grep tomcat | awk '{print $2}' | wc -l
echo "關閉失敗,即將強制刪除tomcat進程!"
sleep 2
pkill tomcat ;sleep 2
if netstat -an | grep 8080 | grep LISTEN >/dev/null;then
echo "強制關閉失敗,即將再次強制刪除tomcat進程!"
pkill java; sleep 2
fi
fi
}
restart() {
stop
start
}web
case "$1" in
start)
start
;;shell
stop)
stop
;;數據庫
restart)
restart
;;apache
*)
echo $"Usage: $0 {start|stop|restart|status}"
esacbootstrap
[root@204 apps]# /etc/init.d/tomcat status
Usage: /etc/init.d/tomcat {start|stop|restart|status}
[root@204 apps]# /etc/init.d/tomcat start
正在判斷服務狀態,請稍等!
請稍等3秒鐘
3
2
1
Tomcat已經正在運行了!
[root@204 apps]#ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 100 :::8080 :::*vim
[root@204 apps]# /etc/init.d/tomcat stop
正在判斷服務狀態,請稍等3秒鐘!
3
2
1
Tomcat 沒有運行
1
[root@204 apps]#ps -ef |grep tomcat #此時已經沒有tomcat服務在運行了!!
root 18598 17020 0 11:41 pts/0 00:00:00 grep --color=auto tomcat
[root@204 apps]#scp /etc/init.d/tomcat 172.18.9.205:/etc/init.d/tomcat
root@172.18.9.205's password:
tomcat 100% 3339 973.5KB/s 00:00 centos
正在判斷服務狀態,請稍等!
請稍等3秒鐘
3
2
1
Tomcat已經正在運行了!
[root@centos7 apps]#chown www.www /apps/tomcat/ -R
[root@204/205 apps]#passwd www
Changing password for user www.
New password:123gxy
BAD PASSWORD: The password is shorter than 7 characters
Retype new password:123gxy
passwd: all authentication tokens updated successfully.
[root@204/205 apps]#/etc/init.d/tomcat stop
正在判斷服務狀態,請稍等3秒鐘!
3
2
1
Tomcat 沒有運行
1
[www@205 ~]$/etc/init.d/tomcat start
正在判斷服務狀態,請稍等!
請稍等3秒鐘
3
2
1
Tomcat 已經成功啓動完成,5秒後判斷是否啓動成功
5
4
3
2
1
Tomcat 已經成功啓動1 個Tomcat進程!,PID爲23324
[root@204/205 apps]#mkdir -p /data/tomcat/webapps
[root@204/205 apps]#chown www.www /data/tomcat/webapps/ -R
[www@205 ~]$vim /apps/tomcat/conf/server.xml
<Host name="localhost" appBase="/data/tomcat/webapps"
unpackWARs="false" autoDeploy="false"> #在該文件的第148行
[www@205 ~]$cd /data/tomcat/webapps/
[www@205 webapps]$mkdir testapp
[www@205 webapps]$vim testapp/index.jsp
testjsp
[www@205 webapps]$/etc/init.d/tomcat restart
Tomcat 已經成功啓動完成,5秒後判斷是否啓動成功
5
4
3
2
1
Tomcat 已經成功啓動1 個Tomcat進程!,PID爲24404
[www@205 webapps]$ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 100 :::8080 :::*
[www@205 webapps]$ll
total 0
drwxrwxr-x. 2 www www 23 Apr 25 13:58 testapp
在204主機上:
[root@204/205 apps]#useradd www -u 2000 #該帳號用於管理tomcat
[root@204/205 apps]#chown www.www /apps/tomcat/ -R
[root@204/205 apps]#passwd www
Changing password for user www.
New password:123gxy
BAD PASSWORD: The password is shorter than 7 characters
Retype new password:123gxy
passwd: all authentication tokens updated successfully.
[root@204 apps]#mkdir -p /data/tomcat/webapps
[root@204 apps]#chown www.www /data/tomcat/webapps/ -R
[root@204 apps]#su - www
[www@204 ~]$vim /apps/tomcat/conf/server.xml
<Host name="localhost" appBase="/data/tomcat/webapps"
unpackWARs="false" autoDeploy="false">
[www@204 ~]$cd /data/tomcat/webapps/
[www@204 webapps]$mkdir testapp
[www@204 webapps]$vim testapp/index.jsp
testapp 204
[www@204 webapps]$/etc/init.d/tomcat restart
Tomcat 已經成功啓動完成,5秒後判斷是否啓動成功
5
4
3
2
1
Tomcat 已經成功啓動1 個Tomcat進程!,PID爲20650
slave:[root@202 ~]#vim /etc/keepalived/keepalived.conf
global_defs {
notification_email {br/>acassen@firewall.loc
failover@firewall.locbr/>sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_iptables #該參數務必加入腳本中,不然在其餘主機上ping不通VIP
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state MASTER
interface ens37
virtual_router_id 120
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.18.9.248 dev eth0 label ens37:0
}
} #該腳本下面的都刪除!!
[root@202 ~]#systemctl restart keepalived
[root@202 ~]#ifconfig #網卡子接口綁定成功
ens37:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.18.9.248 netmask 255.255.255.255 broadcast 0.0.0.0
ether fa:16:3e:75:57:73 txqueuelen 1000 (Ethernet)
[root@204 ~]#ping 172.18.9.248 #加上參數後就ping通了!
PING 172.18.9.248 (172.18.9.248) 56(84) bytes of data.
64 bytes from 172.18.9.248: icmp_seq=48 ttl=64 time=0.029 ms
64 bytes from 172.18.9.248: icmp_seq=49 ttl=64 time=0.046 ms
如今在203 slave上同款:[root@203 ~]#vim /etc/keepalived/keepalived.conf
vrrp_instance VI_1 {
state BACKUP
interface ens37
virtual_router_id 120
priority 80
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.18.9.248 dev ens37 label ens37:0
}
}
[root@202/203 ~]#systemctl restart keepalived
[root@202/203 ~]#systemctl enable keepalived
Created symlink from /etc/systemd/system/multiuser.target.wants/keepalived.service to /usr/lib/systemd/system/keepalived.service.
listen webserver
bind 172.18.9.248:80
mode http
log global
option httplog
server 1 172.18.9.204:8080 check inter 3000 fall 2 rise 5
server 2 172.18.9.205:8080 check inter 3000 fall 2 rise 5
[root@202 ~]#systemctl start haproxy
[root@202 ~]#ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 172.18.9.248:80 :
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.ip_forward = 1 #將2者寫入配置文件,並改成1,打開狀態!
[root@202 ~]#sysctl -p #驗證查看
Jenkins的下載路徑:https://pkg.jenkins.io/redhat-stable/jenkins-2.138.4-1.1.noarch.rpm
[root@201 ~]#wget https://pkg.jenkins.io/redhat-stable/jenkins-2.138.4-1.1.noarch.rpm
[root@201 ~]#yum -y install jenkins-2.138.4-1.1.noarch.rpm
[root@201 ~]#yum install -y jdk-8u191-linux-x64.rpm
[root@201 ~]#vim /etc/sysconfig/jenkins
JENKINS_USER="root/www" 二者都行,默認爲Jenkins,爲了方便登錄才修改的
[root@201 ~]#systemctl start jenkins
[root@201 ~]#systemctl enable jenkins
jenkins.service is not a native service, redirecting to /sbin/chkconfig.
Executing /sbin/chkconfig jenkins on
[root@201 ~]#ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 50 :::8080 :::* #Jenkins的端口也是8080,端口開啓便可訪問頁面!!
查看密碼:[root@201 ~]#cat /var/lib/jenkins/secrets/initialAdminPassword
dab1711156e2417180f029f56100984c
查看日誌:[root@201 ~]#tail -f /var/log/jenkins/jenkins.log
Apr 26, 2019 10:16:10 PM hudson.model.DownloadService$Downloadable load
INFO: Obtained the updated data file for hudson.tasks.Maven.MavenInstaller
Apr 26, 2019 10:16:10 PM hudson.model.AsyncPeriodicWork$1 run
INFO: Finished Download metadata. 19,854 ms
Apr 26, 2019 10:16:21 PM hudson.model.UpdateSite updateData
INFO: Obtained the latest update center data file for UpdateSource default
Apr 26, 2019 10:16:21 PM jenkins.InitReactorRunner$1 onAttained
INFO: Completed initialization
[root@201 ~]#cat /var/lib/jenkins/config.xml #該文件是jenkins的數據庫文件,最好作備份!
[root@201 ~]#cat /var/lib/jenkins/users/jenkinsadmin_7275851894560514220/config.xml #該文件存放密碼,若密碼遺忘,能夠新建一個帳號,獲取密碼後將密碼保存至該文件便可!!
注意:此時若登錄新帳號出現問題能夠在「系統管理-全局安全配置中勾選默認配置」就能夠了