Redis集群创建

2020-12-09  本文已影响0人  dingxutao

redis集群一直是后台开发的重点,自己搭建一个Redis集群并探究集群是对自己非常有帮助的事,本篇文章重点现在我们先搭建一个Redis集群,对Redis其他的知识点将写在后续的文章中。

1:机器环境

    Redis集群搭建的方式有多种,例如Redis主从复制、Sentinel高可用集群等,但从Redis 3.0之后版本支持Redis-cluster集群,Redis-Cluster采用无中心结构,每个节点保存数据和整个集群状态,每个节点都和其他所有节点连接。

Redis > 3.0

linux: CentOS 7.2

2:下载redis

wget http://download.redis.io/releases/redis-4.0.8.tar.gz

tar -zxvf  redis-4.0.8.tar.gz

解压之后会生成 redis-4.0.8 目录

3:编译

cd redis-4.0.8

make

cd src & make test

编译完成之后,你在src目录下能看到这些bin文件:

-rwxr-xr-x 1 root root 2432992 Apr 10 13:34 redis-benchmark 

-rwxr-xr-x 1 root root 25176 Apr 10 13:34 redis-check-aof 

-rwxr-xr-x 1 root root 5192440 Apr 10 13:34 redis-check-rdb 

-rwxr-xr-x 1 root root 2586080 Apr 10 13:34 redis-cli  

lrwxrwxrwx 1 root root 12 Apr 10 13:34 redis-sentinel 

-rwxr-xr-x 1 root root 5192440 Apr 10 13:34 redis-server   

命令解释:

    redis-server:Redis 服务端的启动程序 。

    redis-cli:Redis客户端程序,也可以用 telnet 根据其纯文本协议来操作Redis缓存。

    redis-benchmark:Redis 性能测试工具,测试 Redis 在当前系统下的读写性能。

    redis-check-aof:数据修复。

    redis-check-dump:检查导出工具。     

3:集群

    Redis集群最低需要3主3从的实例,所以需要你启动6份Redis,你可以将上面3编译的文件拷贝成6份,可以放在 /opt/redis_cluster 目录下

(1):cd /opt & mkdir redis_cluster

(2):cp  -r  /opt/redis-4.0.8  ./redis_cluster

(3):mv  redis-4.0.8 redis-6380

重复操作(2)、(3)操作,生成 redis-6381、redis-6382、redis-6383、redis-6384、redis-6385 五份redis目录。

现在假定我们三主三从如下:

    Master:127.0.0.1:6380                 Slave:127.0.0.1:6383

    Master:127.0.0.1:6381                 Slave: 127.0.0.1:6384 

    Master:127.0.0.1:6382                 Slave: 127.0.0.1:6385 

创建集群数据的存放目录:

    cd / & mkdir /data/redis

    cd /data/redis

    mkdir -p 6380  6381    6382    6383    6385   63805

4:修改配置文件 redis.conf

(1):cd /opt/redis_cluster/redis-6380

(2):vim redis.conf

            port 6380

            pidfile /data/6380/redis.pid

            logfile /data/6380/redis.log

            loglevel notice

            dir /data/6380

            dbfilename dump.rdb

            protected-mode no  # 禁用保护模式(避免影响主从复制)

            cluster-enabled yes  # 开启cluster模式 

            cluster-config-file nodes.conf   # 记录集群信息,cluster集群自动维护,不用手动更新、创建

            cluster-node-timeout 5000  # 节点超时时间,目标节点超过指定时间没响应,就标记为FAIL或PFAIL(可能宕机)

            appendonly yes

依次进入redis-6381、redis-6382、redis-6383、redis-6384、redis-6385,根据(2)操作依次修改各自目录的redis.conf文件

5:安装redis-trib.rb需要的依赖环境

安装Epel源:wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

安装Ruby环境:yum install ruby rubygems -y

使用国内源: gem sources --add http://mirrors.aliyun.com/rubygems/ --remove https://rubygems.org/

检查源: gem sources -l

            *** CURRENT SOURCES ***

            http://mirrors.aliyun.com/rubygems/

安装依赖软件: gem install redis -v 3.3.3

拷贝redis-trib.rb 文件:cp  /opt/redis_cluster/redis-6380/src/redis-trib.rb   /opt/redis_cluster

6:启动各Redis实例

/opt/redis_cluster/redis-6380/src/redis-server /opt/redis_cluster/redis-6380/redis.conf

