redis的架构(一)

redis认证

redis的认证比较简单,这里简单来说明一下怎么设置redis的认证:

redis的配置文件中有一个requirepass字段,在后面直接写上对应的密码即可。默认redis的不开启认证的,可以把注释去掉,然后开启认证。

requirepass foobared

redis的默认的密码如上所示,然后启动redis服务。

[root@test2 redis]# redis-cli
127.0.0.1:6379> info
NOAUTH Authentication required.
127.0.0.1:6379> auth foobared             #用auth命令认证给定的密码,也可以在客户端使用-a参数指定密码,
OK
127.0.0.1:6379> keys *
1) "key1"
2) "test"
3) "key"
127.0.0.1:6379>
[root@test2 redis]# redis-cli -p 6378 -a foobared            #注意这里与mysql的-p参数不一样,这里的-p指定的是redis链接的端口号
127.0.0.1:6378> info 
# Server
redis_version:4.0.0

redis的主从架构

这片文章仅说明一些redis的基本搭建问题和一些概念

主从的一些概念

主从架构就是一个简单的master服务器用来写,slave服务器把master上的数据在本地保存一份的形式。

redis的主从配置异常简单,从服务期所必须的选项只有一个slaveof。如果用户在启动redis的时候,指定了一个包含slaveof host port选项的配置文件,那么redis服务器将根据该选项在给定的ip地址和端口号来连接主服务器。对于一个正在运行的redis服务器,用户可以通过发送slaveof no one命令来让服务器终止复制操作,不再接受主服务器的数据更新;也可以通过发送slaveof host port命令来让服务器开始复制一个新的主服务器。

redis开启主从复制的过程:(来自redis实战一书)

redis在复制进行期间也会尽可能地处理接受到的命令请求,但是,如果主从服务器之间的网络带宽不足,或者主服务器没有足够的内存来创建子进程和创建记录写命令的缓冲区,那么redis处理命令请求的效率就会收到影响。因此,尽管这并不是必须的,但在实际中最好还是让主服务器只使用50%~65%的内存,留下30%~45%的内存用于执行bgsave命令和创建记录写命令的缓冲区。

从服务器在进行同步时,会清空自己的所有数据: 从服务器在与主服务器进行初始连接时,数据库中原有的所有数据都将丢失,并被替换成主服务器法来的数据。

当多个从服务器尝试连接同一个主服务器的时候:会出现以下两种情况。

第一:上面表格中的第3步尚未执行: 这时候若有多个从服务器连接同一个主服务器,则所有从服务器都会接受到相同的快照文件和相同的缓冲区写命令。

第二:上面表格中第3步正在执行或已经执行:当主服务器与较早进行连接的从服务器执行完复制所需的5个步骤后,主服务器会与新连接的从服务器执行一次新的步骤1至步骤5

主从的配置

在同一个服务器上运行三个redis实例,主要修改每个redis中配置文件的不同,端口的不同,logfile的不同以及数据文件存储名的不同。三个实例三个配置文件,分别以配置文件的方式启动redis实例。

redis-server  redis.conf 【同样的方式启动第二个,第三个,但是注意修改配置文件】

三个实例监听端口分别为: 6379,6378,6377.

以默认的端口为redis的master服务器,分别在6378,6377的配置文件中设定slaveof参数,配置如下:

slaveof 127.0.0.1 6379         

【因为三个实例都在同一个服务器上,因此使用了127.0.0.1】

分别启动两个实例。若是三个实例没有配置认证,则只要后面的两个从启动成功,这个redis的主从架构已经搭建成功。客户端连接master服务器,打入info命令会查看到对应复制信息。

在主从架构中启用认证

若是主服务器启用了认证,在从的日志文件中会看到如下信息:

