一、使用工具搭建redis集群

1.环境准备

节点 IP 端口
节点1 172.16.1.91 6380,6381
节点2 172.16.1.92 6380,6381
节点3 172.16.1.93 6380,6381
节点4 172.16.1.94 6380,6381

2.搭建redis集群

###############################################################################
###   如果集群有密码,使用工具进行搭建和分配槽位和扩展节点的话,可以临时取消密码    ###
###   redis-cli  -h 172.16.1.53 -p 6380 -a 123 config set requirepass ""    ###
###   临时加上密码                                                           ###
###   redis-cli -h 172.16.1.53 -p 6380 -a 123 config set requirepass 123    ###
###############################################################################


1.删除原数据目录(全都执行)
[root@redis01 ~]# rm -rf /server/redis/6380
[root@redis01 ~]# rm -rf /server/redis/6381

2.创建多节点的目录
[root@redis01 ~]# mkdir /server/redis/{6380,6381} -p

3.配置redis
[root@redis01 ~]# vim /server/redis/6380/redis.conf
daemonize yes
bind 172.16.1.91 127.0.0.1
port 6380
pidfile /server/redis/6380/redis.pid
logfile /server/redis/6380/redis.log
dir /server/redis/6380
cluster-enabled yes
cluster-config-file cluster.conf
cluster-node-timeout 5000

4.拷贝另一节点配置文件
[root@redis01 ~]# cp /server/redis/6380/redis.conf /server/redis/6381/
[root@redis01 ~]# sed -i 's#6380#6381#g' /server/redis/6381/redis.conf

5.推送配置文件到新节点
[root@redis01 ~]# scp -r /server/redis/638* 172.16.1.92:/server/redis/
[root@redis01 ~]# scp -r /server/redis/638* 172.16.1.93:/server/redis/

6.修改其他节点的ip
[root@redis02 ~]# find /server/redis/638* -type f -name "*.conf" | xargs sed -i "s#91#92#g"
[root@redis03 ~]# find /server/redis/638* -type f -name "*.conf" | xargs sed -i "s#91#93#g"

3.启动所有节点的redis

[root@redis01 ~]# redis-server /server/redis/6380/redis.conf 
[root@redis01 ~]# redis-server /server/redis/6381/redis.conf 

#验证启动
[root@redis01 ~]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN      11304/redis-server  
tcp        0      0 172.16.1.91:6379        0.0.0.0:*               LISTEN      11304/redis-server  
tcp        0      0 127.0.0.1:6380          0.0.0.0:*               LISTEN      12100/redis-server  
tcp        0      0 172.16.1.91:6380        0.0.0.0:*               LISTEN      12100/redis-server  
tcp        0      0 127.0.0.1:6381          0.0.0.0:*               LISTEN      12104/redis-server  
tcp        0      0 172.16.1.91:6381        0.0.0.0:*               LISTEN      12104/redis-server   
tcp        0      0 127.0.0.1:16380         0.0.0.0:*               LISTEN      12100/redis-server  
tcp        0      0 172.16.1.91:16380       0.0.0.0:*               LISTEN      12100/redis-server  
tcp        0      0 127.0.0.1:16381         0.0.0.0:*               LISTEN      12104/redis-server  
tcp        0      0 172.16.1.91:16381       0.0.0.0:*               LISTEN      12104/redis-server    
[root@redis01 ~]# ps -ef | grep redis
root      11304      1  0 08:22 ?        00:00:01 redis-server 172.16.1.91:6379
root      12100      1  0 08:58 ?        00:00:00 redis-server 172.16.1.91:6380 [cluster]
root      12104      1  0 08:58 ?        00:00:00 redis-server 172.16.1.91:6381 [cluster]
root      12109  11952  0 08:59 pts/1    00:00:00 grep --color=auto redis

4.安装ruby依赖

#安装ruby依赖
[root@redis01 ~]# yum install ruby rubygems -y
#查看gem源
[root@db01 ~]# gem sources -l
*** CURRENT SOURCES ***
http://rubygems.org/

#添加阿里云的gem源
[root@db01 ~]# gem sources -a http://mirrors.aliyun.com/rubygems/
http://mirrors.aliyun.com/rubygems/ added to sources 

#删除国外gem源
[root@db01 ~]# gem sources  --remove https://rubygems.org/
http://rubygems.org/ removed from sources

#再次查看gem源
[root@db01 ~]# gem sources -l