/opt/redis_cluster/redis-6381/src/redis-server /opt/redis_cluster/redis-6382/redis.conf

......

7:启动集群

/opt/redis_cluster/redis-trib.rb  create  --replicas  1  192.168.190.128:6380  192.168.190.128:6381   192.168.190.128:6382  192.168.190.128:6383  192.168.190.128:6384  192.168.190.128:6385

注意:不建议使用127.0.0.1这样的ip,可能会造成客户端无法连接到集群

命令参数的含义:

选项 create 表示希望创建一个新的集群。

选项 --replicas 1 表示希望集群中的每个主节点创建一个从节点。

之后跟着的多个host:port参数,则是实例的地址列表,希望程序使用这些地址所指示的实例来创建新集群。

ERROR错误提示:如果集群节点少于6个,使用redis-trib.rb创建集群会提示如下信息

>>> Creatingcluster

ERROR: Invalid configuration for cluster creation.

Redis Cluster requires at least 3 master nodes.

This is not possible with 4 nodes and 1 replicas per node.

At least 6 nodes are required.

redis-trib 会打印出一份预想中的配置,如果没问题就可以输入 yes ,redis-trib 就会将这份配置应用到集群当中

>>> Creating cluster

>>> Performing hash slots allocation on 6 nodes...

Using 3 masters:

127.0.0.1:6380

127.0.0.1:6381

127.0.0.1:6382

Adding replica 127.0.0.1:6384 to 127.0.0.1:6380

Adding replica 127.0.0.1:6385 to 127.0.0.1:6381

Adding replica 127.0.0.1:6383 to 127.0.0.1:6382

>>> Trying to optimize slaves allocation for anti-affinity

[WARNING] Some slaves are in the same host as their master

M: 377d7ce63974c82f5b3fcf4e5892ea7a3a6932b9 127.0.0.1:6380

  slots:0-5460 (5461 slots) master

M: 7eb9d21556d11826c420b61ed98aa2107b9dc36c 127.0.0.1:6381

  slots:5461-10922 (5462 slots) master

M: e7dcd03ebbd21de2e8333544005e117ade5770ed 127.0.0.1:6382

  slots:10923-16383 (5461 slots) master

S: e7576762471f895ea9d86d85a901505deb9357b2 127.0.0.1:6383

  replicates 377d7ce63974c82f5b3fcf4e5892ea7a3a6932b9

S: 231e980c377d0dce3165fb2a6c77dc662ea3c29c 127.0.0.1:6384

  replicates 7eb9d21556d11826c420b61ed98aa2107b9dc36c

S: 349a793ae2f95a7dbf15e63e2e5a7777f3be11ac 127.0.0.1:6385

  replicates e7dcd03ebbd21de2e8333544005e117ade5770ed

Can I set the above configuration? (type 'yes' to accept): 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 127.0.0.1:6380)

M: 377d7ce63974c82f5b3fcf4e5892ea7a3a6932b9 127.0.0.1:6380

  slots:0-5460 (5461 slots) master

  1 additional replica(s)

M: 7eb9d21556d11826c420b61ed98aa2107b9dc36c 127.0.0.1:6381

  slots:5461-10922 (5462 slots) master

  1 additional replica(s)

S: 349a793ae2f95a7dbf15e63e2e5a7777f3be11ac 127.0.0.1:6385

  slots: (0 slots) slave

  replicates e7dcd03ebbd21de2e8333544005e117ade5770ed

S: e7576762471f895ea9d86d85a901505deb9357b2 127.0.0.1:6383

  slots: (0 slots) slave

  replicates 377d7ce63974c82f5b3fcf4e5892ea7a3a6932b9

M: e7dcd03ebbd21de2e8333544005e117ade5770ed 127.0.0.1:6382

  slots:10923-16383 (5461 slots) master

  1 additional replica(s)

S: 231e980c377d0dce3165fb2a6c77dc662ea3c29c 127.0.0.1:6384

  slots: (0 slots) slave

  replicates 7eb9d21556d11826c420b61ed98aa2107b9dc36c

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

8:测试集群

/opt/redis_cluster/redis-6380/src/redis-cli -c -p 6380

127.0.0.1:6380> set msg "oldboy"

-> Redirected to slot [6257] located at 127.0.0.1:6381

OK

127.0.0.1:6381> get msg

"oldboy"

参考链接:实现Redis Cluster集群并实现Python链接集群

上一篇下一篇

猜你喜欢

热点阅读