70640:S 22 Apr 04:15:18.659 * Ready to accept connections
70640:S 22 Apr 04:15:18.659 * Connecting to MASTER 127.0.0.1:6379
70640:S 22 Apr 04:15:18.659 * MASTER <-> SLAVE sync started
70640:S 22 Apr 04:15:18.659 * Non blocking connect for SYNC fired the event.
70640:S 22 Apr 04:15:18.659 * Master replied to PING, replication can continue...
70640:S 22 Apr 04:15:18.659 * (Non critical) Master does not understand REPLCONF listening-port: -NOAUTH Authentication required.
70640:S 22 Apr 04:15:18.659 * (Non critical) Master does not understand REPLCONF capa: -NOAUTH Authentication required.
70640:S 22 Apr 04:15:18.659 * Partial resynchronization not possible (no cached master)
70640:S 22 Apr 04:15:18.659 # Unexpected reply to PSYNC from master: -NOAUTH Authentication required.
70640:S 22 Apr 04:15:18.659 * Retrying with SYNC...
70640:S 22 Apr 04:15:18.659 # MASTER aborted replication with an error: NOAUTH Authentication required.

这时候只要在从服务器上配置对应的master密码即可:

# masterauth <master-password>
masterauth foobared

这个参数就是指定master服务器的密码,配置了masterauth参数之后,重新启动从服务器。

[root@test2 redis]# redis-cli -p 6377 -a foobared           #使用密码连接
127.0.0.1:6377> info Replication                            #在从上查看复制信息
# Replication
role:slave
master_host:127.0.0.1
master_port:6379
master_link_status:up
master_last_io_seconds_ago:6
master_sync_in_progress:0
slave_repl_offset:5040
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:bcb68a14424b58d4dc49d075265bb5765c20648f
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:5040
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:4957
repl_backlog_histlen:84

在master上查看对应的复制信息:

127.0.0.1:6379> info Replication
# Replication
role:master
connected_slaves:2
slave0:ip=127.0.0.1,port=6378,state=online,offset=5138,lag=0
slave1:ip=127.0.0.1,port=6377,state=online,offset=5138,lag=0
master_replid:bcb68a14424b58d4dc49d075265bb5765c20648f
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:5138
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:5138
127.0.0.1:6379> 

传输延迟

从节点一般部署在不同机器上,复制时网络延迟就称为需要考虑的问题,redis提供了repl-disable-tcp-nodelay参数用于控制是否关闭TCP_NODELAY,默认关闭。

  • 当关闭时,主节点产生的命令数据无论大小都会及时地发送给从节点,这样主从之间延迟会变很小,但增加了网络带宽的消耗。适用于主从之间的网络环境良好的场景,如同机架或同机房部署。
  • 当开启时,主节点会合并较小的TCP数据包从而节省带宽。默认发送时间间隔取决于linux内核,一般默认是40毫秒。这种配置节省了带宽但增大了主从之间的延迟。适用于主从网络环境复杂或带宽紧张的场景。

检验硬盘的写入

为了验证主服务器是否已经将写数据发送至从服务器,用户需要在向主服务器写入真正的数据之后,再向主服务器写入一个唯一的虚构值,然后通过检查虚构值是否存在于从服务器来判断写入数据是否已经到达从服务器,这个操作很容易就可以实现。另一方面,判断数据是否已经被保存到硬盘里面则要困难得多。对于每秒同步一次AOF文件的redis服务器来说,用户总是可以通过等待1秒来确保数据已经被保存到硬盘里面;但更节约的做法是,检查info命令的输出结果中aof_pending_bio_fsync属性的值是否为0,如果是的话,那么就表示服务器已经将已知的所有数据都保存在硬盘里面了。

redis的主从复制很简单,但是我们知道redis有两种持久化方式aof和rdb持久化,作为主从的两个redis服务器的持久化要保持一致,不然可能会有问题吧!

 redis的哨兵机制

哨兵机制的一些基本概念,贴上两个博客的地址:

https://www.cnblogs.com/PatrickLiu/p/8444546.html

https://www.cnblogs.com/zhoujinyi/p/5569462.html

