Redis 4.0.x的安装与使用

一、Redis简介

1、Redis介绍

 Redis (Remote Dictionary Server)在 2009 年发布,开发者 Salvatore Sanfilippo 是意大利开发者,他本想为自己的公司开发一个用于替换 MySQL 的产品 Redis,但是没有想到他把 Redis开源后大受欢迎,短短几年,Redis就有了很大的用户群体,目前国内外使用的公司有知乎网、新浪微博、GitHub等。

 Redis是一个开源的、遵循BSD协议的、基于内存的而且目前比较流行的键值数据库(key-value database), 是一个非关系型数据库,redis 提供将内存通过网络远程共享的一种服务,提供类似功能的还有memcache,但相比 memcache,redis 还提供了易扩展、高性能、具备数据持久性等功能

2、Redis持久化方式

 Redis 虽然是一个内存级别的缓存程序,即 redis 是使用内存进行数据的缓存的,但是其可以将内存的数据按照一定的策略保存到硬盘上,从而实现数据持久保存的目的,Redis 支持两种不同方式的数据持久化保存机制,分别是 RDB 和 AOF。

2.1、RDB模式

 基于时间的快照,只保留当前最新的一次快照,特点是执行速度比较快,缺点是可能会丢失从上次快照到当前快照未完成之间的数据。

 实现过程:

Redis从主进程先生成一个子进程,使用写时复制机制,子进程将内存的数据保存为一个临时文件,比如 dump.rdb.temp,当数据保存完成之后再将上一次保存的RDB文件替换掉,然后关闭子进程。

 优点:

RDB快照保存了某个时间点的数据,可以通过脚本执行 bgsave(非阻塞)或者 save(阻塞)命令自定义时间点北备份,可以保留多个备份,当出现问题可以恢复到不同时间点的版本;

可以最大化IO的性能,因为父进程在保存RDB文件的时候唯一要做的是生成一个子进程,然后的操作都会由这个子进程操作,父进程无需任何的 IO 操作;

RDB 在大量数据比如几个 G 的数据,恢复的速度比 AOF 的快。

 缺点:

不能时时的保存数据,会丢失自上一次执行备份到当前时间中的内存数据;

数据量非常大时,从父进程生成子进程的时候需要一点时间。

2.2、AOF模式

 按照操作顺序依次将操作添加到指定的日志文件当中。

 实现过程:

AOF与RDB一样使用了写时复制机制,AOF默认为每秒钟 fsync一次,即将执行的命令保存到AOF文件当中。 (fsync是同步内存中redis所有已经修改的文件到存储设备)

 优点:

数据安全性相对较高;AOF使用的fsync策略,默认是appendfsync everysec,即每秒执行一次,这样即使Redis服务器发生故障的话顶多也就丢失 1 秒钟之内的数据;也可以设置不同的 fsync策略,或者设置每次执行命令的时候执行fsync,fsync会在后台执行线程,所以主线程可以继续处理用户的正常请求而不受到写入AOF文件的IO影响;

 缺点:即使有些操作是重复的也会全部记录,同时生成的文件大小要大于RDB格式的文件。

二、Redis的安装

1、yum源安装

(1) 配置yum源与epel源

[ ~]# vim /etc/yum.repos.d/base.repo 
[development]
name=dvdbase repo
baseurl=file:///mnt/cdrom/
enabled=1
gpgcheck=1
gpgkey=file:///mnt/cdrom/RPM-GPG-KEY-CentOS-7

[aliyunEpel]
name=aliyun epel
baseurl=https://mirrors.aliyun.com/epel/$releasever/$basearch
enabled=1
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/epel/RPM-GPG-KEY-EPEL-$releasever

(2) yum安装

[ ~]# yum install -y redis

(3) 启动服务即可

[ ~]# systemctl start redis
[ ~]# ps -ef|grep redis
redis     7463     1  0 15:53 ?        00:00:00 /usr/bin/redis-server 127.0.0.1:6379
root      7482  6913  0 15:53 pts/0    00:00:00 grep --color=auto redis

2、编译安装

(1) 下载源码包,下载地址:http://download.redis.io/releases/  

[ ~]# ll redis-4.0.14.tar.gz 
-rw-r--r-- 1 root root 1740967 Apr 14 15:25 redis-4.0.14.tar.gz

(2) 解压安装包,并进行编译安装

