Redis4.0集群

节点信息规划:
准备部署7001-7004 4个实例,但是redis集群提示最少需要3个主节点,6个节点才能完成启动,所以后面2个是后来加的。
注意:这里因为是测试环境,所以将所有实例都放在了一台机器上,生产建议主备节点不要放在一台机器上
9.png

1、环境安装
~]# cd /opt/
opt]# wget http://download.redis.io/releases/redis-4.0.11.tar.gz
opt]# tar zxf redis-4.0.11.tar.gz
opt]# cd redis-4.0.11
opt]# make test
#yum 安装tcl
opt]# yum install tcl -y
opt]# make test
#将redis的执行脚本拷贝到PATH变量所在目录或加入全局环境变量
opt]# cp -a src/redis-server src/redis-cli src/redis-sentinel src/redis-trib.rb src/redis-check-aof src/redis-check-rdb src/redis-benchmark /usr/local/bin/
2、配置redis集群文件
opt]# mkdir -p redis-cluster
opt]# mkdir -p /opt/redis-cluster/nodes-{7001,7002,7003,7004}
opt]# ln -s redis-4.0.11 redis
按需修改配置文件(以nodes-7001实例为例)
[root@shenghui-ansible opt]# egrep -v "^#|^$" redis-cluster/nodes-7001/redis.conf 
bind 192.168.56.67
protected-mode yes
port 7001
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile redis_7001.pid
loglevel notice
logfile "/opt/redis-cluster/nodes-7001/redis_7001.log"
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /opt/redis-cluster/nodes-7001/
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
slave-lazy-flush no
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble no
lua-time-limit 5000
cluster-enabled yes
cluster-config-file nodes-7001.conf
cluster-node-timeout 5000
cluster-slave-validity-factor 10
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
3、启动redis及创建集群
依次启动各个redis实例
]# redis-server /opt/redis-cluster/nodes-7001/redis.conf
]# redis-server /opt/redis-cluster/nodes-7002/redis.conf
]# redis-server /opt/redis-cluster/nodes-7003/redis.conf
]# redis-server /opt/redis-cluster/nodes-7004/redis.conf
4、创建redis集群
创建redis集群使用redis-trib.rb命令,该命令是基于ruby的脚本,需要安装ruby,ruby-gem以及gem-redis
#如果有老版本,卸载ruby2.0.0,安装ruby2.2.2以上版本
[root@elk01 ~]# ruby -v  #centos7 忽略
ruby 2.0.0p648 (2015-12-16) [x86_64-linux]
[root@elk01 ~]# yum erase ruby
~]# wget https://cache.ruby-lang.org/pub/ruby/2.5/ruby-2.5.3.tar.gz
~]# tar zxf ruby-2.5.3.tar.gz
~]# cd ruby-2.5.3/
ruby-2.5.3]# ./configure
ruby-2.5.3]# make
安装ruby-redis.gem
推荐:如果默认国外的gem源连不上或者很慢,可以试试国内源
添加国内源命令:gem source -a https://gems.ruby-china.com
删除国外源并添加国内源:gem sources --add https://gems.ruby-china.com/ --remove https://rubygems.com/
#检测gem源,注意RubyGems源尽量用新版本,官方建议2.6.X,但是我用2.5.2也没什么问题
[root@shenghui-ansible ~]# gem source -l
*** CURRENT SOURCES ***

