keepalived+lVS(dr)高可用集群
规划
服务器名称 | IP | 描述 |
---|---|---|
master keepalived+lvs | VIP:192.168.238.100 DIP:192.168.238.151 | keepalived的master节点和lvs负载均衡 |
backup keepalived+lvs | VIP:192.168.238.100 DIP:192.168.238.152 | keepalived的备份节点和lvs负载均衡 |
server1 | VIP:192.168.238.100 RIP:192.168.238.153 | web服务器 |
server2 | VIP:192.168.238.100 RIP:192.168.238.154 | web服务器 |
配置主机名
sh
hostnamectl set-hostname master #151执行
hostnamectl set-hostname backup #152执行
hostnamectl set-hostname server1 #153执行
hostnamectl set-hostname server2 #154执行
四台机器都配置dns还有 关闭防火墙、关闭selinux
sh
cat > /etc/hosts << EOF
192.168.238.151 master
192.168.238.152 backup
192.168.238.153 server1
192.168.238.154 server2
EOF
#关闭防火墙
systemctl disable firewalld --now
#关闭selinux
sed -i '/^SELINUX/s/enforcing/disabled/g' /etc/selinux/config
setenforce 0
四台机器都配置时间同步
sh
yum -y install ntpdate bash-completion* net-tools wget
crontab -e
*/1 * * * * /usr/sbin/ntpdate ntp.aliyun.com
151和152安装keepalived 和ipvsadm
sh
yum -y install keepalived
cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
yum -y install ipvsadm
151执行
sh
cat > /etc/keepalived/keepalived.conf << EOF
global_defs {
router_id LVS_master #主和从要有单独的route_id
}
vrrp_instance VI_1 {
state MASTER #主为MASTER 从为BACKUP
interface ens33 #指定配机网卡的设备名
virtual_router_id 51 #指定虚拟路由主从要一致
priority 100 #指定优先级主优先高 从做先级低
advert_int 1 #心跳检测时间间隔
authentication { #keepalived内通信认证方式
auth_type PASS
auth_pass 1111
}
virtual_ipaddress { #指定绑定的vip
192.168.238.100
}
}
EOF
152执行
sh
cat > /etc/keepalived/keepalived.conf << EOF
global_defs {
router_id LVS_backup #主和从要有单独的route_id
}
vrrp_instance VI_1 {
state BACKUP #主为MASTER 从为BACKUP
interface ens33 #指定配机网卡的设备名
virtual_router_id 51 #指定虚拟路由主从要一致
priority 80 #指定优先级主优先高 从做先级低
advert_int 1 #心跳检测时间间隔
authentication { #keepalived内通信认证方式
auth_type PASS
auth_pass 1111
}
virtual_ipaddress { #指定绑定的vip
192.168.238.100
}
}
EOF
151和152启动keepalived
sh
systemctl start keepalived
151和152查看系统是否支持ipvs
sh
lsmod |grep ip_vs
151和152配置LVS负载均衡
sh
ipvsadm -C
ipvsadm -A -t 192.168.238.100:80 -s rr
ipvsadm -a -t 192.168.238.100:80 -r 192.168.238.153:80 -g
ipvsadm -a -t 192.168.238.100:80 -r 192.168.238.154:80 -g
server1和server2安装httpd
yum -y install httpd
echo "server1" > /var/www/html/index.html #server1执行
echo "server2" > /var/www/html/index.html #server2执行
systemctl start httpd
server1和server2编写配置虚拟IP脚本 vim lvs.sh
sh
#!/bin/bash
#description : start realserver
VIP=192.168.238.100
case "$1" in
start)
echo " start LVS of REALServer"
/usr/sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up
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
;;
stop)
/usr/sbin/ifconfig lo:0 down
echo "close LVS Directorserver"
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 "Usage: $0 {start|stop}"
exit 1
esac
执行
sh
source lvs.sh start
在152上测试