下面我们会在上面配置的redis的主从架构基础上配置哨兵模式。

redis的主从架构中,当主出现问题时,需要手动的切换到指定的从上面;redis的哨兵机制通过监控master的状态,当master出现问题时,会自动的完成切换。

上面的架构中是采用的一主两从的模式,分别对应两三个端口6379(master), 6378,6377是两个从的端口!因为有三个redis服务器,这里我们配置两3个sentinel服务(其实每个sentinel服务就是一个redis服务)

在sentinel.conf的配置文件中修改如下配置:

port 26379                                      #监听的端口,默认是26379,其余的两个分别修改为26378,26377
dir "/tmp/redis/sentinel1"                      #
sentinel monitor mymaster 127.0.0.1 6377 2      #监听的redis集群,注意只要指向集群中的master即可,(三个sentinel都只要指向master即可,会自动发现对应的从)
sentinel auth-pass mymaster foobared            #设置认证,注意这条命令一定要配置在上面一条命令的之后位置,不然会报错下面的错误。
daemonize yes                                   #开启sentinel的守护进程
[root@test2 redis]# redis-sentinel sentinel.conf 

*** FATAL CONFIG FILE ERROR ***
Reading the configuration file, at line 71
>>> 'sentinel auth-pass mymaster foobared'
No such master with specified name.
认证位置错误

三个哨兵配置文件只需要改动端口号,以及dir的目录即可,然后启动!

[root@test2 redis]# netstat -lntp |grep redis-sentine
tcp        0      0 0.0.0.0:26378           0.0.0.0:*               LISTEN      70864/redis-sentine 
tcp        0      0 0.0.0.0:26379           0.0.0.0:*               LISTEN      70857/redis-sentine 
tcp        0      0 0.0.0.0:26377           0.0.0.0:*               LISTEN      70869/redis-sentine 

上面说过sentinel也是一个redis服务器,那么就可以连接到sentinel上,查看对应的信息。

sentinel支持的一些命令:

sentinel支持的合法命令如下:

    PING sentinel回复PONG.

    SENTINEL masters 显示被监控的所有master以及它们的状态.

    SENTINEL master <master name> 显示指定master的信息和状态;

    SENTINEL slaves <master name> 显示指定master的所有slave以及它们的状态;

    SENTINEL get-master-addr-by-name <master name> 返回指定master的ip和端口,如果正在进行failover或者failover已经完成,将会显示被提升为master的slave的ip和端口。

    SENTINEL reset <pattern> 重置名字匹配该正则表达式的所有的master的状态信息,清楚其之前的状态信息,以及slaves信息。

    SENTINEL failover <master name> 强制sentinel执行failover,并且不需要得到其他sentinel的同意。但是failover后会将最新的配置发送给其他sentinel。

 redis集群

redis貌似有很多种集群,这里我们先介绍其官方集群redis-cluster,和上面一样不会说明很多原理性的东西,会详细说明搭建过程:

推荐一片博客:redis-cluster 虽然是照这片博客做的,但是每个步骤都有实际操作!

redis版本:

[root@monitor redis_cluster]# redis-server --version
Redis server v=4.0.0 sha=00000000:0 malloc=libc bits=64 build=cc318d51eae57ee3
[root@monitor redis_cluster]# 

环境准备: 两个虚拟机,模拟6台redis服务:

主机: 10.9.8.222  
  端口: 7000 7001 7002 主机:
10.9.8.223
  端口: 7003 7004 7005

在redis的安装目录/usr/local/redis下面创建如下文件:

#主机10.9.8.222下面的文件
redis_cluster ├──
7000 │   └── redis.conf ├── 7001 │   └── redis.conf ├── 7002   └── redis.conf
#主机109.8.223下面的文件 redis_cluster ├──
7003 │   └── redis.conf ├── 7004 │   └── redis.conf ├── 7005 │   └── redis.conf

