Redis集群 脚本实现配置
Redis集群配置
前置准备
安装包redis-4.0.8.tar.gz
安装包redis-3.2.1.gem
主机名 | IP地址 | 端口号 |
---|---|---|
client50 | 192.168.4.50 | 无 |
redis71 | 192.168.4.71 | 6371 |
redis72 | 192.168.4.72 | 6372 |
redis73 | 192.168.4.73 | 6373 |
redis74 | 192.168.4.74 | 6374 |
redis75 | 192.168.4.75 | 6375 |
redis76 | 192.168.4.76 | 6376 |
mgm77 | 192.168.4.77 | 无 |
配置过程
管理端部署
#mgm77 部署ruby脚本运行环境
yum - y install rubygems
gem install redis-3.2.1.gem
#创建管理集群脚本
mkdir /root/bin #创建命令检索目录
tar -zxvf redis-4.0.8.tar.gz
cd redis-4.0.8/src/
cp redis-trib.rb /root/bin/ #创建管理集群脚本
chmod +x /root/bin/redis-trib.rb
redis-trib.rb help #可以查看命令帮助
redis数据库服务器部署
redis安装包在/root/下 脚本也在/root/下
#4.71~4.76部署redis并开启集群功能
vim redis-port.sh #写脚本完成
=================================
#!/bin/bash
_IP_=`ifconfig ens33 | awk -F " " '/inet /{print $2}' | awk -F "." '{print $4}'`
echo $_IP_ #获取本机IP主机位
yum -y install gcc &> /dev/null #安装源码编译需要的gcc
tar -zxf redis-4.0.8.tar.gz &> /dev/null
cd redis-4.0.8/
make && make install
#sed -i '/^port/s/6379/63'$_IP_'/' redis.conf #redis的配置文件里面有了 就不修改了
sed -i '/^bind/s/127.0.0.1/192.168.4.'$_IP_'/' redis.conf #修改访问IP
cd utils/ #以下三步是修改redis交互安装脚本的read 使得可以非交互安装
sed -i '/^_REDIS_PORT=/s/6379/63'$_IP_'/' install_server.sh
sed -i '/read.*-p/s/re/#re/' install_server.sh #脚本里面很骚的是 有一个read -p之间有两个空格
sed -i '146,148d' install_server.sh #if语句直接删除即可
./install_server.sh #执行安装
_FILE_=/etc/init.d/redis_63$_IP_
sed -i '/CLIEXEC.*-p/s/\$REDISPORT/63'$_IP_' -h 192.168.4.'$_IP_'/' $_FILE_
$_FILE_ restart #修改/etc/init.d/redis_63xx脚本中关闭redis的那行 否则-h和-p不对
##到此redis的端口和IP都弄好了 下面是开启redis集群功能 不需要开启到复制上面的就可以了
echo '开启redis集群功能'
_FILE_=/etc/redis/63$_IP_.conf #下面三行修改主配置文件 开启集群功能
sed -i '/cluster-enabled/s/#//' $_FILE_
sed -i '/cluster-config-file/s/#//' $_FILE_
sed -i '/cluster-node-timeout/s/#//' $_FILE_
rm -rf "/var/lib/redis/63$_IP_/*" #清空内容 防止集群启动失败
#_FILE=/etc/init.d/redis_63$_IP_ #用变量执行重启会权限不够
#echo $_FILE_
"/etc/init.d/redis_63$_IP_" restart
ss -nutlp | grep redis-server #查看集群端口是否开启
==========================================
bash ./redis-port.sh #执行脚本
.....
tcp LISTEN 0 128 192.168.4.75:6375 *:* users:(("redis-server",pid=5337,fd=6))
tcp LISTEN 0 128 192.168.4.75:16375 *:* users:(("redis-server",pid=5337,fd=8))
#有两个端口 启动成功
4.77创建集群
redis-trib.rb create -replicas 1 \
> 192.168.4.71:6371 192.168.4.72:6372 192.168.4.73:6373 \
> 192.168.4.74:6374 192.168.4.75:6375 192.168.4.76:6376
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.4.71:6371
192.168.4.72:6372
192.168.4.73:6373
Adding replica 192.168.4.75:6375 to 192.168.4.71:6371
Adding replica 192.168.4.76:6376 to 192.168.4.72:6372
Adding replica 192.168.4.74:6374 to 192.168.4.73:6373
M: 6fa217f88269ea225b3ee445dcc053feff7c29f7 192.168.4.71:6371
slots:0-5460 (5461 slots) master
M: de02105bedfe77c96ba642ff3c36355621c160e0 192.168.4.72:6372
slots:5461-10922 (5462 slots) master
M: 2523000f264f7d85e4d5eebcb9e17e39b45bf2c2 192.168.4.73:6373
slots:10923-16383 (5461 slots) master
S: fc7e3bc5af41906d31bd1385d9058e154813e5c1 192.168.4.74:6374
replicates 2523000f264f7d85e4d5eebcb9e17e39b45bf2c2
S: abac3d03c0efbdfbc9781cf983f50ebbef40991a 192.168.4.75:6375
replicates 6fa217f88269ea225b3ee445dcc053feff7c29f7
S: 987fecc7231c2e156b7897ef26cc704c4bd6eba6 192.168.4.76:6376
replicates de02105bedfe77c96ba642ff3c36355621c160e0
Can I set the above configuration? (type 'yes' to accept): yes #同意上面配置 CRC16分配hash槽数量
>>> 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 192.168.4.71:6371)
M: 6fa217f88269ea225b3ee445dcc053feff7c29f7 192.168.4.71:6371
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: fc7e3bc5af41906d31bd1385d9058e154813e5c1 192.168.4.74:6374
slots: (0 slots) slave
replicates 2523000f264f7d85e4d5eebcb9e17e39b45bf2c2
S: abac3d03c0efbdfbc9781cf983f50ebbef40991a 192.168.4.75:6375
slots: (0 slots) slave
replicates 6fa217f88269ea225b3ee445dcc053feff7c29f7
S: 987fecc7231c2e156b7897ef26cc704c4bd6eba6 192.168.4.76:6376
slots: (0 slots) slave
replicates de02105bedfe77c96ba642ff3c36355621c160e0
M: 2523000f264f7d85e4d5eebcb9e17e39b45bf2c2 192.168.4.73:6373
slots:10923-16383 (5461 slots) master
1 additional replica(s)
M: de02105bedfe77c96ba642ff3c36355621c160e0 192.168.4.72:6372
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.
#查看集群信息
redis-trib.rb info 192.168.4.71:6371
192.168.4.71:6371 (6fa217f8...) -> 0 keys | 5461 slots | 1 slaves.
192.168.4.73:6373 (2523000f...) -> 0 keys | 5461 slots | 1 slaves.
192.168.4.72:6372 (de02105b...) -> 0 keys | 5462 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.
#检测集群
redis-trib.rb check 192.168.4.71:6371
>>> Performing Cluster Check (using node 192.168.4.71:6371)
M: 6fa217f88269ea225b3ee445dcc053feff7c29f7 192.168.4.71:6371
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: fc7e3bc5af41906d31bd1385d9058e154813e5c1 192.168.4.74:6374
slots: (0 slots) slave
replicates 2523000f264f7d85e4d5eebcb9e17e39b45bf2c2
S: abac3d03c0efbdfbc9781cf983f50ebbef40991a 192.168.4.75:6375
slots: (0 slots) slave
replicates 6fa217f88269ea225b3ee445dcc053feff7c29f7
S: 987fecc7231c2e156b7897ef26cc704c4bd6eba6 192.168.4.76:6376
slots: (0 slots) slave
replicates de02105bedfe77c96ba642ff3c36355621c160e0
M: 2523000f264f7d85e4d5eebcb9e17e39b45bf2c2 192.168.4.73:6373
slots:10923-16383 (5461 slots) master
1 additional replica(s)
M: de02105bedfe77c96ba642ff3c36355621c160e0 192.168.4.72:6372
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.
任意节点服务器查看
#4.71
redis-cli -h 192.168.4.71 -p 6371
#查看集群节点信息
192.168.4.71:6371> 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_ping_sent:121
cluster_stats_messages_pong_sent:133
cluster_stats_messages_sent:254
cluster_stats_messages_ping_received:128
cluster_stats_messages_pong_received:121
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:254
#查看集群节点信息
192.168.4.71:6371> cluster nodes
fc7e3bc5af41906d31bd1385d9058e154813e5c1 192.168.4.74:6374@16374 slave 2523000f264f7d85e4d5eebcb9e17e39b45b f2c2 0 1611906472307 4 connected
abac3d03c0efbdfbc9781cf983f50ebbef40991a 192.168.4.75:6375@16375 slave 6fa217f88269ea225b3ee445dcc053feff7c 29f7 0 1611906470276 5 connected
987fecc7231c2e156b7897ef26cc704c4bd6eba6 192.168.4.76:6376@16376 slave de02105bedfe77c96ba642ff3c36355621c1 60e0 0 1611906468245 6 connected
2523000f264f7d85e4d5eebcb9e17e39b45bf2c2 192.168.4.73:6373@16373 master - 0 1611906469666 3 connected 10923 -16383
de02105bedfe77c96ba642ff3c36355621c160e0 192.168.4.72:6372@16372 master - 0 1611906471292 2 connected 5461- 10922
6fa217f88269ea225b3ee445dcc053feff7c29f7 192.168.4.71:6371@16371 myself,master - 0 1611906468000 1 connecte d 0-5460
访问集群
#4.50
redis-cli -c -h 192.168.4.71 -p 6371 #访问集群需要-c参数
添加新的服务器
4.78和4.79都开启redis集群功能
#4.77
redis-trib.rb add-node 192.168.4.78:6378 192.168.4.71:6371 #添加主节点
redis-tris.rb info 192.168.4.73:6373 #查看集群信息 任意一个都可以查看
redis-tris.rb check 192.168.4.73:6373 #检测集群
#因为新加的集群没有hash槽需要分配
redis-trib.rb reshard 192.168.4.73:6373 #从4.73上分配 需要主服务器上分才行
How many slots do you want to move (from 1 to 16384)?4096 #拿出4096个hash 槽给主机192.168.4.78
What is the receiving node ID? c5e0da48f335c46a2ec199faa99b830f537dd8a0 #主机192.168.4.78的id值
Source node #1:all #从当前所有master服务器获取hash槽
Do you want to proceed with the proposed reshard plan (yes/no)?yes #同意以上配置
redis-trib.rb info 192.168.4.73:6373 #查看集群信息 hash槽数量
redis-trib.rb add-node --slave 192.168.4.79:6379 192.168.4.71:6371 #添加从 自动添加到之前的主
redis-tris.rb info 192.168.4.73:6373 #查看集群信息
redis-tris.rb check 192.168.4.73:6373 #检测集群
#4.78上访问
redis-cli -c -h 192.168.4.79 -p 6379
keys * #发现自动同步了主服务器78的数据
移除服务器
#4.77 执行移除命令 移除集群中新加的4.79从服务器
redis-trib.rb del-node 192.168.4.71:6371 f6649ea99b2f01faca26217691222c17a3854381
#注意 这里192.168.4.71: 6371指集群 后面的一串字符是4.79的
redis-trib.rb info 192.168.4.71:6371 #查看集群信息
redis-trib.rb reshard 192.168.4.71:6371 #移除master服务器需要先删除占用的hash槽
How many slots do you want to move (from 1 to 16384)?4096 #移除4096个数槽
What is the receiving node ID? bc5c4e082a5a3391b634cf433a6486c867cfc44b
#要移动给谁的id即目标主机(这里可以随机写一个master的ID)
Source node #1: c5e0da48f335c46a2ec199faa99b830f537dd8a0 #从谁那移动即源主机(这里写4.78的ID)
Source node #2:done //设置完毕
....
Do you want to proceed with the proposed reshard plan (yes/no)?yes #提交
...
#移除集群中新加的4.78 后面那个是4.78的字符串
redis-trib.rb del-node 192.168.4.73:6373 c5e0da48f335c46a2ec199faa99b830f537dd8a0
如何把删除的主机添加回集群
#删除的主机
redis-cli -h 192.168.4.78 -p 6378
cluster reset
exit
#再在管理端添加即可
#也可以删除集群配置文件重启服务再添加
rm -rf /var/lib/redis/6378/nodes-63xx.conf
/etc/ini.d/redis-63xx restart
恢复为独立的redis服务器
redis-cli -h 192.168.4.71 -p 6371 shutdown
rm -rf /var/lib/redis/6371/* #删除集群文件和数据
vim /etc/redis/6371/conf #关闭集群功能
==============================
#cluster-enabled yes
#cluster-config-file nodes-6379.conf
#cluster-node-timeout 15000
====================================
/etc/init.d/redis_6371 start
>cluster info
>info replication
>key *