centos6下redis cluster集群部署过程

Posted 散尽浮华

tags:

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

 

一般来说,redis主从和mysql主从目的差不多,但redis主从配置很简单,主要在从节点配置文件指定主节点ip和端口,比如:slaveof 192.168.10.10 6379,然后启动主从,主从就搭建好了。redis主从中如果主节点发生故障,不会自动切换,需要借助redis的Sentinel(哨兵模式)或者keepalive来实现主的故障转移。

今天介绍下redis cluster集群模式:
redis集群是一个无中心的分布式redis存储架构,可以在多个节点之间进行数据共享,解决了redis高可用、可扩展等问题,redis集群提供了以下两个好处:
1)将数据自动切分(split)到多个节点
2)当集群中的某一个节点故障时,redis还可以继续处理客户端的请求。

一个 Redis 集群包含 16384 个哈希槽(hash slot),数据库中的每个数据都属于这16384个哈希槽中的一个。集群使用公式 CRC16(key) % 16384 来计算键 key 属于哪个槽。集群中的每一个节点负责处理一部分哈希槽。
集群中的主从复制
集群中的每个节点都有1个至N个复制品,其中一个为主节点,其余的为从节点,如果主节点下线了,集群就会把这个主节点的一个从节点设置为新的主节点,继续工作。这样集群就不会因为一个主节点的下线而无法正常工作。

废话不多说,下面记录下搭建redis cluster集群:
由于最小的redis集群需要3个主节点,一台机器可运行多个redis实例,我搭建时使用两台机器,6个redis实例,其中三个主节点,三个从节点作为备份。很多案例使用单台服务器开6个端口,操作差不多,只是配置基本相对简单点,多台服务器更接近生产环境。
本案例redis cluster节点信息:
redis01
172.16.51.175:7000
172.16.51.175:7001
172.16.51.175:7002
redis02
172.16.51.176:7003
172.16.51.176:7004
172.16.51.176:7005
redis03
172.16.51.178:7006
172.16.51.178:7007
172.16.51.178:7008

先说下redis01节点的部署过程(其他两台节点部署过程一致)

个人运维习惯,会专门创建一个app账号,用户部署应用程序。本案例应用程序都部署在/data目录下,将/data权限设置成app
[[email protected] ~]# useradd app
[[email protected] ~]# passwd app
[[email protected] ~]# chown -R app.app /data

前提准备
1)安裝 GCC 编译工具 不然会有编译不过的问题
[[email protected] ~]# yum install -y gcc g++ make gcc-c++ kernel-devel automake autoconf libtool make wget tcl vim ruby rubygems unzip git 

2)升级所有的包,防止出现版本过久不兼容问题
[[email protected] ~]# yum -y update

3)关闭防火墙 节点之前需要开放指定端口,为了方便,生产不要禁用
[[email protected] ~]# /etc/init.d/iptables stop
[[email protected] ~]# setenforce 0
[[email protected] ~]# vim /etc/sysconfig/selinux 
......
SELINUX=disabled
......

redis cluster集群部署
4)下载并编译安装redis
[[email protected] ~]# su - app
[[email protected] ~]$ mkdir /data/software/
[[email protected] software]$ wget http://download.redis.io/releases/redis-4.0.1.tar.gz
[[email protected] software]$ tar -zvxf redis-4.0.1.tar.gz 
[[email protected] software]$ mv redis-4.0.1 /data/
[[email protected] software]$ cd /data/redis-4.0.1/
[[email protected] redis-4.0.1]$ make
--------------------------------------------------------------------------------------
如果因为上次编译失败,有残留的文件,做法如下:
[[email protected] redis-4.0.1]$ make distclean
--------------------------------------------------------------------------------------

5)创建节点
首先在172.16.51.175机器(redis01)上/data/redis-4.0.1目录下创建redis-cluster目录
[[email protected] redis-4.0.1]$ mkdir /opt/redis-4.0.1/redis-cluster

