Linux - redis-cluster搭建
Linux - redis-cluster搭建
1.准备6个数据库节点,也就是6个redis实例,也就是6个配置文件
配置文件如下
redis-7000.conf redis-7002.conf redis-7004.conf
redis-7001.conf redis-7003.conf redis-7005.conf
配置文件,内容如下
port 7000
daemonize yes
dir "/opt/data"
logfile "7000.log"
dbfilename "dump-7000.rdb"
cluster-enabled yes
cluster-config-file nodes-7000.conf
cluster-require-full-coverage no
分别准备6个redis节点,通过sed命令快速创建,仅仅是端口的区分
[root@master clusterredis]# sed "s/7000/7001/g" redis-7000.conf > redis-7001.conf
[root@master clusterredis]#
[root@master clusterredis]# sed "s/7000/7002/g" redis-7000.conf > redis-7002.conf
[root@master clusterredis]# sed "s/7000/7003/g" redis-7000.conf > redis-7003.conf
[root@master clusterredis]#
[root@master clusterredis]#
[root@master clusterredis]#
[root@master clusterredis]# sed "s/7000/7004/g" redis-7000.conf > redis-7004.conf
[root@master clusterredis]# sed "s/7000/7005/g" redis-7000.conf > redis-7005.conf
2.分别启动6个数据库实例
root@master clusterredis]# redis-server redis-7000.conf
[root@master clusterredis]# redis-server redis-7001.conf
[root@master clusterredis]# redis-server redis-7002.conf
[root@master clusterredis]# redis-server redis-7003.conf
[root@master clusterredis]# redis-server redis-7004.conf
[root@master clusterredis]# redis-server redis-7005.conf
3.开始分配redis数据库的槽位, 通过ruby脚本,自动化分配槽位
4.配置ruby的解释器环境
1.下载源码
wget https://cache.ruby-lang.org/pub/ruby/2.3/ruby-2.3.1.tar.gz
2.释放编译文件
#安装ruby
tar -xvf ruby-2.3.1.tar.gz
./configure --prefix=/opt/ruby/
make && make install
3.配置ruby的环境变量
/opt/ruby/bin
source /etc/profile
vim /etc/profile
4.下载ruby操作redis的模块
wget http://rubygems.org/downloads/redis-3.3.0.gem
5.通过ruby的包管理工具安装它
gem install -l redis-3.3.0.gem
6.通过ruby的脚本,自动化分配redis虚拟槽位,开启集群功能
/opt/redis-4.0.10/src/redis-trib.rb create --replicas 1 127.0.0.1:7000 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
7.验证集群是否开启
redis-cli -p 7000 cluster info
如下:
[root@yugo /opt/redis/src 18:42:14]#redis-cli -p 7000 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:1
cluster_stats_messages_ping_sent:10468
cluster_stats_messages_pong_sent:10558
cluster_stats_messages_sent:21026
cluster_stats_messages_ping_received:10553
cluster_stats_messages_pong_received:10468
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:21026
8.登陆redis集群,写入数据,查看数据流向
127.0.0.1:7000> set name chao
-> Redirected to slot [5798] located at 127.0.0.1:7001
OK
127.0.0.1:7001> exit
[root@yugo /opt/redis/src 18:46:07]#redis-cli -c -p 7000
127.0.0.1:7000> ping
PONG
127.0.0.1:7000> keys *
(empty list or set)
127.0.0.1:7000> get name
-> Redirected to slot [5798] located at 127.0.0.1:7001
"chao"
数据会在集群中,在节点间重定向
9.redis集群到此完毕