Redis集群安装

右侧是官网文档(Redis集群规范) http://redis.io/topics/cluster-spec

右侧是上文链接的翻译文档 http://redisdoc.com/topic/cluster-spec.html#cluster-spec

1 、原理简述

此处的集群指的是Cluster, 通过分区/分片来实现一定程度的分布式与高可用部署。

img

2 、集群配置

Redis 最小集群规划,需要包含至少三个主节点,此处测试部署具有三个主服务器和三个从服务器的六节点群集。计划是在一台机器上模拟一个集群,主节点端口7001,7002,7003, 从节点7004,7005,7006,这和生产环境的集群搭建没本质区别。

3 、redis集群安装

安装环境

一台服务器

关闭防火墙和selinux

安装redis

下载Redis源码包

[root@localhost ~]# wget http://download.redis.io/releases/redis-5.0.10.tar.gz

[root@localhost ~]# tar xvzf redis-5.0.10.tar.gz -C /usr/local/

[root@localhost ~]# mv /usr/local/redis-5.0.10/ /usr/local/redis
[root@localhost ~]# cd /usr/local/redis/
[root@localhost redis]# yum -y install gcc make

[root@localhost redis]# make

创建集群目录

[root@localhost ~]# cd /usr/local/

[root@localhost local]# mkdir redis-Cluster/{7001..7006}/data -p

[root@localhost local]# cd redis-Cluster/

[root@localhost redis-Cluster]# vim 7001/redis.conf

port 7001 #端口7000,7001,7002,7003,7004,7005
cluster-enabled yes #开启集群
cluster-config-file nodes.conf #集群配置信息,开启集群后自动生成
cluster-node-timeout 5000 #请求超时时长
appendonly yes #开启aof日志,它会每次写操作都记录一条日志
daemonize yes #redis后台运行
bind 10.36.192.139 #默认ip为127.0.0.1 需要改为其他节点机器可访问的ip 否则创建集群时无法访问对应的端口,无法创建集群
pidfile /usr/local/redis-Cluster/7001/redis_7001.pid #pidfile文件对应7001,7002,7003,7004,7005,7006
dir /usr/local/redis-Cluster/7001/data #数据文件存放位置对应7001,7002,7003,7004,7005,7006

将7001目录下的redis.conf文件拷贝到其他5个目录下并修改端口、pidfile、dir中对应内容

[root@localhost redis-Cluster]# cp 7001/redis.conf 7002/

[root@localhost redis-Cluster]# cp 7001/redis.conf 7003/

[root@localhost redis-Cluster]# cp 7001/redis.conf 7004/

[root@localhost redis-Cluster]# cp 7001/redis.conf 7005/

[root@localhost redis-Cluster]# cp 7001/redis.conf 7006/

使用redis里的redis-server启动

[root@localhost local]# /usr/local/redis/src/redis-server /usr/local/redis-Cluster/7001/redis.conf

[root@localhost local]# /usr/local/redis/src/redis-server /usr/local/redis-Cluster/7002/redis.conf

[root@localhost local]# /usr/local/redis/src/redis-server /usr/local/redis-Cluster/7003/redis.conf

[root@localhost local]# /usr/local/redis/src/redis-server /usr/local/redis-Cluster/7004/redis.conf

[root@localhost local]# /usr/local/redis/src/redis-server /usr/local/redis-Cluster/7005/redis.conf

[root@localhost local]# /usr/local/redis/src/redis-server /usr/local/redis-Cluster/7006/redis.conf

查看是否启动成功

创建集群

[root@localhost local]# /usr/local/redis/src/redis-cli –cluster create –cluster-replicas 1 10.36.192.139:7001 10.36.192.139:7002 10.36.192.139:7003 10.36.192.139:7004 10.36.192.139:7005 10.36.192.139:7006

