Redis集群-cluster-Day2
redis集群
一、集群redis cluster
1、哨兵的不足
1、配置复制
- ansible利用模块和模块
2、中断时间长
-调整参数
3、资源利用率低,只有一台主库对外提供服务
4、3台只能挂1台
- 调整数
5、依赖于redis数据节点
6、主库压力比较大,性能有瓶颈
2、redis cluster
1.槽位分配slot
2.16384个槽位
3.每一个槽都得分配到位,有一个槽没分配,整个集群不可用
4.序号顺序不一定要连续,最重要的是每个节点的槽位数量要大致相同,允许2%的误差
5.集群通讯端口为配置文件里的port加10000,比如6380,通讯端口就是16380
6.故障转移切换全自动,不需要人工干预
7.集群配置文件动态更新,千万千万千万我求你了baby不需要手动更改
8.代码连接redis集群需要插件驱动支持
9.集群内消息传递是同步的
10.集群内的所有已经发现的节点配置文件是自动更新的
11.hash分配算法是足够随机和足够平均的足够稳定
3、集群搭建
========================
所有节点操作
========================
pkill redis
========================
db01操作
========================
mkdir -p /opt/redis_cluster/redis_{6380,6381}/{conf,logs,pid}
mkdir –p /data/redis_cluster/redis_{6380,6381}
cat >/opt/redis_cluster/redis_6380/conf/redis_6380.conf<<EOF
bind 10.0.0.51
port 6380
daemonize yes
pidfile "/opt/redis_cluster/redis_6380/pid/redis_6380.pid"
logfile "/opt/redis_cluster/redis_6380/logs/redis_6380.log"
dbfilename "redis_6380.rdb"
dir "/data/redis_cluster/redis_6380/"
cluster-enabled yes
cluster-config-file nodes_6380.conf
cluster-node-timeout 15000
EOF
cat >/opt/redis_cluster/redis_6381/conf/redis_6381.conf<<EOF
bind 10.0.0.51
port 6381
daemonize yes
pidfile "/opt/redis_cluster/redis_6381/pid/redis_6381.pid"
logfile "/opt/redis_cluster/redis_6381/logs/redis_6381.log"
dbfilename "redis_6381.rdb"
dir "/data/redis_cluster/redis_6381/"
cluster-enabled yes
cluster-config-file nodes_6381.conf
cluster-node-timeout 15000
EOF
redis-server /opt/redis_cluster/redis_6380/conf/redis_6380.conf
redis-server /opt/redis_cluster/redis_6381/conf/redis_6381.conf
========================
db02操作:
========================
mkdir -p /opt/redis_cluster/redis_{6380,6381}/{conf,logs,pid}
mkdir –p /data/redis_cluster/redis_{6380,6381}
cat >/opt/redis_cluster/redis_6380/conf/redis_6380.conf<<EOF
bind 10.0.0.52
port 6380
daemonize yes
pidfile "/opt/redis_cluster/redis_6380/pid/redis_6380.pid"
logfile "/opt/redis_cluster/redis_6380/logs/redis_6380.log"
dbfilename "redis_6380.rdb"
dir "/data/redis_cluster/redis_6380/"
cluster-enabled yes
cluster-config-file nodes_6380.conf
cluster-node-timeout 15000
EOF
cat >/opt/redis_cluster/redis_6381/conf/redis_6381.conf<<EOF
bind 10.0.0.52
port 6381
daemonize yes
pidfile "/opt/redis_cluster/redis_6381/pid/redis_6381.pid"
logfile "/opt/redis_cluster/redis_6381/logs/redis_6381.log"
dbfilename "redis_6381.rdb"
dir "/data/redis_cluster/redis_6381/"
cluster-enabled yes
cluster-config-file nodes_6381.conf
cluster-node-timeout 15000
EOF
redis-server /opt/redis_cluster/redis_6380/conf/redis_6380.conf
redis-server /opt/redis_cluster/redis_6381/conf/redis_6381.conf
========================
db03操作
========================
mkdir -p /opt/redis_cluster/redis_{6380,6381}/{conf,logs,pid}
mkdir –p /data/redis_cluster/redis_{6380,6381}
cat >/opt/redis_cluster/redis_6380/conf/redis_6380.conf<<EOF
bind 10.0.0.53
port 6380
daemonize yes
pidfile "/opt/redis_cluster/redis_6380/pid/redis_6380.pid"
logfile "/opt/redis_cluster/redis_6380/logs/redis_6380.log"
dbfilename "redis_6380.rdb"
dir "/data/redis_cluster/redis_6380/"
cluster-enabled yes
cluster-config-file nodes_6380.conf
cluster-node-timeout 15000
EOF
cat >/opt/redis_cluster/redis_6381/conf/redis_6381.conf<<EOF
bind 10.0.0.53
port 6381
daemonize yes
pidfile "/opt/redis_cluster/redis_6381/pid/redis_6381.pid"
logfile "/opt/redis_cluster/redis_6381/logs/redis_6381.log"
dbfilename "redis_6381.rdb"
dir "/data/redis_cluster/redis_6381/"
cluster-enabled yes
cluster-config-file nodes_6381.conf
cluster-node-timeout 15000
EOF
redis-server /opt/redis_cluster/redis_6380/conf/redis_6380.conf
redis-server /opt/redis_cluster/redis_6381/conf/redis_6381.conf
========================
所有节点查看
========================
ps -ef|grep redis
[root@db01 ~]# sh redis_shell.sh login 6380
10.0.0.51:6380> CLUSTER NODES
22b980e9074343a07575d82a15675ebd7a6060aa :6380 myself,master - 0 0 0 connected
10.0.0.51:6380>
[root@db01 ~]# cat /data/redis_cluster/redis_6380/nodes_6380.conf
22b980e9074343a07575d82a15675ebd7a6060aa :0 myself,master - 0 0 0 connected
vars currentEpoch 0 lastVoteEpoch 0
发现集群
redis-cli -h 10.0.0.51 -p 6380 cluster meet 10.0.0.51 6381
redis-cli -h 10.0.0.51 -p 6380 cluster meet 10.0.0.52 6380
redis-cli -h 10.0.0.51 -p 6380 cluster meet 10.0.0.52 6381
redis-cli -h 10.0.0.51 -p 6380 cluster meet 10.0.0.53 6380
redis-cli -h 10.0.0.51 -p 6380 cluster meet 10.0.0.53 6381
分配槽位
redis-cli -h 10.0.0.51 -p 6380 cluster addslots {0..5461}
redis-cli -h 10.0.0.52 -p 6380 cluster addslots {5462..10922}
redis-cli -h 10.0.0.53 -p 6380 cluster addslots {10923..16383}
redis-cli -h 10.0.0.51 -p 6380 cluster nodes
测试:插入1000行数据测试
for i in {0..1000};do redis-cli -c -h db01 -p 6380 set 58NB_${i} 58V5_${i};done
导出:
for i in {1..1000};do redis-cli -c -h db01 -p 6380 get 58NB_${i} ;done > 100.txt
高可用:
1、先查看节点信息,并保存
[root@db01 ~]# redis-cli -c -h db01 -p 6381 cluster nodes |grep -v "6381" |awk '{print $1,$2}'
22b980e9074343a07575d82a15675ebd7a6060aa 10.0.0.51:6380
80f14d1a348e97f0664479940506ff59ae2bd26e 10.0.0.52:6380
1546cba420db5b11021dce8e2db132a933c10093 10.0.0.53:6380
2、先在txt文本敲,先别在shell里执行
redis-cli -c -h db01 -p 6381 cluster replicate 80f14d1a348e97f0664479940506ff59ae2bd26e
redis-cli -c -h db02 -p 6381 cluster replicate 1546cba420db5b11021dce8e2db132a933c10093
redis-cli -c -h db03 -p 6381 cluster replicate 22b980e9074343a07575d82a15675ebd7a6060aa
3、测试
[root@db01 ~]# sh redis_shell.sh stop 6380
[root@db01 ~]# sh redis_shell.sh login 6381
10.0.0.51:6381> CLUSTER NODES
cf1444bd8e7db1d5d08968a6ba97beeb9ce115e4 10.0.0.53:6381 master - 0 1562230469840 6 connected 0-5461
22b980e9074343a07575d82a15675ebd7a6060aa 10.0.0.51:6380 slave cf1444bd8e7db1d5d08968a6ba97beeb9ce115e4 0 1562230470845 6 connected
80f14d1a348e97f0664479940506ff59ae2bd26e 10.0.0.52:6380 master - 0 1562230468831 5 connected 5462-10922
0b8765f55f8e3cd2617553208d1e05ad11d0f105 10.0.0.52:6381 slave 1546cba420db5b11021dce8e2db132a933c10093 0 1562230468325 4 connected
424deaf1b06ad52cdab1a07f6ec4e70e251b3a7e 10.0.0.51:6381 myself,slave 80f14d1a348e97f0664479940506ff59ae2bd26e 0 0 2 connected
1546cba420db5b11021dce8e2db132a933c10093 10.0.0.53:6380 master - 0 1562230471858 3 connected 10923-16383
[root@db01 ~]# sh redis_shell.sh start 6380
root 16213 1 0 10:13 ? 00:00:56 redis-server 10.0.0.51:6381 [cluster]
root 20808 15984 0 16:54 pts/0 00:00:00 sh redis_shell.sh start 6380
root 20816 1 0 16:54 ? 00:00:00 redis-server /opt/redis_cluster/redis_6380/conf/redis_6380.conf
root 20818 20808 0 16:54 pts/0 00:00:00 grep redis
[root@db01 ~]# sh redis_shell.sh login 6380
10.0.0.51:6380> CLUSTER FAILOVER
OK
[root@db01 /opt/redis_cluster/redis/src]# redis-cli -c -h 10.0.0.51 -p 6381 cluster nodes|grep "master" && redis-cli -c -h 10.0.0.51 -p 6381 cluster nodes|grep "slave"
22b980e9074343a07575d82a15675ebd7a6060aa 10.0.0.51:6380 myself,master - 0 0 7 connected 0-5461
424deaf1b06ad52cdab1a07f6ec4e70e251b3a7e 10.0.0.51:6381 slave 80f14d1a348e97f0664479940506ff59ae2bd26e 0 1562230560903 5 connected
cf1444bd8e7db1d5d08968a6ba97beeb9ce115e4 10.0.0.53:6381 master - 0 1562230559895 6 connected
0b8765f55f8e3cd2617553208d1e05ad11d0f105 10.0.0.52:6381 slave 1546cba420db5b11021dce8e2db132a933c10093 0 1562230556867 4 connected
80f14d1a348e97f0664479940506ff59ae2bd26e 10.0.0.52:6380 master - 0 1562230561917 5 connected 5462-10922
1546cba420db5b11021dce8e2db132a933c10093 10.0.0.53:6380 master - 0 1562230562923 3 connected 10923-16383
使用工具部署集群 Redis Cluster
db01安装命令:
yum makecache fast
yum install rubygems
gem sources --remove https://rubygems.org/
gem sources -a http://mirrors.aliyun.com/rubygems/
gem update –system
gem install redis -v 3.3.5
我们可以停掉所有的节点,然后清空数据,恢复成一个全新的集群,所有机器执行命令
pkill redis
rm -rf /data/redis_cluster/redis_6380/*
rm -rf /data/redis_cluster/redis_6381/*
sh redis_shell.sh start 6380
sh redis_shell.sh start 6381
db01 执行创建集群命令 (一条一条执行)
cd /opt/redis_cluster/redis/src/
./redis-trib.rb create --replicas 1 10.0.0.51:6380 10.0.0.52:6380 10.0.0.53:6380 10.0.0.51:6381 10.0.0.52:6381 10.0.0.53:6381
./redis-trib.rb check 10.0.0.51:6380
./redis-trib.rb rebalance 10.0.0.51:6380
结果:
[root@db01 /opt/redis_cluster/redis/src]# redis-cli -c -h 10.0.0.51 -p 6381 cluster nodes|grep "master" && redis-cli -c -h 10.0.0.51 -p 6381 cluster nodes|grep "slave"
4a553202242e9110f2f212288c17c27c18daeb9b 10.0.0.52:6380 master - 0 1562232699330 2 connected 5461-10922
c6676ac73f4507d841e912313979bf0ce0e760a6 10.0.0.51:6380 master - 0 1562232703362 1 connected 0-5460
e02124f11c3ea09c613115e4b3bc258b0fec6520 10.0.0.53:6380 master - 0 1562232704376 3 connected 10923-16383
b93baeb5911bed7dff6802cbd8be1d4adad16fbc 10.0.0.52:6381 slave c6676ac73f4507d841e912313979bf0ce0e760a6 0 1562232691259 5 connected
4eafdbf7bb0d3f117c93d763a8966c0992ea3bcb 10.0.0.51:6381 myself,slave 4a553202242e9110f2f212288c17c27c18daeb9b 0 0 4 connected
615559201ae450471495b5fb54282e493ebe1a63 10.0.0.53:6381 slave e02124f11c3ea09c613115e4b3bc258b0fec6520 0 1562232695293 6 connected
Redis 集群架构(Cluster) 部署到生产环境规范
Redis 集群架构(Cluster) 添加服务器步骤
在db01上创建新节点
mkdir -p /opt/redis_cluster/redis_{6390,6391}/{conf,logs,pid}
mkdir -p /data/redis_cluster/redis_{6390,6391}
cd /opt/redis_cluster/
cp redis_6380/conf/redis_6380.conf redis_6390/conf/redis_6390.conf
cp redis_6380/conf/redis_6380.conf redis_6391/conf/redis_6391.conf
sed -i 's#6380#6390#g' redis_6390/conf/redis_6390.conf
sed -i 's#6380#6391#g' redis_6391/conf/redis_6391.conf
在db01上启动新节点
bash redis_shell.sh start 6390
bash redis_shell.sh start 6391
使用工具发现节点并重新分配槽位
cd /opt/redis_cluster/redis/src/
./redis-trib.rb add-node 10.0.0.51:6390 10.0.0.51:6380
./redis-trib.rb add-node 10.0.0.51:6391 10.0.0.51:6380
./redis-trib.rb reshard 10.0.0.51:6380
输入需要迁移多少个槽位
4096
yes
########输入db01的6390的ID#########
all
yes
重新做主从关系
先做db03的6381复制db01的6390
再做db01的6391复制db01的6380
[root@db03 ~]# redis-cli -c -h 10.0.0.53 -p 6381 cluster replicate f2646e32bacd4d8aab7eadc1acb047c97b153006
[root@db03 ~]# redis-cli -c -h 10.0.0.51 -p 6391 cluster replicate 234f22e73de889fc14068be9d4f7a29b1d6d4b70
确认和测试
#登陆任意节点查看集群
10.0.0.51:6380> 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:17816
cluster_stats_messages_received:16211
#查看主从关系
[root@db01 /opt/redis_cluster/redis/src]# cat /data/redis_cluster/redis_6380/nodes_6380.conf |grep "slav
436f3e2faa9a2944181e5b8a2d665a11bb2549fc 10.0.0.52:6381 slave 234f22e73de889fc14068be9d4f7a29b1d6d4b70 0
052a046ef654e064fa4f9ef7bf6fd002fb1b0b41 10.0.0.51:6391 slave 234f22e73de889fc14068be9d4f7a29b1d6d4b70 0
4f2168d47fd82a022e2dee28852d9909a51daf7a 10.0.0.51:6381 slave c8a9db70d9a5ff4c4c21cdd883c2c60bea97d18b 0
526d84ae90fed7f87bded43796c20ebeb5a4c7cc 10.0.0.53:6381 slave f2646e32bacd4d8aab7eadc1acb047c97b153006 0
[root@db01 /opt/redis_cluster/redis/src]# cat /data/redis_cluster/redis_6380/nodes_6380.conf |grep -v
100dfb5242d48571a42fccad64315153804d831b 10.0.0.53:6380 master - 0 1562293730060 3 connected 12288-16383
234f22e73de889fc14068be9d4f7a29b1d6d4b70 10.0.0.51:6380 myself,master - 0 0 1 connected 1365-5460
f2646e32bacd4d8aab7eadc1acb047c97b153006 10.0.0.51:6390 master - 0 1562293731397 8 connected 0-1364 5461-
c8a9db70d9a5ff4c4c21cdd883c2c60bea97d18b 10.0.0.52:6380 master - 0 1562293729052 2 connected 6827-10922