三、redis集群搭建
系列导航
环境:centos7
需要的安装包: redis-4.0.10.tar.gz
说明:如下演示环境都在一台服务器上,生产环境需要放在多台服务器上使用
安装步骤
1、安装包解压
拷贝redis-4.0.10.tar.gz 到/opt目录下
cd /opt
tar -xzvf redis-4.0.10.tar.gz
2、编译
[root]# cd /opt/redis-4.0.10
[root]# make
3、修改配置文件
cd /opt/redis-4.0.10
mkdir 7001
mkdir 7002
mkdir 7003
mkdir 7004
mkdir 7005
mkdir 7006
创建配置文件(其他redis_7002.conf,redis_7003.conf, redis_7004.conf, redis_7005.conf,redis_7006.conf配置文件照样例创建)
vi redis_7001.conf
#添加如下内容:
port 7001
bind 127.0.0.1
#关闭保护模式
protected-mode no
port 7001
tcp-backlog 511
timeout 0
tcp-keepalive 300
#开启守护进程模式
daemonize yes
supervised no
#修改pidfile指向路径
pidfile /opt/redis-4.0.10/7001/redis.pid
loglevel verbose
logfile "/opt/redis-4.0.10/7001/redis_log.log"
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir ./
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-disable-tcp-nodelay no
slave-priority 100
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
slave-lazy-flush no
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit 5000
cluster-enabled yes
#修改集群配置文件指向路径
cluster-config-file nodes-7001.conf
cluster-node-timeout 5000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
4、启动redis
[root@minio4 redis-4.0.10]# /opt/redis-4.0.10/src/redis-server /opt/redis-4.0.10/redis_7001.conf
[root@minio4 redis-4.0.10]# /opt/redis-4.0.10/src/redis-server /opt/redis-4.0.10/redis_7002.conf
[root@minio4 redis-4.0.10]# /opt/redis-4.0.10/src/redis-server /opt/redis-4.0.10/redis_7003.conf
[root@minio4 redis-4.0.10]# /opt/redis-4.0.10/src/redis-server /opt/redis-4.0.10/redis_7004.conf
[root@minio4 redis-4.0.10]# /opt/redis-4.0.10/src/redis-server /opt/redis-4.0.10/redis_7005.conf
[root@minio4 redis-4.0.10]# /opt/redis-4.0.10/src/redis-server /opt/redis-4.0.10/redis_7006.conf
5、ruby安装(这个很讨厌)
[root@minio4 ~]# rpm -qa | grep ruby
[root@minio4 ~]# yum install ruby
ruby安装后是2.0版本需要升级到3.0版(需要和redis的版本匹配,本利redis4.0版)
如何升级参考如下道友写的博客亲测有用
https://www.cnblogs.com/name-lizonglin/p/12069114.html
6、创建集群
[root@minio4 src]# cd /opt/redis-4.0.10/src
[root@minio4 src]# ./redis-trib.rb create --replicas 1 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
127.0.0.1:7001
127.0.0.1:7002
127.0.0.1:7003
Adding replica 127.0.0.1:7005 to 127.0.0.1:7001
Adding replica 127.0.0.1:7006 to 127.0.0.1:7002
Adding replica 127.0.0.1:7004 to 127.0.0.1:7003
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: ef2efc3117d9da0a399cba785ecc0b3e000fdfe6 127.0.0.1:7001
slots:0-5460 (5461 slots) master
M: 276d90d980c5db413f65abe761c68c12429906a3 127.0.0.1:7002
slots:5461-10922 (5462 slots) master
M: 7bdbb165ac29d6bd803be1d69a122800a2803bd9 127.0.0.1:7003
slots:10923-16383 (5461 slots) master
S: 0cac24e2a8891b24a33974e239873c4ee7a7167f 127.0.0.1:7004
replicates 7bdbb165ac29d6bd803be1d69a122800a2803bd9
S: 4612663207fc597112cc5f9211c9b75b393fa717 127.0.0.1:7005
replicates ef2efc3117d9da0a399cba785ecc0b3e000fdfe6
S: d1098bdc5d592f2269603765c4b75f30d899c9b4 127.0.0.1:7006
replicates 276d90d980c5db413f65abe761c68c12429906a3
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 127.0.0.1:7001)
M: ef2efc3117d9da0a399cba785ecc0b3e000fdfe6 127.0.0.1:7001
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: 7bdbb165ac29d6bd803be1d69a122800a2803bd9 127.0.0.1:7003
slots:10923-16383 (5461 slots) master
1 additional replica(s)
M: 276d90d980c5db413f65abe761c68c12429906a3 127.0.0.1:7002
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: d1098bdc5d592f2269603765c4b75f30d899c9b4 127.0.0.1:7006
slots: (0 slots) slave
replicates 276d90d980c5db413f65abe761c68c12429906a3
S: 0cac24e2a8891b24a33974e239873c4ee7a7167f 127.0.0.1:7004
slots: (0 slots) slave
replicates 7bdbb165ac29d6bd803be1d69a122800a2803bd9
S: 4612663207fc597112cc5f9211c9b75b393fa717 127.0.0.1:7005
slots: (0 slots) slave
replicates ef2efc3117d9da0a399cba785ecc0b3e000fdfe6
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
7、测试集群
[root@minio4 7001]# cd /opt/redis-4.0.10/src
[root@minio4 src]# ./redis-cli -c -h localhost -p 7001
localhost:7001> set foo bar
-> Redirected to slot [12182] located at 127.0.0.1:7003
OK
127.0.0.1:7003> set hello world
-> Redirected to slot [866] located at 127.0.0.1:7001
OK
127.0.0.1:7001>
cluster info :打印集群的信息
cluster nodes :列出集群当前已知的所有节点( node),以及这些节点的相关信息
如下:
127.0.0.1:7003> cluster nodes
7bdbb165ac29d6bd803be1d69a122800a2803bd9 127.0.0.1:7003@17003 myself,master - 0 1620472701000 3 connected 10923-16383
4612663207fc597112cc5f9211c9b75b393fa717 127.0.0.1:7005@17005 slave ef2efc3117d9da0a399cba785ecc0b3e000fdfe6 0 1620472701000 5 connected
ef2efc3117d9da0a399cba785ecc0b3e000fdfe6 127.0.0.1:7001@17001 master - 0 1620472701000 1 connected 0-5460
0cac24e2a8891b24a33974e239873c4ee7a7167f 127.0.0.1:7004@17004 slave 7bdbb165ac29d6bd803be1d69a122800a2803bd9 0 1620472702285 4 connected
d1098bdc5d592f2269603765c4b75f30d899c9b4 127.0.0.1:7006@17006 slave 276d90d980c5db413f65abe761c68c12429906a3 0 1620472702584 6 connected
276d90d980c5db413f65abe761c68c12429906a3 127.0.0.1:7002@17002 master - 0 1620472701284 2 connected 5461-10922
127.0.0.1:7003> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:3
cluster_stats_messages_ping_sent:634
cluster_stats_messages_pong_sent:662
cluster_stats_messages_meet_sent:4
cluster_stats_messages_sent:1300
cluster_stats_messages_ping_received:660
cluster_stats_messages_pong_received:638
cluster_stats_messages_meet_received:2
cluster_stats_messages_received:1300
到此集群搭建成功
如果后期集群不好用之后彻底恢复
cd /opt/redis-4.0.10
rm -f appendonly.aof dump.rdb nodes-7001.conf nodes-7002.conf nodes-7003.conf nodes-7004.conf nodes-7005.conf nodes-7006.conf
然后重建集群
[root@minio4 src]# cd /opt/redis-4.0.10/src
[root@minio4 src]# ./redis-trib.rb create --replicas 1 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006