接着在redis-cluster目录下,创建名为7000、7001、7002的目录
[[email protected] redis-4.0.1]$ mkdir 7000
[[email protected] redis-4.0.1]$ mkdir 7001
[[email protected] redis-4.0.1]$ mkdir 7002

分别修改这三个配置文件redis.conf
[[email protected] redis-4.0.1]$ cd redis-cluster/
[[email protected] redis-cluster]$ ll
total 12
drwxrwxr-x 2 app app 4096 Nov 16 17:38 7000
drwxrwxr-x 2 app app 4096 Nov 16 17:39 7001
drwxrwxr-x 2 app app 4096 Nov 16 17:39 7002
[[email protected] redis-cluster]$ cat 7000/redis.conf 
port 7000
bind 172.16.51.175
daemonize yes
pidfile /var/run/redis_7000.pid
cluster-enabled yes
cluster-config-file nodes_7000.conf
cluster-node-timeout 10100
appendonly yes
[[email protected] redis-cluster]$ cat 7001/redis.conf 
port 7001
bind 172.16.51.175
daemonize yes
pidfile /var/run/redis_7001.pid
cluster-enabled yes
cluster-config-file nodes_7001.conf
cluster-node-timeout 10100
appendonly yes
[[email protected] redis-cluster]$ cat 7002/redis.conf 
port 7002
bind 172.16.51.175
daemonize yes
pidfile /var/run/redis_7002.pid
cluster-enabled yes
cluster-config-file nodes_7002.conf
cluster-node-timeout 10100
appendonly yes

----------------------------------------------------------------------------------------------------
redis.conf的配置说明:
#端口7000,7001,7002
port 7000

#默认ip为127.0.0.1,需要改为其他节点机器可访问的ip,否则创建集群时无法访问对应的端口,无法创建集群
bind 172.16.51.175

#redis后台运行
daemonize yes

#pidfile文件对应7000,7001,7002
pidfile /var/run/redis_7000.pid

#开启集群,把注释#去掉
cluster-enabled yes

#集群的配置,配置文件首次启动自动生成 7000,7001,7002          
cluster-config-file nodes_7000.conf

#请求超时,默认15秒,可自行设置 
cluster-node-timeout 10100    
        
#aof日志开启,有需要就开启,它会每次写操作都记录一条日志
appendonly yes
----------------------------------------------------------------------------------------------------

接着在另外两台机器上(172.16.51.176,172.16.51.178)重复以上三步,只是把目录改为7003、7004、7005和7006、7007、7008,对应的配置文件也按照这个规则修改即可(即修改redis.conf文件中的端口就行了)

6)启动集群(依次启动7000-7008端口)
#第一个节点机器上执行 3个节点
[[email protected] redis-cluster]$ for((i=0;i<=2;i++)); do /data/redis-4.0.1/src/redis-server /data/redis-4.0.1/redis-cluster/700$i/redis.conf; done

#第二个节点机器上执行 3个节点
[[email protected] redis-cluster]$ for((i=3;i<=5;i++)); do /data/redis-4.0.1/src/redis-server /data/redis-4.0.1/redis-cluster/700$i/redis.conf; done

#第三个节点机器上执行 3个节点
[[email protected] redis-cluster]$ for((i=6;i<=8;i++)); do /data/redis-4.0.1/src/redis-server /data/redis-4.0.1/redis-cluster/700$i/redis.conf; done

