在这个高度信息化的IT时代,企业的生产系统、业务运营、销售和支持、以及日常管理等环节越来越依赖于计算机信息和服务,使得对高可用(HA)技术的应用需求大量上升,以便提供持续的、不间断的计算机系统或网络服务。
专为LVS和HA设计的一款健康检查工具:
原理剖析:
VRRP(虚拟路由冗余协议)是针对路由器的一种备份解决方案
Keepalived备份服务器的配置与master的配置有三个选项不同:
测试双机热备的效果:
执行的测试:
(1)LVS负载调度服务器两台
(2)Web站点服务器两台
(3)NFS共享服务器一台
(4)客户机一台,用于测试验证
注意:需确保相同网段之间能互相通信
[root@nfs ~]# rpm -qa | grep rpcbind //默认虚拟机已安装rpcbind模块
rpcbind-0.2.0-42.el7.x86_64
[root@nfs ~]# yum -y install nfs-utils //确认是否安装nfs-utils软件包
已加载插件:fastestmirror, langpacks
base | 3.6 kB 00:00
Loading mirror speeds from cached hostfile
* base:
软件包 1:nfs-utils-1.3.0-0.48.el7.x86_64 已安装并且是最新版本
无须任何处理
[root@nfs ~]# mkdir /opt/web1
[root@nfs ~]# mkdir /opt/web2
[root@nfs ~]# echo "this is web1.
" > /opt/web1/index.html
[root@nfs ~]# echo "this is web2.
" > /opt/web2/index.html
[root@nfs ~]# vi /etc/exports
/opt/web1 192.168.70.11/32 (ro)
/opt/web2 192.168.70.12/32 (ro)
[root@nfs ~]# systemctl restart nfs
[root@nfs ~]# systemctl restart rpcbind
[root@nfs ~]# showmount -e
Export list for nfs:
/opt/web2 (everyone)
/opt/web1 (everyone)
[root@web1 ~]# yum -y install httpd
[root@web1 ~]# showmount -e 192.168.70.13
Export list for 192.168.70.13:
/opt/web2 (everyone)
/opt/web1 (everyone)
[root@web1 ~]# mount 192.168.70.13:/opt/web1 /var/www/html
[root@web1 ~]# systemctl restart httpd
[root@web1 ~]# netstat -anpt | grep httpd
tcp6 0 0 :::80 :::* LISTEN 55954/httpd
[root@web1 ~]# vi web1.sh
#!/bin/bash
#lvs_dr模式 web1
ifconfig lo:0 192.168.70.200 broadcast 192.168.70.200 netmask 255.255.255.255 up
route add -host 192.168.70.200 dev lo:0
echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
sysctl -p &>/dev/null
[root@web1 ~]# sh web1.sh
[root@web1 ~]# ifconfig //查看虚拟端口
[root@web1 ~]# route -n //查看路由
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.70.10 0.0.0.0 UG 100 0 0 ens33
192.168.70.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
192.168.70.200 0.0.0.0 255.255.255.255 UH 0 0 0 lo
192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
[root@web2 ~]# yum -y install httpd
[root@web2 ~]# mount 192.168.70.13:/opt/web2 /var/www/html
[root@web2 ~]# systemctl start httpd
[root@web2 ~]# netstat -anpt | grep httpd
tcp6 0 0 :::80 :::* LISTEN 54695/httpd
[root@web2 ~]# vi web2.sh
#!/bin/bash
#lvs_dr模式 web2
ifconfig lo:0 192.168.70.200 broadcast 192.168.70.200 netmask 255.255.255.255 up
route add -host 192.168.70.200 dev lo:0
echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
sysctl -p &>/dev/null
[root@web2 ~]# sh web2.sh
[root@web2 ~]# ifconfig //查看虚拟端口
[root@web2 ~]# route -n //查看路由
kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.70.10 0.0.0.0 UG 100 0 0 ens33
192.168.70.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
192.168.70.200 0.0.0.0 255.255.255.255 UH 0 0 0 lo
192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
导入keepalived软件包
1)在LVS两台调度器上进行以下相同的配置
[root@lvs ~]# modprobe ip_vs '//加载ip_vs模块'
[root@lvs ~]# cat /proc/net/ip_vs '//查看ip_vs版本信息'
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
[root@lvs ~]# yum -y install ipvsadm
[root@lvs ~]# yum -y install gcc gcc-c++ make popt-devel kernel-devel openssl-devel
[root@lvs ~]# tar zxvf keepalived-2.0.13.tar.gz
[root@lvs ~]# cd keepalived-2.0.13/
[root@lvs keepalived-2.0.13]# ./configure --prefix=/
[root@lvs keepalived-2.0.13]# make && make install
[root@lvs keepalived-2.0.13]# cp keepalived/etc/init.d/keepalived /etc/init.d/
[root@lvs keepalived-2.0.13]# systemctl enable keepalived.service
Created symlink from /etc/systemd/system/multi-user.target.wants/keepalived.service to /usr/lib/systemd/system/keepalived.service.
2)在LVS_1上的配置
[root@lvs_1 ~]# vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id lvs_1
}
vrrp_instance vi_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 110
advert int 1
authentication {
auth_type PASS
auth_pass 6666
}
virtual_ipaddress {
192.168.70.200
}
}
virtual_server 192.168.70.200 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 6
protocol TCP
real_server 192.168.70.11 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.70.12 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
[root@lvs_1 ~]# systemctl start keepalived.service
[root@lvs_1 ~]# ipaddr
[root@lvs_1 ~]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.70.200:80 rr
-> 192.168.70.11:80 Route 1 0 0
-> 192.168.70.12:80 Route 1 0 0
3)在LVS_2上的配置
[root@lvs_2 ~]# vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id lvs_2
}
vrrp_instance vi_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 105
......//其他的和LVS_1配置相同
[root@lvs_2 ~]# systemctl start keepalived.service
[root@lvs_2 ~]# systemctl status keepalived.service
[root@lvs_2 ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.70.200:80 rr persistent 6
-> 192.168.70.11:80 Route 1 0 0
-> 192.168.70.12:80 Route 1 0 0
[root@lvs_1 ~]# systemctl stop keepalived.service