使用阿里云主机搭建,一台阿里云模拟6个节点,3主3从。
下面开始搭建,不用动脑子,跟着复制粘贴即可
cd /opt
//创建集群目录
mkdir redis_cluster
//进入redis_cluster目录
cd redis_cluster
//下载redis安装包
wget http://download.redis.io/releases/redis-5.0.9.tar.gz
//解压redis安装包
tar -zxvf redis-5.0.9.tar.gz
cd redis-5.0.9
//编译redis
make && make install
//编译完成,修改目录名称
mv redis-5.0.9 7001
//清空redis.conf配置文件
cd 7001
cat /dev/null > redis.conf
//编辑redis.conf文件,添加以下配置
# 端口7001,7002,7003,7004,7005,7006
port 7001
# 默认ip为127.0.0.1,如果用多台机器搭建集群,这个地方需要改成实际的ip
bind 127.0.0.1
# redis后台运行
daemonize yes
# pidfile文件对应7001,7002,7003,7004,7005,7006
pidfile /var/run/redis_7001.pid
# 开启集群 把注释#去掉
cluster-enabled yes
# 集群的配置 配置文件首次启动自动生成7001,7002,7003,7004,7005,7006
cluster-config-file nodes_7001.conf
# 请求超时 默认15秒,可自行设置
cluster-node-timeout 15000
# aof日志开启 有需要就开启,它会每次写操作都记录一条日志
appendonly yes
# 错误日志,7001,7002,7003,7004,7005,7006。记得改
logfile /opt/redis_cluster/7001/redis_log.log
将7001,在当前目录复制5份,分别命名为7002,7003,7004,7005,7006
分别启动6台redis节点
//替换7001为7002,7003,7004,7005,7005
/opt/redis_cluster/7001/src/redis-server /opt/redis_cluster/7001/redis.conf
查看是否启动成功
ps -ef|grep redis
看到以下输出,说明6个节点全部启动成功
root 7796 1 0 22:15 ? 00:00:01 /opt/redis_cluster/7002/src/redis-server 127.0.0.1:7002 [cluster]
root 7825 1 0 22:15 ? 00:00:01 /opt/redis_cluster/7003/src/redis-server 127.0.0.1:7003 [cluster]
root 7854 1 0 22:16 ? 00:00:01 /opt/redis_cluster/7004/src/redis-server 127.0.0.1:7004 [cluster]
root 7883 1 0 22:16 ? 00:00:01 /opt/redis_cluster/7005/src/redis-server 127.0.0.1:7005 [cluster]
root 7912 1 0 22:16 ? 00:00:01 /opt/redis_cluster/7006/src/redis-server 127.0.0.1:7006 [cluster]
root 7996 1 0 22:16 ? 00:00:01 /opt/redis_cluster/7001/src/redis-server 127.0.0.1:7001 [cluster]
使用redis-cli命令创建集群
redis-cli --cluster create 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 --cluster-replicas 1
可能会报错:缺少ruby环境,redis集群的安装需要依赖ruby环境
使用yum命令安装ruby环境
yum -y install ruby ruby-devel rubygems rpm-build
看到如下日志输出,即为安装ruby成功
//省略大部分日志,只保留末尾的日志..
Installed:
rpm-build.x86_64 0:4.11.3-48.el7_9 ruby.x86_64 0:2.0.0.648-39.el7_9 ruby-devel.x86_64 0:2.0.0.648-39.el7_9 rubygems.noarch 0:2.0.14.1-39.el7_9
Dependency Installed:
bzip2.x86_64 0:1.0.6-13.el7 elfutils.x86_64 0:0.176-5.el7 gdb.x86_64 0:7.6.1-120.el7
libyaml.x86_64 0:0.1.4-11.el7_0 perl-Thread-Queue.noarch 0:3.02-2.el7 ruby-irb.noarch 0:2.0.0.648-39.el7_9
ruby-libs.x86_64 0:2.0.0.648-39.el7_9 rubygem-bigdecimal.x86_64 0:1.2.0-39.el7_9 rubygem-io-console.x86_64 0:0.4.2-39.el7_9
rubygem-json.x86_64 0:1.7.7-39.el7_9 rubygem-psych.x86_64 0:2.0.0-39.el7_9 rubygem-rdoc.noarch 0:4.0.0-39.el7_9
unzip.x86_64 0:6.0-24.el7_9
Complete!
继续执行redis-cli命令创建集群
redis-cli --cluster create 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 --cluster-replicas 1
看到以下输出,即为redis集群开始搭建
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 28d9b2f458df1ab8e96ad5129b84069ea7481e45 127.0.0.1:7001
slots:[0-5460] (5461 slots) master
M: 94dbde626faadff257bacdb83f2e12178bfba4f2 127.0.0.1:7002
slots:[5461-10922] (5462 slots) master
M: 3d30f0bc0c1111b27ac417b76fbbe8a13aac67b8 127.0.0.1:7003
slots:[10923-16383] (5461 slots) master
S: 1166dfccde9774bfd7d3ecfe027a192e33ffd22a 127.0.0.1:7004
replicates 94dbde626faadff257bacdb83f2e12178bfba4f2
S: 11541027a13b57be07e34bcbb1261d29a70bec59 127.0.0.1:7005
replicates 3d30f0bc0c1111b27ac417b76fbbe8a13aac67b8
S: c8fe97a667e8d999cb0976b8fc8608edf215f0fc 127.0.0.1:7006
replicates 28d9b2f458df1ab8e96ad5129b84069ea7481e45
Can I set the above configuration? (type 'yes' to accept): yes
输入yes,开始创建集群
看到如下日志,说明redis集群搭建成功
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
连接redis集群看看效果,登录7001节点
//-c : 连接redis集群
redis-cli -h 127.0.0.1 -c -p 7001
设置一个String键值对
127.0.0.1:7001> set testKey testValue
-> Redirected to slot [11136] located at 127.0.0.1:7003
OK
127.0.0.1:7003>
可以看到我们从7001节点上执行的set命令,redis集群给我们重定向到了7003节点上。这个原因是:redis集群是用哈希槽存储数据的,一共16384个哈希槽,分布在6个节点上。每次set数据,redis会使用crc16算法计算key应该被分配到哪个哈希槽上,然后会直接重定向到哈希槽对应的节点上
版权声明:本文为qq_39839075原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。