7)检查服务
检查各 Redis 各个节点启动情况
[[email protected] redis-cluster]$ ps -ef | grep redis  
app       2564  2405  0 20:13 pts/0    00:00:00 grep redis
app      15197     1  0 17:57 ?        00:00:05 /data/redis-4.0.1/src/redis-server 172.16.51.175:7000 [cluster]                   
app      15199     1  0 17:57 ?        00:00:05 /data/redis-4.0.1/src/redis-server 172.16.51.175:7001 [cluster]                   
app      15201     1  0 17:57 ?        00:00:05 /data/redis-4.0.1/src/redis-server 172.16.51.175:7002 [cluster]                   
[[email protected] redis-cluster]$ ps -ef | grep redis  
app       2566  2405  0 20:13 pts/0    00:00:00 grep redis
app      15197     1  0 17:57 ?        00:00:05 /data/redis-4.0.1/src/redis-server 172.16.51.175:7000 [cluster]                   
app      15199     1  0 17:57 ?        00:00:05 /data/redis-4.0.1/src/redis-server 172.16.51.175:7001 [cluster]                   
app      15201     1  0 17:57 ?        00:00:05 /data/redis-4.0.1/src/redis-server 172.16.51.175:7002 [cluster]

8)安装 Ruby(需要切换到root账号下进行安装,app账号下权限不够)
[[email protected] ~]# yum -y install ruby ruby-devel rubygems rpm-build
[[email protected] ~]# gem install redis
-----------------------------------------------------------------------------------------------------
注意:在centos6.x下执行上面的"gem install redis"操作可能会报错,坑很多!
默认yum安装的ruby版本是1.8.7,版本太低,需要升级到ruby2.2以上,否则执行上面安装会报错!

首先安装rvm
[[email protected] ~]# curl -L get.rvm.io | bash -s stable          //可能会报错,需要安装提示进行下面一步操作
[[email protected] ~]# curl -sSL https://rvm.io/mpapis.asc | gpg2 --import -
[[email protected] ~]# find / -name rvm.sh
/etc/profile.d/rvm.sh
[[email protected] ~]# source /etc/profile.d/rvm.sh
[[email protected] ~]# rvm requirements 

然后升级ruby到2.3
[[email protected] ~]# rvm install ruby 2.3.1
[[email protected] ~]# ruby -v
ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-linux]

列出所有ruby版本
[[email protected] ~]# rvm list

设置默认的版本
[[email protected] ~]# rvm --default use 2.3.1

更新下载源
[[email protected] ~]# gem sources --add https://gems.ruby-china.org/ --remove https://rubygems.org
https://gems.ruby-china.org/ added to sources
source https://rubygems.org not present in cache

[[email protected] ~]# gem sources
*** CURRENT SOURCES ***

https://rubygems.org/
https://gems.ruby-china.org/

最后就能顺利安装了
[[email protected] src]# gem install redis
Successfully installed redis-4.0.1
Parsing documentation for redis-4.0.1
Done installing documentation for redis after 1 seconds
1 gem installed
-----------------------------------------------------------------------------------------------------

9)创建集群
千万注意:在任意一台上运行即可,不要在每台机器上都运行,一台就够了!!!!
Redis 官方提供了 redis-trib.rb 这个工具,就在解压目录的 src 目录中
[[email protected] ~]# su - app
[[email protected] ~]$ /data/redis-4.0.1/src/redis-trib.rb create --replicas 1 172.16.51.175:7000 172.16.51.175:7001 172.16.51.175:7002 172.16.51.176:7003 172.16.51.176:7004 172.16.51.176:7005 172.16.51.178:7006 172.16.51.178:7007 172.16.51.178:7008

出现下面信息:
>>> Creating cluster
>>> Performing hash slots allocation on 9 nodes...
Using 4 masters:
172.16.51.175:7000
172.16.51.176:7003
172.16.51.178:7006
172.16.51.175:7001
Adding replica 172.16.51.176:7004 to 172.16.51.175:7000
Adding replica 172.16.51.178:7007 to 172.16.51.176:7003
Adding replica 172.16.51.175:7002 to 172.16.51.178:7006
Adding replica 172.16.51.176:7005 to 172.16.51.175:7001
Adding replica 172.16.51.178:7008 to 172.16.51.175:7000
M: 7c622ac191edd40dd61d9b79b27f6f69d02a5bbf 172.16.51.175:7000
   slots:0-4095 (4096 slots) master
M: 44c81c15b01d992cb9ede4ad35477ec853d70723 172.16.51.175:7001
   slots:12288-16383 (4096 slots) master
