如何配置redis原生集群

如题
已邀请:

以单机配置redis集群为例。准备3主3备共计6个节点。



安装系统依赖




yum install ruby rubygems


更新ruby




wget https://cache.ruby-lang.org/pub/ruby/2.7/ruby-2.7.0.tar.gz


 



节点清单




redis16379  主1      redis16479备1
redis16380  主2      redis16480备2
redis16381  主3      redis16481备3


分别修改对应目录下的配置文件,主要调整参数




bind 127.0.0.1
port 16379 #对应节点对应设置
pidfile "/var/run/redis_16380.pid" #对应节点对应设置
logfile "/home/redisCluster/redis16379/16379.log" #对应节点对应设置
dir "/home/redisCluster/redis16379/" #对应节点对应设置
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 15000
appendonly yes


安装openssl



http://nccloud.yytimes.com/q_63.html



升级 ruby



http://nccloud.yytimes.com/q_64.html



安装redis-trib.rb运行依赖的ruby的包redis-XXX.gem




cd /home/redisCluster
wget https://rubygems.org/downloads/redis-4.1.3.gem
gem install redis-4.1.3.gem


启动6个节点




/home/redisCluster/src/redis-server /home/redisCluster/redis16379/redis.conf
/home/redisCluster/src/redis-server /home/redisCluster/redis16380/redis.conf
/home/redisCluster/src/redis-server /home/redisCluster/redis16381/redis.conf
/home/redisCluster/src/redis-server /home/redisCluster/redis16479/redis.conf
/home/redisCluster/src/redis-server /home/redisCluster/redis16480/redis.conf
/home/redisCluster/src/redis-server /home/redisCluster/redis16481/redis.conf


创建集群




/home/redisCluster/src/redis-cli --cluster create 127.0.0.1:16379 127.0.0.1:16380 127.0.0.1:16381 127.0.0.1:16479 127.0.0.1:16480 127.0.0.1:16481 --cluster-replicas 1
等待集群配置完成



>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383

Adding replica 127.0.0.1:16480 to 127.0.0.1:16379
Adding replica 127.0.0.1:16481 to 127.0.0.1:16380
Adding replica 127.0.0.1:16479 to 127.0.0.1:16381

>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 90141301b92594f876598d6ac99ee32360930c93 127.0.0.1:16379
slots:[0-5460] (5461 slots) master
M: 6249fa8bee783808b2a890f0fa9304b76d0292d0 127.0.0.1:16380
slots:[5461-10922] (5462 slots) master
M: bc77c4fc6f836b47e83d241d01aad2297177bb46 127.0.0.1:16381
slots:[10923-16383] (5461 slots) master

S: e4ccfa14595d949e6b52b1925822fa1c34c4fed8 127.0.0.1:16479
replicates bc77c4fc6f836b47e83d241d01aad2297177bb46
S: abc9089dc7602f0437f3e80c4b068d9942a18997 127.0.0.1:16480
replicates 90141301b92594f876598d6ac99ee32360930c93
S: 5b07bd288945ebb3b92f8c056787fad03bd9a597 127.0.0.1:16481
replicates 6249fa8bee783808b2a890f0fa9304b76d0292d0
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:16379)
M: 90141301b92594f876598d6ac99ee32360930c93 127.0.0.1:16379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: 6249fa8bee783808b2a890f0fa9304b76d0292d0 127.0.0.1:16380
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
M: bc77c4fc6f836b47e83d241d01aad2297177bb46 127.0.0.1:16381
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 5b07bd288945ebb3b92f8c056787fad03bd9a597 127.0.0.1:16481
slots: (0 slots) slave
replicates 6249fa8bee783808b2a890f0fa9304b76d0292d0
S: e4ccfa14595d949e6b52b1925822fa1c34c4fed8 127.0.0.1:16479
slots: (0 slots) slave
replicates bc77c4fc6f836b47e83d241d01aad2297177bb46
S: abc9089dc7602f0437f3e80c4b068d9942a18997 127.0.0.1:16480
slots: (0 slots) slave
replicates 90141301b92594f876598d6ac99ee32360930c93
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.


验证集群功能,连接集群需增加-c 和-p 端口参数




/home/redisCluster/src/redis-cli -c -p 16379
127.0.0.1:16379> set nccloud 1
-> Redirected to slot [7504] located at 127.0.0.1:16380
OK
127.0.0.1:16380> get nccloud
"1"
127.0.0.1:16380> exit
[root@rac157 redisCluster]# /home/redisCluster/src/redis-cli -c -p 16481
127.0.0.1:16481> get nccloud
-> Redirected to slot [7504] located at 127.0.0.1:16380
"1"


 

附:redis集群常用命令



连接




/home/redisCluster/src/redis-cli -c -p 16379



集群
cluster info :打印集群的信息
cluster nodes :列出集群当前已知的所有节点( node),以及这些节点的相关信息。
节点
cluster meet :将 ip 和 port 所指定的节点添加到集群当中,让它成为集群的一份子。
cluster forget :从集群中移除 node_id 指定的节点。
cluster replicate :将当前节点设置为 node_id 指定的节点的从节点。
cluster saveconfig :将节点的配置文件保存到硬盘里面。
槽(slot)
cluster addslots [slot ...] :将一个或多个槽( slot)指派( assign)给当前节点。
cluster delslots [slot ...] :移除一个或多个槽对当前节点的指派。
cluster flushslots :移除指派给当前节点的所有槽,让当前节点变成一个没有指派任何槽的节点。
cluster setslot node :将槽 slot 指派给 node_id 指定的节点,如果槽已经指派给
另一个节点,那么先让另一个节点删除该槽>,然后再进行指派。
cluster setslot migrating :将本节点的槽 slot 迁移到 node_id 指定的节点中。
cluster setslot importing :从 node_id 指定的节点中导入槽 slot 到本节点。
cluster setslot stable :取消对槽 slot 的导入( import)或者迁移( migrate)。

cluster keyslot :计算键 key 应该被放置在哪个槽上。
cluster countkeysinslot :返回槽 slot 目前包含的键值对数量。
cluster getkeysinslot :返回 count 个 slot 槽中的键


 

要回复问题请先登录注册