redis —主从&&集群(CLUSTER)

Posted Oops!

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了redis —主从&&集群(CLUSTER)相关的知识,希望对你有一定的参考价值。

REDIS主从配置

为了节省资源,本实验在一台机器进行。即,在一台机器上启动两个端口,模拟两台机器。

机器准备:
[[email protected] ~]# cp /etc/redis.conf /etc/redis2.conf

[[email protected] ~]# vim /etc/redis2.conf
port 6380
pidfile /var/run/redis_6380.pid
logfile "/tmp/logs/redis2.log"
dir /data/redis2
# slaveof <masterip> <masterport>
slaveof 127.0.0.1 6379
###指定主服务器IP和端口
# masterauth <master-password>
###如果主服务器设定了密码,需要在从服务器上添加该参数

[[email protected] ~]# mkdir /data/redis2

启动Redis:
[[email protected] ~]# redis-server /etc/redis.conf 
[[email protected] ~]# redis-server /etc/redis2.conf 

[[email protected] ~]# ps aux |grep redis
root      2454  0.2  0.4 145244  2356 ?        Ssl  17:18   0:00 redis-server 127.0.0.1:6379
root      2459  0.3  0.4 145244  2332 ?        Ssl  17:19   0:00 redis-server 127.0.0.1:6380
[[email protected] ~]# netstat -lntp |grep redis
tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN      2454/redis-server 1 
tcp        0      0 127.0.0.1:6380          0.0.0.0:*               LISTEN      2459/redis-server 1 
启动成功!!!

至此,Redis主从搭建完毕!!!

查看SLAVE上的数据

[[email protected] ~]# redis-cli -p 6380
127.0.0.1:6380> keys *
 1) "list1"
 2) "hseta"
 3) "set1"
 4) "set3"
 5) "key2"
 6) "k1"
 7) "set4"
 8) "seta"
 9) "k2"
10) "k3"
11) "zseta"
12) "setb"
13) "hash1"
14) "set5"
15) "list2"
16) "mykey"

测试主从

在master上创建数据:
[[email protected] ~]# redis-cli -p 6379
127.0.0.1:6379> del key 
(integer) 1
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> set test 00001
OK

在slave上查看:
[[email protected] ~]# redis-cli -p 6380
127.0.0.1:6380> select 1
127.0.0.1:6380[1]> keys *
1) "test"
127.0.0.1:6380[1]> get test
"00001"

注意: Redis主从和mysql主从不一样,Redis主从不用事先同步数据,它会自动同步。因为master上设置有参数“slave-read-only yes”,即该slave为只读数据库!

20.22 REDIS集群介绍

Redis cluster是分布式集群,支持横向扩展,Redis从V3.0版本后才支持集群功能。Redis集群的工作原理类似于磁盘的raid5。

  • 多个redis节点网络互联,数据共享
  • 所有的节点都是一主一从(可以是多个从),其中从不提供服务,仅作为备用
  • 不支持同时处理多个键(如mset/mget),因为redis需要把键均匀分布在各个节点上,并发量很高的情况下同时创建键值会降低性能并导致不可预测的行为。
  • 支持在线增加、删除节点
  • 客户端可以连任何一个主节点进行读写

技术分享

21.22-21.23 REDIS集群搭建

场景设置

  • 两台机器,分别开启三个Redis服务(端口)
  • A机器上三个端口:7000、7002、7004,全部为主
  • B机器上三个端口:7001、7003、7005,全部为从
  • 两台机器上都要编译安装Redis,然后编译并复制三个不同的Redis.conf,分别设置不同的端口号、dir等参数,还需要增加cluster相关参数,然后分别启动6个Redis服务

准备机器

MASTER(IP:192.168.8.131)

[[email protected] ~]# vim /etc/redis_7000.conf
port 7000
bind 192.168.8.131
daemonize yes
pidfile /var/run/redis_7000.pid
dir /data/redis_data/7000
cluster-enabled yes
##开启cluster功能
cluster-config-file nodes_7000.conf
##该配置文件可以在dir目录下自动生成
cluster-node-timeout 10100
appendonly yes

[[email protected] ~]# vim /etc/redis_7002.conf
port 7002
bind 192.168.8.131
daemonize yes
pidfile /var/run/redis_7002.pid
dir /data/redis_data/7002
cluster-enabled yes
cluster-config-file nodes_7002.conf
cluster-node-timeout 10100
appendonly yes

[[email protected] ~]# vim /etc/redis_7004.conf
port 7004
bind 192.168.8.131
daemonize yes
pidfile /var/run/redis_7004.pid
dir /data/redis_data/7004
cluster-enabled yes
cluster-config-file nodes_7004.conf
cluster-node-timeout 10100
appendonly yes