S: 38f03c27af39723e1828eb62d1775c4b6e2c3638 172.16.51.175:7002
   replicates f1abb62a8c9b448ea14db421bdfe3f1d8075189c
M: 987965baf505a9aa43e50e46c76189c51a8f17ec 172.16.51.176:7003
   slots:4096-8191 (4096 slots) master
S: 6555292fed9c5d52fcf5b983c441aff6f96923d5 172.16.51.176:7004
   replicates 7c622ac191edd40dd61d9b79b27f6f69d02a5bbf
S: 2b5ba254a0405d4efde4c459867b15176f79244a 172.16.51.176:7005
   replicates 44c81c15b01d992cb9ede4ad35477ec853d70723
M: f1abb62a8c9b448ea14db421bdfe3f1d8075189c 172.16.51.178:7006
   slots:8192-12287 (4096 slots) master
S: eb4067373d36d8a8df07951f92794e67a6aac022 172.16.51.178:7007
   replicates 987965baf505a9aa43e50e46c76189c51a8f17ec
S: 2919e041dd3d1daf176d6800dcd262f4e727f366 172.16.51.178:7008
   replicates 7c622ac191edd40dd61d9b79b27f6f69d02a5bbf
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 172.16.51.175:7000)
M: 7c622ac191edd40dd61d9b79b27f6f69d02a5bbf 172.16.51.175:7000
   slots:0-4095 (4096 slots) master
   2 additional replica(s)
S: 6555292fed9c5d52fcf5b983c441aff6f96923d5 172.16.51.176:7004
   slots: (0 slots) slave
   replicates 7c622ac191edd40dd61d9b79b27f6f69d02a5bbf
M: 44c81c15b01d992cb9ede4ad35477ec853d70723 172.16.51.175:7001
   slots:12288-16383 (4096 slots) master
   1 additional replica(s)
S: 2919e041dd3d1daf176d6800dcd262f4e727f366 172.16.51.178:7008
   slots: (0 slots) slave
   replicates 7c622ac191edd40dd61d9b79b27f6f69d02a5bbf
M: f1abb62a8c9b448ea14db421bdfe3f1d8075189c 172.16.51.178:7006
   slots:8192-12287 (4096 slots) master
   1 additional replica(s)
S: eb4067373d36d8a8df07951f92794e67a6aac022 172.16.51.178:7007
   slots: (0 slots) slave
   replicates 987965baf505a9aa43e50e46c76189c51a8f17ec
S: 38f03c27af39723e1828eb62d1775c4b6e2c3638 172.16.51.175:7002
   slots: (0 slots) slave
   replicates f1abb62a8c9b448ea14db421bdfe3f1d8075189c
S: 2b5ba254a0405d4efde4c459867b15176f79244a 172.16.51.176:7005
   slots: (0 slots) slave
   replicates 44c81c15b01d992cb9ede4ad35477ec853d70723
M: 987965baf505a9aa43e50e46c76189c51a8f17ec 172.16.51.176:7003
   slots:4096-8191 (4096 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

10)关闭集群
推荐做法:
[[email protected] ~]$ pkill redis
[[email protected] ~]$ pkill redis
[[email protected] ~]$ pkill redis

或者循环节点逐个关闭
[[email protected] ~]$ for((i=0;i<=2;i++)); do /opt/redis-4.0.1/src/redis-cli -c -h 172.16.51.175 -p 700$i shutdown; done
[[email protected] ~]$ for((i=3;i<=5;i++)); do /opt/redis-4.0.1/src/redis-cli -c -h 172.16.51.176 -p 700$i shutdown; done
[[email protected] ~]$ for((i=6;i<=8;i++)); do /opt/redis-4.0.1/src/redis-cli -c -h 172.16.51.178 -p 700$i shutdown; done

11)集群验证
连接集群测试
参数-C可连接到集群,因为redis.conf将bind改为了ip地址,所以-h参数不可以省略,-p参数为端口号

