redis集群配置
Posted fengjian1585
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了redis集群配置相关的知识,希望对你有一定的参考价值。
客户端分片 程序端实现
代理proxy,访问proxy,proxy指定redis保存位置。 Twemproxy
Redis cluster ,会造成一部分数据丢失,无中心化
1.将数据自动切分(split)到多个节点的能力
2.当集群中的一部分节点失效或者无法进行通信时,仍然可以继续处理命令请求的能力。
codis redis集群解决方案。豌豆荚开源
redis cluster 集群配置
cd /data/
mkdir `seq 7001 7008`
redis.conf 添加,端口需要变化
‘‘‘
port 7000
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
‘‘‘
安装ruby
yum -y install ruby rubygems
安装ruby的redis管理工具
gem install redis
1 gem installed
Installing ri documentation for redis-3.3.0...
Installing RDoc documentation for redis-3.3.0...
创建集群
redis-trib.rb create --replicas 1 172.16.230.167:7001 172.16.230.167:7002 172.16.230.167:7003 172.16.230.167:7004 172.16.230.167:7005 172.16.230.167:7006
客户端连接:
redis-cli -c -h 172.16.230.167 -p 7001
set name 111
set name1 222
会写入到不通哈希槽
cluster nodes
cluster info
添加集群节点:命令中的 add-node 表示我们要让 redis-trib 将一个节点添加到集群里面, add-node 之后跟着的是新节点的 IP 地址和端口号, 再之后跟着的是集群中任意一个已存在节点的 IP 地址和端口号
[[email protected] data]# redis-trib.rb add-node 172.16.230.167:7007 172.16.230.167:7001
>>> Adding node 172.16.230.167:7007 to cluster 172.16.230.167:7001
>>> Performing Cluster Check (using node 172.16.230.167:7001)
M: f2a684e0900e7b7eb91bdb77e3b559f47d0f7237 172.16.230.167:7001
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: 37b6f6478ca6425022521d6a5ecf0aeedf12c601 172.16.230.167:7005
slots: (0 slots) slave
replicates 84995c68e3625c91f4c646d71566dffd709e6d01
S: d6d307c91e6ba6fbba7d898f20fd4772a3e42b9c 172.16.230.167:7004
slots: (0 slots) slave
replicates f2a684e0900e7b7eb91bdb77e3b559f47d0f7237
S: 458a01eeace9fd93544645433ad5b12587d94d3b 172.16.230.167:7006
slots: (0 slots) slave
replicates ffe722398240b59f0c993074d35dbe79386c1ec3
M: ffe722398240b59f0c993074d35dbe79386c1ec3 172.16.230.167:7003
slots:10923-16383 (5461 slots) master
1 additional replica(s)
M: 84995c68e3625c91f4c646d71566dffd709e6d01 172.16.230.167:7002
slots:5461-10922 (5462 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.
>>> Send CLUSTER MEET to node 172.16.230.167:7007 to make it join the cluster.
[OK] New node added correctly.
查询新节点
172.16.230.167:7001> cluster nodes
37b6f6478ca6425022521d6a5ecf0aeedf12c601 172.16.230.167:7005 slave 84995c68e3625c91f4c646d71566dffd709e6d01 0 1467381076060 5 connected
d6d307c91e6ba6fbba7d898f20fd4772a3e42b9c 172.16.230.167:7004 slave f2a684e0900e7b7eb91bdb77e3b559f47d0f7237 0 1467381077061 4 connected
458a01eeace9fd93544645433ad5b12587d94d3b 172.16.230.167:7006 slave ffe722398240b59f0c993074d35dbe79386c1ec3 0 1467381073056 6 connected
ffe722398240b59f0c993074d35dbe79386c1ec3 172.16.230.167:7003 master - 0 1467381074057 3 connected 10923-16383
83e5dd37a0825a4adb77d6da4738bf0c88cd6c39 172.16.230.167:7007 master - 0 1467381075059 0 connected
84995c68e3625c91f4c646d71566dffd709e6d01 172.16.230.167:7002 master - 0 1467381073556 2 connected 5461-10922
f2a684e0900e7b7eb91bdb77e3b559f47d0f7237 172.16.230.167:7001 myself,master - 0 0 1 connected 0-5460
将一个新节点转变为从节点的方法,把7008加入到集群中
[[email protected] data]# redis-trib.rb add-node 172.16.230.167:7008 172.16.230.167:7001
进入到新加入集群的终端中
[[email protected] data]# redis-cli -c -h 172.16.230.167 -p 7008
172.16.230.167:7008> CLUSTER nodes
84995c68e3625c91f4c646d71566dffd709e6d01 172.16.230.167:7002 master - 0 1467381351640 2 connected 5461-10922
37b6f6478ca6425022521d6a5ecf0aeedf12c601 172.16.230.167:7005 slave 84995c68e3625c91f4c646d71566dffd709e6d01 0 1467381350638 2 connected
f2a684e0900e7b7eb91bdb77e3b559f47d0f7237 172.16.230.167:7001 master - 0 1467381352142 1 connected 0-5460
83e5dd37a0825a4adb77d6da4738bf0c88cd6c39 172.16.230.167:7007 master - 0 1467381352641 0 connected
458a01eeace9fd93544645433ad5b12587d94d3b 172.16.230.167:7006 slave ffe722398240b59f0c993074d35dbe79386c1ec3 0 1467381349636 3 connected
ffe722398240b59f0c993074d35dbe79386c1ec3 172.16.230.167:7003 master - 0 1467381348635 3 connected 10923-16383
d6d307c91e6ba6fbba7d898f20fd4772a3e42b9c 172.16.230.167:7004 slave f2a684e0900e7b7eb91bdb77e3b559f47d0f7237 0 1467381348135 1 connected
6b52de99a37444a2f7cb04a65ed4239446dee048 172.16.230.167:7008 myself,master - 0 0 7 connected
172.16.230.167:7008> CLUSTER REPLICATE 83e5dd37a0825a4adb77d6da4738bf0c88cd6c39
其中命令提供的 83e5dd37a0825a4adb77d6da4738bf0c88cd6c39 就是主节点 172.16.230.167:7007 的节点 ID
172.16.230.167:7008> CLUSTER nodes
84995c68e3625c91f4c646d71566dffd709e6d01 172.16.230.167:7002 master - 0 1467381552990 2 connected 5461-10922
37b6f6478ca6425022521d6a5ecf0aeedf12c601 172.16.230.167:7005 slave 84995c68e3625c91f4c646d71566dffd709e6d01 0 1467381554995 2 connected
f2a684e0900e7b7eb91bdb77e3b559f47d0f7237 172.16.230.167:7001 master - 0 1467381553992 1 connected 0-5460
83e5dd37a0825a4adb77d6da4738bf0c88cd6c39 172.16.230.167:7007 master - 0 1467381555997 0 connected
458a01eeace9fd93544645433ad5b12587d94d3b 172.16.230.167:7006 slave ffe722398240b59f0c993074d35dbe79386c1ec3 0 1467381550487 3 connected
ffe722398240b59f0c993074d35dbe79386c1ec3 172.16.230.167:7003 master - 0 1467381550988 3 connected 10923-16383
d6d307c91e6ba6fbba7d898f20fd4772a3e42b9c 172.16.230.167:7004 slave f2a684e0900e7b7eb91bdb77e3b559f47d0f7237 0 1467381549986 1 connected
6b52de99a37444a2f7cb04a65ed4239446dee048 172.16.230.167:7008 myself,slave 83e5dd37a0825a4adb77d6da4738bf0c88cd6c39 0 0 7 connected
对集群进行重新分片:主要对172.16.230.167:7007 集群节点做重新分片
[[email protected] ~]# redis-trib.rb reshard 172.16.230.167:7007
>>> Performing Cluster Check (using node 172.16.230.167:7007)
M: 83e5dd37a0825a4adb77d6da4738bf0c88cd6c39 172.16.230.167:7007
slots: (0 slots) master
1 additional replica(s)
S: d6d307c91e6ba6fbba7d898f20fd4772a3e42b9c 172.16.230.167:7004
slots: (0 slots) slave
replicates f2a684e0900e7b7eb91bdb77e3b559f47d0f7237
S: 6b52de99a37444a2f7cb04a65ed4239446dee048 172.16.230.167:7008
slots: (0 slots) slave
replicates 83e5dd37a0825a4adb77d6da4738bf0c88cd6c39
M: 84995c68e3625c91f4c646d71566dffd709e6d01 172.16.230.167:7002
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: 37b6f6478ca6425022521d6a5ecf0aeedf12c601 172.16.230.167:7005
slots: (0 slots) slave
replicates 84995c68e3625c91f4c646d71566dffd709e6d01
M: f2a684e0900e7b7eb91bdb77e3b559f47d0f7237 172.16.230.167:7001
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: 458a01eeace9fd93544645433ad5b12587d94d3b 172.16.230.167:7006
slots: (0 slots) slave
replicates ffe722398240b59f0c993074d35dbe79386c1ec3
M: ffe722398240b59f0c993074d35dbe79386c1ec3 172.16.230.167:7003
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.
How many slots do you want to move (from 1 to 16384)? 1000 #######分片大小
What is the receiving node ID? 83e5dd37a0825a4adb77d6da4738bf0c88cd6c39 #分配到那个集群节点的master上172.16.230.167:7007
Please enter all the source node IDs.
Type ‘all‘ to use all the nodes as source nodes for the hash slots.
Type ‘done‘ once you entered all the source nodes IDs.
Source node #1:all #所有
Do you want to proceed with the proposed reshard plan (yes/no)?yes
使用 consistency-test.rb检测数据一致性
下载
git clone https://github.com/antirez/redis-rb-cluster.git
[[email protected] redis-rb-cluster]# ruby consistency-test.rb 172.16.230.167 7001
316 R (0 err) | 316 W (0 err) |
1453 R (0 err) | 1453 W (0 err) |
2708 R (0 err) | 2708 W (0 err) |
4010 R (0 err) | 4010 W (0 err) |
5309 R (0 err) | 5309 W (0 err) |
6613 R (0 err) | 6613 W (0 err) |
7915 R (0 err) | 7915 W (0 err) |
9214 R (0 err) | 9214 W (0 err) |
10517 R (0 err) | 10517 W (0 err) |
11817 R (0 err) | 11817 W (0 err) |
13121 R (0 err) | 13121 W (0 err) |
14422 R (0 err) | 14422 W (0 err) |
15724 R (0 err) | 15724 W (0 err) |
17012 R (0 err) | 17012 W (0 err) |
18312 R (0 err) | 18312 W (0 err) |
19628 R (0 err) | 19628 W (0 err) |
20949 R (0 err) | 20949 W (0 err) |
22255 R (0 err) | 22255 W (0 err) |
23294 R (0 err) | 23294 W (0 err) |
24542 R (0 err) | 24542 W (0 err) |
25852 R (0 err) | 25852 W (0 err) |
故障转移测试
查看主节点:
[[email protected] ~]# redis-cli -c -h 172.16.230.167 -p 7001 cluster nodes | grep master
ffe722398240b59f0c993074d35dbe79386c1ec3 172.16.230.167:7003 master - 0 1467383421007 3 connected 11256-16383
83e5dd37a0825a4adb77d6da4738bf0c88cd6c39 172.16.230.167:7007 master - 0 1467383423010 8 connected 0-332 5461-5794 10923-11255
84995c68e3625c91f4c646d71566dffd709e6d01 172.16.230.167:7002 master - 0 1467383426014 2 connected 5795-10922
f2a684e0900e7b7eb91bdb77e3b559f47d0f7237 172.16.230.167:7001 myself,master - 0 0 1 connected 333-5460
使用debug segfault 让主节点崩溃
redis-cli -c -h 172.16.230.167 -p 7001 debug segfault
[[email protected] ~]# redis-cli -c -h 172.16.230.167 -p 7002 cluster nodes | grep master
84995c68e3625c91f4c646d71566dffd709e6d01 172.16.230.167:7002 myself,master - 0 0 2 connected 5795-10922
d6d307c91e6ba6fbba7d898f20fd4772a3e42b9c 172.16.230.167:7004 master - 0 1467383553156 9 connected 333-5460
83e5dd37a0825a4adb77d6da4738bf0c88cd6c39 172.16.230.167:7007 master - 0 1467383556161 8 connected 0-332 5461-5794 10923-11255
ffe722398240b59f0c993074d35dbe79386c1ec3 172.16.230.167:7003 master - 0 1467383553657 3 connected 11256-16383
f2a684e0900e7b7eb91bdb77e3b559f47d0f7237 172.16.230.167:7001 master,fail - 1467383513361 1467383506054 1 disconnected
集群管理
yum -y install httpd php php-redis
git clone https://github.com/erikdubbelboer/phpRedisAdmin.git
cd phpRedisAdmin
git clone https://github.com/nrk/predis.git vendor
###################################
Codis 集群
Codis 由四部分组成:
Codis Proxy (codis-proxy)
Codis Dashboard (codis-config)
Codis Redis (codis-server)
ZooKeeper/Etcd
安装go
yum -y install golang
mkdir /data/gopath/
export GOPATH=/data/gopath
export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL GOPATH
以上是关于redis集群配置的主要内容,如果未能解决你的问题,请参考以下文章