![]()
3.2.2.2:部署 redis 集群:
环境准备:
环境 A:三台服务器,每台服务器启动 6379 和 6380 两个 redis 服务。
172.16.99.127:6379/6380
172.16.99.128:6379/6380
172.16.99.129:6379/6380
另外预留一台服务器做集群添加节点测试。
172.16.99.130:6379/6380
环境 B:生产环境建议直接 6 台服务器。
172.16.99.127
172.16.99.128
172.16.99.129
172.16.99.130
172.16.99.131
172.16.99.132
预留服务器
172.16.99.133
3.2.2.2.1:创建 redis cluster 集群的前提:
1.每个 redis node 节点采用相同的硬件配置、相同的密码
2.每个节点必须开启的参数
cluster-enabled yes #必须开启集群状态,开启后 redis 进程会有 cluster 显示
cluster-config-file nodes-6379.conf #此文件有 redis cluster 集群自动创建和维护,不需要任何手动操作
3.所有 redis 服务器必须没有任何数据
4.先启动为单机 redis 且没有任何 key value
注:我们可以在一个节点源码安装好redis并,修改好配置直接打包传到其他机器上,操作如下
# pwd
/usr/local
# tar -cvf redis.tar.gz redis/
# scp redis.tar.gz root@172.16.99.128:/usr/local/
配置文件/usr/local/redis/etc/redis.conf示范如下:
bind 0.0.0.0
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised systemd
requirepass "123456"
pidfile "/usr/local/redis/run/redis_6379.pid"
loglevel notice
logfile "/usr/local/redis/logs/redis_6379.log"
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error no
rdbcompression yes
rdbchecksum yes
dbfilename "dump_6379.rdb"
dir "/usr/local/redis/data"
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 30
repl-disable-tcp-nodelay no
slave-priority 100
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
slave-lazy-flush no
maxmemory 1073741824
appendonly yes
appendfilename "appendonly_6379.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble no
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
# Generated by CONFIG REWRITE
# slaveof 172.16.99.128 6379
cluster-enabled yes
cluster-config-file nodes-6379.conf
maxclients 4064
3.2.2.2.2:创建集群:
Redis 3 和 4 版本:
需要使用到集群管理工具 redis-trib.rb,这个工具是 redis 官方推出的管理 redis 集群的工具,集成在
redis 的源码 src 目录下,是基于 redis 提供的集群命令封装成简单、便捷、实用的操作工具,redis-trib.rb
是 redis 作者用 ruby 开发完成的,centos 系统 yum 安装的 ruby 存在版本较低问题,如下:
[root@redis-vm1 ~]# yum install ruby rubygems -y
[root@redis-vm1 ~]# find / -name redis-trib.rb
/usr/local/src/redis-4.0.14/src/redis-trib.rb
[root@redis-vm1 ~]# cp /usr/local/src/redis-4.0.14/src/redis-trib.rb /usr/bin/ [root@redis-vm1 src]# gem install redis
Fetching: redis-4.1.2.gem (100%) ERROR: Error installing redis:
redis requires Ruby version >= 2.3.0.
#解决 ruby 版本较低问题:
[root@redis-vm1 src]# yum remove ruby rubygems -y
[root@redis-vm1 src]# wget https://cache.ruby-lang.org/pub/ruby/2.5/ruby-2.5.5.tar.gz
[root@redis-vm1 src]# tar xf ruby-2.5.5.tar.gz
[root@redis-vm1 src]# cd ruby-2.5.5
[root@redis-vm1 ruby-2.5.5]# ./configure
[root@redis-vm1 ruby-2.5.5]# make -j 2
[root@redis-vm1 ruby-2.5.5]# make install
[root@redis-vm1 ruby-2.5.5]# gem install redis # gem install redis -v 4.1.2 联网安装指定版本
注1: 如果要安装固定版本的操作如下
# gem install -l redis-4.1.2.gem
注2:redis-trib.rb只要安装在一个节点上就可以了,主要是用于集群的创建
报错:ruby cannot load such file -- zlib
ruby 安装redis报错
[root@localhost tools]# gem install -l redis-4.1.2.gem
ERROR: Loading command: install (LoadError)
cannot load such file -- zlib
ERROR: While executing gem ... (NoMethodError)
undefined method `invoke_with_build_args' for nil:NilClass
解决办法是:
yum -y install zlib-devel
进入ruby源码文件夹,安装ruby自身提供的zlib包
cd ruby-2.5.5/ext/zlib
ruby ./extconf.rb
修改ruby-2.5.5/ext/zlib/Makefile文件中的zlib.o: $(top_srcdir)/include/ruby.h为zlib.o: ../../include/ruby.h
make
make install
拷贝redis-trib.rb命令到/usr/bin/下,方便执行
# cp redis-4.0.14/src/redis-trib.rb /usr/bin/
验证 redis-trib.rb 命令是否可执行:
[root@redis-vm1 ruby-2.5.4]# redis-trib.rb
Usage: redis-trib <command> <options> <arguments ...>
create host1:port1 ... hostN:portN #创建集群
--replicas <arg> #指定 master 的副本数量
check host:port #检查集群信息
info host:port #查看集群主机信息
fix host:port #修复集群
--timeout <arg>
reshard host:port #在线热迁移集群指定主机的 slots 数据
--from <arg>
--to <arg>
--slots <arg>
--yes
--timeout <arg>
--pipeline <arg>
rebalance host:port #平衡集群中各主机的 slot 数量
--weight <arg>
--auto-weights
--use-empty-masters
--timeout <arg>
--simulate
--pipeline <arg>
--threshold <arg>
add-node new_host:new_port existing_host:existing_port #添加主机到集群
--slave
--master-id <arg>
del-node host:port node_id #删除主机
set-timeout host:port milliseconds #设置节点的超时时间
call host:port command arg arg .. arg #在集群上的所有节点上执行命令
import host:port #导入外部 redis 服务器的数据到当前集群
--from <arg>
--copy
--replace
help (show this help)
[root@redis-vm1 ruby-2.5.5]# vim /usr/local/lib/ruby/gems/2.5.0/gems/redis-4.1.2/lib/redis/client.rb #修改密码为redis 登录密码
![]()
创建 redis cluster 集群:
Redis 3/4 版本:
[root@redis-vm1~]# redis-trib.rb create --replicas 1 172.16.99.127:6379 172.16.99.128:6379 172.16.99.129:6379 172.16.99.130:6379 172.16.99.131:6379 172.16.99.132:6379
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
172.16.99.127:6379
172.16.99.128:6379
172.16.99.129:6379
Adding replica 172.16.99.131:6379 to 172.16.99.127:6379
Adding replica 172.16.99.132:6379 to 172.16.99.128:6379
Adding replica 172.16.99.130:6379 to 172.16.99.129:6379
M: a91742cbe5a8a8b2a1ecc108613b20f62ab1f6bc 172.16.99.127:6379
slots:0-5460 (5461 slots) master
M: 7ddc476c6c44509d3b975fab4fbee47b7a54e2cc 172.16.99.128:6379
slots:5461-10922 (5462 slots) master
M: d2d51e267ee38502f9abac34afeae1c3e1814db2 172.16.99.129:6379
slots:10923-16383 (5461 slots) master
S: 9d80a5143c0e899a06c9155439cf0bf7cc9caf74 172.16.99.130:6379
replicates d2d51e267ee38502f9abac34afeae1c3e1814db2
S: e3f6ad6a03e49fcbca689a5a114529dcb09885c8 172.16.99.131:6379
replicates a91742cbe5a8a8b2a1ecc108613b20f62ab1f6bc
S: 40342fbde4a93679ef4402852ea4cb6051104664 172.16.99.132:6379
replicates 7ddc476c6c44509d3b975fab4fbee47b7a54e2cc
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.99.127:6379)
M: a91742cbe5a8a8b2a1ecc108613b20f62ab1f6bc 172.16.99.127:6379
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: 7ddc476c6c44509d3b975fab4fbee47b7a54e2cc 172.16.99.128:6379
slots:5461-10922 (5462 slots) master
1 additional replica(s)
M: d2d51e267ee38502f9abac34afeae1c3e1814db2 172.16.99.129:6379
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: 9d80a5143c0e899a06c9155439cf0bf7cc9caf74 172.16.99.130:6379
slots: (0 slots) slave
replicates d2d51e267ee38502f9abac34afeae1c3e1814db2
S: 40342fbde4a93679ef4402852ea4cb6051104664 172.16.99.132:6379
slots: (0 slots) slave
replicates 7ddc476c6c44509d3b975fab4fbee47b7a54e2cc
S: e3f6ad6a03e49fcbca689a5a114529dcb09885c8 172.16.99.131:6379
slots: (0 slots) slave
replicates a91742cbe5a8a8b2a1ecc108613b20f62ab1f6bc
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
如果有之前的操作导致 Redis 集群创建报错,则执行清空数据和集群命令:
127.0.0.1:6379> FLUSHALL OK
127.0.0.1:6379> cluster reset
OK
Redis 5 版本:
[root@redis-vm1 ~]# redis-cli -a 123456 --cluster create 172.16.99.127:6379 172.16.99.128:6379 172.16.99.129:6379 172.16.99.130:6379 172.16.99.131:6379 172.16.99.132:6379 --cluster-replicas 1
3.2.2.2.3:检查状态:
由于未设置 masterauth 认证密码,所以主从未建立起来,但是集群已经运行,所以需要在每个 slave控制台使用 config set 设置 masterauth 密码,或者写在每个 redis 配置文件中,最好是在控制点设置密码之后再写入配置文件当中。
172.16.99.130:6379> info Replication
# Replication
role:slave
master_host:172.16.99.130
master_port:6379
master_link_status:down
master_last_io_seconds_ago:10
master_sync_in_progress:0
slave_repl_offset:98
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:fe4455d2435bdce459365baefaf99b4e6012505b
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:98
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:98
3.2.2.2.4:分别设置 masterauth 密码:
[root@redis-vm1 ~]# redis-cli -h 172.16.99.130 -p 6379 -a 123456
Warning: Using a password with '-a' option on the command line interface may not be safe.
172.16.99.130:6379> CONFIG SET masterauth 123456
OK
[root@redis-vm1 ~]# redis-cli -h 172.16.99.131 -p 6379 -a 123456
Warning: Using a password with '-a' option on the command line interface may not be safe.
172.16.99.131:6379> CONFIG SET masterauth 123456
OK
[root@redis-vm1 ~]# redis-cli -h 172.16.99.132 -p 6379 -a 123456
Warning: Using a password with '-a' option on the command line interface may not be safe.
172.16.99.132:6379> CONFIG SET masterauth 123456
OK
3.2.2.2.5:确认 slave 状态为 up:
172.16.99.130:6379> info Replication
# Replication
role:slave
master_host:172.16.99.129
master_port:6379
master_link_status:up
master_last_io_seconds_ago:10
master_sync_in_progress:0
slave_repl_offset:98
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:b038887bf85f8efd42ff581089eff7fa79e94dd0
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:98
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:98
3.2.2.2.6:验证 master 状态:
[root@redis-vm1 ~]# redis-cli -h 172.16.99.127 -p 6379 -a 123456
# Replication
role:master
connected_slaves:1
slave0:ip=172.16.99.131,port=6379,state=online,offset=420,lag=1
master_replid:fe4455d2435bdce459365baefaf99b4e6012505b
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:420
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:420
3.2.2.2.7:验证集群状态:
172.16.99.127:6379> 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:1323
cluster_stats_messages_pong_sent:1436
cluster_stats_messages_sent:2759
cluster_stats_messages_ping_received:1431
cluster_stats_messages_pong_received:1323
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:2759
3.2.2.2.8:查看集群 node 对应关系:
使用命令 cluster nodes:
172.16.99.127:6379> cluster nodes
7ddc476c6c44509d3b975fab4fbee47b7a54e2cc 172.16.99.128:6379@16379 master - 0 1581846649000 2 connected 5461-10922
d2d51e267ee38502f9abac34afeae1c3e1814db2 172.16.99.129:6379@16379 master - 0 1581846650568 3 connected 10923-16383
9d80a5143c0e899a06c9155439cf0bf7cc9caf74 172.16.99.130:6379@16379 slave d2d51e267ee38502f9abac34afeae1c3e1814db2 0 1581846650000 4 connected
40342fbde4a93679ef4402852ea4cb6051104664 172.16.99.132:6379@16379 slave 7ddc476c6c44509d3b975fab4fbee47b7a54e2cc 0 1581846649000 6 connected
a91742cbe5a8a8b2a1ecc108613b20f62ab1f6bc 172.16.99.127:6379@16379 myself,master - 0 1581846649000 1 connected 0-5460
e3f6ad6a03e49fcbca689a5a114529dcb09885c8 172.16.99.131:6379@16379 slave a91742cbe5a8a8b2a1ecc108613b20f62ab1f6bc 0 1581846648000 5 connected
3.2.2.2.9:验证集群写入 key:
172.16.99.127:6379> SET key1 value1 #经过算法计算,当前 key 的槽位需要写入指定的 node
(error) MOVED 9189 172.16.99.128:6379 #槽位不在当前 node 所以无法写入
172.16.99.129:6379> SET key1 value1 (error) MOVED 9189 172.16.99.128:6379
172.16.99.128:6379> SET key1 value1 #指定的 node 就可以写入
OK
172.16.99.128:6379> KEYS *
1) "key1"
172.16.99.127:6379> KEYS * (empty list or set)
172.16.99.129:6379> KEYS * (empty list or set)
3.2.2.2.10:集群状态监控:
Redis 4:
[root@redis-vm1 ~]# redis-trib.rb check 172.16.99.127:6379
>>> Performing Cluster Check (using node 172.16.99.127:6379)
M: a91742cbe5a8a8b2a1ecc108613b20f62ab1f6bc 172.16.99.127:6379
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: 7ddc476c6c44509d3b975fab4fbee47b7a54e2cc 172.16.99.128:6379
slots:5461-10922 (5462 slots) master
1 additional replica(s)
M: d2d51e267ee38502f9abac34afeae1c3e1814db2 172.16.99.129:6379
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: 9d80a5143c0e899a06c9155439cf0bf7cc9caf74 172.16.99.130:6379
slots: (0 slots) slave
replicates d2d51e267ee38502f9abac34afeae1c3e1814db2
S: 40342fbde4a93679ef4402852ea4cb6051104664 172.16.99.132:6379
slots: (0 slots) slave
replicates 7ddc476c6c44509d3b975fab4fbee47b7a54e2cc
S: e3f6ad6a03e49fcbca689a5a114529dcb09885c8 172.16.99.131:6379
slots: (0 slots) slave
replicates a91742cbe5a8a8b2a1ecc108613b20f62ab1f6bc
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@redis-vm1 ~]# redis-trib.rb info 172.16.99.127:6379
172.16.99.127:6379 (a91742cb...) -> 0 keys | 5461 slots | 1 slaves.
172.16.99.128:6379 (7ddc476c...) -> 0 keys | 5462 slots | 1 slaves.
172.16.99.129:6379 (d2d51e26...) -> 0 keys | 5461 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.
Redis 5:
# redis-cli -a 123456 --cluster check 172.16.99.127:6379
- Redis集群简介及部署
1简介 在 Redis 3.0 之前,使用 哨兵(sentinel)机制来监控各个节点之间的状态.Redis Cluster 是 Redis 的 分布式解决方案,在 3.0 版本正式推出,有效地解决了 ...
- Redis集群命令行部署工具
使用之前准备工作: 1)配置好与端口无关的公共redis.conf文件,和工具放在同一目录下 2)配置好与端口相关的模板redis-PORT.conf文件,也和工具放在同一目录下(部署时PORT会被替 ...
- Redis集群与分布式介绍以及搭建Redis-Cluster
1 Redis集群 1.1 什么是集群 集群就是很多服务器组成的一个网络.指的是将多台服务器集中在一起,实现同一业务. 1.2 为什么要集群 一台服务器不能满足开发需要的时候,需要多台服务器来支持.这 ...
- windows下redis集群安装和部署
1.下载windows版本的Redis 官网只提供linux版本的下载 官网下载地址:http://redis.io/download github下载地址:https://github.com/MS ...
- Windows下Redis集群安装与部署
1.下载 Redis-x64-3.2.100.zip 安装程序 官网下载地址:http://redis.io/download GitHub下载地址:https://github.com/micros ...
- Redis集群环境的部署记录
Redis Cluster终于出了Stable,这让人很是激动,等Stable很久了,所以还是先玩玩. 一. 集群简单概念. Redis 集群是一个可以在多个 Redis 节点之间进行数据共享的设施( ...
- 分布式ID系列(4)——Redis集群实现的分布式ID适合做分布式ID吗
首先是项目地址: https://github.com/maqiankun/distributed-id-redis-generator 关于Redis集群生成分布式ID,这里要先了解redis使用l ...
- redis集群+JedisCluster+lua脚本实现分布式锁(转)
https://blog.csdn.net/qq_20597727/article/details/85235602 在这片文章中,使用Jedis clien进行lua脚本的相关操作,同时也使用一部分 ...
- 【Redis学习之十】Redis集群维护
Redis集群增删节点部署环境 redis-3.0.0 VM虚拟机redhat6.5-x64:192.168.1.201.192.168.1.202.192.168.1.203. ...
随机推荐
- Java中对象在内存中的大小、分配等问题
Java创建一个对象的过程 是否对象指向的类已经加载到内存了 如果没有加载,就要经过load.linking(verification.preparation.resolution).initiali ...
- 地图上显示点在点上标注当前点的id
HTML: <div class="form-group field-company-state"> <div style="width:1000px; ...
- SpringBoot的条件注解源码解析
SpringBoot的条件注解源码解析 @ConditionalOnBean.@ConditionalOnMissingBean 启动项目 会在ConfigurationClassBeanDefini ...
- python接口测试3-JSON格式
什么是JSON? 一种轻量级的数据交换格式.它独立于语言和平台,JSON解析器和JSON库支持不同的编程语言.JSON具有自我描述性,很容易理解. 数据格式: { "name":& ...
- JDBC【2】-- JDBC工作原理以及简单封装
目录 1. 工作原理 1.1 加载驱动 1.1.1 类加载相关知识 1.1.2 为什么JDK 1.6之后不需要显示加载了? 1.2 驱动加载完成了,然后呢? 2. 简单封装 1. 工作原理 一般我们主 ...
- 企业安全05-Fastjson <=1.2.47反序列化RCE漏洞(CNVD-2019-22238)
Fastjson <=1.2.47反序列化RCE漏洞(CNVD-2019-22238) 一.漏洞描述 Fastjson 是阿里巴巴的开源JSON解析库,它可以解析 JSON 格式的字符串,支持将 ...
- 20190705_关于winform程序修改程序名后, 报未将对象引用设置到对象的实例
winform做了一个小项目, 其中要用到数据库连接, 字符串, private string ConnStringSource = System.Configuration.Configuratio ...
- Splay树求第k大模板
今天上午借着休息日得机会手撸了一下模板,终于对着模板调出来了.prev和next占用了std namespace里面的东西,然后报警我上次给关了所以.....就花了3个小时吧. inline加不加无所 ...
- 攻防世界 ctf web进阶区 unserialize
进入到题目的界面,看到以下源码 构造payload=?code=O:4:"xctf":1:{s:4:"flag";s:3:"111";} 结 ...
- JVM 垃圾回收?全面详细安排!
写在前面: 小伙伴儿们,大家好!今天来学习Java虚拟机相关内容,作为面试必问的知识点,来深入了解一波! 思维导图: image-20201207153125210 1,判断对象是否死亡 我们在进行垃 ...