可以先在172.16.51.175机器redis 7000 的节点set一个key
[[email protected] ~]$ /data/redis-4.0.1/src/redis-cli -h 172.16.51.175 -c -p 7000
172.16.51.175:7000> set name www.ymq.io
-> Redirected to slot [5798] located at 172.16.51.176:7003
OK
172.16.51.176:7003> get name
"www.ymq.io"
172.16.51.176:7003>

由上面信息可发现redis set name 之后重定向到172.16.51.176机器 redis 7003 这个节点

然后在172.16.51.178机器redis 7008 的节点get一个key
[[email protected] ~]$ /data/redis-4.0.1/src/redis-cli -h 172.16.51.178 -c -p 7008
172.16.51.178:7008> get name
-> Redirected to slot [5798] located at 172.16.51.176:7003
"www.ymq.io"
172.16.51.176:7003> 

发现redis get name 重定向到172.16.51.176机器 redis 7003 这个节点.

如果看到这样的现象,说明redis cluster集群已经是可用的了!!!!!!

12)检查集群状态
[[email protected] ~]$ /data/redis-4.0.1/src/redis-cli -h 172.16.51.175 -c -p 7000
172.16.51.175:7000> 
[[email protected] ~]$ /data/redis-4.0.1/src/redis-trib.rb check 172.16.51.175:7000
>>> Performing Cluster Check (using node 172.16.51.175:7000)
M: 5a43e668f53ff64da68be31afe6dc6ea1f3c14c5 172.16.51.175:7000
   slots:0-4095 (4096 slots) master
   2 additional replica(s)
M: c64b0839e0199f73c5c192cc8c90f12c999f79b2 172.16.51.175:7001
   slots:12288-16383 (4096 slots) master
   1 additional replica(s)
S: 81347f01cf38d8f0faef1ad02676ebb4cffbec9e 172.16.51.176:7005
   slots: (0 slots) slave
   replicates c64b0839e0199f73c5c192cc8c90f12c999f79b2
M: da5dde3f2f02c232784bf3163f5f584b8cf046f2 172.16.51.178:7006
   slots:8192-12287 (4096 slots) master
   1 additional replica(s)
M: b217ab2a6c05497af3b2a859c1bb6b3fae5e0d92 172.16.51.176:7003
   slots:4096-8191 (4096 slots) master
   1 additional replica(s)
S: 0420c49fbc9f1fe16066d189265cca2f5e71c86e 172.16.51.178:7007
   slots: (0 slots) slave
   replicates b217ab2a6c05497af3b2a859c1bb6b3fae5e0d92
S: 5ad89453fb36e50ecc4560de6b4acce1dbbb78b3 172.16.51.176:7004
   slots: (0 slots) slave
   replicates 5a43e668f53ff64da68be31afe6dc6ea1f3c14c5
S: bbd1f279b99b95cf00ecbfab22b6b8dd5eb05989 172.16.51.178:7008
   slots: (0 slots) slave
   replicates 5a43e668f53ff64da68be31afe6dc6ea1f3c14c5
S: e95407b83bfeb30e3cc537161eadc372d6aa1fa2 172.16.51.175:7002
   slots: (0 slots) slave
   replicates da5dde3f2f02c232784bf3163f5f584b8cf046f2
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

