*** 集群对应的主从节点如果同时down,则整个redis集群无法工作
*** 部分是对网上资源的整理,部分是安装过程中存在问题的总结
准备、环境
centos1:192.168.215.156 A
centos2:192.168.215.157 B
yum -y install zlib ruby rubygems
gem install redis
查看ruby安装的软件的结合
gem list
安装redis
[root@A opt]# tar -zxvf redis-3.2.2.tar.gz
[root@A opt]# cd redis-3.2.2
[root@A redis-3.2.2]# cd src/
[root@A src]# make && make install
redis 设置集群的配置
[[email protected] redis-3.2.2]# mkdir cluster/
[[email protected] cluster]# mkdir 7000
[[email protected] cluster]# mkdir 7001
[[email protected] cluster]# mkdir 7002
[[email protected] cluster]# mkdir 7003
[[email protected] cluster]# mkdir 7004
[[email protected] cluster]# mkdir 7005
修改redis的配置文件
[[email protected] 7000]# cat redis.conf
port 7000
#端口7000,7002,7003
bind 192.168.215.156
#默认ip为127.0.0.1 需要改为其他节点机器可访问的ip或者网段,否则创建集群时无法访问对应的端口,无法创建集群
daemonize yes
#redis后台运行
pidfile /var/run/redis_7000.pid
#pidfile文件对应7000,7001,7002
cluster-enabled yes
#开启集群 把注释#去掉
cluster-config-file nodes_7000.conf
#集群的配置 配置文件首次启动自动生成 7000,7001,7002
cluster-node-timeout 15000
#请求超时 默认15秒,可自行设置
appendonly yes
#数据存放AOF
开启每一台机器上的redis的服务
[[email protected] redis-3.2.2]# redis-server cluster/7000/redis.conf
查看端口信息
[[email protected] redis-3.2.2]# ps -ef|grep redis
root 11302 1 0 11:41 ? 00:00:03 redis-server 192.168.215.156:7000 [cluster]
root 11306 1 0 11:41 ? 00:00:03 redis-server 192.168.215.156:7001 [cluster]
root 11310 1 0 11:41 ? 00:00:03 redis-server 192.168.215.156:7002 [cluster]
root 12125 2558 0 12:00 pts/4 00:00:00 redis-cli -h 192.168.215.156 -c -p 7000
root 12135 5806 0 12:15 pts/0 00:00:00 grep redis
开启防火墙iptables策略
例如:6379端口,需要放开6379和16379;6380需要开放6380和16380端口
原则上+10000
保护模式需要修改:protected-mode no
创建集群
[[email protected] redis-3.2.2]# redis-trib.rb create --replicas 1 A:7000 A:7001 A:7002 B:7003 B:7004 B:7005
----replicas 1 标识主节点下有1个从节点
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.215.157:7003
192.168.215.156:7000
192.168.215.157:7004
.
.
.
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
测试集群
集群的链接redis-cli -h host -c -p port
[[email protected] cluster]# redis-cli -h 192.168.215.156 -c -p 7000
[[email protected] redis-3.2.2]# redis-cli -h 192.168.215.157 -c -p 7003
192.168.215.157:7003> SET "xiaozhang" "男"
-> Redirected to slot [6387] located at 192.168.215.156:7000
OK
192.168.215.156:7000> KEYS *
1) "xiaozhang"
192.168.215.156:7000> get "xiaozhang"
"\xe7\x94\xb7"
节点查询 > cluster nodes
./redis-trib.rb check 127.0.0.1:7002
cluster 扩容,增加节点
下面的命令将7006做为master添加到cluster中
./redis-trib.rb add-node IP:7006 IP:7000
把7007当成slave加入
./redis-trib.rb add-node --slave --master-id 226d1af3c95bf0798ea9fed86373b89347f889da 127.0.0.1:7007 127.0.0.1:7000
增加新的节点之后,问题就来了,16384个slot已经被其它3组节点分完了,新节点没有slot,没办法存放缓存,所以需要将slot重新分布。
查看:
? src ./redis-trib.rb info 127.0.0.1:7000
127.0.0.1:7000 (0b7e0d53...) -> 4 keys | 5461 slots | 1 slaves.
127.0.0.1:7001 (e0e8dfdd...) -> 4 keys | 5462 slots | 1 slaves.
127.0.0.1:7006 (226d1af3...) -> 0 keys | 0 slots | 1 slaves. #7006上完全没有slot
127.0.0.1:7002 (ec964a7c...) -> 9 keys | 5461 slots | 1 slaves.
[OK] 17 keys in 4 masters.
0.00 keys per slot on average.
./redis-trib.rb reshard 127.0.0.1:7000
删除节点del-nod
./redis-trib.rb del-node 127.0.0.1:7006 88e16f91609c03277f2ee6ce5285932f58c221c1
时间: 2024-10-29 08:19:34