Redis集群搭建
1 Redis集群简介
Redis集群(Redis Cluster) 是Redis提供的分布式数据库方案,通过分片(sharding) 来进行数据共享,并提供复制和故障转移功能。相比于主从复制、哨兵模式,Redis集群实现了较为完善的高可用方案,解决了存储能力受到单机限制,写操作无法负载均衡的问题。
2 软件包
redis-5.0.8.tar.gz、gcc、readline依赖包和配置文件下载地址:
https://download.csdn.net/download/Zhuge_Dan/20259401
3 基础配置
3.1 关闭SELinux
[root@REDIS ~]# setenforce 0
[root@REDIS ~]# vi /etc/selinux/config
SELINUX=disabled #将SELINUX设置为disabled
[root@REDIS ~]# getenforce
Permissive
3.2 关闭防火墙或开放需要的业务端口
[root@REDIS ~]# systemctl stop firewalld #关闭防火墙
[root@REDIS ~]# systemctl disable firewalld #禁止防火墙开机自启
或者:
[root@REDIS ~]# firewall-cmd --zone=public --add-port=6379-6381/tcp --add-port=6479-6481/tcp --add-port=16379-16381/tcp --add-port=16479-16481/tcp --permanent
success
[root@REDIS ~]# firewall-cmd --reload
success
[root@REDIS ~]# firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: ens33
sources:
services: dhcpv6-client ssh
ports: 6379-6381/tcp 6479-6481/tcp 16379-16381/tcp 16479-16481/tcp
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
3.3 调整max_open_files
[root@REDIS ~]# echo '* soft nofile 65535' >> /etc/security/limits.conf
[root@REDIS ~]# echo '* hard nofile 65535' >> /etc/security/limits.conf
[root@REDIS ~]# cat /etc/security/limits.conf | tail -2
* soft nofile 65535
* hard nofile 65535
[root@REDIS ~]# echo 'ulimit -SHn 65535' >> /etc/rc.d/rc.local
[root@REDIS ~]# cat /etc/rc.d/rc.local | tail -1
ulimit -SHn 65535
[root@REDIS ~]# chmod +x /etc/rc.d/rc.local #为rc.local增加可执行权限
3.4 重启检查SELinux、firewalld和open files
[root@REDIS ~]# reboot
[root@REDIS ~]# getenforce #查看SELinux是否关闭成功
Disabled #关闭成功
[root@REDIS ~]# systemctl status firewalld #查看防火墙状态
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: inactive (dead) #防火墙已关闭
Docs: man:firewalld(1)
[root@REDIS ~]# systemctl is-enabled firewalld #查看防火墙是否开机自启
disabled #防火墙已禁止开机自启
[root@REDIS ~]# ulimit -a | grep open
open files (-n) 65535 #open files已调整为65535
4 Redis集群环境搭建
方便起见,这里集群环境的所有节点全部位于同一个服务器上,共6个节点以端口号区分,3个主节点+3个从节点。
集群的简单架构如图:
4.1 编译安装redis
编译安装redis需要gcc环境。
将gcc上传到服务器上。以上传到/usr/local/目录下为例:
[root@REDIS ~]# rpm -ivh /usr/local/gcc/*.rpm --force
Preparing... ################################# [100%]
Updating / installing...
……
13:gcc-4.8.5-36.el7_6.2 ################################# [ 93%]
14:gcc-c++-4.8.5-36.el7_6.2 ##########