redis集群

Posted 鸽子咕咕咕

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了redis集群相关的知识,希望对你有一定的参考价值。

关闭虚拟防火墙

systemctl stop firewalld
setenforce 0

搭建redis

yum install gcc tcl -y
wget http://download.redis.io/releases/redis-6.2.6.tar.gz
tar -zxvf redis-6.2.6.tar.gz
cd redis-6.2.6
make && make install
redis-server

 

###配置
vim redis.conf
bind 0.0.0.0
daemonize yes
###启动
redis-server redis.conf

 

###复制
cp redis.conf 6384.conf
cp redis.conf 6383.conf
cp redis.conf 6382.conf
cp redis.conf 6381.conf
cp redis.conf 6380.conf
###配置
# 端口号
port 6380
# 后台启动
daemonize yes
# 开启集群
cluster-enabled yes
#集群节点配置文件
cluster-config-file nodes-7000.conf
# 集群连接超时时间
cluster-node-timeout 5000

写个start.sh文件

redis-server 6384.conf
redis-server 6383.conf
redis-server 6382.conf
redis-server 6381.conf
redis-server 6380.conf
redis-server redis.conf
###给权限
chmod +x start.sh
###启动
./ start.sh

创建集群

cluster create#创建集群
[root@localhost geizi]# redis-cli --cluster create --cluster-replicas 1 0.0.0.0:6379 0.0.0.0:6380 0.0.0.0:6381 0.0.0.0:6382 0.0.0.0:6383 0.0.0.0:6384###创建集群
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 0.0.0.0:6383 to 0.0.0.0:6379
Adding replica 0.0.0.0:6384 to 0.0.0.0:6380
Adding replica 0.0.0.0:6382 to 0.0.0.0:6381
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: c25e541fa88c2cc7d5e38dd4d479291f6f5ed093 0.0.0.0:6379
   slots:[0-5460] (5461 slots) master
M: 53898f2add237a1363366ddae5794b8f81a10aab 0.0.0.0:6380
   slots:[5461-10922] (5462 slots) master
M: dad065a7da328fd6de26d98d12070ba1111f3276 0.0.0.0:6381
   slots:[10923-16383] (5461 slots) master
S: 6503e52fe3e06915bf9eb1aa4dfce3a43a2a60d2 0.0.0.0:6382
   replicates dad065a7da328fd6de26d98d12070ba1111f3276
S: 8b6e12734bece00408d8ae9b209eb2df251b8a35 0.0.0.0:6383
   replicates c25e541fa88c2cc7d5e38dd4d479291f6f5ed093
S: 48b0f5149eab6f06fd1cbb9628572b43b788f6c5 0.0.0.0:6384
   replicates 53898f2add237a1363366ddae5794b8f81a10aab
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 0.0.0.0:6379)
M: c25e541fa88c2cc7d5e38dd4d479291f6f5ed093 0.0.0.0:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 8b6e12734bece00408d8ae9b209eb2df251b8a35 127.0.0.1:6383
   slots: (0 slots) slave
   replicates c25e541fa88c2cc7d5e38dd4d479291f6f5ed093
S: 48b0f5149eab6f06fd1cbb9628572b43b788f6c5 127.0.0.1:6384
   slots: (0 slots) slave
   replicates 53898f2add237a1363366ddae5794b8f81a10aab
S: 6503e52fe3e06915bf9eb1aa4dfce3a43a2a60d2 127.0.0.1:6382
   slots: (0 slots) slave
   replicates dad065a7da328fd6de26d98d12070ba1111f3276
M: 53898f2add237a1363366ddae5794b8f81a10aab 127.0.0.1:6380
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
M: dad065a7da328fd6de26d98d12070ba1111f3276 127.0.0.1:6381
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

 

进入集群

[root@localhost geizi]# redis-cli -c -p 6379
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> set name 123456
-> Redirected to slot [5798] located at 127.0.0.1:6380
OK
127.0.0.1:6380> get name
"123456"
127.0.0.1:6380>

查看集群节点

127.0.0.1:6379> cluster nodes
20737e6a4611332665243710bc6a38444d6f2064 127.0.0.1:6381@16381 master - 0 1647913788638 3 connected 10923-16383
d42104ee4eabd44fab9c6cdc5ae76be4d067b455 127.0.0.1:6384@16384 slave 873bd7d0c170ba651da1e744e540703a1087f6e8 0 1647913787000 2 connected
873bd7d0c170ba651da1e744e540703a1087f6e8 127.0.0.1:6380@16380 master - 0 1647913787558 2 connected 5461-10922
a32d9ce52a97df4fe6841ae064a0714e9c1ccc99 127.0.0.1:6382@16382 slave 20737e6a4611332665243710bc6a38444d6f2064 0 1647913787000 3 connected
5c2ed3682f46cfe6b2b7e928ff5764143ed1dad8 127.0.0.1:6383@16383 slave 61d33488d8d2d1393f64cee6370e8dd282a27580 0 1647913788531 1 connected
61d33488d8d2d1393f64cee6370e8dd282a27580 127.0.0.1:6379@16379 myself,master - 0 1647913787000 1 connected 0-5460
127.0.0.1:6379>

 

以上是关于redis集群的主要内容,如果未能解决你的问题,请参考以下文章

redis集群为啥要ruby

如何安装Redis集群

redis集群介绍redis集群搭建配置redis集群操作

分布式缓存技术redis学习系列——redis高级应用(集群搭建集群分区原理集群操作)

Redis集群模式

redis高级应用(集群搭建集群分区原理集群操作)