[ ~]# tar -xf redis-4.0.14.tar.gz 
[ ~]# cd redis-4.0.14/
[ redis-4.0.14]# yum install -y gcc gcc-c++ jemalloc 
[ redis-4.0.14]# make PREFIX=/usr/local/redis install
[ redis-4.0.14]# ll /usr/local/redis/
total 0
drwxr-xr-x 2 root root 134 Apr 14 16:07 bin

(3) 创建配置文件目录与其它相关目录,并将源码包中的配置文件复制到配置文件目录中

[ redis-4.0.14]# mkdir -v /usr/local/redis/{etc,log,data,run}
mkdir: created directory ‘/usr/local/redis/etc’
mkdir: created directory ‘/usr/local/redis/log’
mkdir: created directory ‘/usr/local/redis/data’
mkdir: created directory ‘/usr/local/redis/run’
[ redis-4.0.14]# cp redis.conf /usr/local/redis/etc/

(4) 启动redis

[ redis-4.0.14]# /usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf 
59323:C 14 Apr 16:12:20.731 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
59323:C 14 Apr 16:12:20.731 # Redis version=4.0.14, bits=64, commit=00000000, modified=0, pid=59323, just started
59323:C 14 Apr 16:12:20.732 # Configuration loaded
59323:M 14 Apr 16:12:20.733 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ‘‘-._                                             
      _.-``    `.  `_.  ‘‘-._           Redis 4.0.14 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ‘‘-._                                   
 (    ‘      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|‘` _.-‘|     Port: 6379
 |    `-._   `._    /     _.-‘    |     PID: 59323
  `-._    `-._  `-./  _.-‘    _.-‘                                   
 |`-._`-._    `-.__.-‘    _.-‘_.-‘|                                  
 |    `-._`-._        _.-‘_.-‘    |           http://redis.io        
  `-._    `-._`-.__.-‘_.-‘    _.-‘                                   
 |`-._`-._    `-.__.-‘    _.-‘_.-‘|                                  
 |    `-._`-._        _.-‘_.-‘    |                                  
  `-._    `-._`-.__.-‘_.-‘    _.-‘                                   
      `-._    `-.__.-‘    _.-‘                                       
          `-._        _.-‘                                           
              `-.__.-‘                                               

59323:M 14 Apr 16:12:20.761 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
59323:M 14 Apr 16:12:20.761 # Server initialized
59323:M 14 Apr 16:12:20.761 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add ‘vm.overcommit_memory = 1‘ to /etc/sysctl.conf and then reboot or run the command ‘sysctl vm.overcommit_memory=1‘ for this to take effect.
59323:M 14 Apr 16:12:20.761 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command ‘echo never > /sys/kernel/mm/transparent_hugepage/enabled‘ as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
...
#默认是前台运行,如要改为后台运行,改配置文件中的 daemonize 为 yes

(5) 解决告警信息

#启动后会发现有三个WARNING信息,yum安装的启动也存在
#下面来先解决这三台WARNING提示
[ redis-4.0.14]# vim /etc/sysctl.conf 
#添加以下两行
net.core.somaxconn = 512        #解决第一个WARNING提示
vm.overcommit_memory = 1        #解决第二个WARNING提示
[ redis-4.0.14]# sysctl -p
net.core.somaxconn = 512
vm.overcommit_memory = 1
 
#解析第三个WARNING提示
[ redis-4.0.14]# echo never > /sys/kernel/mm/transparent_hugepage/enabled    
#以上配置重启主机后会丢失,永久生效的方式
#写入 /etc/rc.local 文件中,并给 /etc/rc.local 加执行权限
[ redis-4.0.14]# vim /etc/rc.local
echo never > /sys/kernel/mm/transparent_hugepage/enabled
[ redis-4.0.14]# chmod +x /etc/rc.local
#之后重新启时即可
[ redis-4.0.14]# /usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf

(7) 如要方便使用,可以给命令创建软链接

[ redis-4.0.14]# ln -sv /usr/local/redis/bin/redis-* /usr/bin/
‘/usr/bin/redis-benchmark’ -> ‘/usr/local/redis/bin/redis-benchmark’
‘/usr/bin/redis-check-aof’ -> ‘/usr/local/redis/bin/redis-check-aof’
‘/usr/bin/redis-check-rdb’ -> ‘/usr/local/redis/bin/redis-check-rdb’
‘/usr/bin/redis-cli’ -> ‘/usr/local/redis/bin/redis-cli’
‘/usr/bin/redis-sentinel’ -> ‘/usr/local/redis/bin/redis-sentinel’
‘/usr/bin/redis-server’ -> ‘/usr/local/redis/bin/redis-server’

(8) CentOS7上如使用systemd启动则准备启动脚本

[ redis-4.0.14]# vim /usr/lib/systemd/system/redis.service
[Unit]
Description=Redis persistent key-value database
After=network.target
After=network-online.target
Wants=network-online.target

[Service]
#ExecStart=/usr/bin/redis-server /etc/redis.conf --supervised systemd
ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf --supervised systemd
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
Type=notify
User=root    
Group=root
RuntimeDirectory=redis
RuntimeDirectoryMode=0755

[Install]
WantedBy=multi-user.target

#上述配置中,建议新建普通用户运行,将User和Group修改一下
[ redis-4.0.14]# useradd -r -s /sbin/nologin redis
[ redis-4.0.14]# chown -R redis:redis /usr/local/redis/

三、相关使用配置

1、主从复制

 主机:两台,一台当 Master (192.168.27.7/24),一台当 Slave (192.168.27.17/24)

 软件:redis-4.0.14.tar.gz

 (1) 两台主机都安装 redis 服务,以上面编译安装为例

 (2) 主服务器配置

[ ~]# vim /usr/local/redis/etc/redis.conf 
#修改以下配置项
bind 0.0.0.0
daemonize yes
pidfile /usr/local/redis/run/redis_6379.pid
logfile "/usr/local/redis/log/redis_6379.log"dir /usr/local/redis/datarequirepass 123456

 (3) 从服务器配置

[ ~]# vim /usr/local/redis/etc/redis.conf 
bind 0.0.0.0
daemonize yes
pidfile /usr/local/redis/run/redis_6379.pid
logfile "/usr/local/redis/log/redis_6379.log"
dir /usr/local/redis/data
slaveof 192.168.27.7 6379    #主要是这两行,其它行只是优化配置,也可默认
masterauth 123456
requirepass 123456

 (4) 启动redis服务

[ ~]# redis-server /usr/local/redis/etc/redis.conf
[ ~]# redis-server /usr/local/redis/etc/redis.conf

 (5) 在主服务器上查看,并测试同步

[ ~]# redis-cli 
127.0.0.1:6379> AUTH 123456
OK
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:1    #显示了从节点
slave0:ip=192.168.27.17,port=6379,state=online,offset=98,lag=1
master_replid:c56587fcd8929c9dc34a3dc65864488f7c3222c3
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
127.0.0.1:6379> set k1 v1    #增加一个key,再到从服务器个看是否同步
OK
127.0.0.1:6379> get k1
"v1"

[ ~]# redis-cli 
127.0.0.1:6379> AUTH 123456
OK
127.0.0.1:6379> KEYS *    #可以看到已同步
1) "k1"

 主从复制原理:

