Redis入门到精通-Redis集群搭建

数据库 waitig 593℃ 百度已收录 0评论

在Redis3.x之前,提供了sentinel工具来监控各个Master的状态,如果Master异常,则会做主从切换,将slave作为Master。其配置也是稍微的复杂,并且各方面表现一般。

​ 现在redis3.x已经支持集群的容错功能,并且非常简单。

​ 参考文档

​ Redis 官方集群指南:http://redis.io/topics/cluster-tutorial
Redis 官方集群规范:
http://redis.io/topics/cluster-spec
​ Redis
集群指南(中文翻译,紧供参考):
http://redisdoc.com/topic/cluster-tutorial.html

Redis 集群规范(中文翻译,紧供参考):
http://redisdoc.com/topic/cluster-spec.html


1 集群规划


​ 要让 Redis3.0 集群正常工作至少需要 3 个 Master 节点,要想实现高可用,每个 Master 节点要配备至少 1 个 Slave 节点。根据以上特点和要求,进行如下的集群实施规划:

​ 使用 6 台服务器(物理机或虚拟机,我本机电脑只有8G内存,我采用文件系统隔离模拟6台服务器)部署 3 个 Master + 3 个 Slave;


主机名 IP 服务端口(默认6379) 集群端口(服务端口+10000) 主/从
redis-01 10.0.31.144 7111 17111 Master
redis-02 10.0.31.144 7112 17112 Master
Redis-03 10.0.31.144 7113 17113 Master
redis-04 10.0.31.144 7114 17114 Slave
redis-05 10.0.31.144 7115 17115 Slave
redis-06 10.0.31.144 7116 17116 Slave

2 环境准备

​ IP:10.0.31.144

​ 环境:CentOS 6.6

​ redis集群需要适应ruby命令

[root@localhost ~]yum install ruby
[root@localhost ~]yum install rubygems
[root@localhost ~]gem install redis  //安装redis和ruby的接口
Centos默认支持ruby到2.0.0,可gem 安装redis需要最低是2.2.2
解决办法是 先安装rvm,再把ruby版本提升至2.3.3

参考文档:http://blog.csdn.net/fengye_yulu/article/details/77628094

解决国内gem不用能的问题
# gem sources -r https://rubygems.org/
# gem sources -a https://ruby.taobao.org/
# gem sources -l
*** CURRENT SOURCES ***
https://ruby.taobao.org


​ Redis版本:redis-3.X

​ 用户:root

​ 安装目录:/usr/local/redis-cluster:

[root@localhost ~]# mkdir /usr/local/redis-cluster
[root@localhost ~]# cd /usr/local/redis-cluster/
[root@localhost redis-cluster]# mkdir 7111
[root@localhost redis-cluster]# mkdir 7112
[root@localhost redis-cluster]# mkdir 7113
[root@localhost redis-cluster]# mkdir 7114
[root@localhost redis-cluster]# mkdir 7115
[root@localhost redis-cluster]# mkdir 7116
[root@localhost redis-cluster]# pwd
/usr/local/redis-cluster
[root@localhost redis-cluster]# ll
总用量 24
drwxr-xr-x 2 root root 4096 1021 16:06 7111
drwxr-xr-x 2 root root 4096 1021 16:06 7112
drwxr-xr-x 2 root root 4096 1021 16:06 7113
drwxr-xr-x 2 root root 4096 1021 16:06 7114
drwxr-xr-x 2 root root 4096 1021 16:06 7115
drwxr-xr-x 2 root root 4096 1021 16:06 7116
[root@localhost redis-cluster]#

3 集群配置

(1)本案例中的单机部署伪集群

​ 本机上已安装过单机版redis,把之前的//usr/local/src/redis3.2/scr/redis.conf分别copy到/usr/local/redis-cluster/711X文件夹下,并分别重命名为 redis-711X.conf,进行配置修改。

[root@localhost ~]# cp //usr/local/src/redis3.2/scr/redis.conf  /usr/local/redis-cluster/7111/redis-7111.conf
[root@localhost ~]# cp //usr/local/src/redis3.2/scr/redis.conf  /usr/local/redis-cluster/7112/redis-7112.conf
[root@localhost ~]# cp //usr/local/src/redis3.2/scr/redis.conf  /usr/local/redis-cluster/7113/redis-7113.conf
[root@localhost ~]# cp //usr/local/src/redis3.2/scr/redis.conf  /usr/local/redis-cluster/7114/redis-7114.conf
[root@localhost ~]# cp //usr/local/src/redis3.2/scr/redis.conf  /usr/local/redis-cluster/7115/redis-7115.conf
[root@localhost ~]# cp //usr/local/src/redis3.2/scr/redis.conf  /usr/local/redis-cluster/7116/redis-7116.conf