#使用gem安装redis的ruby插件
[root@db01 ~]# gem install redis -v 3.3.3
Successfully installed redis-3.3.3
1 gem installed
Installing ri documentation for redis-3.3.3...
Installing RDoc documentation for redis-3.3.3...

5.工具命令

[root@redis01 ~]# redis-trib.rb 
1. create:创建集群
2. check:检查集群
3. info:查看集群信息
4. fix:修复集群
5. reshard:在线迁移slot
6. rebalance:平衡集群节点slot数量
7. add-node:添加新节点
8. del-node:删除节点
9. set-timeout:设置节点的超时时间
10. call:在集群所有节点上执行命令
11. import:将外部redis数据导入集群

6.关联所有的节点redis

#关联所有的redis
[root@redis01 ~]# redis-trib.rb create --replicas 1 172.16.1.91:6380 172.16.1.91:6381 172.16.1.92:6380 172.16.1.92:6381 172.16.1.93:6380 172.16.1.93:6381
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
172.16.1.91:6380
172.16.1.92:6380
172.16.1.93:6380
Adding replica 172.16.1.92:6381 to 172.16.1.91:6380
Adding replica 172.16.1.91:6381 to 172.16.1.92:6380
Adding replica 172.16.1.93:6381 to 172.16.1.93:6380
M: aab4e4389b3014605ab9549f9545b1bd2545f74c 172.16.1.91:6380
   slots:0-5460 (5461 slots) master
S: 1457aeaa2e857391313749361adff59426599af5 172.16.1.91:6381
   replicates 18071a787dd947627024b521aa8bdf43eeccaa87
M: 18071a787dd947627024b521aa8bdf43eeccaa87 172.16.1.92:6380
   slots:5461-10922 (5462 slots) master
S: 7f7bf0bf4e3166d6fcf4cd51fe1290063aacac96 172.16.1.92:6381
   replicates aab4e4389b3014605ab9549f9545b1bd2545f74c
M: 20c8c6db033b6951009a2206aa3c42b70591ce9f 172.16.1.93:6380
   slots:10923-16383 (5461 slots) master
S: 0a2003129940e168fd2606e47562edd17275bf2f 172.16.1.93:6381
   replicates 20c8c6db033b6951009a2206aa3c42b70591ce9f
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join...
>>> Performing Cluster Check (using node 172.16.1.91:6380)
M: aab4e4389b3014605ab9549f9545b1bd2545f74c 172.16.1.91:6380
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
M: 20c8c6db033b6951009a2206aa3c42b70591ce9f 172.16.1.93:6380
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: 1457aeaa2e857391313749361adff59426599af5 172.16.1.91:6381
   slots: (0 slots) slave
   replicates 18071a787dd947627024b521aa8bdf43eeccaa87
S: 7f7bf0bf4e3166d6fcf4cd51fe1290063aacac96 172.16.1.92:6381
   slots: (0 slots) slave
   replicates aab4e4389b3014605ab9549f9545b1bd2545f74c
M: 18071a787dd947627024b521aa8bdf43eeccaa87 172.16.1.92:6380
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: 0a2003129940e168fd2606e47562edd17275bf2f 172.16.1.93:6381
   slots: (0 slots) slave
   replicates 20c8c6db033b6951009a2206aa3c42b70591ce9f
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

#使用工具分配集群时总是会出现一个问题:总有一台机器自己做自己的从库
[root@redis01 ~]# redis-cli -h 172.16.1.92 -p 6381
172.16.1.92:6381> CLUSTER REPLICATE 20c8c6db033b6951009a2206aa3c42b70591ce9f
OK
[root@redis01 ~]# redis-cli -h 172.16.1.93 -p 6381
172.16.1.93:6381> CLUSTER REPLICATE aab4e4389b3014605ab9549f9545b1bd2545f74c
OK

7.查看集群节点和状态

#查看集群节点
[root@redis01 ~]# redis-cli -p 6380 cluster nodes
20c8c6db033b6951009a2206aa3c42b70591ce9f 172.16.1.93:6380 master - 0 1589420951365 5 connected 10923-16383
1457aeaa2e857391313749361adff59426599af5 172.16.1.91:6381 slave 18071a787dd947627024b521aa8bdf43eeccaa87 0 1589420952876 3 connected
aab4e4389b3014605ab9549f9545b1bd2545f74c 172.16.1.91:6380 myself,master - 0 0 1 connected 0-5460
7f7bf0bf4e3166d6fcf4cd51fe1290063aacac96 172.16.1.92:6381 slave 20c8c6db033b6951009a2206aa3c42b70591ce9f 0 1589420951365 5 connected
18071a787dd947627024b521aa8bdf43eeccaa87 172.16.1.92:6380 master - 0 1589420952372 3 connected 5461-10922
0a2003129940e168fd2606e47562edd17275bf2f 172.16.1.93:6381 slave aab4e4389b3014605ab9549f9545b1bd2545f74c 0 1589420951868 6 connected