1) 从服务器连接主服务器,并发送同步请求(SYNC);

2) 主服务器接收到从服务器的SYNC命令后,开始执行BGSAVE命令生成RDB快照文件,并使用缓冲区记录此后执行的所有写命令;

3) 主服务器BGSAVE命令执行完毕后,向所有从服务器发送快照文件,并在发送期间继续记录执行的写命令;

4) 从服务器收到快照文件后丢弃所有旧数据,载入收到的快照;

5) 主服务器快照发送完毕后,开始向从服务器发送缓冲区中的写命令; 

6) 从服务器完成对快照的载入后,开始接收命令请求,并执行来自主服务器缓冲区中的写命令;

7) 后期的同步,从服务器先发送自己 salve_repl_offset 位置,只同步新增加的数据,不再全量同步。

2、高可用

 主从同步架构无法实现 master 和 slave 角色的自动切换,需要手动切换,因此,redis在2.6+版本中集成了 sentinel 机制,redis的哨兵模式到了2.8版本之后就稳定了下来。

 Sentinel 进程是用于监控 redis 集群中 master 服务器的工作状态,在 master 服务器发生故障时,实现 master 和 slave 的切换。

 实验过程:

 主机:三台,一主两从,master (192.168.27.7/24),slave1 (192.168.27.17/24),slave2 (192.168.27.27/27)

 软件:redis-4.0.14.tar.gz

 (1) 三台主机都安装好redis服务

 (2) 先实现一主两从的主从架构,相关配置以上实验

[ ~]# redis-cli 
127.0.0.1:6379> AUTH 123456
OK
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2    #可以看到2个从节点
slave0:ip=192.168.27.17,port=6379,state=online,offset=2824,lag=1
slave1:ip=192.168.27.27,port=6379,state=online,offset=2824,lag=1
master_replid:c56587fcd8929c9dc34a3dc65864488f7c3222c3
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:2824
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:2824

 (3) 编辑哨兵的配置文件 sentinel.conf,此配置文件在源码包中有提供模板

