注:测试环境因此nginx和Apache都部署在同一台real server上
创新互联网站建设公司,提供网站设计制作、网站设计,网页设计,建网站,PHP网站建设等专业做网站服务;可快速的进行网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,是专业的做网站团队,希望更多企业前来合作!
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。
如图: