搭建环境:

    两台director,两台RS

    director1:ip(172.16.125.5),安装好keepalived;

    director2:ip(172.16.125.6),安装好keepalived;

    RS1:ip(172.16.125.7),安装好httpd;

    RS2:ip(172.16.125.8),安装好httpd;

    vip(1):172.16.125.100,vip(2):172.16.125.110。

    在此处keepalived实现lvs的高可用,使用了lvs的dr模型。关闭iptables和selinux,且实现时间同步。


使用主从模式配置过程:

    (1)首先配置RS,在每一台的RS上安装好httpd,同时为了实现lvs调度算法的效果,在此故意使得每一台RS上提供的网页内容不一样。

    (2)首先设置内核参数,保证请求vip的报文必须经过director进行调度,才能到达后端RS上。



echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce



    (3)进行每一台RS上lo接口的vip的绑定,同时设置路由。



~]# ifconfig lo:0 172.16.125.100/32 broadcast 172.16.125.100 up
~]# route add -host 172.16.125.100 dev lo:0


      通过以上设置,完成了后端RS的设置。

    (4)进行两台director上keepalived服务的配置。首先设置一个虚拟路由器组,其虚拟ip为172.16.125.100(也就是vip),在虚拟主机内部配置两台RS,即后端的两台RS,具体配置文件如下所示。

    配置文件一(在主keepalived服务器上的配置):


global_defs {
   notification_email {
     root@localhost
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
 
vrrp_instance VI_1 {
    state MASTER
    interface eno16777736
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 125110
    }
    virtual_ipaddress {
        172.16.125.100/16 dev eno16777736 label eno16777736:0
    }
}
 
virtual_server 172.16.125.100 80 {
    delay_loop 3
    lb_algo rr
    lb_kind DR
    nat_mask 255.255.0.0
    protocol TCP
 
    real_server 172.16.125.7 80 {
        weight 1
        HTTP_GET {
            url {
              path /
              status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
 
    real_server 172.16.125.8 80 {
        weight 1
        HTTP_GET {
            url {
              path /
              status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
	    }
    }   
}


     在从keepalived所在的主机上的配置,不同的地方有两条:一,state设置为BACKUP,表明该主机为从服务器;二,priority(优先级)要低于主keepalived服务器上的优先级。

    在两台调度器上同时启动keepalived服务,进行查看集群服务的相关信息。

        一、使用ipvsadm查看负载均衡集群服务信息:

        

keepalived rabbimtmq 高可用方案_HTTP

        二、查看vip是否已经加在主keepalived服务器上了:

        

keepalived rabbimtmq 高可用方案_后端_02

        三、在客户端访问vip,看是否能够实现负载均衡调度,此处使用轮询(rr)算法。

            

keepalived rabbimtmq 高可用方案_服务器_03

        四、在主keepalived服务器上关闭keepalived服务,看高可用资源是否会发生转移,同时进行vip的访问,是否能够继续进行负载均衡调度。

    

keepalived rabbimtmq 高可用方案_HTTP_04


使用双主模型实现keepalived+lvs:

    (1)在后端RS上设置内核参数,保证请求vip的报文必须经过director进行调度,才能到达后端RS上。设置第一个vip(172.16.125.100),同时设置路由,详细配置步骤如主从模式的RS的vip的设置,在此处不再赘述。

        在后端RS上lo端口上配置第二个vip(172.16.125.110),同时设置路由。



~]# ifconfig lo:1 172.16.125.110/32 broadcast 172.16.125.110 up
~]# route add -host 172.16.125.110 dev lo:1



    (2)在keepalived的服务器上修改配置文件,增加一个新的虚拟路由器,详细配置如下配置文件所示:

        主keepalived服务器1上的配置:



global_defs {
   notification_email {
     root@localhost
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
 
vrrp_instance VI_1 {
    state MASTER
    interface eno16777736
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 125110
    }
    virtual_ipaddress {
    172.16.125.100/16 dev eno16777736 label eno16777736:0
    }
}
 
virtual_server 172.16.125.100 80 {
    delay_loop 3
    lb_algo rr
    lb_kind DR
    nat_mask 255.255.0.0
    protocol TCP
 
    real_server 172.16.125.7 80 {
        weight 1
        HTTP_GET {
            url {
              path /
              status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
 
    real_server 172.16.125.8 80 {
        weight 1
        HTTP_GET {
            url {
              path /
              status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
	}
    }   
}
 
vrrp_instance VI_2 {
    state BACKUP
    interface eno16777736
    virtual_router_id 110
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 123456789
    }
    virtual_ipaddress {
    172.16.125.110/16 dev eno16777736 label eno16777736:1
    }
}
 
virtual_server 172.16.125.110 80 {
    delay_loop 3
    lb_algo rr
    lb_kind DR
    nat_mask 255.255.0.0
    protocol TCP
 
    real_server 172.16.125.7 80 {
        weight 1
        HTTP_GET {
            url {
              path /
              status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
 
    real_server 172.16.125.8 80 {
        weight 1
        HTTP_GET {
            url {
              path /
              status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
	}
    }   
}


    主keepalived服务器2上的配置: 


global_defs {
   notification_email {
     root@localhost
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
 
vrrp_instance VI_1 {
    state BACKUP
    interface eno16777736
    virtual_router_id 51
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 125110
    }
    virtual_ipaddress {
    172.16.125.100/16 dev eno16777736 label eno16777736:0
    }
}
 
virtual_server 172.16.125.100 80 {
    delay_loop 3
    lb_algo rr
    lb_kind DR
    nat_mask 255.255.0.0
    protocol TCP
 
    real_server 172.16.125.7 80 {
        weight 1
        HTTP_GET {
            url {
              path /
              status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
 
    real_server 172.16.125.8 80 {
        weight 1
        HTTP_GET {
            url {
              path /
              status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
	}
    }   
}
 
vrrp_instance VI_2 {
    state MASTER
    interface eno16777736
    virtual_router_id 110
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 123456789
    }
    virtual_ipaddress {
    172.16.125.110/16 dev eno16777736 label eno16777736:1
    }
}
 
virtual_server 172.16.125.110 80 {
    delay_loop 3
    lb_algo rr
    lb_kind DR
    nat_mask 255.255.0.0
    protocol TCP
 
    real_server 172.16.125.7 80 {
        weight 1
        HTTP_GET {
            url {
              path /
              status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
 
    real_server 172.16.125.8 80 {
        weight 1
        HTTP_GET {
            url {
              path /
              status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
	}
    }   
}


    (3)在两台高可用服务器上启动keepalived服务,查看vip是否分别加载到高可用服务器上。

            

keepalived rabbimtmq 高可用方案_网络_05

            

keepalived rabbimtmq 高可用方案_HTTP_06

        在客户端进行测试,分别访问vip(172.16.125.100)和vip(172.16.125.110)。

            

keepalived rabbimtmq 高可用方案_网络_07

        可以看到,访问两个vip都可以正常访问,并且实现了rr(轮询)调度算法。



转载于:https://blog.51cto.com/angrybird/1706591