目录
一、特点 可自由指定主从 可自定义分配哈希槽 先建立单个实例,在分配主从
二、生产级redis cluster部署 :一台服务器即可(192.168.1.134)
1关闭防火墙
2获取redis6.2.12tar包(官网获取)
3.为了方便管理创建单独目录
4.修改配置文件
5.启动服务
6.用集群模式登录任意节点,将其他节点全部添加到集群
7.分配槽
8.分配主从(有槽的为主)
9.添加节点
10.删除节点 #一条命令便可以清空数据
三、测试
1.关闭7001端口查看
2.启动7001端口查看
3.手动切换主从
四、总结
一、特点 可自由指定主从 可自定义分配哈希槽 先建立单个实例,在分配主从
二、生产级redis cluster部署 :一台服务器即可(192.168.1.134)
1关闭防火墙
[root@localhost ~] systemctl stop firewalld
[root@localhost ~] setenforce 0
[root@localhost ~] iptables -F
2获取redis6.2.12tar包(官网获取)
[root@localhost ~] rz -E
rz waiting to receive.
[root@localhost ~] ll
总用量 2440
-rw-------. 1 root root 1257 6月 16 18:40 anaconda-ks.cfg
-rw-r--r--. 1 root root 2494460 7月 3 15:10 redis-6.2.12.tar.gz
[root@localhost ~] tar zxvf redis-6.2.12.tar.gz
[root@localhost redis-6.2.12] yum -y install gcc gcc-c++
[root@localhost redis-6.2.12] yum -y install make&&make install
#果如make报错
[root@localhost redis-6.2.12] rm -rf redis-6.2.12
[root@localhost ~] tar zxvf redis-6.2.12.tar.gz
[root@localhost redis-6.2.12] yum -y install make&&make install
[root@localhost redis-6.2.12] ./src/redis-server redis.conf
13341:M 04 Jul 2023 09:32:59.469 * monotonic clock: POSIX clock_gettime
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 6.2.12 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 13341
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | https://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
3.为了方便管理创建单独目录
[root@localhost ~] mkdir /usr/local/redis_cluster
[root@localhost ~] cd /usr/local/redis_cluster/
[root@localhost redis_cluster] mkdir -p 7001/data 7002/data 7003/data 7004/data 7005/data 7006/data
[root@localhost redis_cluster] mkdir bin
#命令优化
[root@localhost ~] cd redis-6.2.12/src/
[root@localhost src] cp mkreleasehdr.sh redis-benchmark redis-check-aof redis-cli redis-server redis-trib.rb /usr/local/redis_cluster/bin
4.修改配置文件
[root@localhost ~] cp redis-6.2.12/redis.conf /usr/local/redis_cluster/7001
[root@localhost ~] vim /usr/local/redis_cluster/7001/redis.conf
#删除注释行
:g/^#/d
#删除空行
:g/^$/d
bind 127.0.0.1 # 端口号,可注释,可修改本机
port 7001 #端口
daemonize yes #后台运行
dir /usr/local/redis_cluster/7001/data/ #数据文件存放位置
appendonly yes #开启AOF模式
protected-mode no #为了可以远程链接,关闭保护模式
#开启状态会影响后面添加节点都操作
pidfile /var/run/redis_7001.pid #pid和端口号对应
cluster-enabled yes #开启集群,手动添加
cluster-config-file "node6379.conf" #节点信息文件 ,手动添加
其他目录同样修改
[root@localhost ~] cp -rf /usr/local/redis_cluster/7001/redis.conf /usr/local/redis_cluster/7002/redis.conf
[root@localhost ~] cp -rf /usr/local/redis_cluster/7001/redis.conf /usr/local/redis_cluster/7003/redis.conf
[root@localhost ~] cp -rf /usr/local/redis_cluster/7001/redis.conf /usr/local/redis_cluster/7004/redis.conf
[root@localhost ~] cp -rf /usr/local/redis_cluster/7001/redis.conf /usr/local/redis_cluster/7005/redis.conf
[root@localhost ~] cp -rf /usr/local/redis_cluster/7001/redis.conf /usr/local/redis_cluster/7006/redis.conf
[root@localhost ~] vim /usr/local/redis_cluster/7002/redis.conf
:%s/7001/7002/g
3 次替换,共 3 行
5.启动服务
[root@localhost ~] redis-server /usr/local/redis_cluster/7001/redis.conf
[root@localhost ~] redis-server /usr/local/redis_cluster/7002/redis.conf
[root@localhost ~] redis-server /usr/local/redis_cluster/7003/redis.conf
[root@localhost ~] redis-server /usr/local/redis_cluster/7004/redis.conf
[root@localhost ~] redis-server /usr/local/redis_cluster/7005/redis.conf
[root@localhost ~] redis-server /usr/local/redis_cluster/7006/redis.conf
[root@localhost ~] ps -ef |grep redis
root 18141 1 0 10:27 ? 00:00:00 redis-server *:7001 [cluster]
root 18154 1 0 10:27 ? 00:00:00 redis-server *:7002 [cluster]
root 18163 1 0 10:27 ? 00:00:00 redis-server *:7003 [cluster]
root 18171 1 0 10:27 ? 00:00:00 redis-server *:7004 [cluster]
root 18181 1 0 10:27 ? 00:00:00 redis-server *:7005 [cluster]
root 18189 1 0 10:27 ? 00:00:00 redis-server *:7006 [cluster]
root 18213 8124 0 10:28 pts/0 00:00:00 grep --color=auto redis
6.用集群模式登录任意节点,将其他节点全部添加到集群
[root@localhost ~] redis-cli -p 7001 -c
127.0.0.1:7001> cluster meet 192.168.1.134 7002
OK
127.0.0.1:7001> cluster meet 192.168.1.134 7003
OK
127.0.0.1:7001> cluster meet 192.168.1.134 7004
OK
127.0.0.1:7001> cluster meet 192.168.1.134 7005
OK
127.0.0.1:7001> cluster meet 192.168.1.134 7006
OK
#发现所有节点全是主节点
[root@localhost ~] redis-cli -p 7001 cluster nodes
7ca1820b293bce3f587c316763e2c4a617d06281 192.168.1.134:7006@17006 master - 0 1688438019290 5 connected
eb0677149be54440a019a7f36bd5845da33ba954 192.168.1.134:7001@17001 myself,master - 0 1688438018000 0 connected
6436284b1a8ff18bd669a6f245b751279153a58f 192.168.1.134:7004@17004 master - 0 1688438015255 3 connected
a2e70ea811b17da6769f2233ba1bf0551f04efd3 192.168.1.134:7005@17005 master - 0 1688438017272 4 connected
d98777ec27cd2499130c84c6b07d6fb7ea468d04 192.168.1.134:7002@17002 master - 0 1688438016000 1 connected
8a8ce34ff6f862b4f81d54334fcb083b0a50c2c9 192.168.1.134:7003@17003 master - 0 1688438018282 2 connected
7.分配槽
[root@localhost ~] redis-cli -p 7001 cluster addslots {0..5000}
OK
[root@localhost ~] redis-cli -p 7002 cluster addslots {5001..10000}
OK
[root@localhost ~] redis-cli -p 7003 cluster addslots {10001..16383}
OK
#可以看见槽分配完毕
[root@localhost ~] redis-cli -p 7001 cluster nodes
7ca1820b293bce3f587c316763e2c4a617d06281 192.168.1.134:7006@17006 master - 0 1688439098753 5 connected
eb0677149be54440a019a7f36bd5845da33ba954 192.168.1.134:7001@17001 myself,master - 0 1688439099000 0 connected 0-5000 #7001
6436284b1a8ff18bd669a6f245b751279153a58f 192.168.1.134:7004@17004 master - 0 1688439099763 3 connected
a2e70ea811b17da6769f2233ba1bf0551f04efd3 192.168.1.134:7005@17005 master - 0 1688439098000 4 connected
d98777ec27cd2499130c84c6b07d6fb7ea468d04 192.168.1.134:7002@17002 master - 0 1688439097000 1 connected 5001-10000 #7002
8a8ce34ff6f862b4f81d54334fcb083b0a50c2c9 192.168.1.134:7003@17003 master - 0 1688439097745 2 connected 10001-16383 #7003
8.分配主从(有槽的为主)
#建立主从关系 7004作为 节点ID为eb0677149be54440a019a7f36bd5845da33ba954的从服务器
[root@localhost ~] redis-cli -p 7004 cluster replicate eb0677149be54440a019a7f36bd5845da33ba954
OK
[root@localhost ~] redis-cli -p 7005 cluster replicate d98777ec27cd2499130c84c6b07d6fb7ea468d04
OK
[root@localhost ~] redis-cli -p 7006 cluster replicate 8a8ce34ff6f862b4f81d54334fcb083b0a50c2c9
OK
[root@localhost ~] redis-cli -p 7001 -c cluster nodes
7ca1820b293bce3f587c316763e2c4a617d06281 192.168.1.134:7006@17006 slave 8a8ce34ff6f862b4f81d54334fcb083b0a50c2c9 0 1688440393326 2 connected
eb0677149be54440a019a7f36bd5845da33ba954 192.168.1.134:7001@17001 myself,master - 0 1688440393000 0 connected 0-5000
6436284b1a8ff18bd669a6f245b751279153a58f 192.168.1.134:7004@17004 slave eb0677149be54440a019a7f36bd5845da33ba954 0 1688440394334 0 connected
a2e70ea811b17da6769f2233ba1bf0551f04efd3 192.168.1.134:7005@17005 slave d98777ec27cd2499130c84c6b07d6fb7ea468d04 0 1688440392000 1 connected
d98777ec27cd2499130c84c6b07d6fb7ea468d04 192.168.1.134:7002@17002 master - 0 1688440394000 1 connected 5001-10000
8a8ce34ff6f862b4f81d54334fcb083b0a50c2c9 192.168.1.134:7003@17003 master - 0 1688440392318 2 connected 10001-16383
9.添加节点
[root@localhost redis_cluster] mkdir 7007
[root@localhost redis_cluster] cp -rf /usr/local/redis_cluster/7001/redis.conf /usr/local/redis_cluster/7007/redis.conf
#vim替换 :%s/7001/7007/g
[root@localhost 7007] mkdir /usr/local/redis_cluster/7007/data
[root@localhost 7007] redis-server redis.conf
[root@localhost 7007] redis-cli --cluster add-node 192.168.1.134:7007 192.168.1.134:7001
>>> Adding node 192.168.1.134:7007 to cluster 192.168.1.134:7001
>>> Performing Cluster Check (using node 192.168.1.134:7001)
M: eb0677149be54440a019a7f36bd5845da33ba954 192.168.1.134:7001
slots:[0-5000] (5001 slots) master
1 additional replica(s)
S: 7ca1820b293bce3f587c316763e2c4a617d06281 192.168.1.134:7006
slots: (0 slots) slave
replicates 8a8ce34ff6f862b4f81d54334fcb083b0a50c2c9
S: 6436284b1a8ff18bd669a6f245b751279153a58f 192.168.1.134:7004
slots: (0 slots) slave
replicates eb0677149be54440a019a7f36bd5845da33ba954
S: a2e70ea811b17da6769f2233ba1bf0551f04efd3 192.168.1.134:7005
slots: (0 slots) slave
replicates d98777ec27cd2499130c84c6b07d6fb7ea468d04
M: d98777ec27cd2499130c84c6b07d6fb7ea468d04 192.168.1.134:7002
slots:[5001-10000] (5000 slots) master
1 additional replica(s)
M: 8a8ce34ff6f862b4f81d54334fcb083b0a50c2c9 192.168.1.134:7003
slots:[10001-16383] (6383 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.
>>> Send CLUSTER MEET to node 192.168.1.134:7007 to make it join the cluster.
[OK] New node added correctly.
[root@localhost 7007] redis-cli -p 7007 cluster replicate eb0677149be54440a019a7f36bd5845da33ba954
OK
#发现7001端口有2个从服务器
[root@localhost 7007] redis-cli -c -p 7001 cluster nodes
7ca1820b293bce3f587c316763e2c4a617d06281 192.168.1.134:7006@17006 slave 8a8ce34ff6f862b4f81d54334fcb083b0a50c2c9 0 1688441488330 2 connected
eb0677149be54440a019a7f36bd5845da33ba954 192.168.1.134:7001@17001 myself,master - 0 1688441486000 0 connected 0-5000
6436284b1a8ff18bd669a6f245b751279153a58f 192.168.1.134:7004@17004 slave #一 eb0677149be54440a019a7f36bd5845da33ba954 0 1688441486313 0 connected
a2e70ea811b17da6769f2233ba1bf0551f04efd3 192.168.1.134:7005@17005 slave d98777ec27cd2499130c84c6b07d6fb7ea468d04 0 1688441488000 1 connected
d98777ec27cd2499130c84c6b07d6fb7ea468d04 192.168.1.134:7002@17002 master - 0 1688441487000 1 connected 5001-10000
8a8ce34ff6f862b4f81d54334fcb083b0a50c2c9 192.168.1.134:7003@17003 master - 0 1688441489338 2 connected 10001-16383
5b1b413f227bc64b596633bac69343dda26fd4b5 192.168.1.134:7007@17007 slave #二 eb0677149be54440a019a7f36bd5845da33ba954 0 1688441487322 0 connected
10.删除节点 #一条命令便可以清空数据
[root@localhost ~] redis-cli --cluster del-node 192.168.1.134:7007 5b1b413f227bc64b596633bac69343dda26fd4b5
>>> Removing node 5b1b413f227bc64b596633bac69343dda26fd4b5 from cluster 192.168.1.134:7007
>>> Sending CLUSTER FORGET messages to the cluster...
>>> Sending CLUSTER RESET SOFT to the deleted node.
[root@localhost ~] redis-cli -c -p 7001 cluster nodes
7ca1820b293bce3f587c316763e2c4a617d06281 192.168.1.134:7006@17006 slave 8a8ce34ff6f862b4f81d54334fcb083b0a50c2c9 0 1688441785807 2 connected
eb0677149be54440a019a7f36bd5845da33ba954 192.168.1.134:7001@17001 myself,master - 0 1688441784000 0 connected 0-5000
6436284b1a8ff18bd669a6f245b751279153a58f 192.168.1.134:7004@17004 slave eb0677149be54440a019a7f36bd5845da33ba954 0 1688441783000 0 connected
a2e70ea811b17da6769f2233ba1bf0551f04efd3 192.168.1.134:7005@17005 slave d98777ec27cd2499130c84c6b07d6fb7ea468d04 0 1688441782000 1 connected
d98777ec27cd2499130c84c6b07d6fb7ea468d04 192.168.1.134:7002@17002 master - 0 1688441783791 1 connected 5001-10000
8a8ce34ff6f862b4f81d54334fcb083b0a50c2c9 192.168.1.134:7003@17003 master - 0 1688441784799 2 connected 10001-16383
想再加入节点,重复第9步操作即可
三、测试
1.关闭7001端口查看
[root@localhost 7001] redis-cli -p 7001 shutdown
[root@localhost 7001] redis-cli -c -p 7002 cluster nodes
7ca1820b293bce3f587c316763e2c4a617d06281 192.168.1.134:7006@17006 slave 8a8ce34ff6f862b4f81d54334fcb083b0a50c2c9 0 1688442771000 2 connected
eb0677149be54440a019a7f36bd5845da33ba954 192.168.1.134:7001@17001 master,fail - 1688442731500 1688442727000 0 disconnected
6436284b1a8ff18bd669a6f245b751279153a58f 192.168.1.134:7004@17004 master - 0 1688442770000 7 connected 0-5000
8a8ce34ff6f862b4f81d54334fcb083b0a50c2c9 192.168.1.134:7003@17003 master - 0 1688442769000 2 connected 10001-16383
a2e70ea811b17da6769f2233ba1bf0551f04efd3 192.168.1.134:7005@17005 slave d98777ec27cd2499130c84c6b07d6fb7ea468d04 0 1688442771850 1 connected
d98777ec27cd2499130c84c6b07d6fb7ea468d04 192.168.1.134:7002@17002 myself,master - 0 1688442769000 1 connected 5001-10000
#发现7004变成了master,继承0-5000槽
2.启动7001端口查看
[root@localhost 7001] redis-server /usr/local/redis_cluster/7001/redis.conf
[root@localhost 7001] redis-cli -c -p 7002 cluster nodes
7ca1820b293bce3f587c316763e2c4a617d06281 192.168.1.134:7006@17006 slave 8a8ce34ff6f862b4f81d54334fcb083b0a50c2c9 0 1688442948377 2 connected
eb0677149be54440a019a7f36bd5845da33ba954 192.168.1.134:7001@17001 slave 6436284b1a8ff18bd669a6f245b751279153a58f 0 1688442946359 7 connected
6436284b1a8ff18bd669a6f245b751279153a58f 192.168.1.134:7004@17004 master - 0 1688442947000 7 connected 0-5000
8a8ce34ff6f862b4f81d54334fcb083b0a50c2c9 192.168.1.134:7003@17003 master - 0 1688442947369 2 connected 10001-16383
a2e70ea811b17da6769f2233ba1bf0551f04efd3 192.168.1.134:7005@17005 slave d98777ec27cd2499130c84c6b07d6fb7ea468d04 0 1688442949386 1 connected
d98777ec27cd2499130c84c6b07d6fb7ea468d04 192.168.1.134:7002@17002 myself,master - 0 1688442944000 1 connected 5001-10000
#7001重新加入 变成了7004的从服务器
3.手动切换主从
root@localhost 7001] redis-cli -h 192.168.1.134 -c -p 7001 cluster failover
OK
[root@localhost 7001] redis-cli -c -p 7002 cluster nodes
7ca1820b293bce3f587c316763e2c4a617d06281 192.168.1.134:7006@17006 slave 8a8ce34ff6f862b4f81d54334fcb083b0a50c2c9 0 1688443189345 2 connected
eb0677149be54440a019a7f36bd5845da33ba954 192.168.1.134:7001@17001 master - 0 1688443188336 8 connected 0-5000
6436284b1a8ff18bd669a6f245b751279153a58f 192.168.1.134:7004@17004 slave eb0677149be54440a019a7f36bd5845da33ba954 0 1688443190353 8 connected
8a8ce34ff6f862b4f81d54334fcb083b0a50c2c9 192.168.1.134:7003@17003 master - 0 1688443186000 2 connected 10001-16383
a2e70ea811b17da6769f2233ba1bf0551f04efd3 192.168.1.134:7005@17005 slave d98777ec27cd2499130c84c6b07d6fb7ea468d04 0 1688443188000 1 connected
d98777ec27cd2499130c84c6b07d6fb7ea468d04 192.168.1.134:7002@17002 myself,master - 0 1688443187000 1 connected 5001-10000
7001端口又变成主服务器了文章来源:https://www.toymoban.com/news/detail-526925.html
四、总结
6.2.10版本的 redis cluster 集群 比4.0版本操作要简单很多,一个命令就可以清空要删除的节点的数据,故障转移和之前一样,从服务器会替代主服务器,主服务器回来后会替代从服务器文章来源地址https://www.toymoban.com/news/detail-526925.html
到了这里,关于redis(6.0以上版本)哨兵、生产级redis cluster集群部署、测试、特点的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!