一。redis集群简介
Redis 集群没有使用一致性hash, 而是引入了 哈希槽的概念集群内置了16384个slot(哈希槽).
Redis 集群的主从复制模型,异步复制。
redis集群采用P2P模式,是完全去中心化的,不存在中心节点或者代理节点;redis集群是没有统一的入口的,客户端(client)连接集群的时候连接集群中的任意节点(node)即可
因为集群内置了16384个slot(哈希槽),并且把所有的物理节点映射到了这16384[0-16383]个slot上,或者说把这些slot均等的分配给了各个节点。当需要在Redis集群存放一个数据(key-value)时,redis会先对这个key进行crc16算法,然后得到一个结果。再把这个结果对16384进行求余,这个余数会对应[0-16383]其中一个槽,进而决定key-value存储到哪个节点中。所以一旦某个节点挂了,该节点对应的slot就无法使用,那么就会导致集群无法正常工作。
二。redis集群配置部署
配置redis cluster集群
1.源码编译
tar -zxf redis-3.2.6.tar.gz
cd redis-3.2.6/src
make
2.创建redis-cluster目录作为集群工作目录
mkdir /home/lifh/redis/redis-cluster
cp redis/redis-3.2.6/src/redis-server /home/lifh/redis/redis-cluster/redis01
cp redis/redis-3.2.6/src/redis-cli /home/lifh/redis/redis-cluster/redis01
cp redis/redis-3.2.6/redis.conf /home/lifh/redis/redis-cluster/redis01
编辑redis.conf文件中,具体修改4处地方:
- 端口号修改为7001。
- 开启集群创建模式,打开注释即可。
- 修改后台运行daemonize yes
- 绑定地址 bind 134.32.213.129
- 配置调整 #protected-mode yes protected-mode no
redis.conf配置文件
bind 134.32.213.129
protected-mode no
port 7001
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile ""
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir ./
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
appendonly no
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
lua-time-limit 5000
cluster-enabled yes
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
masterauth "passwd"
requirepass "passwd"
复制其他节点,并相应调整其他配置文件redis.conf 中的参数配置
cp -r redis/redis-cluster/redis01 redis/redis-cluster/redis02
cp -r redis/redis-cluster/redis01 redis/redis-cluster/redis03
cp -r redis/redis-cluster/redis01 redis/redis-cluster/redis04
cp -r redis/redis-cluster/redis01 redis/redis-cluster/redis05
cp -r redis/redis-cluster/redis01 redis/redis-cluster/redis06
cp -r redis/redis-cluster/redis01 redis/redis-cluster/redis07
cp -r redis/redis-cluster/redis01 redis/redis-cluster/redis08
3启动实例:
cd redis01
./redis-server redis.conf
cd ../redis02
./redis-server redis.conf
4.安装ruby(只在其中一台机器就可以)
如果采用redis5.05版本,不需要安装ruby.
按照redis/soft/install.list 中的说明安装部署(centos linux 6.5);
centos linux 6.5
###redis cluster depend of ruby soft,install sequence is follow
1.install ruby modules
rpm -ivh compat-readline5-5.2-17.1.el6.x86_64.rpm
rpm -ivh ruby-libs-1.8.7.352-12.el6_4.x86_64.rpm
rpm -ivh ruby-1.8.7.352-12.el6_4.x86_64.rpm
rpm -ivh ruby-irb-1.8.7.352-12.el6_4.x86_64.rpm
rpm -ivh ruby-rdoc-1.8.7.352-12.el6_4.x86_64.rpm
rpm -ivh rubygems-1.3.7-5.el6.noarch.rpm
2.gem install redis-3.2.1.gem
Successfully installed redis-3.2.1
1 gem installed
Installing ri documentation for redis-3.2.1...
Installing RDoc documentation for redis-3.2.1...
chmod -R a+x /usr/lib/ruby
5.配置集群:
4台主机8个节点,可以保证主从节点分布在两台机器上。
cp redis/src/redis-trib.rb redis/redis-cluster
./redis-trib.rb create --replicas 1 134.32.213.129:7001 134.32.213.129:7002 134.32.213.130:7003 134.32.213.130:7004 134.32.213.131:7005 134.32.213.131:7006 134.32.213.132:7007 134.32.213.132:7008
采用redis5.0.5版本使用下面的命令创建集群
[lifh@o2oaiopapp01 redis01V5]$ ./redis-cli --cluster create 134.32.213.129:7001 134.32.213.129:7002 134.32.213.129:7003 134.32.213.129:7004 134.32.213.129:7005 134.32.213.129:7006 134.32.213.129:7007 134.32.213.129:7008 --cluster-replicas 1
6.核查集群状态
[lifh@o2oaiopapp01 redis-cluster]$ redis01/redis-cli -h 134.32.213.129 -p 7001
134.32.213.129: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:8
cluster_size:4
cluster_current_epoch:8
cluster_my_epoch:1
cluster_stats_messages_sent:1501
cluster_stats_messages_received:1501
134.32.213.129:7001> cluster nodes
8566cfafce002145ace68b347cc5a511d27cd240 134.32.213.130:7004 slave a935fcbcfbe6f509d19dfcd6670ce3354f248f7a 0 1570934137171 4 connected
df0b3c0f1366b2af7ab52d78b1ea7c4e6513c985 134.32.213.132:7008 slave 1bdfd9a8f8bdf96ee675e882755ea56570d5a528 0 1570934134167 8 connected
a935fcbcfbe6f509d19dfcd6670ce3354f248f7a 134.32.213.129:7001 myself,master - 0 0 1 connected 12288-16383
d8e94c51febcd2e323da46e9c61152d1b9abd158 134.32.213.130:7003 master - 0 1570934138172 3 connected 8192-12287
87b9e4f774bf31c762d04679ab89a0952919e14b 134.32.213.131:7006 slave cd95bb0a4c360373710889f4251d3f79b45afaa7 0 1570934135169 7 connected
1bdfd9a8f8bdf96ee675e882755ea56570d5a528 134.32.213.131:7005 master - 0 1570934132663 5 connected 4096-8191
0f642b10638a89a14be7263f12933b6e87e8096b 134.32.213.129:7002 slave d8e94c51febcd2e323da46e9c61152d1b9abd158 0 1570934133164 3 connected
cd95bb0a4c360373710889f4251d3f79b45afaa7 134.32.213.132:7007 master - 0 1570934136169 7 connected 0-4095
134.32.213.129:7001>
7.配置验证
参数文件(redis.conf)
masterauth "passwd"
requirepass "passwd"
redis01/redis-cli -h 134.32.213.129 -p 7001 -a passwd -c
set str1 1
set str2 2
set str3 3
(error) MOVED 原因为客户端没有启动集群模式
8.redis服务停止命令
redis-cli -p 6380 -a ordercenter shutdown