每个配置文件中需要更改如下几项:

port  7000                               //每个配置文件绑定各自的端口号        
bind 本机ip                              //可以设置为0.0.0.0,也可以设置为监听本机ip和回环ip
daemonize    yes                         //redis后台运行
pidfile  /var/run/redis_7000.pid         //pidfile文件对应7000,7001,7002
cluster-enabled  yes                     //开启集群  把注释#去掉
cluster-config-file  nodes_7000.conf     //集群的配置  配置文件首次启动自动生成 7000,7001,7002
cluster-node-timeout  15000              //请求超时  默认15秒,可自行设置
appendonly  yes                          //aof日志开启  有需要就开启,它会每次写操作都记录一条日志

标红的选项为开启集群的配置,注意配置中的不同端口对应不同的文件。

配置文件完成之后,就是启用redis。[下面是redis-server的帮助文档,采用指定配置文件启动方式]

[root@monitor redis]# redis-server --help
Usage: ./redis-server [/path/to/redis.conf] [options]
       ./redis-server - (read config from stdin)
       ./redis-server -v or --version
       ./redis-server -h or --help
       ./redis-server --test-memory <megabytes>

Examples:
       ./redis-server (run the server with default conf)
       ./redis-server /etc/redis/6379.conf
       ./redis-server --port 7777
       ./redis-server --port 7777 --slaveof 127.0.0.1 8888
       ./redis-server /etc/myredis.conf --loglevel verbose

Sentinel mode:
       ./redis-server /etc/sentinel.conf --sentinel

启动之后,查看端口,会发现redis不仅会自动监听配置的端口,还会监听比当前端口大10000的端口。

[root@test2 src]# netstat -lntp   【这是222这台主机上的,223主机不再列出】
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 127.0.0.1:17002         0.0.0.0:*               LISTEN      102600/redis-server 
tcp        0      0 10.9.8.222:17002        0.0.0.0:*               LISTEN      102600/redis-server 
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1315/sshd           
tcp        0      0 127.0.0.1:7000          0.0.0.0:*               LISTEN      102595/redis-server 
tcp        0      0 10.9.8.222:7000         0.0.0.0:*               LISTEN      102595/redis-server 
tcp        0      0 127.0.0.1:7001          0.0.0.0:*               LISTEN      102605/redis-server 
tcp        0      0 10.9.8.222:7001         0.0.0.0:*               LISTEN      102605/redis-server 
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      2895/master         
tcp        0      0 127.0.0.1:7002          0.0.0.0:*               LISTEN      102600/redis-server 
tcp        0      0 10.9.8.222:7002         0.0.0.0:*               LISTEN      102600/redis-server 
tcp        0      0 0.0.0.0:10050           0.0.0.0:*               LISTEN      78604/zabbix_agentd 
tcp        0      0 127.0.0.1:17000         0.0.0.0:*               LISTEN      102595/redis-server 
tcp        0      0 10.9.8.222:17000        0.0.0.0:*               LISTEN      102595/redis-server 
tcp        0      0 127.0.0.1:17001         0.0.0.0:*               LISTEN      102605/redis-server 
tcp        0      0 10.9.8.222:17001        0.0.0.0:*               LISTEN      102605/redis-server 

所有的redis服务启动之后就是创建集群:使用redis自带的工具:redis-trib.rb,在/usr/local/redis/src下面。

[root@monitor src]# ./redis-trib.rb create --replicas 1 10.9.8.222:7000 10.9.8.222:7001 10.9.8.222:7002 10.9.8.223:7003 10.9.8.223:7004 10.9.8.223:7005
/usr/bin/env: ruby: No such file or directory
[root@monitor src]# 

#命令中--replicas 1表示为每个master创建一个从。

然后安装ruby:

