Keepalived搭建LVS高可用性集群系统
! Configuration File for keepalived global_defs { notification_email { acassen@firewall.loc failover@firewall.loc sysadmin@firewall.loc } notification_email_from Alexandre.Cassen@firewall.loc smtp_server 192.168.200.1 smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_instance VI_1 { state MASTER interface enp0s3 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 5211 } virtual_ipaddress { 192.168.1.200 } } virtual_server 192.168.1.200 80 { # 虚拟服务器ip地址和port delay_loop 6 # 设置健康检查时间 单位秒 lb_algo rr # 设置调度算法 lb_kind DR # 设置实现负载均衡机制 有 NAT、TUN、DR三种 persistence_timeout 50 #会话保持时间,当一个用户50秒内没有进行操作,下次的访问操作才会分发到其它节点 protocol TCP # 指定转发协议 TCP/UDP real_server 192.168.1.227 80 { # 配置服务节点,真实服务器ip地址和port weight 3 # 权重
TCP_CHECK { # real server 的状态检查部分
connect_port 80 # 连接端口 80
connect_timeout 3 # 表示3秒无响应超时
retry 3 # 表示重试次数
delay_before_retry 3 # 表示重试间隔
}
}
real_server 192.168.1.219 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
retry 3
delay_before_retry 3
}
}
}
real server
yum install -y ipvsadm
服务启动脚本,此操作是在回环设备上绑定了一个虚拟IP地址,并设定其子网掩码为255.255.255.255 与Director Server上的虚拟IP保持互通,然后禁用了本机的ARP请求
vim /etc/init.d/lvsrs
#!/bin/bash #description: start real server VIP=192.168.1.200 /etc/rc.d/init.d/functions case "$1" in start) # echo " start LVS of Real Server !" 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) #echo " Stop LVS of Real Server !" 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 Stoped" ;; *) echo "Usage: $0 {start|stop}" exit 1 esac exit 0
测试
real server
chmod u+x /etc/init.d/lvsrs
service lvsrs [start|stop]
director server
systemctl start keepalived.service tail -f /var/log/messages