linux-redis cluster集群快速搭建
前言
redis 3.0之后版本支持redis-cluster集群,Redis-Cluster采用无中心结构,每个节点保存数据和整个集群状态,每个节点都和其他所有 节点连接。
其结构特点:
1、所有的redis节点彼此互联(PING-PONG机制),内部使用二进制协议优化传输速度和带宽。
2、节点的fail是通过集群中超过半数的节点检测失效时才生效。
3、客户端与redis节点直连,不需要中间proxy层.客户端不需要连接集群所有节点,连接集群中任何一个可用节点即可。
4、redis-cluster把所有的物理节点映射到[0-16383]slot上(不一定是平均分配),cluster 负责维护node<->slot<->value。
5、Redis集群预分好16384个slot,当需要在 Redis 集群中放置一个 key-value 时,根据 CRC16(key) mod 16384的值,决定将一个key放到哪个slot中。
配置
环境简介:
部署6个redis节点,为3主3从。
每个节点添加cluster配置:
(修改8001端口到对应的端口就可以了)
cluster-enabled yes
cluster-config-file nodes-8001.conf
cluster-node-timeout 15000
protected-mode yes
port 8001
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
pidfile /opt/redis/logs/redis_8001.pid
loglevel notice
logfile "/opt/redis/logs/redis-8001.log"
databases 16
always-show-logo yes
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump_8001.rdb
dir "/opt/redis/data"
slave-read-only yes
appendonly yes
appendfilename "appendonly_8001.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
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
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
如果是测试也可以在一台机子上部署伪分布式集群。
启动6个节点
查看状态,这时候6个节点是没有握手的,使用redis-cli进行操作
/opt/redis/bin/redis-cli -p 8001 -c
#查看节点状态
cluster nodes
7ec63a08d2355448d16a84b502974832c52359e1 :8001@18001 myself,master - 0 0 0 connected
#添加节点
cluster meet 127.0.0.1 8002
cluster meet 127.0.0.1 8003
cluster meet 127.0.0.1 8004
cluster meet 127.0.0.1 8005
cluster meet 127.0.0.1 8006
#查看节点状态
cluster nodes
95ab86e82b78cce76ab40e5c5d738a2c68044340 127.0.0.1:8005@18005 master - 0 1534341796000 4 connected
7ec63a08d2355448d16a84b502974832c52359e1 127.0.0.1:8001@18001 myself,master - 0 1534341797000 2 connected
c971636e57a7a9bfc6537064fe438cb78b7d82e1 127.0.0.1:8006@18006 master - 0 1534341798286 0 connected
b12f7ab5c229fdf817aa7b5a4c534463ce223c62 127.0.0.1:8003@18003 master - 0 1534341796000 0 connected
3f4af91be1577d94d31fc5ec048d03c86b3c7837 127.0.0.1:8002@18002 master - 0 1534341796283 1 connected
83f9fa5903fd47cb993ff1f051a6a10218e15c2f 127.0.0.1:8004@18004 master - 0 1534341797285 3 connected
#分配slot
/opt/redis/bin/redis-cli -h 127.0.0.1 -p 8001 cluster addslots {0..5461}
/opt/redis/bin/redis-cli -h 127.0.0.1 -p 8002 cluster addslots {5462..10922}
/opt/redis/bin/redis-cli -h 127.0.0.1 -p 8003 cluster addslots {10923..16383}
#设置主从,后面一长串是上面看到节点的node_id
/opt/redis/bin/redis-cli -h 127.0.0.1 -p 8004 cluster replicate 7ec63a08d2355448d16a84b502974832c52359e1
/opt/redis/bin/redis-cli -h 127.0.0.1 -p 8005 cluster replicate 3f4af91be1577d94d31fc5ec048d03c86b3c7837
/opt/redis/bin/redis-cli -h 127.0.0.1 -p 8006 cluster replicate b12f7ab5c229fdf817aa7b5a4c534463ce223c62
#查看节点状态
cluster nodes
95ab86e82b78cce76ab40e5c5d738a2c68044340 127.0.0.1:8005@18005 slave 3f4af91be1577d94d31fc5ec048d03c86b3c7837 0 1534343095000 4 connected
7ec63a08d2355448d16a84b502974832c52359e1 127.0.0.1:8001@18001 myself,master - 0 1534343093000 2 connected 0-5461
c971636e57a7a9bfc6537064fe438cb78b7d82e1 127.0.0.1:8006@18006 slave b12f7ab5c229fdf817aa7b5a4c534463ce223c62 0 1534343093000 5 connected
b12f7ab5c229fdf817aa7b5a4c534463ce223c62 127.0.0.1:8003@18003 master - 0 1534343095702 5 connected 10923-16383
3f4af91be1577d94d31fc5ec048d03c86b3c7837 127.0.0.1:8002@18002 master - 0 1534343094699 1 connected 5462-10922
83f9fa5903fd47cb993ff1f051a6a10218e15c2f 127.0.0.1:8004@18004 slave 7ec63a08d2355448d16a84b502974832c52359e1 0 1534343093000 3 connected
总结
搭建redis cluster集群还是比较简单的,维护嘛!还在摸索,感觉还是codis的图形界面好设置。