#查看集群状态
[root@redis01 ~]# redis-cli -p 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:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_sent:4196
cluster_stats_messages_received:4196

8.插入数据查看

#批量插入数据
[root@redis01 ~]# for i in {1..1000};do echo $i;redis-cli -c -h 172.16.1.91 -p 6380 set k_${i} v_${i};done

#查看节点是否平均
[root@redis01 ~]# redis-trib.rb info 172.16.1.91:6380
172.16.1.91:6380 (aab4e438...) -> 339 keys | 5461 slots | 1 slaves.
172.16.1.93:6380 (20c8c6db...) -> 335 keys | 5461 slots | 1 slaves.
172.16.1.92:6380 (18071a78...) -> 326 keys | 5462 slots | 1 slaves.
[OK] 1000 keys in 3 masters.
0.06 keys per slot on average.

二、redis集群修改节点

#添加或者删除节点流程
1.新节点创建槽位(这个槽位源节点还没有释放删除)
2.源节点查看槽位中的数据
3.源节点将数据导出至新节点对应的槽位
4.源节点槽位数据迁移完毕,删除源槽位
5.判断移动的槽位数量,每一个槽位依次循环迁移数据

1.添加节点

1)添加新机器节点

[root@redis04 ~]# mkdir /server/redis/{6380,6381} -p
[root@redis04 ~]# vim /server/redis/6380/redis.conf
[root@redis04 ~]# vim /server/redis/6381/redis.conf
[root@redis04 ~]# redis-server /server/redis/6380/redis.conf
[root@redis04 ~]# redis-server /server/redis/6381/redis.conf

2)将新节点的主库添加到集群

[root@redis01 ~]# redis-trib.rb add-node 172.16.1.94:6380 172.16.1.91:6380
#或者(二选一)(如果上面的命令报错,则使用下面的命令)
[root@redis01 ~]# redis-cli -h 172.16.1.91 -p 6380 -a 123 CLUSTER MEET 172.16.1.94 6380

#查看节点信息
[root@redis01 ~]# redis-cli -p 6380 -a 123 cluster nodes

3)重新分配槽位

#查看各节点槽位
[root@redis01 ~]# redis-trib.rb info 172.16.1.91:6380
172.16.1.91:6380 (aab4e438...) -> 339 keys | 5461 slots | 1 slaves.
172.16.1.93:6380 (20c8c6db...) -> 335 keys | 5461 slots | 1 slaves.
172.16.1.92:6380 (18071a78...) -> 326 keys | 5462 slots | 1 slaves.
172.16.1.94:6380 (0dc0a9ad...) -> 0 keys | 0 slots | 0 slaves.
[OK] 1000 keys in 4 masters.
0.06 keys per slot on average.

#重新分配槽位
[root@redis01 ~]# redis-trib.rb reshard 172.16.1.91:6380

#你想移动多少槽位到新节点
How many slots do you want to move (from 1 to 16384)? 4096

#接收槽位的节点ID是什么
What is the receiving node ID? 新节点的ID

#键入'all'将所有节点用作哈希槽的源节点。
#输入所有源节点id后,键入“done”。
#源节点# 1:
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

#您想继续执行建议的reshard计划吗
Do you want to proceed with the proposed reshard plan (yes/no)? yes

#重新分配完槽位,查看结果
[root@redis01 6380]# redis-trib.rb info 172.16.1.91:6380
172.16.1.91:6380 (2da5199c...) -> 249 keys | 4096 slots | 1 slaves.
172.16.1.93:6380 (4c162900...) -> 250 keys | 4096 slots | 1 slaves.
172.16.1.94:6380 (d80e483f...) -> 256 keys | 4096 slots | 0 slaves.
172.16.1.92:6380 (5e042385...) -> 245 keys | 4096 slots | 1 slaves.
[OK] 1000 keys in 4 masters.
0.06 keys per slot on average.

4)添加新节点的从库

