zoukankan      html  css  js  c++  java
  • 基于keepalived的redis系统master双机热备,读数据负载均衡设置方案

    硬件:
    机器    ip    作用
    master    192.168.0.2    redis系统的master主机
    slave1    192.168.0.3    redis系统的slave机器,和master组成双机热备
    slave2    192.168.0.4    

    redis系统的slave机器,和slave1构成读数据的负载均衡系统

    软件:

    keepalived,下载地址:www.keepalived.org

    lvs,下载地址:http://www.linuxvirtualserver.org

    redis,下载地址:www.redis.io

    centos 6.4版本

    安装:

    不采用编译安装的方式,使用yum安装

    1.安装kernel-devel:yum install kernel-devel.这是ipvsadm需要的

    2.安装lvs:yum install ipvsadm

    3.安装keepalived:yum install keepalived

    如果无法联网,请去这里考古寻找自己需要的rpm文件

    base:http://tel.mirrors.163.com/centos/6/os/x86_64/Packages/
    update:http://tel.mirrors.163.com/centos/6/updates/x86_64/Packages/

    下面是需要的rpm文件列表截图

    kernel-devel需要的文件



    以及需要的证书



    lvs需要的文件



    keepalived需要的文件



    注意,三台机器都需要安装keepalived

    配置keepalived:

    1.centos必要配置:

    /etc/sysctl.conf文件

    net.ipv4.ip_forward=1#转发开启

    2.keepalived必要配置:

    master修改/etc/keepalived/keepalived.conf为如下

    ! Configuration File for keepalived
    
    global_defs {
       notification_email {
         邮箱
       }
       notification_email_from 邮箱
       smtp_server 邮箱服务器地址
       smtp_connect_timeout 30
       router_id LVS_DEVEL
    }
    vrrp_instance VI_1 {
        state MASTER
        interface eth0 #eth0是要绑定的网卡
        virtual_router_id 100 #同一个vrrp_instance中的值必须一样  
        priority 160   #master的值要高于backup的
        advert_int 1  
        authentication {
            auth_type PASS
            auth_pass 1111
        }    
        virtual_ipaddress {
            192.168.0.5  #用于双机热备的虚拟ip
        }
       
    }
    virtual_server 192.168.0.5 6379 {
        delay_loop 3
        lb_algo wrr
        lb_kind DR
        persistence_timeout 30
        protocol TCP
        real_server 192.168.0.2 6379 {
            weight 8
            notify_down redis服务失败后要执行的脚本的路径/脚本名  #服务失败后要执行的脚本
            TCP_CHECK {
            connect_timeout 1
            nb_get_retry 3
            delay_before_retry 3
            connect_port 6379
            }
        }
    }

    slave1修改/etc/keepalived/keepalived.conf为如下

    ! Configuration File for keepalived
    
    global_defs {
       notification_email {
        邮箱
       }
       notification_email_from 邮箱
       smtp_server 邮箱服务器地址
       smtp_connect_timeout 30
       router_id LVS_DEVEL
    }
    
    vrrp_instance VI_1 {
        state MASTER
        interface eth0 #eth0是要绑定的网卡
        virtual_router_id 100 #同一个vrrp_instance中的值必须一样  
        priority 160   #master的值要高于backup的
        advert_int 1  
        authentication {
            auth_type PASS
            auth_pass 1111
        }    
        virtual_ipaddress {
            192.168.0.5  #用于双机热备的虚拟ip
        }
       notify_master 将slave重新转换为slave的脚本
    }
    vrrp_instance VI_2 {
        state MASTER  #将此slave作为读数据的master
        interface eth0
        virtual_router_id 101
        priority 151
        advert_int 1
        authentication {
        auth_type PASS
        auth_pass 1111
        }
        virtual_ipaddress {
        192.168.0.6  #用于读取数据的负载均衡的虚拟ip
        }
    }
    virtual_server 192.168.0.5 6379 {
        delay_loop 3
        lb_algo wrr
        lb_kind DR
        persistence_timeout 30
        protocol TCP
        real_server 192.168.0.2 6379 {
            weight 1    
                    notify_down redis服务失败后要执行的脚本的路径/脚本名  #服务失败后要执行的脚本
            TCP_CHECK {
            connect_timeout 1
            nb_get_retry 2
            delay_before_retry 1
            connect_port 6379
            }
        }
        real_server 192.168.0.3 6379 {
            weight 8
            TCP_CHECK {
            connect_timeout 10
            nb_get_retry 3
            delay_before_retry 3
            connect_port 6379
            }
        }
    }
    virtual_server 192.168.0.6 6379 {
        delay_loop 3
        lb_algo wrr
        lb_kind DR
        persistence_timeout 30
        protocol TCP
        real_server 192.168.0.2 6379 {
            weight 8
            TCP_CHECK {
            connect_timeout 10
            nb_get_retry 3
            delay_before_retry 3
            connect_port 6379
            }
        }
        real_server 192.168.0.3 6379 {
            weight 7
            TCP_CHECK {
            connect_timeout 10
            nb_get_retry 3
            delay_before_retry 3
            connect_port 6379
            }
        }
    }
    


    slave2修改/etc/keepalived/keepalived.conf为如下

    ! Configuration File for keepalived
    
    global_defs {
       notification_email {
         邮箱
       }
       notification_email_from 邮箱
       smtp_server 邮箱服务器地址
       smtp_connect_timeout 30
       router_id LVS_DEVEL
    }
    vrrp_instance VI_2 {
        state BACKUP
        interface eth0
        virtual_router_id 101
        priority 149
        advert_int 1
        authentication {
        auth_type PASS
        auth_pass 1111
        }
        virtual_ipaddress {
        192.168.0.6
        }
    }
    virtual_server 192.168.0.6 6379 {
        delay_loop 3
        lb_algo wrr
        lb_kind DR
        persistence_timeout 30
        protocol TCP
        real_server 192.168.0.2 6379 {
            weight 8
            TCP_CHECK {
            connect_timeout 10
            nb_get_retry 3
            delay_before_retry 3
            connect_port 6379
            }
        }
        real_server 192.168.0.3 6379 {
            weight 7
            TCP_CHECK {
            connect_timeout 10
            nb_get_retry 3
            delay_before_retry 3
            connect_port 6379
            }
        }
    }



    配置redis:
    master无需特殊配置 slave1则设置为master的从机
    slave2则需要设置为192.168.0.5的从机,否则在master失效后slave2会无法继续读取数据

    需要的脚本:

    在master执行的脚本:

     #!/usr/bin/env bash
    service keepalived stop #需要用户具有权限,不中断keepalived服务虚拟ip无法转移 

    在slave1执行的脚本:
    #!/usr/bin/env bash
    
    /usr/local/bin/redis-cli -h 127.0.0.1 -p 6379 slaveof NO ONE #将slave1转换为redis的


    slave1第二个脚本,在master服务重启后将slave1重新转换为slave状态
    最终效果: 192.168.0.5 提供了redis的双机热备服务,192.168.0.6则提供了数据读取的负载均衡
    #!/usr/bin/env bash
    
    /usr/local/bin/redis-cli slaveof 192.168.0.2 6379 #将slave1重新转换为redis的slave 


    需要注意,master每次需要先启动redis服务然后再启动keepalived

  • 相关阅读:
    【CS Round #46 (Div. 1.5) B】Letters Deque
    【CS Round #46 (Div. 1.5) A】Letters Deque
    【Codeforces Round #432 (Div. 2) A】 Arpa and a research in Mexican wave
    【Codeforces Round #432 (Div. 2) B】Arpa and an exam about geometry
    【Codeforces Round #432 (Div. 1) A】 Five Dimensional Points
    【2017 Multi-University Training Contest
    Managing remote devices
    防止表单重复提交的解决方案整理
    防止表单重复提交的解决方案整理
    日期格式化函数
  • 原文地址:https://www.cnblogs.com/AI001/p/3368883.html
Copyright © 2011-2022 走看看