搭建高可用redis-cluster集群

KeepAlived+Redis+Haproxy实现主从热备、负载均衡、秒级切换实战

  • Redis+Keepalived+Haproxy 的集群架构,分别用六个端口,实现多路复用,最终实现主从热备、负载均衡、秒级切换。

一、部署Redis集群

1、环境

  • 一台虚拟机模拟6个节点,创建出3 master、3 salve 环境
  • redis1: 192.168.182.120:(6001~6006)

2、安装 redis 实例 (6节点)

配置截图

[root@redis-s redis]# ls
6001  6002  6003  6004  6005  6006

其中一个节点的conf文件

[root@redis-s redis]# cat 6001/conf/redis.conf 
bind 0.0.0.0
protected-mode no
port 6001
daemonize no
dir /redis/6001/data
cluster-enabled yes
cluster-config-file /redis/6001/conf/nodes.conf
cluster-node-timeout 5000
appendonly yes
daemonize yes
pidfile /redis/6001/redis.pid
logfile /redis/6001/log/redis.log
requirepass redis
masterauth redis

3、检查 redis 启动情况

[root@redis-s ~]# sh start.redis.sh 
[root@redis-s ~]# netstat -tnlp |grep redis
tcp        0      0 0.0.0.0:6001            0.0.0.0:*               LISTEN      88031/redis-server  
tcp        0      0 0.0.0.0:6002            0.0.0.0:*               LISTEN      88034/redis-server  
tcp        0      0 0.0.0.0:6003            0.0.0.0:*               LISTEN      88041/redis-server  
tcp        0      0 0.0.0.0:6004            0.0.0.0:*               LISTEN      88048/redis-server  
tcp        0      0 0.0.0.0:6005            0.0.0.0:*               LISTEN      88055/redis-server  
tcp        0      0 0.0.0.0:6006            0.0.0.0:*               LISTEN      88061/redis-server  
tcp        0      0 0.0.0.0:16001           0.0.0.0:*               LISTEN      88031/redis-server  
tcp        0      0 0.0.0.0:16002           0.0.0.0:*               LISTEN      88034/redis-server  
tcp        0      0 0.0.0.0:16003           0.0.0.0:*               LISTEN      88041/redis-server  
tcp        0      0 0.0.0.0:16004           0.0.0.0:*               LISTEN      88048/redis-server  
tcp        0      0 0.0.0.0:16005           0.0.0.0:*               LISTEN      88055/redis-server  
tcp        0      0 0.0.0.0:16006           0.0.0.0:*               LISTEN      88061/redis-server

4、创建集群

[root@redis-s redis]#./redis-cli --cluster create 192.168.182.120:6001 192.168.182.120:6002 192.168.182.120:6003 192.168.182.120:6004 192.168.182.120:6005 192.168.182.120:6006 --cluster-replicas 1 -a redis

查看redis集群状态

127.0.0.1:6001> CLUSTER NODES
5b140543ba736c7429783086a4698ce23f5ea009 192.168.182.120:6004@16004 slave e40ed46e827115fabf18fe71a527d8b9279761c0 0 1593782850256 4 connected
e40ed46e827115fabf18fe71a527d8b9279761c0 192.168.182.120:6003@16003 master - 0 1593782849000 3 connected 10923-16383
dbb64e167721771fb57acd37f0c4510eb23dc9b5 192.168.182.120:6001@16001 myself,master - 0 1593782850000 1 connected 0-5460
a703d5e2a4a47627d56d964c4ab524542d5a9c66 192.168.182.120:6002@16002 master - 0 1593782849000 2 connected 5461-10922
b8081e72a84b826abc33801aeae397deb8810e60 192.168.182.120:6005@16005 slave dbb64e167721771fb57acd37f0c4510eb23dc9b5 0 1593782849037 5 connected
e2908aaae69ed7f239927d2a62977877cafb0b65 192.168.182.120:6006@16006 slave a703d5e2a4a47627d56d964c4ab524542d5a9c66 0 1593782849247 6 connected

