当前位置:网站首页>CentOS安装Redis多主多从集群
CentOS安装Redis多主多从集群
2022-06-26 09:41:00 【xianghan收藏册】
CentOS 单机安装Redis Cluster(3主3从)
为了节省机器,我们直接把6个Redis实例安装在同一台机器上(3主3从),只是使用不同的端口号。
机器IP 192.168.56.183
cd /usr/local/redis-5.0.5
mkdir redis-cluster
cd redis-cluster
mkdir 6291 6292 6293 6294 6295 6296
复制redis配置文件到6291目录
cp /usr/local/redis-5.0.5/redis.conf /usr/local/redis-5.0.5/redis-cluster/6291
修改6291的redis.conf配置文件,内容:
cd /usr/local/redis-5.0.5/redis-cluster/6291
>redis.conf
vim redis.conf
port 6291
requirepass 123456
daemonize yes
protected-mode no
dir /usr/local/redis-5.0.5/redis-cluster/6291/
cluster-enabled yes
cluster-config-file nodes-6291.conf
cluster-node-timeout 5000
appendonly yes
pidfile /var/run/redis_6291.pid
注意,外网集群要添加这个配置:
# 实际给各节点网卡分配的IP(公网IP)
cluster-announce-ip 47.xx.xx.xx
# 节点映射端口
cluster-announce-port ${PORT}
# 节点总线端口
cluster-announce-bus-port 1${PORT}
把6291下的redis.conf复制到其他5个目录。
cd /usr/local/redis-5.0.5/redis-cluster/6291
cp redis.conf ../6292
cp redis.conf ../6293
cp redis.conf ../6294
cp redis.conf ../6295
cp redis.conf ../6296
批量替换内容
cd /usr/local/redis-5.0.5/redis-cluster
sed -i 's/6291/6292/g' 6292/redis.conf
sed -i 's/6291/6293/g' 6293/redis.conf
sed -i 's/6291/6294/g' 6294/redis.conf
sed -i 's/6291/6295/g' 6295/redis.conf
sed -i 's/6291/6296/g' 6296/redis.conf
启动6个Redis节点
cd /usr/local/redis-5.0.5/
./src/redis-server redis-cluster/6291/redis.conf
./src/redis-server redis-cluster/6292/redis.conf
./src/redis-server redis-cluster/6293/redis.conf
./src/redis-server redis-cluster/6294/redis.conf
./src/redis-server redis-cluster/6295/redis.conf
./src/redis-server redis-cluster/6296/redis.conf
是否启动了6个进程
ps -ef|grep redis
创建集群
旧版本中的redis-trib.rb已经废弃了,直接用–cluster命令
注意用绝对IP,不要用127.0.0.1
cd /usr/local/redis-5.0.5/src/
redis-cli -a 123456 --cluster create 192.168.56.183:6291 192.168.56.183:6292 192.168.56.183:6293 192.168.56.183:6294 192.168.56.183:6295 192.168.56.183:6296 --cluster-replicas 1
Redis会给出一个预计的方案,对6个节点分配3主3从,如果认为没有问题,输入yes确认
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 127.0.0.1:6295 to 127.0.0.1:6291
Adding replica 127.0.0.1:6296 to 127.0.0.1:6292
Adding replica 127.0.0.1:6294 to 127.0.0.1:6293
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: dfdc9c0589219f727e4fd0ad8dafaf7e0cfb4f1c 127.0.0.1:6291
slots:[0-5460] (5461 slots) master
M: 8c878b45905bba3d7366c89ec51bd0cd7ce959f8 127.0.0.1:6292
slots:[5461-10922] (5462 slots) master
M: aeeb7d7076d9b25a7805ac6f508497b43887e599 127.0.0.1:6293
slots:[10923-16383] (5461 slots) master
S: ebc479e609ff8f6ca9283947530919c559a08f80 127.0.0.1:6294
replicates aeeb7d7076d9b25a7805ac6f508497b43887e599
S: 49385ed6e58469ef900ec48e5912e5f7b7505f6e 127.0.0.1:6295
replicates dfdc9c0589219f727e4fd0ad8dafaf7e0cfb4f1c
S: 8d6227aefc4830065624ff6c1dd795d2d5ad094a 127.0.0.1:6296
replicates 8c878b45905bba3d7366c89ec51bd0cd7ce959f8
Can I set the above configuration? (type 'yes' to accept):
注意看slot的分布:
6291 [0-5460] (5461个槽)
6292 [5461-10922] (5462个槽)
6293 [10923-16383] (5461个槽)
集群创建完成
>>> 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 127.0.0.1:6291)
M: dfdc9c0589219f727e4fd0ad8dafaf7e0cfb4f1c 127.0.0.1:6291
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: 8c878b45905bba3d7366c89ec51bd0cd7ce959f8 127.0.0.1:6292
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
M: aeeb7d7076d9b25a7805ac6f508497b43887e599 127.0.0.1:6293
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 8d6227aefc4830065624ff6c1dd795d2d5ad094a 127.0.0.1:6296
slots: (0 slots) slave
replicates aeeb7d7076d9b25a7805ac6f508497b43887e599
S: ebc479e609ff8f6ca9283947530919c559a08f80 127.0.0.1:6294
slots: (0 slots) slave
replicates dfdc9c0589219f727e4fd0ad8dafaf7e0cfb4f1c
S: 49385ed6e58469ef900ec48e5912e5f7b7505f6e 127.0.0.1:6295
slots: (0 slots) slave
replicates 8c878b45905bba3d7366c89ec51bd0cd7ce959f8
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
重置集群的方式是在每个节点上个执行cluster reset,然后重新创建集群
连接到客户端
redis-cli -p 6291
redis-cli -p 6292
redis-cli -p 6293
批量写入值
cd /usr/local/redis-5.0.5/redis-cluster/
vim setkey.sh
脚本内容
#!/bin/bash
for ((i=0;i<20000;i++))
do
echo -en "helloworld" | redis-cli -h 192.168.56.183 -p 6291 -c -x set name$i >>redis.log
done
chmod +x setkey.sh
./setkey.sh
每个节点分布的数据
127.0.0.1:6292> dbsize
(integer) 6683
127.0.0.1:6293> dbsize
(integer) 6665
127.0.0.1:6291> dbsize
(integer) 6652
其他命令,比如添加节点、删除节点,重新分布数据:
redis-cli --cluster help
Cluster Manager Commands:
create host1:port1 ... hostN:portN
--cluster-replicas <arg>
check host:port
--cluster-search-multiple-owners
info host:port
fix host:port
--cluster-search-multiple-owners
reshard host:port
--cluster-from <arg>
--cluster-to <arg>
--cluster-slots <arg>
--cluster-yes
--cluster-timeout <arg>
--cluster-pipeline <arg>
--cluster-replace
rebalance host:port
--cluster-weight <node1=w1...nodeN=wN>
--cluster-use-empty-masters
--cluster-timeout <arg>
--cluster-simulate
--cluster-pipeline <arg>
--cluster-threshold <arg>
--cluster-replace
add-node new_host:new_port existing_host:existing_port
--cluster-slave
--cluster-master-id <arg>
del-node host:port node_id
call host:port command arg arg .. arg
set-timeout host:port milliseconds
import host:port
--cluster-from <arg>
--cluster-copy
--cluster-replace
help
For check, fix, reshard, del-node, set-timeout you can specify the host and port of any working node in the cluster.
附录:
集群命令
cluster info :打印集群的信息
cluster nodes :列出集群当前已知的所有节点(node),以及这些节点的相关信息。
cluster meet :将 ip 和 port 所指定的节点添加到集群当中,让它成为集群的一份子。
cluster forget <node_id> :从集群中移除 node_id 指定的节点(保证空槽道)。
cluster replicate <node_id> :将当前节点设置为 node_id 指定的节点的从节点。
cluster saveconfig :将节点的配置文件保存到硬盘里面。
槽slot命令
cluster addslots [slot …] :将一个或多个槽(slot)指派(assign)给当前节点。
cluster delslots [slot …] :移除一个或多个槽对当前节点的指派。
cluster flushslots :移除指派给当前节点的所有槽,让当前节点变成一个没有指派任何槽的节点。
cluster setslot node <node_id> :将槽 slot 指派给 node_id 指定的节点,如果槽已经指派给另一个节点,那么先让另一个节点删除该槽>,然后再进行指派。
cluster setslot migrating <node_id> :将本节点的槽 slot 迁移到 node_id 指定的节点中。
cluster setslot importing <node_id> :从 node_id 指定的节点中导入槽 slot 到本节点。
cluster setslot stable :取消对槽 slot 的导入(import)或者迁移(migrate)。
键命令
cluster keyslot :计算键 key 应该被放置在哪个槽上。
cluster countkeysinslot :返回槽 slot 目前包含的键值对数量。
cluster getkeysinslot :返回 count 个 slot 槽中的键
边栏推荐
- Mysql database operation commands (constantly updated)
- 36 qtextedit control input multiline text
- About multi table query of MySQL
- Jar version conflict resolution
- Battery historian analyzes battery consumption
- 微软 Edge 浏览器 IE 模式标签页出现卡死情况,已通过回滚更新修复
- Establishment of smart dialogue platform for wechat official account
- 开发者,微服务架构到底是什么?
- 二叉树常见面试题
- 六月集训(第26天) —— 并查集
猜你喜欢