[ ~]# cp redis-4.0.14/sentinel.conf /usr/local/redis/etc/
#master的配置
[ ~]# grep -vE "^#|^$" /usr/local/redis/etc/sentinel.conf
bind 192.168.27.7
port 26379
daemonize yes
pidfile "redis-sentinel.pid"
logfile "/usr/local/redis/log/sentinel_26379.log"
dir /usr/local/redis/data
sentinel monitor mymaster 192.168.27.7 6379 2
sentinel auth-pass mymaster 123456
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
#slave1的配置
[ ~]# grep -vE "^#|^$" /usr/local/redis/etc/sentinel.conf
bind 192.168.27.17
port 26379
daemonize yes
pidfile "redis-sentinel.pid"
logfile "/usr/local/redis/log/sentinel_26739.log"
dir /usr/local/redis/data
sentinel monitor mymaster 192.168.27.7 6379 2
sentinel auth-pas`s mymaster 123456
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
#slave2的配置
[ ~]# grep -vE "^#|^$" /usr/local/redis/etc/sentinel.conf
bind 192.168.27.27
port 26379
daemonize yes
pidfile "redis-sentinel.pid"
logfile "/usr/local/redis/log/sentinel_26739.log"
dir /usr/local/redis/data
sentinel monitor mymaster 192.168.27.7 6379 2
sentinel auth-pass mymaster 123456
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes

 (4) 启动三台服务器的哨兵服务

[ ~]# redis-sentinel /usr/local/redis/etc/sentinel.conf
[ ~]# redis-sentinel /usr/local/redis/etc/sentinel.conf
[ ~]# redis-sentinel /usr/local/redis/etc/sentinel.conf

#查看哨兵信息
[ ~]# redis-cli -h 192.168.27.7 -p 26379
192.168.27.7:26379> info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=192.168.27.7:6379,slaves=2,sentinels=3

 (5) 测试

[ ~]# ps -ef|grep redis
root     34164     1  0 21:23 ?        00:00:09 redis-server 0.0.0.0:6379
root     38532     1  0 22:37 ?        00:00:00 redis-sentinel 192.168.27.7:26379 [sentinel]
root     38585 32446  0 22:37 pts/0    00:00:00 grep --color=auto redis
[ ~]# kill -9 34164    #关掉master的redis服务
#查看日志,可以看到主切换到192.168.27.27了
[ ~]# tail -f /usr/local/redis/log/sentinel_26739.log 
38532:X 17 Apr 22:39:05.739 # +sdown master mymaster 192.168.27.7 6379
38532:X 17 Apr 22:39:05.806 # +new-epoch 1
38532:X 17 Apr 22:39:05.809 # +vote-for-leader defaf23ae6169827f46b3f7637576195a28be9bf 1
38532:X 17 Apr 22:39:05.812 # +odown master mymaster 192.168.27.7 6379 #quorum 3/2
38532:X 17 Apr 22:39:05.812 # Next failover delay: I will not start a failover before Fri Apr 17 22:45:06 2020
38532:X 17 Apr 22:39:06.721 # +config-update-from sentinel defaf23ae6169827f46b3f7637576195a28be9bf 192.168.27.27 26379 @ mymaster 192.168.27.7 6379
38532:X 17 Apr 22:39:06.721 # +switch-master mymaster 192.168.27.7 6379 192.168.27.17 6379
38532:X 17 Apr 22:39:06.722 * +slave slave 192.168.27.27:6379 192.168.27.27 6379 @ mymaster 192.168.27.17 6379
38532:X 17 Apr 22:39:06.722 * +slave slave 192.168.27.7:6379 192.168.27.7 6379 @ mymaster 192.168.27.17 6379
38532:X 17 Apr 22:39:36.785 # +sdown slave 192.168.27.7:6379 192.168.27.7 6379 @ mymaster 192.168.27.17 6379
#在slave1上查看,其状态已切换为master,且从为slave2
[ ~]# redis-cli 
127.0.0.1:6379> info replication
NOAUTH Authentication required.
127.0.0.1:6379> 
[ ~]# redis-cli 
127.0.0.1:6379> AUTH 123456
OK
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.27.27,port=6379,state=online,offset=198216,lag=1
master_replid:92dcec1cc90fef5135d7e49358a27e391715b250
master_replid2:c56587fcd8929c9dc34a3dc65864488f7c3222c3
master_repl_offset:198216
second_repl_offset:137317
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:198216
#假设此时人工修复master
[ ~]# vim /usr/local/redis/etc/redis.conf 
#看以下内容是否有 
masterauth "123456"
#然后重启
[ ~]# redis-server /usr/local/redis/etc/redis.conf
[ ~]# redis-cli 
127.0.0.1:6379> AUTH 123456
OK
127.0.0.1:6379> info Replication
# Replication
role:slave
master_host:192.168.27.17    #主服务器变为了slave1
master_port:6379
master_link_status:up    #连接也正常
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:352579
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:92dcec1cc90fef5135d7e49358a27e391715b250
master_replid2:c56587fcd8929c9dc34a3dc65864488f7c3222c3
master_repl_offset:352579
second_repl_offset:1299
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1299
repl_backlog_histlen:351281

3、Redis Cluster 集群

 在哨兵机制中,可以解决 redis 高可用的问题,即 master 故障后可以自动将 slave 提升为 master,但无法解决 redis 单机写入的瓶颈问题,即单机的 redis 写入性能受限于单机 的内存大小、并发数量、网卡速率等因素(slave不可写),因此 redis 官方在 3.0 版本之后推出了无中心架构的 redis cluster 机制,在无中心的 redis 集群中,其每个节点保存当前节点数据和整个集群状态,每个节点都和其他所有节点连接。

 创建 redis cluster 集群的前提:

- 每个 redis node 节点采用相同的硬件配置、相同的密码
- 每个节点必须开启的参数
    cluster-enabled yes #必须开启集群状态,开启后 redis 进程会有 cluster 显示
    cluster-config-file nodes-6380.conf #此文件有 redis cluster 集群自动创建和维护,不需要任何手动操作
- 所有 redis 服务器必须没有任何数据
- 先启动为单机 redis 且没有任何 key value

 实验过程:

 主机:四台,node1(192.168.27.7/24),node2(192.168.27.17/24),node3(192.168.27.27/24),node4(192.168.27.37/24)

 软件:redis-4.0.14.tar.gz

 (1) 四台服务器都安装 redis 服务,并配置 redis.conf 打开集群功能

#开启以下两个配置
cluster-enabled yes
cluster-config-file nodes-6379.conf

 (2) redis3 和4版本,需要使用到集群管理工具 redis-trib.rb,这个工具是 redis 官方推出的管理 redis 集群的工具,集成在redis 的源码 src 目录下,是基于 redis 提供的集群命令封装成简单、便捷、实用的操作工具。但redis-trib.rb依赖ruby,而yum源中的ruby版本过低,所以需要编译安装(编译安装到一台节点就可以,用于管理集群,没必要都安装)

[ ~]# wget https://cache.ruby-lang.org/pub/ruby/2.5/ruby-2.5.5.tar.gz
#未避免后续报错,先安装一些依赖包
[ ~]# yum install -y gcc gcc-c++ openssl openssl-devel zlib zlib-devel
[ ~]# tar -xf ruby-2.5.5.tar.gz 
[ ~]# cd ruby-2.5.5/
[ ruby-2.5.5]# ./configure
[ ruby-2.5.5]# make && make install
[ ruby-2.5.5]# gem install redis    
Fetching: redis-4.1.3.gem (100%)
Successfully installed redis-4.1.3
Parsing documentation for redis-4.1.3
Installing ri documentation for redis-4.1.3
Done installing documentation for redis after 1 seconds
1 gem installed

#如出错以与报错,请参考以下链接
#https://blog.csdn.net/linkvivi/article/details/79814458
[ ruby-2.5.5]# gem install redis 
ERROR:  Loading command: install (LoadError)
    cannot load such file -- zlib
ERROR:  While executing gem ... (NoMethodError)
    undefined method `invoke_with_build_args‘ for nil:NilClass

 (2) 修改ruby连接redis时的登录密码,默认为空