[root@monitor src]# yum install -y ruby           #安装ruby
[root@monitor src]# ./redis-trib.rb create --replicas 1 10.9.8.222:7000 10.9.8.222:7001 10.9.8.222:7002 10.9.8.223:7003 10.9.8.223:7004 10.9.8.223:7005        #继续执行
/usr/share/rubygems/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- redis (LoadError)
    from /usr/share/rubygems/rubygems/core_ext/kernel_require.rb:55:in `require'
    from ./redis-trib.rb:25:in `<main>'
[root@monitor src]# gem install redis          #执行之后会发现仍然报错,要求ruby>2.2.2,centos自带的ruby好像是2.0吧!
Fetching: redis-4.1.0.gem (100%)
ERROR:  Error installing redis:
    redis requires Ruby version >= 2.2.2

升级ruby

百度有很多说怎么升级的,这里提供一个地址:升级ruby, 我自己是按照这个方法解决的。

[root@monitor src]# gem sources -a http://mirrors.aliyun.com/rubygems/
http://mirrors.aliyun.com/rubygems/ added to sources
[root@monitor src]# gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
gpg: directory `/root/.gnupg' created
gpg: new configuration file `/root/.gnupg/gpg.conf' created
gpg: WARNING: options in `/root/.gnupg/gpg.conf' are not yet active during this run
gpg: keyring `/root/.gnupg/secring.gpg' created
gpg: keyring `/root/.gnupg/pubring.gpg' created
gpg: requesting key D39DC0E3 from hkp server keys.gnupg.net
gpg: requesting key 39499BDB from hkp server keys.gnupg.net
gpg: /root/.gnupg/trustdb.gpg: trustdb created
gpg: key D39DC0E3: public key "Michal Papis (RVM signing) <mpapis@gmail.com>" imported
gpg: key 39499BDB: public key "Piotr Kuczynski <piotr.kuczynski@gmail.com>" imported
gpg: no ultimately trusted keys found
gpg: Total number processed: 2
gpg:               imported: 2  (RSA: 2)
[root@monitor src]# curl -sSL https://get.rvm.io | bash -s stable
Downloading https://github.com/rvm/rvm/archive/1.29.7.tar.gz
Downloading https://github.com/rvm/rvm/releases/download/1.29.7/1.29.7.tar.gz.asc
gpg: Signature made Thu 03 Jan 2019 05:01:48 PM EST using RSA key ID 39499BDB
gpg: Good signature from "Piotr Kuczynski <piotr.kuczynski@gmail.com>"
gpg: WARNING: This key is not certified with a trusted signature!
gpg:          There is no indication that the signature belongs to the owner.
Primary key fingerprint: 7D2B AF1C F37B 13E2 069D  6956 105B D0E7 3949 9BDB
GPG verified '/usr/local/rvm/archives/rvm-1.29.7.tgz'
Creating group 'rvm'
Installing RVM to /usr/local/rvm/
Installation of RVM in /usr/local/rvm/ is almost complete:

  * First you need to add all users that will be using rvm to 'rvm' group,
    and logout - login again, anyone using rvm will be operating with `umask u=rwx,g=rwx,o=rx`.

  * To start using RVM you need to run `source /etc/profile.d/rvm.sh`
    in all your open shell windows, in rare cases you need to reopen all shell windows.
  * Please do NOT forget to add your users to the rvm group.
     The installer no longer auto-adds root or users to the rvm group. Admins must do this.
     Also, please note that group memberships are ONLY evaluated at login time.
     This means that users must log out then back in before group membership takes effect!
[root@monitor src]# source /etc/profile.d/rvm.sh
[root@monitor src]# rvm install 2.5
Searching for binary rubies, this might take some time.
No binary rubies available for: centos/7/x86_64/ruby-2.5.3.
Continuing with compilation. Please read 'rvm help mount' to get more information on binary rubies.
Checking requirements for centos.
Installing requirements for centos.
Installing required packages: patch, autoconf, automake, bison, bzip2, gcc-c++, libffi-devel, libtool, patch, readline-devel, sqlite-devel, zlib-devel, openssl-devel.................................
Requirements installation successful.
-bash: /usr/local/rvm/scripts/functions/manage/install/centos: No such file or directory
Installing Ruby from source to: /usr/local/rvm/rubies/ruby-2.5.3, this may take a while depending on your cpu(s)...
ruby-2.5.3 - #downloading ruby-2.5.3, this may take a while depending on your connection...
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 13.5M  100 13.5M    0     0  83242      0  0:02:50  0:02:50 --:--:--  123k
ruby-2.5.3 - #extracting ruby-2.5.3 to /usr/local/rvm/src/ruby-2.5.3.....
ruby-2.5.3 - #configuring...................................................................
ruby-2.5.3 - #post-configuration..
ruby-2.5.3 - #compiling.....................................................................................
ruby-2.5.3 - #installing.............................
ruby-2.5.3 - #making binaries executable..
ruby-2.5.3 - #downloading rubygems-2.7.9
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  842k  100  842k    0     0   155k      0  0:00:05  0:00:05 --:--:--  176k
No checksum for downloaded archive, recording checksum in user configuration.
ruby-2.5.3 - #extracting rubygems-2.7.9.....
ruby-2.5.3 - #removing old rubygems........
ruby-2.5.3 - #installing rubygems-2.7.9....................................
ruby-2.5.3 - #gemset created /usr/local/rvm/gems/ruby-2.5.3@global
ruby-2.5.3 - #importing gemset /usr/local/rvm/gemsets/global.gems................................................................
ruby-2.5.3 - #generating global wrappers.......
ruby-2.5.3 - #gemset created /usr/local/rvm/gems/ruby-2.5.3
ruby-2.5.3 - #importing gemsetfile /usr/local/rvm/gemsets/default.gems evaluated to empty gem list
ruby-2.5.3 - #generating default wrappers.......
ruby-2.5.3 - #adjusting #shebangs for (gem irb erb ri rdoc testrb rake).
Install of ruby-2.5.3 - #complete 
Ruby was built without documentation, to build it run: rvm docs generate-ri
几条命令的详细过程

下面单列几条命令:

gem sources -a http://mirrors.aliyun.com/rubygems/
gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
source /etc/profile.d/rvm.sh
rvm install 2.5

#最后删除原来的ruby
gem sources --remove https://rubygems.org/

#然后执行
[root@monitor src]# gem install redis #这步不执行,会报错误

【签名出错:】

[root@new-ruifu02-at-last ~]# gem sources -a http://mirrors.aliyun.com/rubygems/
source http://mirrors.aliyun.com/rubygems/ already present in the cache
[root@new-ruifu02-at-last ~]# gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
gpg: requesting key D39DC0E3 from hkp server keys.gnupg.net
gpg: requesting key 39499BDB from hkp server keys.gnupg.net
gpgkeys: HTTP fetch error 6: Could not resolve host: keys.gnupg.net; Unknown error
gpgkeys: HTTP fetch error 6: Could not resolve host: keys.gnupg.net; Unknown error
gpg: no valid OpenPGP data found.
gpg: Total number processed: 0

在执行gem添加的签名的时候总是出错,解决办法如下,把那个签名下载下来,然后导入。【提供方案的文档:https://stackoverflow.com/questions/29218225/key-issue-with-installing-rvm-ruby-version-manager】

[root@new-ruifu02-at-last ~]# wget  https://rvm.io/mpapis.asc         #下载签名
--2020-01-15 17:11:13--  https://rvm.io/mpapis.asc
Resolving rvm.io (rvm.io)... 151.101.194.49, 151.101.2.49, 151.101.66.49, ...
Connecting to rvm.io (rvm.io)|151.101.194.49|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 37428 (37K) [application/octet-stream]
Saving to: ‘mpapis.asc’

100%[=======================================================================================================================================>] 37,428      --.-K/s   in 0.1s    

2020-01-15 17:11:13 (262 KB/s) - ‘mpapis.asc’ saved [37428/37428]

[root@new-ruifu02-at-last ~]# ls
adduser.sh  anaconda-ks.cfg  initialization-script.sh  mpapis.asc
[root@new-ruifu02-at-last ~]# ls .gnupg/
gpg.conf  pubring.gpg  pubring.gpg~  secring.gpg  trustdb.gpg
[root@new-ruifu02-at-last ~]# rm .gnupg/*
rm: remove regular file ‘.gnupg/gpg.conf’? y
rm: remove regular file ‘.gnupg/pubring.gpg’? y
rm: remove regular file ‘.gnupg/pubring.gpg~’? y
rm: remove regular empty file ‘.gnupg/secring.gpg’? y
rm: remove regular file ‘.gnupg/trustdb.gpg’? y
[root@new-ruifu02-at-last ~]# gpg --import mpapis.asc
gpg: keyring `/root/.gnupg/secring.gpg' created
gpg: keyring `/root/.gnupg/pubring.gpg' created
gpg: /root/.gnupg/trustdb.gpg: trustdb created
gpg: key D39DC0E3: public key "Michal Papis (RVM signing) <mpapis@gmail.com>" imported
gpg: Total number processed: 1
gpg:               imported: 1  (RSA: 1)
gpg: no ultimately trusted keys found

[root@new-ruifu02-at-last ~]# curl -sSL https://get.rvm.io | bash           #
Downloading https://github.com/rvm/rvm/archive/master.tar.gz
Creating group 'rvm'
Installing RVM to /usr/local/rvm/
Installation of RVM in /usr/local/rvm/ is almost complete:

  * First you need to add all users that will be using rvm to 'rvm' group,
    and logout - login again, anyone using rvm will be operating with `umask u=rwx,g=rwx,o=rx`.

  * To start using RVM you need to run `source /etc/profile.d/rvm.sh`
    in all your open shell windows, in rare cases you need to reopen all shell windows.
  * Please do NOT forget to add your users to the rvm group.
     The installer no longer auto-adds root or users to the rvm group. Admins must do this.
     Also, please note that group memberships are ONLY evaluated at login time.
     This means that users must log out then back in before group membership takes effect!
Thanks for installing RVM 🙏
Please consider donating to our open collective to help us maintain RVM.

👉  Donate: https://opencollective.com/rvm/donate

然后执行创建集群的命令,若是报如下错误:

[root@test2 src]# ./redis-trib.rb create --replicas 1 10.9.8.222:7000 10.9.8.222:7001 10.9.8.222:7002 10.9.8.223:7003 10.9.8.223:7004 10.9.8.223:7005
>>> Creating cluster
[ERR] Node 10.9.8.222:7000 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.
[root@test2 src]# 

或者:
Can I set the above configuration? (type 'yes' to accept): yes
/usr/local/rvm/scripts/gems/ruby-2.2.2/gems/redis-4.1.0/lib/redis/client.rb:124:in `call': ERR Slot 6918 is already busy (Redis::CommandError)