https://gems.ruby-china.com
redis-cluster]# gem install redis -v 3.3.5 #注:不要使用4.x版本,不然后期增加实例,迁移哈希槽会失败
创建集群报错:
root@elk01 redis-cluster]# redis-trib.rb create --replicas 1 192.168.56.67:7001 192.168.56.67:7002 192.168.56.67:7003 192.168.56.67:7004
>>> Creating cluster
*** ERROR: Invalid configuration for cluster creation. #错误表示redis集群至少要3个主,6个节点
*** Redis Cluster requires at least 3 master nodes.
*** This is not possible with 4 nodes and 1 replicas per node.
*** At least 6 nodes are required.
#新增两个节点
redis-cluster]# mkdir -p nodes-{7005,7006}
#拷贝并修改下redis配置ip
redis-cluster]# cp nodes-7001/redis.conf nodes-7005/
redis-cluster]# cp nodes-7001/redis.conf nodes-7006/
启动redis
redis-server /opt/redis-cluster/nodes-7005/redis.conf
redis-server /opt/redis-cluster/nodes-7006/redis.conf
[root@shenghui-ansible ~]# ps -ef|grep redis
root 26651 1 0 Nov13 ? 00:01:10 redis-server 127.0.0.1:7002 [cluster]
root 26656 1 0 Nov13 ? 00:01:10 redis-server 127.0.0.1:7003 [cluster]
root 26661 1 0 Nov13 ? 00:01:09 redis-server 127.0.0.1:7004 [cluster]
root 26666 1 0 Nov13 ? 00:01:10 redis-server 127.0.0.1:7005 [cluster]
root 26671 1 0 Nov13 ? 00:01:09 redis-server 127.0.0.1:7006 [cluster]
root 26725 1 0 Nov13 ? 00:01:11 redis-server 127.0.0.1:7001 [cluster]
#创建集群成功
redis-cluster]# redis-trib.rb create --replicas 1 192.168.56.67:7001 192.168.56.67:7002 192.168.56.67:7003 192.168.56.67:7004 192.168.56.67:7005 192.168.56.67:7006
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.56.67:7001
192.168.56.67:7002
192.168.56.67:7003
Adding replica 192.168.56.67:7004 to 192.168.56.67:7001
Adding replica 192.168.56.67:7005 to 192.168.56.67:7002
Adding replica 1192.168.56.67:7006 to 192.168.56.67:7003
M: 23df12c2bafde34f5bdd53d3463ad20b8ab507d2 192.168.56.67:7001
集群测试:
[root@shenghui-ansible ~]# redis-cli -c -p 7001 #登陆操作集群,-c参数是必须的
127.0.0.1:7001> 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:7
cluster_my_epoch:7
cluster_stats_messages_ping_sent:84881
...
127.0.0.1:7001> cluster nodes
21da5db87c968a926ac98db20c6b3e5db98e86b4 192.168.56.67:7001@17001 myself,slave 64f7850f021d1150b41fc654f247e075f8573813 0 1542181496000 1 connected
bbba5557051520f7d825819415a806b8716f811c 127.0.0.1:7002@17002 master - 0 1542181497150 2 connected 5461-10922
f707863e9846cf9e7c68fd7cdae5b72aa4f6839b 127.0.0.1:7004@17004 slave 66ebc6a414a533a9a2ed27fbcdbea88b9d8ed4db 0 1542181498161 4 connected
ecdf34327e274a8294f7225ddbb5d3f4cd0b7bde 127.0.0.1:7006@17006 slave bbba5557051520f7d825819415a806b8716f811c 0 1542181497000 6 connected
64f7850f021d1150b41fc654f247e075f8573813 127.0.0.1:7005@17005 master - 0 1542181495000 7 connected 0-5460
66ebc6a414a533a9a2ed27fbcdbea88b9d8ed4db 127.0.0.1:7003@17003 master - 0 1542181495132 3 connected 10923-16383
127.0.0.1:7001> set liubai 'papa'
-> Redirected to slot [11252] located at 127.0.0.1:7003
OK
127.0.0.1:7003> get liubai
"papa"
nodes-7001宕机查看数据(redis集群去中心化,连接哪一台都可以):
~]# kill -9 26725  #生长不建议使用kill,登陆redis-cli 使用shutdown关闭redis
[root@shenghui-ansible ~]# redis-cli -c -p 7002
127.0.0.1:7002> get liubai
-> Redirected to slot [11252] located at 127.0.0.1:7003 #数据在nodes3,从自动切换选举为主
"papa"
127.0.0.1:7003> cluster nodes
bbba5557051520f7d825819415a806b8716f811c 127.0.0.1:7002@17002 master - 0 1542181782880 2 connected 5461-10922
21da5db87c968a926ac98db20c6b3e5db98e86b4 127.0.0.1:7001@17001 slave,fail 64f7850f021d1150b41fc654f247e075f8573813 1542181666613 1542181664000 7 disconnected
64f7850f021d1150b41fc654f247e075f8573813 127.0.0.1:7005@17005 master - 0 1542181780860 7 connected 0-5460
66ebc6a414a533a9a2ed27fbcdbea88b9d8ed4db 127.0.0.1:7003@17003 myself,master - 0 1542181781000 3 connected 10923-16383
f707863e9846cf9e7c68fd7cdae5b72aa4f6839b 127.0.0.1:7004@17004 slave 66ebc6a414a533a9a2ed27fbcdbea88b9d8ed4db 0 1542181781869 4 connected
ecdf34327e274a8294f7225ddbb5d3f4cd0b7bde 127.0.0.1:7006@17006 slave bbba5557051520f7d825819415a806b8716f811c 0 1542181780000 6 connected
127.0.0.1:7003> get liubai
"papa"

0 个评论

要回复文章请先登录注册