创建各配置文件对应的数据库目录:
[[email protected] ~]# mkdir /data/redis_data
[[email protected] ~]# mkdir /data/redis_data/{7000,7002,7004}

依次启动Redis服务7000,7002,7004:
[[email protected] ~]# redis-server /etc/redis_7000.conf

启动完成后,结果如下:
[[email protected] ~]# ps aux |grep redis
root     14423  0.6  0.5 145248  2640 ?        Ssl  19:35   0:00 redis-server 192.168.8.131:7000 [cluster]
root     14438  3.5  0.5 145248  2636 ?        Ssl  19:37   0:00 redis-server 192.168.8.131:7002 [cluster]
root     14443 13.8  0.5 145248  2636 ?        Ssl  19:37   0:01 redis-server 192.168.8.131:7004 [cluster]

注: 此处bind应该对应自己服务器的IP。

SLAVE(IP:192.168.8.132)

首先要先安装好Redis,然后执行如下操作:

[[email protected] ~]# vim /etc/redis_7001.conf
port 7001
bind 192.168.8.132
daemonize yes
pidfile /var/run/redis_7001.pid
dir /data/redis_data/7001
cluster-enabled yes
cluster-config-file nodes_7001.conf
cluster-node-timeout 10100
appendonly yes

[[email protected] ~]# vim /etc/redis_7003.conf
port 7003
bind 192.168.8.132
daemonize yes
pidfile /var/run/redis_7003.pid
dir /data/redis_data/7003
cluster-enabled yes
cluster-config-file nodes_7003.conf
cluster-node-timeout 10100
appendonly yes

[[email protected] ~]# vim /etc/redis_7005.conf
port 7005
bind 192.168.8.132
daemonize yes
pidfile /var/run/redis_7005.pid
dir /data/redis_data/7005
cluster-enabled yes
cluster-config-file nodes_7005.conf
cluster-node-timeout 10100
appendonly yes

创建各配置文件对应的数据库目录:
[[email protected] ~]# mkdir /data/redis_data
[[email protected] ~]# mkdir /data/redis_data/{7001,7003,7005}

依次启动Redis服务7001,7003,7005:
[[email protected] ~]# redis-server /etc/redis_7001.conf

启动完成后结果如下:
[[email protected] ~]# ps aux |grep redis
root      5971  0.2  0.5 145248  2632 ?        Ssl  19:41   0:00 redis-server 192.168.8.132:7001 [cluster]
root      5976  0.1  0.5 145248  2636 ?        Ssl  19:41   0:00 redis-server 192.168.8.132:7003 [cluster]
root      5981  0.1  0.5 145248  2632 ?        Ssl  19:41   0:00 redis-server 192.168.8.132:7005 [cluster]

安装RUBY V2.2(MASTER)

Redis集群需要ruby的支持,需要先安装ruby(Ruby只需在一台机器上运行)。Redis4.0需要使用Ruby2.2,安装方法如下(因为本机自带的是2.0版本的ruby,所以需要使用如下方法把源码包包制作成yum安装包,然后借助yum工具安装ruby2.2——升级ruby版本):

安装yum开发工具组:
[[email protected] ~]# yum -y groupinstall "Development Tools"

升级库文件:
[[email protected] ~]# yum -y install gdbm-devel libdb4-devel libffi-devel libyaml libyaml-devel ncurses-devel openssl-devel readline-devel tcl-devel

[[email protected] ~]# cd /root/

创建制作rpm包的目录:
[[email protected] ~]# mkdir -p rpmbuild/{BUILD,BUILDROOT,RPMS,SOURCES,SPECS,SRPMS}

下载Ruby的源码包:
[[email protected] ~]#  wget http://cache.ruby-lang.org/pub/ruby/2.2/ruby-2.2.3.tar.gz -P rpmbuild/SOURCES

下载specs文件,用于制作rpm包:
[[email protected] ~]#  wget https://raw.githubusercontent.com/tjinjin/automate-ruby-rpm/master/ruby22x.spec -P rpmbuild/SPECS

制作rpm包:
[[email protected] ~]# rpmbuild -bb rpmbuild/SPECS/ruby22x.spec
##此处需要耐心等待…

安装Ruby2.2:
[[email protected] ~]# yum -y localinstall rpmbuild/RPMS/x86_64/ruby-2.2.3-1.el7.centos.x86_64.rpm

[[email protected] ~]# ruby -v
ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-linux]

至此,ruby 2.2安装完毕!
注: 除此方法之外,还可以编译安装ruby。

配置集群

安装Redis配置集群的工具:
[[email protected] ~]# gem install redis