5、集群验证

127.0.0.1:6001> set name haitao
-> Redirected to slot [5798] located at 192.168.182.120:6002
OK

验证

127.0.0.1:6006> get name
-> Redirected to slot [5798] located at 192.168.182.120:6002
"haitao"

二、部署Keepalived,实现主从热备、秒级切换

1、环境

  • 两台虚拟机或者选择集群中的任意两个节点配置
  • keepalived1+haproxy:192.168.182.100
  • keepalived2+haproxy:192.168.182.110
  • VIP地址:192.168.182.200

2、安装keepalived

3、修改配置文件

1、keepalived1 配置

[root@ha1 ~]# cat /etc/keepalived/keepalived.conf 
[root@ha2 ~]# cat /etc/keepalived/keepalived.conf 
! Configuration File for keepalived
global_defs {
   router_id HA1        
}
vrrp_script check_haproxy {
   script "/etc/keepalived/check_haproxy.sh"
   interval 5
}
vrrp_instance VI_1 {
    state BACKUP
    nopreempt                           
    interface eth0                              
    virtual_router_id 80                
    priority 100                            
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.182.200
    }
track_script {
        check_haproxy
    }
}

2、keepalived2 配置

[root@ha2 ~]# cat /etc/keepalived/keepalived.conf 
! Configuration File for keepalived
global_defs {
   router_id HA2        
}
vrrp_script check_haproxy {
   script "/etc/keepalived/check_haproxy.sh"
   interval 5
}
vrrp_instance VI_1 {
    state BACKUP
    nopreempt                           
    interface eth0                              
    virtual_router_id 80                
    priority 100                            
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.182.200
    }
track_script {
        check_haproxy
    }
}

3、健康检测脚本 haproxy_chk.sh

[root@ha2 ~]# cat /etc/keepalived/check_haproxy.sh 
#!/bin/bash                                                                                                     
/usr/bin/curl -I http://127.0.0.1:1314/haproxystats &>/dev/null 
if [ $? -ne 0 ];then                                                                            
        /usr/bin/systemctl stop keepalived                                      
fi

4、开启服务验证是VIP

[root@ha2 ~]# ip a |grep eth
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:13:2a:a8 brd ff:ff:ff:ff:ff:ff
    inet 192.168.182.110/24 brd 192.168.182.255 scope global noprefixroute eth0
    inet 192.168.182.200/32 scope global eth0

三、部署haproxy,实现访问6379端口时,轮询访问六个节点

1、安装 haproxy

2、创建 haproxy.conf

[root@ha2 ~]# cat /etc/haproxy/haproxy.cfg
global
    log                     127.0.0.1 local0
    chroot                  /var/lib/haproxy
    pidfile                 /var/run/haproxy.pid
    maxconn                 4000
    user                    haproxy
    group                   haproxy
    daemon

defaults
    mode                     http
    log                      global
    option                   dontlognull
    retries                  3
    maxconn                  3000
    contimeout               50000
    clitimeout               50000
    srvtimeout               50000

listen stats
    bind                    *:1314
    stats                   enable
    stats                   hide-version
    stats uri               /haproxystats
    stats realm             Haproxy stats
    stats auth              admin:admin
    stats admin             if TRUE
listen  redis
        bind *:6379
        mode tcp   #{ tcp|http|health } 设定启动的实例的协议类型,此处要选择tcp,因为redis是基于tcp协议运行的
        balance roundrobin
        server redis1 192.168.182.120:6001 check
        server redis2 192.168.182.120:6002 check
        server redis3 192.168.182.120:6003 check
        server redis4 192.168.182.120:6004 check
        server redis5 192.168.182.120:6005 check
        server redis6 192.168.182.120:6006 check

3、haproxy 监控页面访问验证

http://192.168.182.200:1314/haproxystats
  • 端口1314 账号密码 admin:admin
  • 有以下效果

--------------------当你发现自己突然嫉妒起其他人时,就请安静下来学习吧!--------------------