keepalived+lvs+nginx高可用集群搭建

ip主机名备注
192.168.174.128master关闭防火墙、安全规则
192.168.174.129backup关闭防火墙、安全规则
192.168.174.130ecshop搭建好ecshop
192.168.174.131ecshop1搭建好ecshop1

keepalived主备搭建参考
keepalived邮件告警参考

四台虚拟机,两台搭建keepalived主备切换,两台搭建ecshop商城

修改keepalived的配置文件(主备都要)

[root@master ~]# vi /etc/keepalived/keepalived.conf
global_defs {
   notification_email {
     root@localhost
   }
   notification_email_from root@aminglinux.com
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_script chk_nginx {
    script "/usr/local/sbin/check_ng.sh"
    interval 3
}

vrrp_instance VI_1 {
    state MASTER
    interface eno16777736
    virtual_router_id 79
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass admin
    }
    virtual_ipaddress {
        192.168.174.100
    }

    track_script {
        chk_nginx
    }
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"

}
##########添加以下,主备参数一样就行
virtual_server 192.168.174.100 80 {		//配置一个VIP 工作在TCP的80端口上
    delay_loop 6			//刷新页面的间隔单位s
    lb_algo rr				//负载均衡的调度算法
    lb_kind DR				//工作在DR模型上
    nat_mask 255.255.255.0
    #persistence_timeout 50		//是否启用IPVS持久连接,这项要注销,否则连接50S才会切换
    protocol TCP			//采用的是TCP协议

    real_server 192.168.174.130 80 {		//vip地址
        weight 1							//权重
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }

    real_server 192.168.174.131 80 {		//vip地址
        weight 1							//权重
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}

在两台ecshop上执行脚本

[root@db01 ~]# cat /usr/local/sbin/vip_access.sh 
#!/bin/bash
# Written by NetSeek
# description: Config realserver lo and apply noarp

WEB_VIP="192.168.174.100"
./etc/rc.d/init.d/functions
case "$1" in
start)
ifconfig lo:0 $WEB_VIP netmask 255.255.255.255 broadcast $WEB_VIP
/sbin/route add -host $WEB_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 $WEB_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"
       ;;
status)
        # Status of LVS-DR real server.
        islothere=`/sbin/ifconfig lo:0 | grep $WEB_VIP`
        isrothere=`netstat -rn | grep "lo:0" | grep $web_VIP`
        if [ ! "$islothere" -o ! "isrothere" ]
        then
            echo "LVS-DR real server Stopped."
        else
            echo "LVS-DR Running."
        fi
        ;;
*)

echo "$0: Usage: $0 {start|status|stop}"
exit 1
;;
esac
exit 0

[root@master ~]# chmod +x /usr/local/sbin/vip_access.sh 
[root@master ~]# cat /usr/local/sbin/vip_access.sh 

之后重启主备keepalived

[root@master ~]# systemctl restart keepalived

配置主备tcp/tcpfin/udp超时时间

[root@master ~]# ipvsadm --set 1 2 1 

测试访问
在这里插入图片描述
关闭主节点keepalived,vip跑到从节点上,再次测试访问

[root@master ~]# systemctl stop keepalived

在这里插入图片描述
测试成功

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值