[root@redis01 6380]# redis-trib.rb add-node --slave --master-id 2da5199c6e217fe6c95c86eff84fe30ebb374e7a 172.16.1.94:6381 172.16.1.91:6380

#命令            添加节点  从节点   指定主节点    主节点的ID           从节点的地址      集群中任意一台机器
redis-trib.rb add-node --slave --master-id   2da5199c6e21...  172.16.1.94:6381  172.16.1.91:6380

#添加后查看节点信息
[root@redis01 6380]# redis-trib.rb info 172.16.1.91:6380
172.16.1.91:6380 (2da5199c...) -> 249 keys | 4096 slots | 1 slaves.
172.16.1.93:6380 (4c162900...) -> 250 keys | 4096 slots | 1 slaves.
172.16.1.94:6380 (d80e483f...) -> 256 keys | 4096 slots | 1 slaves.
172.16.1.92:6380 (5e042385...) -> 245 keys | 4096 slots | 1 slaves.
[OK] 1000 keys in 4 masters.
0.06 keys per slot on average.

#调整主从复制分片,保证每一个节点主库的从库都不跟主库在一台机器上

5)模拟故障

[root@redis01 ~]# redis-trib.rb check 172.16.1.91:6380
#重新分配槽位时 ctrl+c 有几率出现故障

#查看不出来错误
[root@redis01 ~]# redis-cli -p 6380 cluster nodes
[root@redis01 ~]# redis-cli -p 6380 cluster info

#通过检查查看错误
[root@redis01 ~]# redis-trib.rb check 172.16.1.91:6380
[WARNING] Node 172.16.1.94:6380 has slots in migrating state (50).
[WARNING] The following slots are open: 50
#问题1:172.16.1.94:6380节点的第50槽位正在写出
migrating  写出中
importing  写入中

#错误2:172.16.1.94:6380节点再写出,并且172.16.1.92:6380节点在写入
[WARNING] Node 172.16.1.94:6380 has slots in migrating state (284).
[WARNING] Node 172.16.1.92:6380 has slots in importing state (284).
[WARNING] The following slots are open: 284

#错误3:172.16.1.92:6380节点的1482槽位在写入
[root@redis01 ~]# redis-trib.rb check 172.16.1.91:6380
[WARNING] Node 172.16.1.94:6380 has slots in importing state (1482).
[WARNING] The following slots are open: 1482

#连接错误的机器查看
[root@redis01 ~]# redis-cli -h 172.16.1.94 -p 6380 cluster nodes
d80e483f4012831652849019fd703fe96805b45b 172.16.1.94:6380 myself,master - 0 0 7 connected 51-1482 5461-6826 10923-12287 [50->-2da5199c6e217fe6c95c86eff84fe30ebb374e7a]

6)修复故障

#shi用fix修复故障节点
[root@redis01 ~]# redis-trib.rb fix 172.16.1.94:6380

#如果运气不好,没有修复成功,只能将错误的槽位删除再重新添加槽位
怎么判断修复好了:redis-trib.rb reshard 172.16.1.91:6380  判断,但不要执行,如果有红色警告,则没修复好
[root@redis01 ~]# redis-cli -h 172.16.1.94 -p 6380 CLUSTER DELSLOTS 1482
[root@redis01 ~]# redis-cli -h 172.16.1.94 -p 6380 CLUSTER ADDSLOTS 1482

#修复节点槽位以后会出现槽位不平均的情况,如果误差小于2%,不会重新分配
[root@redis01 ~]# redis-trib.rb rebalance 172.16.1.91:6380
>>> Performing Cluster Check (using node 172.16.1.91:6380)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
*** No rebalancing needed! All nodes are within the 2.0% threshold.

#平衡槽位个数
[root@redis01 ~]# redis-trib.rb rebalance 172.16.1.91:6380
>>> Performing Cluster Check (using node 172.16.1.91:6380)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Rebalancing across 4 nodes. Total weight = 4
Moving 167 slots from 172.16.1.92:6380 to 172.16.1.94:6380
#######################################################################################################################################################################
Moving 67 slots from 172.16.1.92:6380 to 172.16.1.91:6380
###################################################################

2.删除节点

1)重新分配槽位

#重新分配槽位
[root@redis01 ~]# redis-trib.rb reshard 172.16.1.91:6380
#移动多少槽位
How many slots do you want to move (from 1 to 16384)? 1365
#移动给谁
What is the receiving node ID? 节点1的ID
#谁来拿出这些槽位
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: 要删除的节点ID
Source node #2: done
#是否确定
Do you want to proceed with the proposed reshard plan (yes/no)? yes

