keepalived+lvs实现高可用

目录

环境

1.配置real-server服务器

2.配置keepalived和lvs

3.测试:

概述

keepalived+lvs实现高可用:

lvs可以监控后端服务器,当服务器宕机之后可以对其进行故障切换。

keepalived是对VIP进行检测,当某一个主机的vip错误,则会将vip漂移到另一个节点上。

环境

5台主机---(两台主机--keeplived,lvs  两台主机--real-server服务器  一台主机--客户端)

1.配置real-server服务器

后端服务器---real-serer

1.yum install -y httpd

2.编写lvs脚本
vim  /etc/init.d/lvs_rs
(脚本过长)

3.给脚本权限
chmod +x /etc/init.d/lvs_rs

4.添加为系统服务
chkconfig --add lvs_rs

4.当前启动
/etc/init.d/lvs_rs start

5.查看本机IP是否产生vip
6.配置服务器页面
echo "xxxxx" > /var/www/html/index.html

2.配置keepalived和lvs

主备节点都需要配置
1.keepalived中自带了lvs的模块,所以下载ipvsadm后配置在keepalived的配置文件里即可
yum install -y ipvsadm
2.对于keepalived的配置之前有,这里显示keepalived中lvs的配置
vim /etc/keepalived/keepalived.conf
[root@localhost ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL1
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.27.135
}
}
virtual_server 192.168.27.135 80 {       设置的vip
    delay_loop 6
    lb_algo rr
    lb_kind DR
    protocol TCP
real_server 192.168.27.122 80 {          后端的real-server服务器的ip
    weight 1
TCP_CHECK {
    connect_timeout 3
    nb_get_retry 3
    delay_before_retry 3
    connect_port 80
}
}
real_server 192.168.27.123 80 {
    weight 1
TCP_CHECK {
    connect_timeout 3
    nb_get_retry 3
    delay_before_retry 3
    connect_port 80
}
}
}

重新启动keepalived,检查VIP是否存在。

3.测试:

客户端使用VIP进行访问:

for ((i=1;i<=6;i++));do curl vip ;done

 返回的页面是后端不同服务器提供的页面,当real-server中的一个宕机之后,会将请求发送给另一台服务器,防止长时间请求宕机的服务器。

当前端一台keepalived宕机之后,会进行vip漂移,由另一个keepalived接管主节点上的所有服务,客户端仍然可以访问。

猜你喜欢

转载自blog.csdn.net/weixin_62173637/article/details/132355040
今日推荐