企业应用中,单台服务器承担应用存在单点故障的危险
单点故障一旦发生,企业服务将发生中断,造成极大的危害
专为LVS和HA设计的一款健康检查] C具
支持故障自动切换(Failover)
支持节 点健康状态检查(Health Checking)
官方网站: http://www.keepalived.org/
1、Keepalived采用VRRP热备份协议
实现Linux服务器的多机热备功能
2、VRRP (虚拟路由冗余协议) 是针对路由器的一种备份解决方案
由多台路由器组成-一个热备组, 通过共用的虚拟IP地址对外提供服务
每个热备组内同时只有-台主路由器提供服务,其他路由器处于冗余状态
若当前在线的路由器失效,则其他路由器会根据设置的优先级自动接替虚拟IP地址,继续提供服务
3、Keepalived可实现多机热备,每个热备组可有多台服务器
案例:负载均衡+高可用群集 1、Keepalived的设计目标是构建高可用的LVS负载均衡群集,可以调用ipvsadm工具来创建虚拟服务器、管理服务器池,而不仅仅用作双机热备 2、使用Keepalived构建LVS群集更加简便易用 3、主要优势 对LVS负载调度器实现热备切换,提高可用性 对服务器池中的节点进行健康检查,自动移除失效节点,恢复后再重新加入
案例拓扑 ■在基于LVS+Keepalived实现的LVS群集结构中,至少包括两台热备的负载调度器,三台以上的节点服务器
五台机器都采用仅主机模式
DR1:192.168.100.128
DR2:192.168.100.129
WEB1:192.168.100.201
WEB2:192.168.100.202
client:192.168.100.20
setenforce 0
iptables -F
yum install keepalived ipvsadm -y
vim /etc/sysctl.conf
net.ipv4.ip_forward=1 ##proc响应关闭重定向功能 net.ipv4.conf.all.send_redirects=0 net.ipv4.conf.default.send_redirects=0 net.ipv4.conf.ens33.send_redirects=0
sysctl -p ##使配置文件生效
##DR1网卡配置文件
cd /etc/sysconfig/network-scripts/
cp -p ifcfg-ens33 ifcfg-ens33:0
vim ifcfg-ens33
vim ifcfg-ens33:0
systemctl restart network ##编辑完成后重启网卡
cd /etc/sysconfig/network-scripts/
cp -p ifcfg-ens33 ifcfg-ens33:0
vim ifcfg-ens33
vim ifcfg-ens33:0
systemctl restart network ##编辑完成后重启网卡
cd /etc/init.d/
vi dr.sh
#!/bin/bash GW=192.168.100.1 VIP=192.168.100.10 RIP1=192.168.100.201 RIP2=192.168.100.202 case “$1” in start) /sbin/ipvsadm --save > /etc/sysconfig/ipvsadm systemctl start ipvsadm /sbin/ifconfig ens33:0 $VIP netmask 255.255.255.255 broadcast $VIP up /sbin/route add -host $VIP dev ens33:0 /sbin/ipvsadm -A -t $VIP:80 -s rr /sbin/ipvsadm -a -t $VIP:80 -r $RIP1:80 -g /sbin/ipvsadm -a -t $VIP:80 -r $RIP2:80 -g echo “ipvsadm starting-------------------[ok]” ;; stop) /sbin/ipvsadm -C systemctl stop ipvsadm ifconfig ens33:0 down route del $VIP echo “ipvsamd stoped---------------------[ok]” ;; status) if [!-e /var/lock/subsys/ipvsadm ];then echo “ipvsadm stoped------------------------” exit 1 else echo “ipvsamd Runing---------------------[ok]” fi ;; *) echo “Usage:$0{start|stop|status}” exit 1 esac exit 0
chmod +x dr.sh ##添加文件的执行权限 service dr.sh start ##启动文件
yum install httpd -y
cd /etc/sysconfig/network-scripts/
cp -p ifcfg-lo ifcfg-lo:0
vim ifcfg-lo:0
DEVICE=lo:0 IPADDR=192.168.100.10 NETMASK=255.255.255.0 ONBOOT=yes
vim ifcfg-ens33
TYPE=“Ethernet” PROXY_METHOD=“none” BROWSER_ONLY=“no” BOOTPROTO=“static” DEFROUTE=“yes” IPV4_FAILURE_FATAL=“no” IPV6INIT=“yes” IPV6_AUTOCONF=“yes” IPV6_DEFROUTE=“yes” IPV6_FAILURE_FATAL=“no” IPV6_ADDR_GEN_MODE=“stable-privacy” NAME=“ens33” DEVICE=“ens33” UUID=“c6759283-4ba1-4bb4-88c8-8edc352c2017” ONBOOT=“yes” IPADDR=“192.168.100.201” NETMASK=“255.255.255.0” GATEWAY=“192.168.100.1”
systemctl restart network ##重启网卡
cd /etc/init.d/
vim web.sh
#!/bin/bash VIP=192.168.100.10 case “$1” in start) ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $VIP /sbin/route add -host $VIP dev lo:0 echo “1” > /proc/sys/net/ipv4/conf/lo/arp_ignore echo “2” > /proc/sys/net/ipv4/conf/lo/arp_announce echo “1” > /proc/sys/net/ipv4/conf/all/arp_ignore echo “2” > /proc/sys/net/ipv4/conf/all/arp_announce sysctl -p > /dev/null 2>&1 echo “RealServer Start OK” ;; stop) ifconfig lo:0 down route del $VIP /dev/null 2>&1 echo “0” > /proc/sys/net/ipv4/conf/lo/arp_ignore echo “0” > /proc/sys/net/ipv4/conf/lo/arp_announce echo “0” > /proc/sys/net/ipv4/conf/all/arp_ignore echo “0” > /proc/sys/net/ipv4/conf/all/arp_announce echo “RealServer Stopd” ;; *) echo “Usage:$0{start|stop}” exit 1 esac exit 0
chmod +x web.sh
service web.sh start
cd /var/www/html/
<h1>this is kgc web!</h1> ##web1主界面中内容
<h1>this is test web!</h1> ##web2主界面中内容
systemctl restart httpd ##重启服务
cd /etc/keepalived/
vim keepalived.conf
global_defs{
…
smtp_server 127.0.0.1 ##指向本地
router_id LVS_01 ##指定名称,备份服务器不同名称
…
}
vrrp_instance VI_1 { state MASTER ##备份服务是BACKUP virtual_router_id 10 ##组号相同 priority 100 ##优先级备份小于主 … authentication { auth_type PASS auth_pass 1111 ##密码信息不用修改 } virtual_ipaddress { 192.168.100.10 ##虚拟地址 } }
virtual_server 192.168.100.10 80 { … lb_kind DR ##LVS模式 …
real_server 192.168.100.201 80 { weight 1 TCP_CHECK{ connect_port 80 ##添加端口 connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } real_server 192.168.100.202 80 { weight 1 TCP_CHECK{ connect_port 80 connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } }
}
scp keepalived.conf root@192.168.100.129:/etc/keepalived/
vim keepalived.conf ##另一台调度主机配置文件修改内容如下所示
systemctl start keepalived.service
service network restart
Restarting network (via systemctl): Job for network.service failed. See ‘systemctl status network.service’ and ‘journalctl -xn’ for details.
[root@localhost ~]# systemctl status network.service
network.service - LSB: Bring up/down networking
Loaded: loaded (/etc/rc.d/init.d/network)
Active: failed (Result: exit-code) since三 2014-11-05 15:30:10 CST; 1min 5s ago
11月 05 15:30:10 localhost.localdomain network[2920]: RTNETLINK answers: File exists
11月 05 15:30:10 localhost.localdomain network[2920]: RTNETLINK answers: File exists
11月 05 15:30:10 localhost.localdomain network[2920]: RTNETLINK answers: File exists
11月 05 15:30:10 localhost.localdomain network[2920]: RTNETLINK answers: File exists
11月 05 15:30:10 localhost.localdomain network[2920]: RTNETLINK answers: File exists
11月 05 15:30:10 localhost.localdomain network[2920]: RTNETLINK answers: File exists
11月 05 15:30:10 localhost.localdomain network[2920]: RTNETLINK answers: File exists
11月 05 15:30:10 localhost.localdomain systemd[1]: network.service: control process exited, code=exited status=1
11月 05 15:30:10 localhost.localdomain systemd[1]: *Failed to start LSB: Bring up/down networking.*
11月 05 15:30:10 localhost.localdomain systemd[1]: Unit network.service entered failed state.
systemctl stop NetworkManager
systemctl enable NetworkManager
systemctl start NetworkManager
service network restart
##把这个ip判断注释掉 可以通过 /arping快速定位
vim /etc/sysconfig/network-scripts/ifup-eth
service network restart