微软 Edge 浏览器 IE 模式标签页出现卡死情况,已通过回滚更新修复

創建對象的時候堆內存的分配

MySQL第十四次作业--电子商城项目

如何更改微信小程序二维码物料颜色

36 qtextedit control input multiline text

Develop current learning objectives and methods

MySQL第六次作业-查询数据-多条件

P1296 whispers of cows (quick row + binary search)

Call API interface to generate QR code of wechat applet with different colors

首批12家企业入驻!广州首个集中展销老字号产品专柜开张
随机推荐
JVM垃圾回收什么情况会进入老年代
Global and Chinese market of aluminum sunshade systems 2022-2028: Research Report on technology, participants, trends, market size and share
六月集训(第26天) —— 并查集
Under the double reduction, the amount of online education has plummeted. Share 12 interesting uses of webrtc
Constraintlayout control uses full Raiders
开发者,微服务架构到底是什么?
Blog post index summary --c #
MySQL项目8总结
基础-MySQL
【深度优先搜索】312.戳气球
exec系列函数(execl、execlp、execle、execv、execvp)使用
C中字符串基本操作
MySQL第四章总结
What is in the method area - class file, class file constant pool, runtime constant pool
Mysql database operation commands (constantly updated)
MySQL第五章总结
二叉树常见面试题
String class intern() method and string constant pool
SSM项目小例子,SSM整合图文详细教程
Threading model in webrtc native