Performing hash slots allocation on 6 nodes…
Master[0] -> Slots 0 – 5460
Master[1] -> Slots 5461 – 10922
Master[2] -> Slots 10923 – 16383
Adding replica 10.36.192.139:7005 to 10.36.192.139:7001
Adding replica 10.36.192.139:7006 to 10.36.192.139:7002
Adding replica 10.36.192.139:7004 to 10.36.192.139:7003
Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: d413dfa79fc2ec7a91963005890a3d464feaeb08 10.36.192.139:7001
slots:[0-5460] (5461 slots) master
M: 707912d909dbb444eaffa97e10093af5d64e9dff 10.36.192.139:7002
slots:[5461-10922] (5462 slots) master
M: 6659ad514131b6bbc733297e15f8fa6130eb6e4c 10.36.192.139:7003
slots:[10923-16383] (5461 slots) master
S: 698e6187888d4fbc005a97f5126c157e09dbfca7 10.36.192.139:7004
replicates 707912d909dbb444eaffa97e10093af5d64e9dff
S: 297564b89b2824b2d55e45bd8ecc27c0a1f38fd5 10.36.192.139:7005
replicates 6659ad514131b6bbc733297e15f8fa6130eb6e4c
S: 0088c8bcfa252dabe681963b6ddf83e5bf10de7c 10.36.192.139:7006
replicates d413dfa79fc2ec7a91963005890a3d464feaeb08

输入yes
Can I set the above configuration? (type ‘yes’ to accept): yes
Nodes configuration updated
Assign a different config epoch to each node
Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
.
Performing Cluster Check (using node 10.36.192.139:7001)
M: d413dfa79fc2ec7a91963005890a3d464feaeb08 10.36.192.139:7001
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: 6659ad514131b6bbc733297e15f8fa6130eb6e4c 10.36.192.139:7003
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 698e6187888d4fbc005a97f5126c157e09dbfca7 10.36.192.139:7004
slots: (0 slots) slave
replicates 707912d909dbb444eaffa97e10093af5d64e9dff
M: 707912d909dbb444eaffa97e10093af5d64e9dff 10.36.192.139:7002
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: 297564b89b2824b2d55e45bd8ecc27c0a1f38fd5 10.36.192.139:7005
slots: (0 slots) slave
replicates 6659ad514131b6bbc733297e15f8fa6130eb6e4c
S: 0088c8bcfa252dabe681963b6ddf83e5bf10de7c 10.36.192.139:7006
slots: (0 slots) slave
replicates d413dfa79fc2ec7a91963005890a3d464feaeb08
[OK] All nodes agree about slots configuration.
Check for open slots…
Check slots coverage…
[OK] All 16384 slots covered.

客户端登录查看是否成功

[root@localhost local]# redis-cli -c-h 10.36.192.139 -p 7001
10.36.192.139:7001> cluster nodes
6659ad514131b6bbc733297e15f8fa6130eb6e4c 10.36.192.139:7003@17003 master – 0 1699327215277 3 connected 10923-16383
698e6187888d4fbc005a97f5126c157e09dbfca7 10.36.192.139:7004@17004 slave 707912d909dbb444eaffa97e10093af5d64e9dff 0 1699327215000 4 connected
707912d909dbb444eaffa97e10093af5d64e9dff 10.36.192.139:7002@17002 master – 0 1699327214273 2 connected 5461-10922
297564b89b2824b2d55e45bd8ecc27c0a1f38fd5 10.36.192.139:7005@17005 slave 6659ad514131b6bbc733297e15f8fa6130eb6e4c 0 1699327214574 5 connected
d413dfa79fc2ec7a91963005890a3d464feaeb08 10.36.192.139:7001@17001 myself,master – 0 1699327215000 1 connected 0-5460
0088c8bcfa252dabe681963b6ddf83e5bf10de7c 10.36.192.139:7006@17006 slave d413dfa79fc2ec7a91963005890a3d464feaeb08 0 1699327216282 6 connected

redis集群配置成功

4 、验证故障转移

7001和7006是一对主从,将7001进程杀掉7006切换成主库

[root@localhost local]# kill -9 1460

将7006也杀掉集群会坏掉

redis集群部署shell小脚本