您好,登錄后才能下訂單哦!
這篇“如何用Keepalived+Nginx+Tomcat實現高可用Web集群”文章的知識點大部分人都不太理解,所以小編給大家總結了以下內容,內容詳細,步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“如何用Keepalived+Nginx+Tomcat實現高可用Web集群”文章吧。
keepalived+nginx+tomcat 實現高可用web集群
一、nginx的安裝過程
1.下載nginx安裝包,安裝依賴環境包
(1)安裝 c++編譯環境
yum -y install gcc #c++
(2)安裝pcre
yum -y install pcre-devel
(3)安裝zlib
yum -y install zlib-devel
(4)安裝nginx
定位到nginx 解壓文件位置,執行編譯安裝命令
[root@localhost nginx-1.12.2]# pwd /usr/local/nginx/nginx-1.12.2 [root@localhost nginx-1.12.2]# ./configure && make && make install
(5)啟動nginx
安裝完成后先尋找那安裝完成的目錄位置
[root@localhost nginx-1.12.2]# whereis nginx nginx: /usr/local/nginx [root@localhost nginx-1.12.2]#
進入nginx子目錄sbin啟動nginx
[root@localhost sbin]# ls nginx [root@localhost sbin]# ./nginx & [1] 5768 [root@localhost sbin]#
查看nginx是否啟動
或通過進程查看nginx啟動情況
[root@localhost sbin]# ps -aux|grep nginx root 5769 0.0 0.0 20484 608 ? ss 14:03 0:00 nginx: master process ./nginx nobody 5770 0.0 0.0 23012 1620 ? s 14:03 0:00 nginx: worker process root 5796 0.0 0.0 112668 972 pts/0 r+ 14:07 0:00 grep --color=auto nginx [1]+ 完成 ./nginx [root@localhost sbin]#
到此nginx安裝完成并啟動成功。
(6)nginx快捷啟動和開機啟動配置
編輯nginx快捷啟動腳本【 注意nginx安裝路徑 , 需要根據自己的nginx路徑進行改動 】
[root@localhost init.d]# vim /etc/rc.d/init.d/nginx
#!/bin/sh # # nginx - this script starts and stops the nginx daemon # # chkconfig: - 85 15 # description: nginx is an http(s) server, http(s) reverse \ # proxy and imap/pop3 proxy server # processname: nginx # config: /etc/nginx/nginx.conf # config: /usr/local/nginx/conf/nginx.conf # pidfile: /usr/local/nginx/logs/nginx.pid # source function library. . /etc/rc.d/init.d/functions # source networking configuration. . /etc/sysconfig/network # check that networking is up. [ "$networking" = "no" ] && exit 0 nginx="/usr/local/nginx/sbin/nginx" prog=$(basename $nginx) nginx_conf_file="/usr/local/nginx/conf/nginx.conf" [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx lockfile=/var/lock/subsys/nginx make_dirs() { # make required directories user=`$nginx -v 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -` if [ -z "`grep $user /etc/passwd`" ]; then useradd -m -s /bin/nologin $user fi options=`$nginx -v 2>&1 | grep 'configure arguments:'` for opt in $options; do if [ `echo $opt | grep '.*-temp-path'` ]; then value=`echo $opt | cut -d "=" -f 2` if [ ! -d "$value" ]; then # echo "creating" $value mkdir -p $value && chown -r $user $value fi fi done } start() { [ -x $nginx ] || exit 5 [ -f $nginx_conf_file ] || exit 6 make_dirs echo -n $"starting $prog: " daemon $nginx -c $nginx_conf_file retval=$? echo [ $retval -eq 0 ] && touch $lockfile return $retval } stop() { echo -n $"stopping $prog: " killproc $prog -quit retval=$? echo [ $retval -eq 0 ] && rm -f $lockfile return $retval } restart() { #configtest || return $? stop sleep 1 start } reload() { #configtest || return $? echo -n $"reloading $prog: " killproc $nginx -hup retval=$? echo } force_reload() { restart } configtest() { $nginx -t -c $nginx_conf_file } rh_status() { status $prog } rh_status_q() { rh_status >/dev/null 2>&1 } case "$1" in start) rh_status_q && exit 0 $1 ;; stop) rh_status_q || exit 0 $1 ;; restart|configtest) $1 ;; reload) rh_status_q || exit 7 $1 ;; force-reload) force_reload ;; status) rh_status ;; condrestart|try-restart) rh_status_q || exit 0 ;; *) echo $"usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" exit 2 esac
為啟動腳本授權 并加入開機啟動
[root@localhost init.d]# chmod -r 777 /etc/rc.d/init.d/nginx [root@localhost init.d]# chkconfig nginx
啟動nginx
[root@localhost init.d]# ./nginx start
將nginx加入系統環境變量
[root@localhost init.d]# echo 'export path=$path:/usr/local/nginx/sbin'>>/etc/profile && source /etc/profile
nginx命令 [ service nginx (start|stop|restart) ]
[root@localhost init.d]# service nginx start starting nginx (via systemctl): [ 確定 ]
tips: 快捷命令
service nginx (start|stop|restart)
二、keepalived安裝和配置
1.安裝keepalived依賴環境
yum install -y popt-devel yum install -y ipvsadm yum install -y libnl* yum install -y libnf* yum install -y openssl-devel
2.編譯keepalived并安裝
[root@localhost keepalived-1.3.9]# ./configure [root@localhost keepalived-1.3.9]# make && make install
3.將keepalive 安裝成系統服務
[root@localhost etc]# mkdir /etc/keepalived [root@localhost etc]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
手動復制默認的配置文件到默認路徑
[root@localhost etc]# mkdir /etc/keepalived [root@localhost etc]# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ [root@localhost etc]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
為keepalived 創建軟鏈接
[root@localhost sysconfig]# ln -s /usr/local/keepalived/sbin/keepalived /usr/sbin/
設置keepalived開機自啟動
[root@localhost sysconfig]# chkconfig keepalived on 注意:正在將請求轉發到“systemctl enable keepalived.service”。 created symlink from /etc/systemd/system/multi-user.target.wants/keepalived.service to /usr/lib/systemd/system/keepalived.service
啟動keepalived服務
[root@localhost keepalived]# keepalived -d -f /etc/keepalived/keepalived.conf
關閉keepalived服務
[root@localhost keepalived]# killall keepalived
三、集群規劃和搭建
環境準備:
centos 7.2
keepalived version 1.4.0 - december 29, 2017
nginx version: nginx/1.12.2
tomcat version:8
集群規劃清單
虛擬機 | ip | 說明 |
---|---|---|
keepalived+nginx1[master] | 192.168.43.101 | nginx server 01 |
keeepalived+nginx[backup] | 192.168.43.102 | nginx server 02 |
tomcat01 | 192.168.43.103 | tomcat web server01 |
tomcat02 | 192.168.43.104 | tomcat web server02 |
vip | 192.168.43.150 | 虛擬漂移ip |
1.更改tomcat默認歡迎頁面,用于標識切換web
更改tomcatserver01 節點root/index.jsp 信息,加入tomcatip地址,并加入nginx值,即修改節點192.168.43.103信息如下:
<div id="asf-box"> <h1>${pagecontext.servletcontext.serverinfo}(192.168.224.103)<%=request.getheader("x-nginx")%></h1> </div>
更改tomcatserver02 節點root/index.jsp信息,加入tomcatip地址,并加入nginx值,即修改節點192.168.43.104信息如下:
<div id="asf-box"> <h1>${pagecontext.servletcontext.serverinfo}(192.168.224.104)<%=request.getheader("x-nginx")%></h1> </div>
2.啟動tomcat服務,查看tomcat服務ip信息,此時nginx未啟動,因此request-header沒有nginx信息。
3.配置nginx代理信息
1.配置master節點[192.168.43.101]代理信息
upstream tomcat { server 192.168.43.103:8080 weight=1; server 192.168.43.104:8080 weight=1; } server{ location / { proxy_pass http://tomcat; proxy_set_header x-nginx "nginx-1"; } #......其他省略 }
2.配置backup節點[192.168.43.102]代理信息
upstream tomcat { server 192.168.43.103:8080 weight=1; server 192.168.43.104:8080 weight=1; } server{ location / { proxy_pass http://tomcat; proxy_set_header x-nginx "nginx-2"; } #......其他省略 }
3.啟動master 節點nginx服務
[root@localhost init.d]# service nginx start starting nginx (via systemctl): [ 確定 ]
此時訪問 192.168.43.101 可以看到103和104節點tcomat交替顯示,說明nginx服務已經將請求負載到了2臺tomcat上。
4.同理配置backup[192.168.43.102] nginx信息,啟動nginx后,訪問192.168.43.102后可以看到backup節點已起到負載的效果。
4.配置keepalived 腳本信息
1. 在master節點和slave節點 /etc/keepalived目錄下添加check_nginx.sh 文件,用于檢測nginx的存貨狀況,添加keepalived.conf文件
check_nginx.sh文件信息如下:
#!/bin/bash #時間變量,用于記錄日志 d=`date --date today +%y%m%d_%h:%m:%s` #計算nginx進程數量 n=`ps -c nginx --no-heading|wc -l` #如果進程為0,則啟動nginx,并且再次檢測nginx進程數量, #如果還為0,說明nginx無法啟動,此時需要關閉keepalived if [ $n -eq "0" ]; then /etc/rc.d/init.d/nginx start n2=`ps -c nginx --no-heading|wc -l` if [ $n2 -eq "0" ]; then echo "$d nginx down,keepalived will stop" >> /var/log/check_ng.log systemctl stop keepalived fi fi
添加完成后,為check_nginx.sh 文件授權,便于腳本獲得執行權限。
[root@localhost keepalived]# chmod -r 777 /etc/keepalived/check_nginx.sh
2.在master 節點 /etc/keepalived目錄下,添加keepalived.conf 文件,具體信息如下:
vrrp_script chk_nginx { script "/etc/keepalived/check_nginx.sh" //檢測nginx進程的腳本 interval 2 weight -20 } global_defs { notification_email { //可以添加郵件提醒 } } vrrp_instance vi_1 { state master #標示狀態為master 備份機為backup interface ens33 #設置實例綁定的網卡(ip addr查看,需要根據個人網卡綁定) virtual_router_id 51 #同一實例下virtual_router_id必須相同 mcast_src_ip 192.168.43.101 priority 250 #master權重要高于backup 比如backup為240 advert_int 1 #master與backup負載均衡器之間同步檢查的時間間隔,單位是秒 nopreempt #非搶占模式 authentication { #設置認證 auth_type pass #主從服務器驗證方式 auth_pass 123456 } track_script { check_nginx } virtual_ipaddress { #設置vip 192.168.43.150 #可以多個虛擬ip,換行即可 } }
3.在backup節點 etc/keepalived目錄下添加 keepalived.conf 配置文件
信息如下:
vrrp_script chk_nginx { script "/etc/keepalived/check_nginx.sh" //檢測nginx進程的腳本 interval 2 weight -20 } global_defs { notification_email { //可以添加郵件提醒 } } vrrp_instance vi_1 { state backup #標示狀態為master 備份機為backup interface ens33 #設置實例綁定的網卡(ip addr查看) virtual_router_id 51 #同一實例下virtual_router_id必須相同 mcast_src_ip 192.168.43.102 priority 240 #master權重要高于backup 比如backup為240 advert_int 1 #master與backup負載均衡器之間同步檢查的時間間隔,單位是秒 nopreempt #非搶占模式 authentication { #設置認證 auth_type pass #主從服務器驗證方式 auth_pass 123456 } track_script { check_nginx } virtual_ipaddress { #設置vip 192.168.43.150 #可以多個虛擬ip,換行即可 } }
tips: 關于配置信息的幾點說明
state - 主服務器需配成master,從服務器需配成backup
interface - 這個是網卡名,我使用的是vm12.0的版本,所以這里網卡名為ens33
mcast_src_ip - 配置各自的實際ip地址
priority - 主服務器的優先級必須比從服務器的高,這里主服務器配置成250,從服務器配置成240
virtual_ipaddress - 配置虛擬ip(192.168.43.150)
authentication - auth_pass主從服務器必須一致,keepalived靠這個來通信
virtual_router_id - 主從服務器必須保持一致
5.集群高可用(ha)驗證
step1 啟動master機器的keepalived和 nginx服務
[root@localhost keepalived]# keepalived -d -f /etc/keepalived/keepalived.conf [root@localhost keepalived]# service nginx start
查看服務啟動進程
[root@localhost keepalived]# ps -aux|grep nginx root 6390 0.0 0.0 20484 612 ? ss 19:13 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf nobody 6392 0.0 0.0 23008 1628 ? s 19:13 0:00 nginx: worker process root 6978 0.0 0.0 112672 968 pts/0 s+ 20:08 0:00 grep --color=auto nginx
查看keepalived啟動進程
[root@localhost keepalived]# ps -aux|grep keepalived root 6402 0.0 0.0 45920 1016 ? ss 19:13 0:00 keepalived -d -f /etc/keepalived/keepalived.conf root 6403 0.0 0.0 48044 1468 ? s 19:13 0:00 keepalived -d -f /etc/keepalived/keepalived.conf root 6404 0.0 0.0 50128 1780 ? s 19:13 0:00 keepalived -d -f /etc/keepalived/keepalived.conf root 7004 0.0 0.0 112672 976 pts/0 s+ 20:10 0:00 grep --color=auto keepalived
使用 ip add 查看虛擬ip綁定情況,如出現192.168.43.150 節點信息則綁定到master節點
[root@localhost keepalived]# ip add 1: lo: <loopback,up,lower_up> mtu 65536 qdisc noqueue state unknown qlen 1 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 qlen 1000 link/ether 00:0c:29:91:bf:59 brd ff:ff:ff:ff:ff:ff inet 192.168.43.101/24 brd 192.168.43.255 scope global ens33 valid_lft forever preferred_lft forever inet 192.168.43.150/32 scope global ens33 valid_lft forever preferred_lft forever inet6 fe80::9abb:4544:f6db:8255/64 scope link valid_lft forever preferred_lft forever inet6 fe80::b0b3:d0ca:7382:2779/64 scope link tentative dadfailed valid_lft forever preferred_lft forever inet6 fe80::314f:5fe7:4e4b:64ed/64 scope link tentative dadfailed valid_lft forever preferred_lft forever 3: virbr0: <no-carrier,broadcast,multicast,up> mtu 1500 qdisc noqueue state down qlen 1000 link/ether 52:54:00:2b:74:aa brd ff:ff:ff:ff:ff:ff inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0 valid_lft forever preferred_lft forever 4: virbr0-nic: <broadcast,multicast> mtu 1500 qdisc pfifo_fast master virbr0 state down qlen 1000 link/ether 52:54:00:2b:74:aa brd ff:ff:ff:ff:ff:ff
step 2 啟動backup節點nginx服務和keepalived服務,查看服務啟動情況,如backup節點出現了虛擬ip,則keepalvied配置文件有問題,此情況稱為腦裂。
[root@localhost keepalived]# clear [root@localhost keepalived]# ip add 1: lo: <loopback,up,lower_up> mtu 65536 qdisc noqueue state unknown qlen 1 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 qlen 1000 link/ether 00:0c:29:14:df:79 brd ff:ff:ff:ff:ff:ff inet 192.168.43.102/24 brd 192.168.43.255 scope global ens33 valid_lft forever preferred_lft forever inet6 fe80::314f:5fe7:4e4b:64ed/64 scope link valid_lft forever preferred_lft forever 3: virbr0: <no-carrier,broadcast,multicast,up> mtu 1500 qdisc noqueue state down qlen 1000 link/ether 52:54:00:2b:74:aa brd ff:ff:ff:ff:ff:ff inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0 valid_lft forever preferred_lft forever 4: virbr0-nic: <broadcast,multicast> mtu 1500 qdisc pfifo_fast master virbr0 state down qlen 1000 link/ether 52:54:00:2b:74:aa brd ff:ff:ff:ff:ff:ff
step 3 驗證服務
瀏覽并多次強制刷新地址: http://192.168.43.150 ,可以看到103和104多次交替顯示,并顯示nginx-1,則表明 master節點在進行web服務轉發。
step 4 關閉master keepalived服務和nginx服務,訪問web服務觀察服務轉移情況
[root@localhost keepalived]# killall keepalived [root@localhost keepalived]# service nginx stop
此時強制刷新192.168.43.150發現 頁面交替顯示103和104并顯示nginx-2 ,vip已轉移到192.168.43.102上,已證明服務自動切換到備份節點上。
step 5 啟動master keepalived 服務和nginx服務
此時再次驗證發現,vip已被master重新奪回,并頁面交替顯示 103和104,此時顯示nginx-1
四、keepalived搶占模式和非搶占模式
keepalived的ha分為搶占模式和非搶占模式,搶占模式即master從故障中恢復后,會將vip從backup節點中搶占過來。非搶占模式即master恢復后不搶占backup升級為master后的vip。
非搶占模式配置:
1> 在vrrp_instance塊下兩個節點各增加了nopreempt指令,表示不爭搶vip
2> 節點的state都為backup 兩個keepalived節點都啟動后,默認都是backup狀態,雙方在發送組播信息后,會根據優先級來選舉一個master出來。由于兩者都配置了nopreempt,所以master從故障中恢復后,不會搶占vip。這樣會避免vip切換可能造成的服務延遲。
以上就是關于“如何用Keepalived+Nginx+Tomcat實現高可用Web集群”這篇文章的內容,相信大家都有了一定的了解,希望小編分享的內容對大家有幫助,若想了解更多相關的知識內容,請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。