Redis集群特性:
redis集群方式支持主从自动切换
redis集群只有一个库,单例或者主从的话有多个库
redis集群去中心化,只要通过其中一个入口连接即可
实验环境
主机名 IP 端口
node1 192.168.14.210 7001、8001
node2 192.168.14.211 7002、8002
node3 192.168.14.212 7003、8003
一、安装Redis
1、关闭防火墙和selinux
[root@node1 ~]# systemctl stop firewalld
[root@node1 ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
2、下载redis
[root@node1 ~]# wget http://download.redis.io/releases/redis-5.0.7.tar.gz
3、安装编译依赖环境
[root@node1 ~]# yum install -y wget gcc gcc-c++ make tar openssl openssl-devel cmake
4、编译安装
[root@node1 ~]# tar -zxvf redis-5.0.7.tar.gz -C /usr/local/
[root@node1 ~]# cd /usr/local/redis-5.0.7/
[root@node1 redis-5.0.7]# make && make install
node2和node3操作同上一致,此处省略
二、Redis集群部署
redis自带集群搭建,一般使用三主三从来构建,演示主使用7000 7001 7002,从使用8000 8001 8002
1、创建自定义配置文件存放目录
[root@node1 ~]# mkdir -p /usr/local/redis/cluster
2、拷贝配置文件
[root@node1 ~]# cp /usr/local/redis-5.0.7/redis.conf /usr/local/redis/cluster/redis_7001.conf
[root@node1 ~]# cp /usr/local/redis-5.0.7/redis.conf /usr/local/redis/cluster/redis_8001.conf
3、创建数据存放路径,修改属主和属组
[root@node1 ~]# useradd redis
[root@node1 ~]# chown -R redis:redis /usr/local/redis
[root@node1 ~]# mkdir -p /data/redis/cluster/{redis_7001,redis_8001}
[root@node1 ~]# chown -R redis:redis /data/redis
4、修改配置文件
[root@node1 ~]# cat /usr/local/redis/cluster/redis_7001.conf
bind 192.168.14.210
port 7001
daemonize yes
pidfile "/var/run/redis_7001.pid"
logfile "/usr/local/redis/cluster/redis_7001.log"
dir "/data/redis/cluster/redis_7001"
#replicaof 192.168.14.210 6379
masterauth 123456
requirepass 123456
appendonly yes
cluster-enabled yes
cluster-config-file nodes_7001.conf
cluster-node-timeout 15000
[root@node1 ~]# cat /usr/local/redis/cluster/redis_8001.conf
bind 192.168.14.210
port 8001
daemonize yes
pidfile "/var/run/redis_8001.pid"
logfile "/usr/local/redis/cluster/redis_8001.log"
dir "/data/redis/cluster/redis_8001"
#replicaof 192.168.14.210 6379
masterauth 123456
requirepass 123456
appendonly yes
cluster-enabled yes
cluster-config-file nodes_8001.conf
cluster-node-timeout 15000
node2和node3操作基本同上,只是端口需要修改,此处省略
5、三台主机启动redis
#node1
[root@node1 ~]# redis-server /usr/local/redis/cluster/redis_7001.conf
[root@node1 ~]# redis-server /usr/local/redis/cluster/redis_8001.conf
#node2
[root@node2 ~]# redis-server /usr/local/redis/cluster/redis_7002.conf
[root@node2 ~]# redis-server /usr/local/redis/cluster/redis_8002.conf
#node3
[root@node3 ~]# redis-server /usr/local/redis/cluster/redis_7003.conf
[root@node3 ~]# redis-server /usr/local/redis/cluster/redis_8003.conf
6、启动集群
[root@node1 ~]# redis-cli -a 123456 --cluster create 192.168.14.210:7001 192.168.14.210:8001 192.168.14.211:7002 192.168.14.211:8002 192.168.14.212:7003 192.168.14.212:8003 --cluster-replicas 1
三、验证集群
1、连接任意一台客户端即可
[root@node1 ~]# redis-cli -c -h 192.168.14.210 -p 7001 -a 123456
提示:-a访问服务端密码,-c表示集群模式,指定ip地址和端口号
2、查看集群状态(主从关系)
192.168.14.210:7001> CLUSTER NODES
3、查看集群信息
192.168.14.210:7001> CLUSTER INFO
4、在node1写入数据,在node2能查询到数据;
另外,集群数据被分为三份,采用哈希槽 (hash slot)的方式来分配16384个slot的话,它们三个节点分别承担的slot 区间是:
节点node1覆盖0-5460
节点node2覆盖5461-10922
节点node3覆盖10923-16383