#分配第二次
[root@redis01 ~]# redis-trib.rb reshard 172.16.1.91:6380
#移动多少槽位
How many slots do you want to move (from 1 to 16384)? 1365
#移动给谁
What is the receiving node ID? 节点2的ID
#谁来拿出这些槽位
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: 要删除的节点ID
Source node #2: done
#是否确定
Do you want to proceed with the proposed reshard plan (yes/no)? yes

#分配第三次
[root@redis01 ~]# redis-trib.rb reshard 172.16.1.91:6380
#移动多少槽位
How many slots do you want to move (from 1 to 16384)? 1366
#移动给谁
What is the receiving node ID? 节点3的ID
#谁来拿出这些槽位
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: 要删除的节点ID
Source node #2: done
#是否确定
Do you want to proceed with the proposed reshard plan (yes/no)? yes

#再次查看节点槽位
[root@redis01 ~]# redis-trib.rb info 172.16.1.91:6380
172.16.1.91:6380 (2da5199c...) -> 677 keys | 5461 slots | 1 slaves.
172.16.1.93:6380 (4c162900...) -> 667 keys | 5462 slots | 2 slaves.
172.16.1.94:6380 (d80e483f...) -> 0 keys | 0 slots | 0 slaves.
172.16.1.92:6380 (5e042385...) -> 656 keys | 5461 slots | 1 slaves.
[OK] 2000 keys in 4 masters.
0.12 keys per slot on average.

2)删除节点

#删除槽位为空的节点
[root@redis01 ~]# redis-trib.rb del-node 172.16.1.94:6380 d80e483f4012831652849019fd703fe96805b45b
>>> Removing node d80e483f4012831652849019fd703fe96805b45b from cluster 172.16.1.94:6380
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.

#删除对应机器上的从节点
[root@redis01 ~]# redis-trib.rb del-node 172.16.1.94:6381 b144b3c27f8ee13af0345078bbce76f3b878d02b
>>> Removing node b144b3c27f8ee13af0345078bbce76f3b878d02b from cluster 172.16.1.94:6381
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.

三、redis数据迁移

1.安装迁移工具

[root@redis01 ~]# git clone https://github.com/vipshop/redis-migrate-tool
    [root@redis01 ~]# yum install -y autoconf automake bzip2 libtool
[root@redis01 ~]# cd redis-migrate-tool
[root@redis01 ~/redis-migrate-tool]# autoreconf -fvi    
[root@redis01 ~/redis-migrate-tool]# ./configure
[root@redis01 ~/redis-migrate-tool]# make && make install

2.编写脚本

[root@redis01 ~]# vim tocluster.sh
[source]
type: single
servers:
 - 172.16.1.91:6379

[target]
type: redis cluster
servers:
 - 172.16.1.91:6380

[common]
listen: 0.0.0.0:8888:Q

3.单节点生成数据

[root@redis01 ~]# for i in {1..1000};do echo $i;redis-cli -h 172.16.1.91 -p 6379 set lhd_${i} lhd_${i};done

4.执行脚本迁移数据

[root@redis01 ~]# redis-migrate-tool -c tocluster.sh

四、文件审查工具

1.安装工具

https://github.com/sripathikrishnan/redis-rdb-tools

[root@redis01 ~]# yum install -y python-pip gcc python-devel
[root@redis01 ~]# pip install rdbtools python-lzf
[root@redis01 ~]# git clone https://github.com/sripathikrishnan/redis-rdb-tools
[root@redis01 ~]# cd redis-rdb-tools/
[root@redis01 ~/redis-rdb-tools]# python setup.py install

2.手动生成rdb文件

[root@redis01 ~]# redis-cli 
127.0.0.1:6379> bgsave
Background saving started
127.0.0.1:6379> quit

3.使用工具分析key的大小

[root@redis01 ~]# rdb -c memory /server/redis/6379/dump.rdb -f 1.txt

#取出最大的key
[root@redis01 ~]# awk -F "," '{print $4,$3}' 1.txt | sort -r

#不用工具
[root@redis01 ~]# redis-cli -h 127.0.0.1 -p 6379 --bigkeys
Copyright © 高程程 all right reserved,powered by Gitbook修订于: 2021-05-18 21:15:03

results matching ""

    No results matching ""