基于keepalived+LVS-NAT实现——高可用的负载均衡架构

一、keepalived的作用

keepalived是一款基于VRRP(Virtual Router Redundancy Protocol,虚拟路由冗余协议)的服务

VRRP协议
    可以认为是实现路由器高可用的协议,简单的说,当一个路由器故障时可以由另一个备份路由器继续提供相同的服务。
高可用集群实现时主要关注
        状态切换
        资源转移
keepalived主要实现的是IP漂移和状态切换
其余资源需要在BACKUP的主机上配置好

二、实现基于keepalive高可用的LVS负载均衡架构

实现LVS—NAT模式
定义两个IP一次性都漂移,一个VIP ,一个DIP

1.keepalived(主)配置文件

/etc/keepalived/keepalived.conf 
! Configuration File for keepalived

global_defs {
   notification_email {
     root@localhost 
   }
   notification_email_from root_keepalived
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
} 
vrrp_instance VI_1 {
    state BACKUP
    interface eth2
    virtual_router_id 14
    priority 200
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 2121
    }
    virtual_ipaddress {
        172.17.17.1
    }

}
vrrp_instance VI_2 {
    state BACKUP
    interface eth1
    virtual_router_id 15
    priority 200
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 2121
    }
    virtual_ipaddress {
        192.168.17.1
    }
}
virtual_server 172.17.17.1 80 {
    delay_loop 2
    lb_algo wrr
    lb_kind NAT
    persistence_timeout 50
    protocol TCP

    real_server 192.168.17.175 80 {
        weight 3
        HTTP_GET {
            url { 
              path /
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
   real_server 192.168.17.176 80 {
        weight 2
        HTTP_GET {
            url { 
              path /
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}

2.keepalived(从)配置文件

/etc/keepalived/keepalived.conf 
! Configuration File for keepalived

global_defs {
   notification_email {
     root@localhost 
   }
   notification_email_from root_keepalived
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
} 
vrrp_instance VI_1 {
    state BACKUP
    interface eth2
    virtual_router_id 14
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 2121
    }
    virtual_ipaddress {
        172.17.17.1
    }

}
vrrp_instance VI_2 {
    state BACKUP
    interface eth1
    virtual_router_id 15
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 2121
    }
    virtual_ipaddress {
        192.168.17.1
    }
}
virtual_server 172.17.17.1 80 {
    delay_loop 2
    lb_algo wrr
    lb_kind NAT
    persistence_timeout 50
    protocol TCP

    real_server 192.168.17.175 80 {
        weight 3
        HTTP_GET {
            url { 
              path /
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
   real_server 192.168.17.176 80 {
        weight 2
        HTTP_GET {
            url { 
              path /
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}

3.负载均衡服务器(主、从)开启IP转发功能

vim /etc/sysctl.conf   
    net.ipv4.ip_forward=1
sysctl  -p

4.RS把网管设置为DIP

 route add default gw 192.168.17.1

你可能感兴趣的:(linux-集群)