13)列出集群节点
列出集群当前已知的所有节点(node),以及这些节点的相关信息
[[email protected] ~]$ /data/redis-4.0.1/src/redis-cli -h 172.16.51.175 -c -p 7000
172.16.51.175:7000> cluster nodes
5a43e668f53ff64da68be31afe6dc6ea1f3c14c5 172.16.51.175:[email protected] myself,master - 0 1510836027000 1 connected 0-4095
c64b0839e0199f73c5c192cc8c90f12c999f79b2 172.16.51.175:[email protected] master - 0 1510836030068 2 connected 12288-16383
81347f01cf38d8f0faef1ad02676ebb4cffbec9e 172.16.51.176:[email protected] slave c64b0839e0199f73c5c192cc8c90f12c999f79b2 0 1510836031000 6 connected
da5dde3f2f02c232784bf3163f5f584b8cf046f2 172.16.51.178:[email protected] master - 0 1510836031000 7 connected 8192-12287
b217ab2a6c05497af3b2a859c1bb6b3fae5e0d92 172.16.51.176:[email protected] master - 0 1510836030000 4 connected 4096-8191
0420c49fbc9f1fe16066d189265cca2f5e71c86e 172.16.51.178:[email protected] slave b217ab2a6c05497af3b2a859c1bb6b3fae5e0d92 0 1510836029067 8 connected
5ad89453fb36e50ecc4560de6b4acce1dbbb78b3 172.16.51.176:[email protected] slave 5a43e668f53ff64da68be31afe6dc6ea1f3c14c5 0 1510836032672 5 connected
bbd1f279b99b95cf00ecbfab22b6b8dd5eb05989 172.16.51.178:[email protected] slave 5a43e668f53ff64da68be31afe6dc6ea1f3c14c5 0 1510836031000 9 connected
e95407b83bfeb30e3cc537161eadc372d6aa1fa2 172.16.51.175:[email protected] slave da5dde3f2f02c232784bf3163f5f584b8cf046f2 0 1510836031672 7 connected

14)打印集群信息
[[email protected] ~]$ /data/redis-4.0.1/src/redis-cli -h 172.16.51.175 -c -p 7000
172.16.51.175:7000> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:9
cluster_size:4
cluster_current_epoch:9
cluster_my_epoch:1
cluster_stats_messages_ping_sent:8627
cluster_stats_messages_pong_sent:8581
cluster_stats_messages_sent:17208
cluster_stats_messages_ping_received:8573
cluster_stats_messages_pong_received:8626
cluster_stats_messages_meet_received:8
cluster_stats_messages_received:17207

15)集群命令
语法格式
redis-cli -c -p port

集群
cluster info :打印集群的信息
cluster nodes :列出集群当前已知的所有节点( node),以及这些节点的相关信息。

节点
cluster meet <ip> <port> :将 ip 和 port 所指定的节点添加到集群当中,让它成为集群的一份子。
cluster forget <node_id> :从集群中移除 node_id 指定的节点。
cluster replicate <node_id> :将当前节点设置为 node_id 指定的节点的从节点。
cluster saveconfig :将节点的配置文件保存到硬盘里面。

槽(slot)
cluster addslots <slot> [slot ...] :将一个或多个槽( slot)指派( assign)给当前节点。
cluster delslots <slot> [slot ...] :移除一个或多个槽对当前节点的指派。
cluster flushslots :移除指派给当前节点的所有槽,让当前节点变成一个没有指派任何槽的节点。
cluster setslot <slot> node <node_id> :将槽 slot 指派给 node_id 指定的节点,如果槽已经指派给另一个节点,那么先让另一个节点删除该槽>,然后再进行指派。
cluster setslot <slot> migrating <node_id> :将本节点的槽 slot 迁移到 node_id 指定的节点中。
cluster setslot <slot> importing <node_id> :从 node_id 指定的节点中导入槽 slot 到本节点。
cluster setslot <slot> stable :取消对槽 slot 的导入( import)或者迁移( migrate)。

键
cluster keyslot <key> :计算键 key 应该被放置在哪个槽上。
cluster countkeysinslot <slot> :返回槽 slot 目前包含的键值对数量。
cluster getkeysinslot <slot> <count> :返回 count 个 slot 槽中的键 。



















以上是关于centos6下redis cluster集群部署过程的主要内容,如果未能解决你的问题,请参考以下文章

redis 3.0.7 cluster 集群部署

redis3.0.7 cluster 集群部署

redis 集群架构 cluster sentinel

redis 集群架构 cluster sentinel

redis cluster官方集群的搭建笔记

Redis Cluster的部署与维护