[ ruby-2.5.5]# vim /usr/local/lib/ruby/gems/2.5.0/gems/redis-4.1.3/lib/redis/client.rb 
#找到
      :password => nil,
#改为redis的登录密码
      :password => "123456",

 (3) 创建 redis cluster 集群

#先启动各服务器的redis服务
[ ~]# redis-server /usr/local/redis/etc/redis.conf
[ ~]# redis-server /usr/local/redis/etc/redis.conf
[ ~]# redis-server /usr/local/redis/etc/redis.conf
[ ~]# redis-server /usr/local/redis/etc/redis.conf 
[ ~]# redis-server /usr/local/redis/etc/redis.conf 
[ ~]# redis-server /usr/local/redis/etc/redis.conf 
#再在安装了redis-trib.rb 的机器上创建集群
[ ~]# redis-trib.rb create --replicas 1 192.168.27.7:6379 192.168.27.17:6379 192.168.27.27:6379 192.168.27.37:6379 192.168.27.47:6379 192.168.27.57:6379
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.27.7:6379
192.168.27.17:6379
192.168.27.27:6379
Adding replica 192.168.27.47:6379 to 192.168.27.7:6379
Adding replica 192.168.27.57:6379 to 192.168.27.17:6379
Adding replica 192.168.27.37:6379 to 192.168.27.27:6379
M: d744d70fb2ece5e8a1a567232d3a610e7e82fd39 192.168.27.7:6379
   slots:0-5460 (5461 slots) master
