Redis 4.0 集群搭建完美方案及注意事项

一、部署Redis集群环境准备

1.准备6台redis服务器(默认配置)

Redis 4.0 集群搭建完美方案及注意事项_第1张图片

安装教程详见《edis4.0.10 linux环境详细安装教程及介绍》

2.配置redis集群

1)在每台redis服务器做集群配置然后重启服务

IP 规划:

redis 服务器 ip 地址及端口规划
– redisA 192.168.4.51 6351    – redisB 192.168.4.52 6352
– redisC 192.168.4.53 6353    –redisD 192.168.4.54 6354

– redisE 192.168.4.55 6355    – redisF 192.168.4.56 6356

redis集群服务器配置步骤:

1.修改配置文件

# vim /etc/redis/6379.conf
bind IP 地址 // 只写物理接口 IP 地址

daemonize yes // 守护进程方式运行

port xxxx // 端口号不要使用默认的 6379

cluster-enabled yes // 开启集群

cluster-config-file no5x.conf // 集群的配置文件不要使用默认的名称

cluster-node-timeout 5000 // 请求超时 5 秒

2.重启服务

由于已经把redis默认端口修了,登入服务时需要指定端口

[root@db1 opt]# redis-cli -h 192.168.4.51 -p 6351 shutdown  //关闭服务

[root@db1 opt]# /etc/init.d/redis_6379 start    //启动服务

Starting Redis server...

3.检查是否开启集群服务

> cluster nodes # 查看本机信息
> cluster info # 查看集群信息

]# redis-cli -c -h 192.168.4.56 -p 6356
192.168.4.56:6356> cluster nodes
9d34277832cf73950c4b51c692065d4ba62b4ff3 :6356@16356 myself,master - 0 0 0 connected
]# cat /var/lib/redis/6379/nodes-6356.conf     //redis开启集群后默认回在数据库目录下生成配置文件
9d34277832cf73950c4b51c692065d4ba62b4ff3 :0@0 myself,master - 0 0 0 connected
vars currentEpoch 0 lastVoteEpoch 0

二、搭建Redis集群管理服务

在这里我们把192.168.4.51作为redis集群的管理服务器,以下包需要在redisA安装

2.1.准备安装包

redis-3.2.1.gem 

ruby 

rubuygems 

ruby-devel-2.0

2.2 把源吗包里面src目录下redis-trib.rb拷贝到 /usr/local/sbin/目录下

[root@db0 redis-4.0.10]# cd src/
[root@db0 src]# cp redis-trib.rb /usr/local/bin/

2.3安装ruby脚本运行环境包

# yum install -y ruby rubygems
# rpm -ivh --nodeps ruby-devel-2.00.648-30.el7.x86_64.rpm  忽略依赖关系安装
# gem install redis-3.2.1.gem 

2.4使用redis-trib.rb创建集群

注意:创建集群前需要保证所以Redis从库内存数据和本地数据文件为空,确保每台服务器卡开启集群功能。

命令格式:redis-trub.rb eare --replicas 1

--replicas 1 表示 自动为每一个 master 节点分配一个 slave 节点

# redis-trib.rb create --replicas  1 192.168.4.51:6351 192.168.4.52:6352 192.168.4.53:6353 192.168.4.54:6354 192.168.4.55:6355 192.168.4.56:6356
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.4.51:6351
192.168.4.52:6352
192.168.4.53:6353
Adding replica 192.168.4.55:6355 to 192.168.4.51:6351
Adding replica 192.168.4.56:6356 to 192.168.4.52:6352
Adding replica 192.168.4.54:6354 to 192.168.4.53:6353
M: 8caf3ae34ed83ca83d27edadd11130677fca5e1c 192.168.4.51:6351
   slots:0-5460 (5461 slots) master
M: 9264fabe9d3fe363d353481147f82f3b652b5ae4 192.168.4.52:6352
   slots:5461-10922 (5462 slots) master
M: f04173772a74912dd220586d6c69b480fe62827e 192.168.4.53:6353
   slots:10923-16383 (5461 slots) master
S: 7cc533d6b45fab75c5d4f99d222afc2683fd5a42 192.168.4.54:6354
   replicates f04173772a74912dd220586d6c69b480fe62827e
S: 6638a3c63d672e2004459fac890e6c4aa42fb40e 192.168.4.55:6355
   replicates 8caf3ae34ed83ca83d27edadd11130677fca5e1c
