lvs+keepalived

Lvs + keepalived

基本服务器宕机的主从切换配置

1、准备192.168.230.10、192.168.230.13两台主机分别作为Lvs服务器

 192.168.230.10 主Lvs服务器

 192.168.230.13 备Lvs服务器

2、两台nginx服务器,分别安装keepalived配置/etc/keepalived/keepalived.conf

[root@node1 keepalived]# vi keepalived.conf
[root@node1 keepalived]# cp keepalived.conf keepalived.confbackup  

[root@node4 ~]# yum -y install keepalived
[root@node4 keepalived]# cp keepalived.conf keepalived.confbackup  

删除虚拟地址
[root@node1 ~]# ip addr del 192.168.230.100/24 dev eth0

2.1、在192.168.230.10主机上进行主Lvs配置操作:

主Lvs

修改主Lvs 下/etc/keepalived/keepalived.conf 文件

! Configuration File for keepalived

global_defs {
   notification_email {
     root@localhost #发送提醒邮件的目标地址可有多个
     [email protected]
  }
   notification_email_from test@localhost#发送邮件的from地址,可以随意写,邮件地址不存在都无所谓
   smtp_server 127.0.0.1#邮件服务的地址,一般写本地
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_instance VI_1 {
    state MASTER# MASTER 主 和 从 
    interface eth0#VIP需要绑定的网卡名称
    virtual_router_id 51
    priority 101#优先级 主的优先级要高
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.230.100/24 dev eth0 label eth0:0 #设置VIP
    }
}

virtual_server 192.168.230.100 80 {#设置虚拟lvs服务,VIP PORT
    delay_loop 6
    lb_algo rr#调度算法wrr
    lb_kind DR#lvs的模式
    nat_mask 255.255.255.0
    persistence_timeout 50 #同一个IP地址在50秒内lvs转发给同一个后端服务器
    protocol TCP

    real_server 192.168.230.11 80 {#设置真实服务器的心跳机制 RID PORT
        weight 1#权重
        HTTP_GET {#心跳检测的方式
            url {
              path /#心跳检查的地址
              status_code 200#心跳检查返回的状态
            }
            connect_timeout 2 #超时时间
            nb_get_retry 3#重复检查3次
            delay_before_retry 1#每隔1秒钟再次检查
        }
    }
    real_server 192.168.230.12 80 {#第二个真实服务器设置
        weight 1
        HTTP_GET {
            url {
              path /
              status_code 200
            }
            connect_timeout 2 
            nb_get_retry 3
            delay_before_retry 1
        }
    }
}

2.2、在192.168.230.13主机上进行备Lvs配置操作:

备Lvs

修改备Lvs下/etc/keepalived/keepalived.conf文件

配置备Lvs时

需要注意:需要修改state为BACKUP,priority比MASTER低,virtual_router_id和master 

! Configuration File for keepalived

global_defs {
   notification_email {
     root@localhost #发送提醒邮件的目标地址可有多个
     [email protected]
  }
   notification_email_from test@localhost#发送邮件的from地址,可以随意写,邮件地址不存在都无所谓
   smtp_server 127.0.0.1#邮件服务的地址,一般写本地
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_instance VI_1 {
    state BACKUP# MASTER 主 和 从 
    interface eth0#VIP需要绑定的网卡名称
    virtual_router_id 51
    priority 100#优先级 主的优先级要高
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.230.100/24 dev eth0 label eth0:0 #设置VIP
    }
}

virtual_server 192.168.230.100 80 {#设置虚拟lvs服务,VIP PORT
    delay_loop 6
    lb_algo rr#调度算法wrr
    lb_kind DR#lvs的模式
    nat_mask 255.255.255.0
    persistence_timeout 50 #同一个IP地址在50秒内lvs转发给同一个后端服务器
    protocol TCP

    real_server 192.168.230.11 80 {#设置真实服务器的心跳机制 RID PORT
        weight 1#权重
        HTTP_GET {#心跳检测的方式
            url {
              path /#心跳检查的地址
              status_code 200#心跳检查返回的状态
            }
            connect_timeout 2 #超时时间
            nb_get_retry 3#重复检查3次
            delay_before_retry 1#每隔1秒钟再次检查
        }
    }
    real_server 192.168.230.12 80 {#第二个真实服务器设置
        weight 1
        HTTP_GET {
            url {
              path /
              status_code 200
            }
            connect_timeout 2 
            nb_get_retry 3
            delay_before_retry 1
        }
    }
}

3、在RS 检查web服务是否正常

在192.168.230.11后台服务器操作:

[root@node2 ~]# yum -y install httpd
[root@node2 bin]# cd /var/www/html
[root@node2 html]# vi index.html
<html>
   <h1>matrix2!</h1>
</html>
[root@node2 ~]# service httpd start

在浏览器地址栏访问:http://192.168.230.11/,查看web服务是否正常

在192.168.230.12后台服务器操作:

[root@node3 ~]# yum -y install httpd
[root@node3 bin]# cd /var/www/html
[root@node3 html]# vi index.html
<html>
   <h1>matrix3!</h1>
</html>
[root@node3 ~]# service httpd start

在浏览器地址栏访问:http://192.168.230.12/,查看web服务是否正常
lvs+keepalived_第1张图片

4、启动两台lvs服务器的keepalived服务

[root@node1 ~]# service keepalived start
[root@node1 keepalived]# service keepalived status
keepalived (pid  17333) is running...
[root@node1 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.230.100:80 rr persistent 50
  -> 192.168.230.11:80            Route   1      0          0         
  -> 192.168.230.12:80            Route   1      0          0  

实时监控,并输出日志
[root@node1 keepalived]# tail -f /var/log/messages


[root@node4 ~]# service keepalived start
[root@node4 keepalived]# service keepalived status
keepalived (pid  17333) is running...
[root@node1 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.230.100:80 rr persistent 50
  -> 192.168.230.11:80            Route   1      0          0         
  -> 192.168.230.12:80            Route   1      0          0   

在浏览器地址栏访问:http://192.168.230.100/,查看web服务是否正常

lvs+keepalived_第2张图片

5、关闭主Lvs的keepalived,查看备Lvs是否接管职能

此时如果主机宕机则备机会顶替主机获得虚拟IP

在192.168.230.10主Lvs中:

[root@node1 keepalived]# service keepalived stop

在192.168.230.13备Lvs中:

[root@node1 keepalived]# ifconfig

发现备Lvs接管了集群

lvs+keepalived_第3张图片

在192.168.230.10主Lvs中:

[root@node1 keepalived]# service keepalived start
[root@node1 keepalived]# ifconfig

发现主Lvs重新接管集群

lvs+keepalived_第4张图片

你可能感兴趣的:(lvs+keepalived)