将命令redis-trib.rb加入环境变量目录下:
[[email protected] ~]# cp /usr/local/src/redis-4.0.2/src/redis-trib.rb  /usr/bin/

[[email protected] ~]# redis-trib.rb create --replicas 1 192.168.8.131:7000 192.168.8.131:7002 192.168.8.131:7004  192.168.8.132:7001 192.168.8.132:7003 192.168.8.132:7005
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

##注意:redis-trib.rb create --replicas 1  此处的参数“1”

至此,Redis集群配置完成!

21.25 REDIS集群操作

因为Redis集群是分布式结构,所以可以连接任何一个端口。

连接:
[[email protected] ~]# redis-cli -c -h 192.168.8.131 -p 7000
##-c:=cluster,表示以集群方式连接

创建数据:
192.168.8.131:7000> set cluster1 adaitest
-> Redirected to slot [8483] located at 192.168.8.132:7001
OK
##该操作会被重定向到192.168.8.132:7001
192.168.8.132:7001> set cluster2 adai222
-> Redirected to slot [4416] located at 192.168.8.131:7000
OK
192.168.8.131:7000> set cluster3 adaitest333
OK
192.168.8.131:7000> set cluster4 adai2323
-> Redirected to slot [12678] located at 192.168.8.131:7002
OK

查看数据:
192.168.8.131:7002> get cluster1
-> Redirected to slot [8483] located at 192.168.8.132:7001
"adaitest"
192.168.8.132:7001> get cluster2
-> Redirected to slot [4416] located at 192.168.8.131:7000
"adai222"
192.168.8.131:7000> get cluster3
"adaitest333"
192.168.8.131:7000> get cluster4
-> Redirected to slot [12678] located at 192.168.8.131:7002
"adai2323"

集群相关的操作

查看集群的状态:
[[email protected] ~]# redis-trib.rb check 192.168.8.131:7000

列出节点:
[[email protected] ~]# redis-cli -c -h 192.168.8.131 -p 7000
192.168.8.131:7000> cluster nodes

查看集群信息:
192.168.8.131:7000> cluster info

添加节点(执行该操作前先在slave创建redis_7007.conf并启动):
192.168.8.131:7000> cluster meet 192.168.8.132 7007
OK

192.168.8.131:7000> cluster nodes
52e4b3484838be21fcf53b84198e362efd54bd39 192.168.8.132:[email protected] master - 0 1507035952000 0 connected
##此时7007以master身份存在

再添加一个节点:
192.168.8.131:7000> cluster meet 192.168.8.131 7006
OK
192.168.8.131:7000> cluster nodes
677f27fb209ce45c823126fe38dbcf0b9fc43d93 192.168.8.131:[email protected] master - 0 1507036137147 0 connected
##同样是以master身份存在

##即,使用以上方式添加的新节点都是以master身份存在!

将当前节点设置为指定节点的从:

先更换到要设置的节点:
[[email protected] ~]# redis-cli -c -h 192.168.8.131 -p 7006

设定为7007的从:
192.168.8.131:7006> cluster replicate 52e4b3484838be21fcf53b84198e362efd54bd39
OK

查看:
192.168.8.131:7006> cluster nodes
52e4b3484838be21fcf53b84198e362efd54bd39 192.168.8.132:[email protected] master - 0 1507036429244 7 connected
677f27fb209ce45c823126fe38dbcf0b9fc43d93 192.168.8.131:[email protected] myself,slave 52e4b3484838be21fcf53b84198e362efd54bd39 0 1507036429000 0 connected
#对比node号,即7006为7007的从。。


移除某节点:
192.168.8.131:7006> cluster forget 52e4b3484838be21fcf53b84198e362efd54bd39
(error) ERR Can‘t forget my master!
192.168.8.131:7006> cluster forget 677f27fb209ce45c823126fe38dbcf0b9fc43d93
(error) ERR I tried hard but I can‘t forget myself...
## 即,不能移除master节点和当前所在节点


[[email protected] ~]# redis-cli -c -h 192.168.8.131 -p 7000
192.168.8.131:7000> cluster forget 677f27fb209ce45c823126fe38dbcf0b9fc43d93
OK

查看:
192.168.8.131:7000> cluster nodes
#此时7006已经不存在了。

保存当前配置:
192.168.8.131:7000> CLUSTER SAVECONFIG
OK

以上是关于redis —主从&&集群(CLUSTER)的主要内容,如果未能解决你的问题,请参考以下文章

redis学习篇主从&哨兵&集群架构详解

redis主从集群 -

RedisSpringBoot集群搭建redis主从复制&哨兵模式

Redis简单介绍4-主从复制&哨兵监控&集群分片

Redis-----大集合(主从复制,哨兵,集群)

Redis-----大集合(主从复制,哨兵,集群)