您好,登錄后才能下訂單哦!
注:測試環境因此nginx和Apache都部署在同一臺real server上
Master和backup和real server服務器公用。
VIP |
172.20.0.210 |
Master |
172.20.0.22 |
Backup |
172.20.0.25 |
Real_server |
172.20.0.22 |
Real_server |
172.20.0.25 |
Web :nginx |
port:8220 |
Web :Apache |
port:8221 |
創建相應的目錄
[root@SALT1 ~]# mkdir software [root@SALT1 ~]# cd software/ |
下載安裝包:
相應的命令為:
wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz
查看內核參數
[root@SALT1 software]# uname -a Linux SALT1 2.6.32-431.el6.x86_64 #1 SMP Sun Nov 10 22:19:54 EST 2013 x86_64 x86_64 x86_64 GNU/Linux |
創建軟連接,不創建此鏈接,ipvsadm編譯安裝不過去。
[root@SALT1 software]# ln -s /usr/src/kernels/2.6.32-431.el6.x86_64/ /usr/src/linux |
編譯安裝ipvsadm
[root@SALT1 software]# ls ipvsadm-1.24.tar.gz [root@SALT1 software]# tar -xvf ipvsadm-1.24.tar.gz ipvsadm-1.24/config_stream.c ipvsadm-1.24/config_stream.h ……. [root@SALT1 software]# cd ipvsadm-1.24 [root@SALT1 ipvsadm-1.24]# make && make install make -C libipvs make[1]: Entering directory `/root/software/ipvsadm-1.24/libipvs' gcc -Wall -Wunused -Wstrict-prototypes -g -O2 -I/usr/src/linux/include -DHAVE_NET_IP_VS_H …….. |
加載LVS的 IP_VS模塊
[root@ccb log]# ipvsadm IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn [root@ccb log]# lsmod |grep ip_vs ip_vs 115643 0 libcrc32c 1246 1 ip_vs ipv6 321422 31 ip_vs |
下載安裝包
相應的命令為:
[root@SALT1 software]# wget http://www.keepalived.org/software/keepalived-1.1.19.tar.gz --2016-07-13 14:09:03-- http://www.keepalived.org/software/keepalived-1.1.19.tar.gz Resolving www.keepalived.org... 37.59.63.157, 2001:41d0:8:7a9d::1 Connecting to www.keepalived.org|37.59.63.157|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 241437 (236K) [application/x-gzip] Saving to: “keepalived-1.1.19.tar.gz”
100%[===================================================================================================================>] 241,437 190K/s in 1.2s
2016-07-13 14:09:06 (190 KB/s) - “keepalived-1.1.19.tar.gz” saved [241437/241437]
|
編譯安裝keepalived
[root@SALT1 software]# tar -xvf keepalived-1.1.19.tar.gz keepalived-1.1.19/ keepalived-1.1.19/config.log keepalived-1.1.19/configure keepalived-1.1.19/README keepalived-1.1.19/ChangeLog keepalived-1.1.19/Makefile.in ……..
[root@SALT1 software]# cd keepalived-1.1.19 [root@SALT1 keepalived-1.1.19]# ./configure --prefix=/usr/local/keepalived checking for gcc... gcc …… [root@SALT1 keepalived-1.1.19]# [root@SALT1 keepalived-1.1.19]# make make -C lib || exit 1; make[1]: Entering directory `/root/software/keepalived-1.1.19/lib' gcc -g -O2 -I. -Wall -Wunused -Wstrict-prototypes -c memory.c gcc -g -O2 -I. -Wall -Wunused -Wstrict-prototypes -c utils.c …… Make complete [root@SALT1 keepalived-1.1.19]# make install make -C keepalived install make[1]: Entering directory `/root/software/keepalived-1.1.19/keepalived' install -d /usr/local/keepalived/sbin …… |
創建相應的目錄
[root@ccb keepalived-1.1.19]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/ [root@ccb keepalived-1.1.19]# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ [root@ccb keepalived-1.1.19]# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/ [root@ccb keepalived-1.1.19]# mkdir /etc/keepalived/ [root@ccb keepalived-1.1.19]#cd [root@ccb ~]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/ |
至此lvskeepalived安裝完成。
[root@localhost ~]# vi /etc/keepalived/keepalived.conf #global define global_defs { router_id node-A }
vrrp_sync_group test { group { test } }
vrrp_instance test { state MASTER interface eth2 lvs_sync_daemon_interface eth2 virtual_router_id 50 priority 180 advert_int 3 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 172.20.0.210 } } virtual_server 172.20.0.210 8220 { delay_loop 3 lb_algo rr lb_kind DR persistence_timeout 1 protocol TCP
real_server 172.20.0.22 8220 { weight 1 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 connect_port 8220 } } real_server 172.20.0.25 8220 { weight 1 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 connect_port 8220 } }
} virtual_server 172.20.0.210 8221 { delay_loop 3 lb_algo rr lb_kind DR persistence_timeout 1 protocol TCP real_server 172.20.0.25 8221 { weight 1 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 connect_port 8221 } } real_server 172.20.0.22 8221 { weight 1 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 connect_port 8221 } } } |
[root@localhost ~]# vi /etc/keepalived/keepalived.conf #global define global_defs { router_id node-B }
vrrp_sync_group test { group { test } }
vrrp_instance test { state BACKUP interface eth2 lvs_sync_daemon_interface eth2 virtual_router_id 50 priority 150 advert_int 3 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 172.20.0.210 } } virtual_server 172.20.0.210 8220 { delay_loop 3 lb_algo rr lb_kind DR persistence_timeout 1 protocol TCP
real_server 172.20.0.22 8220 { weight 1 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 connect_port 8220 } } real_server 172.20.0.25 8220 { weight 1 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 connect_port 8220 } }
} virtual_server 172.20.0.210 8221 { delay_loop 3 lb_algo rr lb_kind DR persistence_timeout 1 protocol TCP real_server 172.20.0.25 8221 { weight 1 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 connect_port 8221 } } real_server 172.20.0.22 8221 { weight 1 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 connect_port 8221 } } } |
確認啟動nginx和apache:
[root@localhost sbin]# lsof -i:8220 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME nginx 22005 root 6u IPv4 86789 0t0 TCP *:8220 (LISTEN) nginx 22006 nobody 6u IPv4 86789 0t0 TCP *:8220 (LISTEN) [root@localhost sbin]# lsof -i:8221 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME httpd 22012 root 4u IPv6 86820 0t0 TCP *:8221 (LISTEN) httpd 22015 apache 4u IPv6 86820 0t0 TCP *:8221 (LISTEN) httpd 22016 apache 4u IPv6 86820 0t0 TCP *:8221 (LISTEN) httpd 22017 apache 4u IPv6 86820 0t0 TCP *:8221 (LISTEN) httpd 22018 apache 4u IPv6 86820 0t0 TCP *:8221 (LISTEN) httpd 22019 apache 4u IPv6 86820 0t0 TCP *:8221 (LISTEN) httpd 22020 apache 4u IPv6 86820 0t0 TCP *:8221 (LISTEN) httpd 22021 apache 4u IPv6 86820 0t0 TCP *:8221 (LISTEN) httpd 22022 apache 4u IPv6 86820 0t0 TCP *:8221 (LISTEN) |
在兩臺real_server上分別創建lvsDR_node腳本:
[root@localhost ~]# vi /etc/init.d/lvsDR_node #!/bin/bash
# descript : start real server DR
VIP=172.20.0.210
. /etc/rc.d/init.d/functions
case "$1" in
start)
echo "start LVS of RealServer DR"
/sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up
echo "1" > /proc/sys/net/ipv4/conf/all/arp_ignore
echo "2" > /proc/sys/net/ipv4/conf/all/arp_announce
;;
stop)
/sbin/ifconfig lo:0 down
echo "close LVS of RealServer DR"
echo "0" > /proc/sys/net/ipv4/conf/all/arp_ignore
echo "0" > /proc/sys/net/ipv4/conf/all/arp_announce
;;
*)
echo "Usage : $0 {start|stop}"
exit 1
esac |
分別啟動master和backup服務器上面的keepalived:
[root@localhost ~]# /etc/init.d/keepalived start Starting keepalived: [ OK ] [root@localhost ~]# |
啟動real_server上面的lvsDR_node
[root@localhost ~]# /etc/init.d/lvsDR_node start start LVS of RealServer DR |
Master:
backup
現在VIP是在master服務器(172.20.0.22)服務器上,為了確保web應用切換之后應用也能正常訪問,將master服務器上的nginx和apache停掉。:
可以看到22服務器上nginx和Apache都不能訪問了
Nginx:
Apache:
查看狀態:
使用VIP訪問:
如上圖可以看到web應用切換之后web應用依然可以通過VIP正常訪問。
切換VIP
Master:
Backup
Nginx驗證:
Apache驗證:
注:當master上的keepalived再次啟動,VIP又會立刻切換到master上,而backup會自動切換為backup state。
如圖:
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。