此处的集群指的是Cluster, 通过分区/分片来实现一定程度的分布式与高可用部署。
准备三台redis机器,每台配置不同的节点及监听的端口。
192.168.231.185 7001 7002
192.168.231.183 7003 7004
192.168.231.190 7005 7006
三台服务器均安装redis,设置软链接
ln -s /data/application/redis/src/redis-server /usr/bin
ln -s /data/application/redis/src/redis-cli /usr/bin
在185服务器
cd /data/application
mkdir {7001,7002}
[root@localhost ~]# cd /data/application/
[root@localhost application]# ls
7001 7002 redis-5.0.10
cd 7001
手动编写配置文件
vim redis.conf
port 7001
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
daemonize yes
bind 0.0.0.0
pidfile /data/application/7001/redis_7001.pid
dir /data/application/7001/data
保存退出后
mkdir /data/application/7001/data
7002的操作与7001一模一样,讲配置文件7001改成7002,在创建一个 /data/application/7002/data 目录即可
在183服务器创建
[root@localhost ~]# cd /data/application/
[root@localhost application]# ls
7003 7004 redis-5.0.10
像7001一样,手动创建配置文件 将7001改成7004 ,7003即可
在手动创建 /data/application/7003/data
/data/application/7004/data
在190服务器创建
[root@localhost ~]# cd /data/application/
[root@localhost application]# ls
7005 7006 redis-5.0.10
像7001一样,手动创建配置文件 将7001改成7006 ,7005即可
在手动创建 /data/application/7005/data
/data/application/7006/data
[root@localhost application]# redis-server /data/application/7005/redis.conf
12362:C 07 Nov 2023 11:09:23.894 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
12362:C 07 Nov 2023 11:09:23.894 # Redis version=5.0.10, bits=64, commit=00000000, modified=0, pid=12362, just started
12362:C 07 Nov 2023 11:09:23.894 # Configuration loaded
如果返回结果是3行内容,则成功
[root@localhost application]# netstat -nplt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 920/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1295/master
tcp 0 0 0.0.0.0:7005 0.0.0.0:* LISTEN 12363/redis-server
tcp 0 0 0.0.0.0:7006 0.0.0.0:* LISTEN 12368/redis-server
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 12260/redis-server
tcp 0 0 0.0.0.0:17005 0.0.0.0:* LISTEN 12363/redis-server
tcp 0 0 0.0.0.0:17006 0.0.0.0:* LISTEN 12368/redis-server
tcp6 0 0 :::22 :::* LISTEN 920/sshd
tcp6 0 0 ::1:25 :::* LISTEN 1295/master
有7001 7002的服务器端口得有7001 7002 17001 17002,以此类推
[root@localhost ~]# /data/application/redis-5.0.10/src/redis-cli --cluster create --cluster-replicas 1 192.168.231.185:7001 192.168.231.185:7002 192.168.231.183:7003 192.168.231.183:7004 192.168.231.190:7005 192.168.231.190:7006
返回结果
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.231.183:7004 to 192.168.231.185:7001
Adding replica 192.168.231.190:7006 to 192.168.231.183:7003
Adding replica 192.168.231.185:7002 to 192.168.231.190:7005
M: 34d2f2e85aee0a9c7011dba417a5a11e650324e8 192.168.231.185:7001
slots:[0-5460] (5461 slots) master
S: bf22e640e4364446b17a4efa7b0fbd4680ae3935 192.168.231.185:7002
replicates 2f06af28ed5e0fea9870cae6796251055f7c6094
M: 37d3f471f2d7ce7213457b195470264d9188c639 192.168.231.183:7003
slots:[5461-10922] (5462 slots) master
S: f4e113e5de2002ce548a27c5111c2ae80eb2a269 192.168.231.183:7004
replicates 34d2f2e85aee0a9c7011dba417a5a11e650324e8
M: 2f06af28ed5e0fea9870cae6796251055f7c6094 192.168.231.190:7005
slots:[10923-16383] (5461 slots) master
S: b0373f2e8592359ae77ab4e01765257dcbc4ae93 192.168.231.190:7006
replicates 37d3f471f2d7ce7213457b195470264d9188c639
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 192.168.231.185:7001)
M: 34d2f2e85aee0a9c7011dba417a5a11e650324e8 192.168.231.185:7001
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: b0373f2e8592359ae77ab4e01765257dcbc4ae93 192.168.231.190:7006
slots: (0 slots) slave
replicates 37d3f471f2d7ce7213457b195470264d9188c639
M: 37d3f471f2d7ce7213457b195470264d9188c639 192.168.231.183:7003
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: bf22e640e4364446b17a4efa7b0fbd4680ae3935 192.168.231.185:7002
slots: (0 slots) slave
replicates 2f06af28ed5e0fea9870cae6796251055f7c6094
M: 2f06af28ed5e0fea9870cae6796251055f7c6094 192.168.231.190:7005
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: f4e113e5de2002ce548a27c5111c2ae80eb2a269 192.168.231.183:7004
slots: (0 slots) slave
replicates 34d2f2e85aee0a9c7011dba417a5a11e650324e8
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
创建新集群命令:命令create,选项
--replicas 1
意味着我们希望每个创建的主服务器都有一个从服 其他参数是我要用于创建新集群的实例的地址列表。
登录
[root@localhost ~]# /data/application/redis-5.0.10/src/redis-cli -c -h 192.168.231.183 -p 7004
查看集群状态
192.168.231.183:7004> cluster info
cluster_state:ok ######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:9
cluster_my_epoch:7
cluster_stats_messages_ping_sent:15587
cluster_stats_messages_pong_sent:4959
cluster_stats_messages_meet_sent:1
cluster_stats_messages_fail_sent:12
cluster_stats_messages_auth-req_sent:5
cluster_stats_messages_auth-ack_sent:2
cluster_stats_messages_sent:20566
cluster_stats_messages_ping_received:4955
cluster_stats_messages_pong_received:4841
cluster_stats_messages_fail_received:9
cluster_stats_messages_auth-req_received:2
cluster_stats_messages_auth-ack_received:2
cluster_stats_messages_received:9809
集群客户端连接方式 redis-cli -c
[root@localhost ~]# redis-cli -c -p 192.168.231.185 -p 7001
127.0.0.1:7001>
127.0.0.1:7001> set name zzz
-> Redirected to slot [5798] located at 192.168.231.190:7006
OK
192.168.231.190:7006> set age 88
-> Redirected to slot [741] located at 192.168.231.183:7004
OK
192.168.231.183:7004> set aaaa llll
OK
不加 -c会报错
[root@localhost ~]# redis-cli -p 192.168.231.185 -p 7001
127.0.0.1:7001> set name zzz
(error) MOVED 5798 192.168.231.190:7006
192.168.231.183:7004> cluster nodes
b0373f2e8592359ae77ab4e01765257dcbc4ae93 192.168.231.190:7006@17006 master - 0 1699353315712 9 connected 5461-10922
2f06af28ed5e0fea9870cae6796251055f7c6094 192.168.231.190:7005@17005 slave bf22e640e4364446b17a4efa7b0fbd4680ae3935 0 1699353314552 8 connected
37d3f471f2d7ce7213457b195470264d9188c639 192.168.231.183:7003@17003 slave b0373f2e8592359ae77ab4e01765257dcbc4ae93 0 1699353315248 9 connected
bf22e640e4364446b17a4efa7b0fbd4680ae3935 192.168.231.185:7002@17002 master - 0 1699353315481 8 connected 10923-16383
34d2f2e85aee0a9c7011dba417a5a11e650324e8 192.168.231.185:7001@17001 slave f4e113e5de2002ce548a27c5111c2ae80eb2a269 0 1699353314336 7 connected
f4e113e5de2002ce548a27c5111c2ae80eb2a269 192.168.231.183:7004@17004 myself,master - 0 1699353312000 7 connected 0-5460
7002是主, 它开头是bf 那么 7005就是它对应的从
7006是主,它开头是b0 那么它对应的从是7003
7004是主,它开头是f4 那么它对应的从是7001
我们将7004down掉
[root@localhost application]# ps aux | grep redis
root 5624 0.1 0.4 153996 8068 ? Ssl 10:36 0:44 /data/application/redis-5.0.10/src/redis-server 127.0.0.1:6379
root 6084 0.2 0.7 167308 14300 ? Ssl 11:08 1:03 ./src/redis-server 0.0.0.0:7004 [cluster]
root 6202 0.2 0.7 161164 14200 ? Ssl 11:56 0:57 /data/application/redis-5.0.10/src/redis-server 0.0.0.0:7003 [cluster]
root 7461 0.0 0.0 112824 980 pts/2 R+ 18:39 0:00 grep --color=auto redis
[root@localhost application]# kill -9 6084
[root@localhost application]# ps aux | grep redis
root 5624 0.1 0.4 153996 8068 ? Ssl 10:36 0:44 /data/application/redis-5.0.10/src/redis-server 127.0.0.1:6379
root 6202 0.2 0.7 161164 14200 ? Ssl 11:56 0:57 /data/application/redis-5.0.10/src/redis-server 0.0.0.0:7003 [cluster]
root 7463 0.0 0.0 112828 980 pts/2 S+ 18:39 0:00 grep --color=auto redis
127.0.0.1:7001> cluster nodes
f4e113e5de2002ce548a27c5111c2ae80eb2a269 192.168.231.183:7004@17004 master,fail - 1699353579914 1699353578555 7 disconnected
37d3f471f2d7ce7213457b195470264d9188c639 192.168.231.183:7003@17003 slave b0373f2e8592359ae77ab4e01765257dcbc4ae93 0 1699353627140 9 connected
bf22e640e4364446b17a4efa7b0fbd4680ae3935 192.168.231.185:7002@17002 master - 0 1699353626008 8 connected 10923-16383
b0373f2e8592359ae77ab4e01765257dcbc4ae93 192.168.231.190:7006@17006 master - 0 1699353627372 9 connected 5461-10922
34d2f2e85aee0a9c7011dba417a5a11e650324e8 192.168.231.185:7001@17001 myself,master - 0 1699353625000 12 connected 0-5460
2f06af28ed5e0fea9870cae6796251055f7c6094 192.168.231.190:7005@17005 slave bf22e640e4364446b17a4efa7b0fbd4680ae3935 0 1699353627256 8 connected
7004显示master fail,而它对应的7001由从,变成了主
这时候在启动7004,查看主从状态信息
启动7004
[root@localhost application]# /data/application/redis-5.0.10/src/redis-server ./7004/redis.conf
查看主从
127.0.0.1:7001> cluster nodes
f4e113e5de2002ce548a27c5111c2ae80eb2a269 192.168.231.183:7004@17004 slave 34d2f2e85aee0a9c7011dba417a5a11e650324e8 0 1699353782653 12 connected
37d3f471f2d7ce7213457b195470264d9188c639 192.168.231.183:7003@17003 slave b0373f2e8592359ae77ab4e01765257dcbc4ae93 0 1699353781524 9 connected
bf22e640e4364446b17a4efa7b0fbd4680ae3935 192.168.231.185:7002@17002 master - 0 1699353782418 8 connected 10923-16383
b0373f2e8592359ae77ab4e01765257dcbc4ae93 192.168.231.190:7006@17006 master - 0 1699353782886 9 connected 5461-10922
34d2f2e85aee0a9c7011dba417a5a11e650324e8 192.168.231.185:7001@17001 myself,master - 0 1699353782000 12 connected 0-5460
2f06af28ed5e0fea9870cae6796251055f7c6094 192.168.231.190:7005@17005 slave bf22e640e4364446b17a4efa7b0fbd4680ae3935 0 1699353783219 8 connected
同时down掉一组主从
down掉7002 7005这一组主从
查出进程号,然后kill -9 加pid
再次查看集群信息
127.0.0.1:7001> cluster info
cluster_state:fail ---------------已关闭
cluster_slots_assigned:16384
cluster_slots_ok:10923
cluster_slots_pfail:0
cluster_slots_fail:5461
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:13
cluster_my_epoch:12
cluster_stats_messages_ping_sent:62591
cluster_stats_messages_pong_sent:51718
cluster_stats_messages_fail_sent:7
cluster_stats_messages_auth-req_sent:5
cluster_stats_messages_auth-ack_sent:1
cluster_stats_messages_sent:114322
cluster_stats_messages_ping_received:51714
cluster_stats_messages_pong_received:51359
cluster_stats_messages_fail_received:18
cluster_stats_messages_auth-req_received:3
cluster_stats_messages_auth-ack_received:2
cluster_stats_messages_received:103096
当我们down掉随机的主时,它对应的从会自动变成主,保持集群
当我们down掉一组主从时,集群会关闭
解决gem install redis 因为网络问题造成连接超时的问题,把gem镜像换成国内源
[root@redis-cluster redis-Cluster]# gem sources -l
*** CURRENT SOURCES ***
https://rubygems.org/
[root@redis-cluster redis-Cluster]# gem sources --remove https://rubygems.org/
https://rubygems.org/ removed from sources
[root@redis-cluster redis-Cluster]# gem sources --add https://mirrors.tuna.tsinghua.edu.cn/rubygems/ 回车
https://mirrors.tuna.tsinghua.edu.cn/rubygems/ added to sources
[root@redis-cluster redis-Cluster]# gem sources -l
*** CURRENT SOURCES ***
https://mirrors.tuna.tsinghua.edu.cn/rubygems/
[root@redis-cluster redis-Cluster]# gem install redis
Fetching: redis-4.0.1.gem (100%)
ERROR: Error installing redis:
redis requires Ruby version >= 2.2.2.
我这里CentOS7.5默认安装的Ruby版本为2.0,需升级为2.2.2以上(ruby -v),先安装RVM,用RVM升级Ruby
1、sudo yum install curl 安装curl
2、http://www.rvm.io/ 官网首页就可以看到 $ gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
3、下载rvm $ \curl -sSL https://get.rvm.io | bash -s stable
4、查找配置文件 find / -name rvm.sh
5、配置文件生效 source /etc/profile.d/rvm.sh
6、下载rvm依赖 rvm requirements
7、查看rvm库ruby版本 rvm list known
8、安装ruby指定版本 rvm install ruby-2.5.1
9、使用ruby版本默认 rvm use 2.5.1 default
10.gem install redis
集群数据不为空,Node 192.168.100.21:7001 is not empty
[root@redis-cluster src]# ./redis-trib.rb create --replicas 1 192.168.100.21:7000 192.168.100.21:7001 192.168.100.21:7002 192.168.100.21:7003 192.168.100.21:7004 192.168.100.
21:7005>>> Creating cluster
[ERR] Node 192.168.100.21:7001 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.
清空数据,在将每个节点下aof、rdb、nodes.conf本地备份文件删除 ,重新创建即可成功
[root@redis-cluster src]# redis-cli -h 192.168.100.21 -p 7001
192.168.100.21:7001> flushdb
OK
Redis 的配置文件位于 Redis 安装目录下,文件名为 redis.conf,配置项说明如下:
1.Redis默认不是以守护进程的方式运行,可以通过该配置项修改,使用yes启用守护进程
daemonize no
当Redis以守护进程方式运行时,Redis默认会把pid写入/var/run/redis.pid文件,可以通过pidfile指定
pidfile /var/run/redis.pid
2. 指定Redis监听端口,默认端口为6379,
port 6379
3.绑定的主机地址
bind 127.0.0.1
4.当 客户端闲置多长时间后关闭连接,如果指定为0,表示关闭该功能
timeout 300
5.指定日志记录级别,Redis总共支持四个级别:debug、verbose、notice、warning,默认为verbose
loglevel verbose
6.日志记录方式,默认为标准输出,如果配置Redis为守护进程方式运行,而这里又配置为日志记录方式为标准输出,则日志将会发送给/dev/null
logfile stdout
7.设置数据库的数量,默认数据库为0,可以使用SELECT
databases 16
8.指定在多长时间内,有多少次更新操作,就将数据同步到数据文件,可以多个条件配合
save
Redis默认配置文件中提供了三个条件:
save 900 1
save 300 10
save 60 10000
分别表示900秒(15分钟)内有1个更改,300秒(5分钟)内有10个更改以及60秒内有10000个更改。
9.指定存储至本地数据库时是否压缩数据,默认为yes,Redis采用LZF压缩,如果为了节省CPU时间,可以关闭该选项,但会导致数据库文件变的巨大
rdbcompression yes
10.指定本地数据库文件名,默认值为dump.rdb
dbfilename dump.rdb
11.指定本地数据库存放目录
dir ./
12.设置当本机为slav服务时,设置master服务的IP地址及端口,在Redis启动时,它会自动从master进行数据同步
slaveof
13.当master服务设置了密码保护时,slav服务连接master的密码
masterauth
14.设置Redis连接密码,如果配置了连接密码,客户端在连接Redis时需要通过AUTH
requirepass foobared
15.设置同一时间最大客户端连接数,默认无限制,Redis可以同时打开的客户端连接数为Redis进程可以打开的最大文件描述符数,如果设置 maxclients 0,表示不作限制。当客户端连接数到达限制时,Redis会关闭新的连接并向客户端返回max number of clients reached错误信息
maxclients 128
16.指定Redis最大内存限制,Redis在启动时会把数据加载到内存中,达到最大内存后,Redis会先尝试清除已到期或即将到期的Key,当此方法处理 后,仍然到达最大内存设置,将无法再进行写入操作,但仍然可以进行读取操作。Redis新的vm机制,会把Key存放内存,Value会存放在swap区
maxmemory
17.指定是否在每次更新操作后进行日志记录,Redis在默认情况下是异步的把数据写入磁盘,如果不开启,可能会在断电时导致一段时间内的数据丢失。因为 redis本身同步数据文件是按上面save条件来同步的,所以有的数据会在一段时间内只存在于内存中。默认为no
appendonly no
18.指定更新日志文件名,默认为appendonly.aof
appendfilename appendonly.aof
19.指定更新日志条件,共有3个可选值: no:表示等操作系统进行数据缓存同步到磁盘(快) always:表示每次更新操作后手动调用fsync()将数据写到磁盘(慢,安全) everysec:表示每秒同步一次(折衷,默认值)
appendfsync everysec
20.指定是否启用虚拟内存机制,默认值为no,简单的介绍一下,VM机制将数据分页存放,由Redis将访问量较少的页即冷数据swap到磁盘上,访问多的页面由磁盘自动换出到内存中(在后面的文章我会仔细分析Redis的VM机制)
vm-enabled no
21.虚拟内存文件路径,默认值为/tmp/redis.swap,不可多个Redis实例共享
vm-swap-file /tmp/redis.swap
22.将所有大于vm-max-memory的数据存入虚拟内存,无论vm-max-memory设置多小,所有索引数据 都是内存存储的(Redis的索引数据 就是keys),也就是说,当vm-max-memory设置为0的时候,其实是所有value都存在于磁盘。默认值为0
vm-max-memory 0
23.Redis swap文件分成了很多的page,一个对象可以保存在多个page上面,但一个page上不能被多个对象共享,vm-page-size是要根据存储的 数据大小来设定的,作者建议如果存储很多小对象,page大小最好设置为32或者64bytes;如果存储很大大对象,则可以使用更大的page,如果不 确定,就使用默认值
vm-page-size 32
24.设置swap文件中的page数量,由于页表(一种表示页面空闲或使用的bitmap)是在放在内存中的,,在磁盘上每8个pages将消耗1byte的内存。
vm-pages 134217728
25.设置访问swap文件的线程数,最好不要超过机器的核数,如果设置为0,那么所有对swap文件的操作都是串行的,可能会造成比较长时间的延迟。默认值为4
vm-max-threads 4
26.设置在向客户端应答时,是否把较小的包合并为一个包发送,默认为开启
glueoutputbuf yes
27.指定在超过一定的数量或者最大的元素超过某一临界值时,采用一种特殊的哈希算法
hash-max-zipmap-entries 64
hash-max-zipmap-value 512
28.指定是否激活重置哈希,默认为开启(后面在介绍Redis的哈希算法时具体介绍)
activerehashing yes
29.指定包含其它的配置文件,可以在同一主机上多个Redis实例之间使用同一份配置文件,而同时各个实例又拥有自己的特定配置文件
include /path/to/local.conf