解决上述问题:连接上集群的每一个节点,执行flushdb命令解决第一个问题,执行cluster reset命令解决第二个问题。

继续再执行上面的创建集群命令:

[root@test2 src]# ./redis-trib.rb create --replicas 1 10.9.8.222:7000 10.9.8.222:7001 10.9.8.222:7002 10.9.8.223:7003 10.9.8.223:7004 10.9.8.223:7005
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...                    #这里会自动为6个节点创建3个主从集群的,下面是主从的信息
Using 3 masters:
10.9.8.222:7000
10.9.8.223:7003
10.9.8.222:7001
Adding replica 10.9.8.223:7004 to 10.9.8.222:7000
Adding replica 10.9.8.222:7002 to 10.9.8.223:7003
Adding replica 10.9.8.223:7005 to 10.9.8.222:7001
M: 2359d9cb8872a9adbb06a2c2c18363147006da81 10.9.8.222:7000
   slots:0-5460 (5461 slots) master
M: 15e9a0d363d8cdd50e4257237b158cf645960a0b 10.9.8.222:7001
   slots:10923-16383 (5461 slots) master
S: c40ad7c21e2b799a9289684b72c5cf88f0779be0 10.9.8.222:7002
   replicates 65f1666ceb5c383f281a5aba54e1b0ea8eb9ba45