​ 配置集群的redis-7111X.conf

[root@localhost ~]# vim /usr/local/redis-cluster/711X/redis-711X.conf

​ 配置参数:

配置选项 选项值 说明
daemonize yes 是否作为守护进程运行
pidfile /var/run/redis-711X.pid 为后台运行的进程提供一个pid
port 711X 监听端口,默认6379,集群通讯端口默认为此地址+10000
databases 1 可用数据库,默认16,默认数据库存储在DB0中,无特殊需求,建议设置一个数据库
cluster-enable yes 打开redis集群
cluster-config-file //usr/local/src/redis3.2/cluster/711X/nodes711X.conf 集群配置文件(启动自动生成),不用人为干涉
cluster-migration-barrier 1 数据迁移的副本临界数,这个参数表示的是,一个主节点在拥有多少个好的从节点的时候就要割让一个从节点出来给另一个没有任何从节点的主节点。
cluster-required-full-coverage Yes 如果一些kes space没有被集群中任何节点覆盖,集群将停止接收写入
appendonly yes 启用aof持久化方式
dir /usr/local/redis-cluster/711X 节点数据持久化存放目录(建议配置)
cluster-node-timeout 15000 节点互连超时时间(毫秒)
bind 当前机器IP 绑定当前机器IP

(2)如果准备的是6台虚拟机或物理机

在每一台机器上都执行以下步骤:(通单机版安装)

1.编译和安装所需要的包:

[root@localhost ~] # yum install gcc tcl

2.上传或下载redis稳定版3.x 到软件源码目录 /usr/local/src/

[root@localhost src]# cd /usr/local/src/
[root@localhost src]# wget http://download.redis.io/releases/redis-3.2.11.tar.gz

3.解压(重命名为redis3.2):

[root@localhost src]# tar -zxvf redis-3.2.11.tar.gz
[root@localhost src]# mv redis-3.2.11 redis3.2

4.创建安装目录

[root@localhost utils]# mkdir /usr/local/redis

5.编译安装(使用PREFIX指定安装目录)

​ 进入到/usr/local/src/redis-3.1/src目录

[root@localhost utils]# cd ../src/
[root@localhost src]# make PREFIX=/usr/local/redis install

6.配置集群

创建集群配置目录,并拷贝 redid.conf 配置文件到各节点配置目录:192.168.1.111

# mkdir -p /usr/local/redis-cluster/7111
# cp /usr/local/src/redis3.2/redis.conf /usr/local/redis-cluster/7111/redis-7111.conf

192.168.1.112

# mkdir -p /usr/local/redis-cluster/7112
# cp /usr/local/src/redis3.2/redis.conf /usr/local/redis-cluster/7112/redis-7112.conf

192.168.1.113

# mkdir -p /usr/local/redis-cluster/7113
# cp /usr/local/src/redis3.2/redis.conf /usr/local/redis-cluster/7113/redis-7113.conf

192.168.1.114

# mkdir -p /usr/local/redis-cluster/7114
# cp /usr/local/src/redis3.2/redis.conf /usr/local/redis-cluster/7114/redis-7114.conf

192.168.1.115

# mkdir -p /usr/local/redis-cluster/7115
# cp /usr/local/src/redis3.2/redis.conf /usr/local/redis-cluster/7115/redis-7115.conf

192.168.1.116

# mkdir -p /usr/local/redis-cluster/7116
# cp /usr/local/src/redis3.2/redis.conf /usr/local/redis-cluster/7116/redis-7116.conf

具体配置内容参看(1)中的配置案例。

4 集群创建

​ 分别启动6个redis实例,然后检查是否启动成功

[root@localhost ~]# /usr/local/redis/bin/redis-server /usr/local/redis-cluster/7111/redis-7111.conf
[root@localhost ~]# /usr/local/redis/bin/redis-server /usr/local/redis-cluster/7112/redis-7112.conf
[root@localhost ~]# /usr/local/redis/bin/redis-server /usr/local/redis-cluster/7113/redis-7113.conf
[root@localhost ~]# /usr/local/redis/bin/redis-server /usr/local/redis-cluster/7114/redis-7114.conf
[root@localhost ~]# /usr/local/redis/bin/redis-server /usr/local/redis-cluster/7115/redis-7115.conf
[root@localhost ~]# /usr/local/redis/bin/redis-server /usr/local/redis-cluster/7116/redis-7116.conf

​ 验证:

[root@localhost ~]# ps -ef|grep redis
root      1948     1  0 18:52 ?        00:00:00 /usr/local/redis/bin/redis-server 10.0.31.144:7111 [cluster]
root      1956     1  0 18:53 ?        00:00:00 /usr/local/redis/bin/redis-server 10.0.31.144:7112 [cluster]
root      1960     1  0 18:53 ?        00:00:00 /usr/local/redis/bin/redis-server 10.0.31.144:7113 [cluster]
root      1964     1  0 18:53 ?        00:00:00 /usr/local/redis/bin/redis-server 10.0.31.144:7114 [cluster]
root      1968     1  0 18:54 ?        00:00:00 /usr/local/redis/bin/redis-server 10.0.31.144:7115 [cluster]
root      1973     1  0 18:54 ?        00:00:00 /usr/local/redis/bin/redis-server 10.0.31.144:7116 [cluster]
root      1978  1720  0 18:54 pts/0    00:00:00 grep redis

​ 设置集群关系:

​ 使用/usr/local/src/redis3.2/src/redis-trib.rb命令

[root@localhost src]# ./redis-trib.rb create --replicas 1 10.0.31.144:7111 10.0.31.144:7112 10.0.31.144:7113 10.0.31.144:7114 10.0.31.144:7115 10.0.31.144:7116
>>> Creating cluster
Connecting to node 10.0.31.144:7111: OK
Connecting to node 10.0.31.144:7112: OK
Connecting to node 10.0.31.144:7113: OK
Connecting to node 10.0.31.144:7114: OK
Connecting to node 10.0.31.144:7115: OK
Connecting to node 10.0.31.144:7116: OK
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
10.0.31.144:7111
10.0.31.144:7112
10.0.31.144:7113
Adding replica 10.0.31.144:7114 to 10.0.31.144:7111
Adding replica 10.0.31.144:7115 to 10.0.31.144:7112
Adding replica 10.0.31.144:7116 to 10.0.31.144:7113
M: 88da0083291b0a732ec9ca502b2cfccfc296dcbb 10.0.31.144:7111
   slots:0-5460 (5461 slots) master
M: 815a0c11695a42cdb46104444f8c29a9ac5ea897 10.0.31.144:7112
   slots:5461-10922 (5462 slots) master
M: 203bc8721443aa8432b9397cfe0daa07f5d603a0 10.0.31.144:7113
   slots:10923-16383 (5461 slots) master
S: c639cce2bbe1334d15e7dcba2635f14b7b0ff4af 10.0.31.144:7114
   replicates 88da0083291b0a732ec9ca502b2cfccfc296dcbb
S: 4f8dd744609a135777d07d18e8faf18c6833e96d 10.0.31.144:7115
   replicates 815a0c11695a42cdb46104444f8c29a9ac5ea897
S: a350fb4762033ded64d6dbb778004889de673e37 10.0.31.144:7116
   replicates 203bc8721443aa8432b9397cfe0daa07f5d603a0
   Can I set the above configuration? (type 'yes' to accept): //输入yes

集群创建过程说明:

(1) 给定 redis-trib 程序的命令是 create , 这表示我们希望创建一个新的集群;

(2) 这里的 –replicas 1 表示每个主节点下有一个从节点;

(3) 之后跟着的其它参数则是实例的地址列表,程序使用这些地址所指示的实例来创建新集群;总的来说,以上命令的意思就是让 redis-trib 程序创建一个包含三个主节点和三个从节点的集群。接着,redis-trib
会打印出一份预想中的配置给你看,如果你觉得没问题的话(注意核对主从关系是否是
你想要的),就可以输入 yes , redis-trib 就会将这份配置应用到集群当中。

5 集群测试

使用redis-cli进入集群环境

5.1 登录集群

[root@localhost src]# /usr/local/redis/bin/redis-cli -c -h 10.0.31.144 -p 7111
10.0.31.144:7111> 

​ -c :集群模式

​ -h:指定ip地址

​ -p:指定端口号

5.2查看集群信息

​ cluster info :查看集群信息

​ cluster nodes :查看节点列表

5.3数据验证

[root@localhost src]# /usr/local/redis/bin/redis-cli -c -h 10.0.31.144 -p 7111
10.0.31.144:7111> set a 1
-> Redirected to slot [15495] located at 10.0.31.144:7113 //将a设置到了7113
OK
10.0.31.144:7113> quit
[root@localhost src]# /usr/local/redis/bin/redis-cli -c -h 10.0.31.144 -p 7112
10.0.31.144:7112> get a
-> Redirected to slot [15495] located at 10.0.31.144:7113 //从7113获取到了a
"1"
10.0.31.144:7113> 


本文由【waitig】发表在等英博客
本文固定链接:Redis入门到精通-Redis集群搭建
欢迎关注本站官方公众号,每日都有干货分享!
等英博客官方公众号
点赞 (0)分享 (0)