S: 9d34277832cf73950c4b51c692065d4ba62b4ff3 192.168.4.56:6356
   replicates 9264fabe9d3fe363d353481147f82f3b652b5ae4
Can I set the above configuration? (type 'yes' to accept):yes 
......


[OK] All 16384 slots covered.    //集群创建成功

检查集群信息

从以下信息得出 192.168.4.51-53 为主库 54-56为从库,从库绑定了对应主库的ID值

# redis-trib.rb check 192.168.4.51:6351
>>> Performing Cluster Check (using node 192.168.4.51:6351)
M: 8caf3ae34ed83ca83d27edadd11130677fca5e1c 192.168.4.51:6351
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
M: f04173772a74912dd220586d6c69b480fe62827e 192.168.4.53:6353
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: 9d34277832cf73950c4b51c692065d4ba62b4ff3 192.168.4.56:6356
   slots: (0 slots) slave
   replicates 9264fabe9d3fe363d353481147f82f3b652b5ae4
S: 7cc533d6b45fab75c5d4f99d222afc2683fd5a42 192.168.4.54:6354
   slots: (0 slots) slave
   replicates f04173772a74912dd220586d6c69b480fe62827e
S: 6638a3c63d672e2004459fac890e6c4aa42fb40e 192.168.4.55:6355
   slots: (0 slots) slave
   replicates 8caf3ae34ed83ca83d27edadd11130677fca5e1c
M: 9264fabe9d3fe363d353481147f82f3b652b5ae4 192.168.4.52:6352
   slots:5461-10922 (5462 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.

2.5 使用ruby脚本检查任意一台集群信息

查看机信息

[root@db5 ~]# redis-cli -h 192.168.4.55 -p 6355
192.168.4.55:6355> cluster nodes
9d34277832cf73950c4b51c692065d4ba62b4ff3 192.168.4.56:6356@16356 slave 9264fabe9d3fe363d353481147f82f3b652b5ae4 0 1530775096561 6 connected
7cc533d6b45fab75c5d4f99d222afc2683fd5a42 192.168.4.54:6354@16354 slave f04173772a74912dd220586d6c69b480fe62827e 0 1530775096661 4 connected
f04173772a74912dd220586d6c69b480fe62827e 192.168.4.53:6353@16353 master - 0 1530775096561 3 connected 10923-16383
8caf3ae34ed83ca83d27edadd11130677fca5e1c 192.168.4.51:6351@16351 master - 0 1530775096000 1 connected 0-5460
6638a3c63d672e2004459fac890e6c4aa42fb40e 192.168.4.55:6355@16355 myself,slave 8caf3ae34ed83ca83d27edadd11130677fca5e1c 0 1530775096000 5 connected
9264fabe9d3fe363d353481147f82f3b652b5ae4 192.168.4.52:6352@16352 master - 0 1530775097663 2 connected 5461-10922

检查集群状态

192.168.4.55:6355> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:1947
cluster_stats_messages_pong_sent:1797
cluster_stats_messages_meet_sent:2
cluster_stats_messages_sent:3746
cluster_stats_messages_ping_received:1794
cluster_stats_messages_pong_received:1949
cluster_stats_messages_meet_received:3
cluster_stats_messages_received:3746
192.168.4.55:6355> 

2.6同步测试

在客户端访问任意master角色的redis服务器写入数据

# redis-cli -c -h 192.168.4.52 -p 6352
192.168.4.52:6352> set name zx
OK
192.168.4.52:6352>

在对应的从库查看

# redis-cli -c -h 192.168.4.52 -p 6352
192.168.4.56:6356> keys *
1) "name"
192.168.4.56:6356> get name
-> Redirected to slot [5798] located at 192.168.4.52:6352
"zx"

Redis集群补充:

1. redis-trib.rb 脚本常用选项:


– add-node 添加新节点

– check 对节点主机做检查

– reshard 对节点主机重新分片

– add-node --slave 添加从节点主机

– del-node 删除节点主机


2. Redis 配置文件常用配置选项:

– port 6379 // 端口

– bind 127.0.0.1 //IP 地址

– tcp-backlog 511 //tcp 连接总数

– timeout 0 // 连接超时时间

– tcp-keepalive 300 // 长连接时间

– daemonize yes // 守护进程方式运行

– databases 16 // 数据库个数

– logfile /var/log/redis_6379.log //pid 文件

– maxclients 10000 // 并发连接数量

– dir /var/lib/redis/6379 // 数据库目录

你可能感兴趣的:(linux集群架构)