M: 65f1666ceb5c383f281a5aba54e1b0ea8eb9ba45 10.9.8.223:7003
   slots:5461-10922 (5462 slots) master
S: ada5c0cfb0d4177cf7e4710d24f8ec3b80f5b411 10.9.8.223:7004
   replicates 2359d9cb8872a9adbb06a2c2c18363147006da81
S: c78fec0a2cd02b69bb9a2825d526cbc70890447d 10.9.8.223:7005
   replicates 15e9a0d363d8cdd50e4257237b158cf645960a0b
Can I set the above configuration? (type 'yes' to accept): yes               #这里会让你回答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 10.9.8.222:7000)
M: 2359d9cb8872a9adbb06a2c2c18363147006da81 10.9.8.222:7000
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: c78fec0a2cd02b69bb9a2825d526cbc70890447d 10.9.8.223:7005
   slots: (0 slots) slave
   replicates 15e9a0d363d8cdd50e4257237b158cf645960a0b
M: 15e9a0d363d8cdd50e4257237b158cf645960a0b 10.9.8.222:7001
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
M: 65f1666ceb5c383f281a5aba54e1b0ea8eb9ba45 10.9.8.223:7003
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: c40ad7c21e2b799a9289684b72c5cf88f0779be0 10.9.8.222:7002
   slots: (0 slots) slave
   replicates 65f1666ceb5c383f281a5aba54e1b0ea8eb9ba45