M: 881406fedfdeb7898ac535dd980cb1725d2cb01f 192.168.27.17:6379
   slots:5461-10922 (5462 slots) master
M: 2a238e59fa2bb8a8fc6e8ad8675567d8ad3dab37 192.168.27.27:6379
   slots:10923-16383 (5461 slots) master
S: 9be5ddbb13918ea048b1a911e6f3e7d3f3825ca8 192.168.27.37:6379
   replicates 2a238e59fa2bb8a8fc6e8ad8675567d8ad3dab37
S: 480c4641c92effbefd19631339c834eb5ed6fda7 192.168.27.47:6379
   replicates d744d70fb2ece5e8a1a567232d3a610e7e82fd39
S: d74dbb0f2f60a2770a4c7cda8e9cf0b8b76bf15e 192.168.27.57:6379
   replicates 881406fedfdeb7898ac535dd980cb1725d2cb01f
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.27.7:6379)
M: d744d70fb2ece5e8a1a567232d3a610e7e82fd39 192.168.27.7:6379
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
M: 2a238e59fa2bb8a8fc6e8ad8675567d8ad3dab37 192.168.27.27:6379
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: 9be5ddbb13918ea048b1a911e6f3e7d3f3825ca8 192.168.27.37:6379
   slots: (0 slots) slave
   replicates 2a238e59fa2bb8a8fc6e8ad8675567d8ad3dab37
M: 881406fedfdeb7898ac535dd980cb1725d2cb01f 192.168.27.17:6379
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: d74dbb0f2f60a2770a4c7cda8e9cf0b8b76bf15e 192.168.27.57:6379
   slots: (0 slots) slave
   replicates 881406fedfdeb7898ac535dd980cb1725d2cb01f
S: 480c4641c92effbefd19631339c834eb5ed6fda7 192.168.27.47:6379
   slots: (0 slots) slave
   replicates d744d70fb2ece5e8a1a567232d3a610e7e82fd39
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

 (4) 查看集群信息

# redis-trib.rb check IP:PORT
[ ~]# redis-trib.rb check 192.168.27.7:6379
>>> Performing Cluster Check (using node 192.168.27.7:6379)
M: d744d70fb2ece5e8a1a567232d3a610e7e82fd39 192.168.27.7:6379
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
M: 2a238e59fa2bb8a8fc6e8ad8675567d8ad3dab37 192.168.27.27:6379
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: 9be5ddbb13918ea048b1a911e6f3e7d3f3825ca8 192.168.27.37:6379
   slots: (0 slots) slave
   replicates 2a238e59fa2bb8a8fc6e8ad8675567d8ad3dab37
M: 881406fedfdeb7898ac535dd980cb1725d2cb01f 192.168.27.17:6379
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: d74dbb0f2f60a2770a4c7cda8e9cf0b8b76bf15e 192.168.27.57:6379
   slots: (0 slots) slave
   replicates 881406fedfdeb7898ac535dd980cb1725d2cb01f
S: 480c4641c92effbefd19631339c834eb5ed6fda7 192.168.27.47:6379
   slots: (0 slots) slave
   replicates d744d70fb2ece5e8a1a567232d3a610e7e82fd39
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
# redis-trib.rb info IP:PORT
[ ~]# redis-trib.rb info 192.168.27.7:6379
192.168.27.7:6379 (d744d70f...) -> 0 keys | 5461 slots | 1 slaves.
192.168.27.27:6379 (2a238e59...) -> 0 keys | 5461 slots | 1 slaves.
192.168.27.17:6379 (881406fe...) -> 0 keys | 5462 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.

相关推荐