CentOS 7 keepalived+LVS

LVS架构中 , 不管是NAT模式还是DR模式 , 当后端的RS宕机了 , 调度器还是会把请求转发到宕掉的RS上 , 然而keepalived可以解决该问题 , 它不仅仅有高可用的功能 , 还有负载均衡的功能 

完整的keepalived+LVS构架需要有两台调度器来实现高可用 , 提供调度服务的只需要一台服务器 , 另一台作为备用 

高可用的备用服务器只需对照主服务器稍微修改keepalived.conf的几个配置项就可以实现 ,下面就只演示负载均衡

主keepalived(调度器) : 192.168.94.11

真实web服务器1 : 192.168.94.22

真实web服务器2 : 192.168.94.33

VIP : 192.168.94.111

[root@lb ~]# yum -y install keepalived
[root@lb ~]# cd /etc/keepalived/
[root@lb keepalived]# cp keepalived.conf keepalived.conf.bak
[root@lb keepalived]# vim keepalived.conf
vrrp_instance VI_1 {                                                                         
    #备用服务器上为 BACKUP
    state MASTER
    #绑定vip的网卡为ens33
    interface ens33
    virtual_router_id 51
    #备用服务器上为90
    priority 100 
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass damowang
    }   
    virtual_ipaddress {
        192.168.94.111
    }   
}
virtual_server 192.168.94.111 80 {
    #(每隔10秒查询realserver状态) 
    delay_loop 10
    #(lvs 算法) 
    lb_algo wlc 
    #(DR模式) 
    lb_kind DR
    #(同一IP的连接60秒内被分配到同一台realserver)
    persistence_timeout 60
    #(用TCP协议检查realserver状态) 
    protocol TCP 

    real_server 192.168.94.22 80 {
        #(权重) 
        weight 100 
        TCP_CHECK {
        #(10秒无响应超时) 
        connect_timeout 10
        nb_get_retry 3
        delay_before_retry 3
        connect_port 80
        }   
    }   
    real_server 192.168.94.33 80 {
        weight 100 
        TCP_CHECK {
        connect_timeout 10
        nb_get_retry 3
        delay_before_retry 3
        connect_port 80
        }
     }
}

keepalived服务器配置完成

如果之前有ipvsadm的规则 , 先清空 , 再重新加载网卡 , 可以把之前设置的VIP清掉

[root@lb keepalived]# ipvsadm -C
[root@lb keepalived]# systemctl restart network

配置web服务器

因为keepalived的配置文件中定义的LVS模式是DR模式 , 所以再把前面DR模式LVS脚本执行一遍即可

CentOS 7 DR模式LVS搭建

[root@web1 ~]# sh /usr/local/sbin/lvs_dr_rs.sh 
[root@web2 ~]# sh /usr/local/sbin/lvs_dr_rs.sh 

最后一步

[root@lb keepalived]# systemctl start keepalived  # 开启keepalived服务
[root@lb keepalived]# systemctl status keepalived  # 查看服务是否正常运行

测试 :

用浏览器访问VIP 然后把其中一台web服务器关掉 , 再刷新浏览器(Ctrl+F5) , 这样就不会有缓存了 , 

同时也可以在调度器上查看连接数

[root@lb keepalived]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.94.111:80 wlc persistent 60
  -> 192.168.94.22:80             Route   100    0          0         
  -> 192.168.94.33:80             Route   100    5          0         

[root@wab2 ~]# systemctl stop httpd

[root@lb keepalived]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.94.111:80 wlc persistent 60
  -> 192.168.94.22:80             Route   100    2          0         

[root@wab2 ~]# systemctl start httpd

[root@lb keepalived]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.94.111:80 wlc persistent 60
  -> 192.168.94.22:80             Route   100    4          1         
  -> 192.168.94.33:80             Route   100    3          0         

 

转载于:https://www.cnblogs.com/bigdevilking/p/9363267.html

你可能感兴趣的:(网络,后端,开发工具)