S: ada5c0cfb0d4177cf7e4710d24f8ec3b80f5b411 10.9.8.223:7004
   slots: (0 slots) slave
   replicates 2359d9cb8872a9adbb06a2c2c18363147006da81
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@test2 src]#

然后可以连上集群查询对应的集群信息:

[root@monitor redis_cluster]# redis-cli -p 7004
127.0.0.1:7004> cluster slots
1) 1) (integer) 0                          #每个集群的slot信息
   2) (integer) 5460
   3) 1) "10.9.8.222"
      2) (integer) 7000
      3) "2359d9cb8872a9adbb06a2c2c18363147006da81"
   4) 1) "10.9.8.223"
      2) (integer) 7004
      3) "ada5c0cfb0d4177cf7e4710d24f8ec3b80f5b411"
2) 1) (integer) 10923
   2) (integer) 16383
   3) 1) "10.9.8.222"
      2) (integer) 7001
      3) "15e9a0d363d8cdd50e4257237b158cf645960a0b"
   4) 1) "10.9.8.223"
      2) (integer) 7005
      3) "c78fec0a2cd02b69bb9a2825d526cbc70890447d"
3) 1) (integer) 5461
   2) (integer) 10922
   3) 1) "10.9.8.223"
      2) (integer) 7003
      3) "65f1666ceb5c383f281a5aba54e1b0ea8eb9ba45"
   4) 1) "10.9.8.222"
      2) (integer) 7002
      3) "c40ad7c21e2b799a9289684b72c5cf88f0779be0"
127.0.0.1:7004>

redis集群在创建的时候不能配置密码认证,因此在集群创建完成之后可以配置密码认证:

[ops@vm51 src]$ redis-cli -p 6380 -h 10.9.68.51
10.9.68.51:6380> config set requirepass helloredis
OK
10.9.68.51:6380> auth helloredis
OK
10.9.68.51:6380> config rewrite

至此集群创建完!

posted @ 2019-04-25 18:56  夜间独行的浪子  阅读(1864)  评论(0编辑  收藏  举报