Redis的Cluster配置

                                      作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

一.安装Redis并启动

1>.修改Redis的配置文件(本实验只有三个节点)

[root@node101.yinzhengjie.org.cn ~]# egrep -v "^#|^$" /etc/redis.conf
bind node101.yinzhengjie.org.cn
protected-mode yes
port
tcp-backlog
unixsocket "/tmp/redis.sock"
timeout
tcp-keepalive
daemonize no
supervised systemd
pidfile "/var/run/redis_6379.pid"
loglevel notice
logfile "/var/log/redis/redis.log"
databases
save
save
save
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename "dump.rdb"
dir "/var/lib/redis"
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay
repl-disable-tcp-nodelay no
slave-priority
requirepass "yinzhengjie"
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit
cluster-enabled yes                          #我们需要启用cluster功能
cluster-config-file redis-cluster.conf               #指定cluster的配置文件
cluster-node-timeout                       #指定节点的超时时间
slowlog-log-slower-than
slowlog-max-len
latency-monitor-threshold
notify-keyspace-events ""
hash-max-ziplist-entries
hash-max-ziplist-value
list-max-ziplist-size -
list-compress-depth
set-max-intset-entries
zset-max-ziplist-entries
zset-max-ziplist-value
hll-sparse-max-bytes
activerehashing yes
client-output-buffer-limit normal
client-output-buffer-limit slave 256mb 64mb
client-output-buffer-limit pubsub 32mb 8mb
hz
aof-rewrite-incremental-fsync yes
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# egrep -v "^#|^$" /etc/redis.conf

[root@node102.yinzhengjie.org.cn ~]#
[root@node102.yinzhengjie.org.cn ~]# egrep -v "^#|^$" /etc/redis.conf
bind node102.yinzhengjie.org.cn
protected-mode yes
port
tcp-backlog
unixsocket "/tmp/redis.sock"
timeout
tcp-keepalive
daemonize no
supervised systemd
pidfile "/var/run/redis_6379.pid"
loglevel notice
logfile "/var/log/redis/redis.log"
databases
save
save
save
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename "dump.rdb"
dir "/var/lib/redis"
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay
repl-disable-tcp-nodelay no
slave-priority
requirepass "yinzhengjie"
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit
cluster-enabled yes
cluster-config-file redis-cluster.conf
cluster-node-timeout
slowlog-log-slower-than
slowlog-max-len
latency-monitor-threshold
notify-keyspace-events ""
hash-max-ziplist-entries
hash-max-ziplist-value
list-max-ziplist-size -
list-compress-depth
set-max-intset-entries
zset-max-ziplist-entries
zset-max-ziplist-value
hll-sparse-max-bytes
activerehashing yes
client-output-buffer-limit normal
client-output-buffer-limit slave 256mb 64mb
client-output-buffer-limit pubsub 32mb 8mb
hz
aof-rewrite-incremental-fsync yes
[root@node102.yinzhengjie.org.cn ~]#
[root@node102.yinzhengjie.org.cn ~]#

[root@node102.yinzhengjie.org.cn ~]# egrep -v "^#|^$" /etc/redis.conf

[root@node103.yinzhengjie.org.cn ~]# egrep -v "^#|^$" /etc/redis.conf
bind node103.yinzhengjie.org.cn
protected-mode yes
port
tcp-backlog
unixsocket "/tmp/redis.sock"
timeout
tcp-keepalive
daemonize no
supervised systemd
pidfile "/var/run/redis_6379.pid"
loglevel notice
logfile "/var/log/redis/redis.log"
databases
save
save
save
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename "dump.rdb"
dir "/var/lib/redis"
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay
repl-disable-tcp-nodelay no
slave-priority
requirepass "yinzhengjie"
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit
cluster-enabled yes
cluster-config-file redis-cluster.conf
cluster-node-timeout
slowlog-log-slower-than
slowlog-max-len
latency-monitor-threshold
notify-keyspace-events ""
hash-max-ziplist-entries
hash-max-ziplist-value
list-max-ziplist-size -
list-compress-depth
set-max-intset-entries
zset-max-ziplist-entries
zset-max-ziplist-value
hll-sparse-max-bytes
activerehashing yes
client-output-buffer-limit normal
client-output-buffer-limit slave 256mb 64mb
client-output-buffer-limit pubsub 32mb 8mb
hz
aof-rewrite-incremental-fsync yes
[root@node103.yinzhengjie.org.cn ~]#
[root@node103.yinzhengjie.org.cn ~]#

[root@node103.yinzhengjie.org.cn ~]# egrep -v "^#|^$" /etc/redis.conf

2>.启动Redis集群

[root@node101.yinzhengjie.org.cn ~]# systemctl start redis
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# systemctl status redis
● redis.service - Redis persistent key-value database
Loaded: loaded (/usr/lib/systemd/system/redis.service; disabled; vendor preset: disabled)
Drop-In: /etc/systemd/system/redis.service.d
└─limit.conf
Active: active (running) since Sun -- :: CST; 5min ago
Main PID: (redis-server)
CGroup: /system.slice/redis.service
└─ /usr/bin/redis-server node101.yinzhengjie.org.cn: [cluster] Apr :: node101.yinzhengjie.org.cn systemd[]: Starting Redis persistent key-value database...
Apr :: node101.yinzhengjie.org.cn systemd[]: Started Redis persistent key-value database.
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# systemctl start redis

[root@node102.yinzhengjie.org.cn ~]# systemctl start redis
[root@node102.yinzhengjie.org.cn ~]#
[root@node102.yinzhengjie.org.cn ~]# systemctl status redis
● redis.service - Redis persistent key-value database
Loaded: loaded (/usr/lib/systemd/system/redis.service; disabled; vendor preset: disabled)
Drop-In: /etc/systemd/system/redis.service.d
└─limit.conf
Active: active (running) since Sun -- :: CST; 3s ago
Main PID: (redis-server)
CGroup: /system.slice/redis.service
└─ /usr/bin/redis-server node102.yinzhengjie.org.cn: [cluster] Apr :: node102.yinzhengjie.org.cn systemd[]: Starting Redis persistent key-value database...
Apr :: node102.yinzhengjie.org.cn systemd[]: Started Redis persistent key-value database.
[root@node102.yinzhengjie.org.cn ~]#

[root@node102.yinzhengjie.org.cn ~]# systemctl start redis

[root@node103.yinzhengjie.org.cn ~]# systemctl start redis
[root@node103.yinzhengjie.org.cn ~]#
[root@node103.yinzhengjie.org.cn ~]# systemctl status redis
● redis.service - Redis persistent key-value database
Loaded: loaded (/usr/lib/systemd/system/redis.service; disabled; vendor preset: disabled)
Drop-In: /etc/systemd/system/redis.service.d
└─limit.conf
Active: active (running) since Sat -- :: PDT; 3s ago
Main PID: (redis-server)
CGroup: /system.slice/redis.service
└─ /usr/bin/redis-server node103.yinzhengjie.org.cn: [cluster] Apr :: node103.yinzhengjie.org.cn systemd[]: Starting Redis persistent key-value database...
Apr :: node103.yinzhengjie.org.cn systemd[]: Started Redis persistent key-value database.
[root@node103.yinzhengjie.org.cn ~]#
[root@node103.yinzhengjie.org.cn ~]#

[root@node103.yinzhengjie.org.cn ~]# systemctl start redis

3>.登录Redis服务器测试

[root@node103.yinzhengjie.org.cn ~]# redis-cli  -h node101.yinzhengjie.org.cn -a yinzhengjie
node101.yinzhengjie.org.cn:>
node101.yinzhengjie.org.cn:>

[root@node103.yinzhengjie.org.cn ~]# redis-cli -h node101.yinzhengjie.org.cn -a yinzhengjie

二.配置Redis Cluster

1>.查看cluster相关命令的帮助信息

[root@node103.yinzhengjie.org.cn ~]# redis-cli  -h node101.yinzhengjie.org.cn -a yinzhengjie
node101.yinzhengjie.org.cn:>
node101.yinzhengjie.org.cn:> HELP @cluster CLUSTER ADDSLOTS slot [slot ...]
summary: Assign new hash slots to receiving node
since: 3.0. CLUSTER COUNT-FAILURE-REPORTS node-id
summary: Return the number of failure reports active for a given node
since: 3.0. CLUSTER COUNTKEYSINSLOT slot
summary: Return the number of local keys in the specified hash slot
since: 3.0. CLUSTER DELSLOTS slot [slot ...]
summary: Set hash slots as unbound in receiving node
since: 3.0. CLUSTER FAILOVER [FORCE|TAKEOVER]
summary: Forces a slave to perform a manual failover of its master.
since: 3.0. CLUSTER FORGET node-id
summary: Remove a node from the nodes table
since: 3.0. CLUSTER GETKEYSINSLOT slot count
summary: Return local key names in the specified hash slot
since: 3.0. CLUSTER INFO -
summary: Provides info about Redis Cluster node state
since: 3.0. CLUSTER KEYSLOT key
summary: Returns the hash slot of the specified key
since: 3.0. CLUSTER MEET ip port
summary: Force a node cluster to handshake with another node
since: 3.0. CLUSTER NODES -
summary: Get Cluster config for the node
since: 3.0. CLUSTER REPLICATE node-id
summary: Reconfigure a node as a slave of the specified master node
since: 3.0. CLUSTER RESET [HARD|SOFT]
summary: Reset a Redis Cluster node
since: 3.0. CLUSTER SAVECONFIG -
summary: Forces the node to save cluster state on disk
since: 3.0. CLUSTER SET-CONFIG-EPOCH config-epoch
summary: Set the configuration epoch in a new node
since: 3.0. CLUSTER SETSLOT slot IMPORTING|MIGRATING|STABLE|NODE [node-id]
summary: Bind a hash slot to a specific node
since: 3.0. CLUSTER SLAVES node-id
summary: List slave nodes of the specified master node
since: 3.0. CLUSTER SLOTS -
summary: Get array of Cluster slot to node mappings
since: 3.0. READONLY -
summary: Enables read queries for a connection to a cluster slave node
since: 3.0. READWRITE -
summary: Disables read queries for a connection to a cluster slave node
since: 3.0. node101.yinzhengjie.org.cn:>

node101.yinzhengjie.org.cn:6379> HELP @cluster

2>.命令行分配切片范围

[root@node103.yinzhengjie.org.cn ~]#
[root@node103.yinzhengjie.org.cn ~]# redis-cli -h node101.yinzhengjie.org.cn -a yinzhengjie -c cluster addslots {..}
OK
[root@node103.yinzhengjie.org.cn ~]#
[root@node103.yinzhengjie.org.cn ~]# redis-cli -h node102.yinzhengjie.org.cn -a yinzhengjie -c cluster addslots {..}
OK
[root@node103.yinzhengjie.org.cn ~]#
[root@node103.yinzhengjie.org.cn ~]# redis-cli -h node103.yinzhengjie.org.cn -a yinzhengjie -c cluster addslots {..}
OK
[root@node103.yinzhengjie.org.cn ~]#
[root@node103.yinzhengjie.org.cn ~]#

3>.互相暴露各个节点

[root@node103.yinzhengjie.org.cn ~]# redis-cli -h node103.yinzhengjie.org.cn -a yinzhengjie
node103.yinzhengjie.org.cn:>
node103.yinzhengjie.org.cn:> CLUSTER INFO
cluster_state:fail
cluster_slots_assigned:
cluster_slots_ok:
cluster_slots_pfail:
cluster_slots_fail:
cluster_known_nodes:
cluster_size:
cluster_current_epoch:
cluster_my_epoch:
cluster_stats_messages_sent:
cluster_stats_messages_received:
node103.yinzhengjie.org.cn:>

node103.yinzhengjie.org.cn:6379> CLUSTER INFO

node103.yinzhengjie.org.cn:> CLUSTER MEET 172.30.1.101
OK
node103.yinzhengjie.org.cn:>
node103.yinzhengjie.org.cn:> CLUSTER MEET 172.30.1.102
OK
node103.yinzhengjie.org.cn:>
node103.yinzhengjie.org.cn:> CLUSTER INFO
cluster_state:ok
cluster_slots_assigned:
cluster_slots_ok:
cluster_slots_pfail:
cluster_slots_fail:
cluster_known_nodes:
cluster_size:
cluster_current_epoch:
cluster_my_epoch:
cluster_stats_messages_sent:
cluster_stats_messages_received:
node103.yinzhengjie.org.cn:>

node103.yinzhengjie.org.cn:6379> CLUSTER INFO

[root@node102.yinzhengjie.org.cn ~]# redis-cli -h node102.yinzhengjie.org.cn -a yinzhengjie
node102.yinzhengjie.org.cn:>
node102.yinzhengjie.org.cn:>
node102.yinzhengjie.org.cn:>
node102.yinzhengjie.org.cn:> CLUSTER INFO
cluster_state:ok
cluster_slots_assigned:
cluster_slots_ok:
cluster_slots_pfail:
cluster_slots_fail:
cluster_known_nodes:
cluster_size:
cluster_current_epoch:
cluster_my_epoch:
cluster_stats_messages_sent:
cluster_stats_messages_received:
node102.yinzhengjie.org.cn:>
node102.yinzhengjie.org.cn:>

node102.yinzhengjie.org.cn:6379> CLUSTER INFO

[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# redis-cli -h node101.yinzhengjie.org.cn -a yinzhengjie
node101.yinzhengjie.org.cn:>
node101.yinzhengjie.org.cn:> CLUSTER INFO
cluster_state:ok
cluster_slots_assigned:
cluster_slots_ok:
cluster_slots_pfail:
cluster_slots_fail:
cluster_known_nodes:
cluster_size:
cluster_current_epoch:
cluster_my_epoch:
cluster_stats_messages_sent:
cluster_stats_messages_received:
node101.yinzhengjie.org.cn:>
node101.yinzhengjie.org.cn:>

node101.yinzhengjie.org.cn:6379> CLUSTER INFO

4>.验证Redis的写操作

[root@node101.yinzhengjie.org.cn ~]# redis-cli -h node101.yinzhengjie.org.cn -a yinzhengjie
node101.yinzhengjie.org.cn:>
node101.yinzhengjie.org.cn:>
node101.yinzhengjie.org.cn:> SET name Jason                                      #注意,我们在node101.yinzhengjie.org.cn创建数据时失败啦!返回的是错误,说是name这个字段被hash到5798这个分片上,而该分片在172.30.1.102:6379这个节点上,因此我们应该去该节点配置!
(error) MOVED 172.30.1.102:
node101.yinzhengjie.org.cn:>
node101.yinzhengjie.org.cn:>
node101.yinzhengjie.org.cn:>
node101.yinzhengjie.org.cn:> quit
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# redis-cli -h node102.yinzhengjie.org.cn -a yinzhengjie          #于是,按照其错误的提示,我们登录到172.30.1.102:6379这个节点上去设置KEY
node102.yinzhengjie.org.cn:>
node102.yinzhengjie.org.cn:> SET name Jason                                     #果不其然,在该节点就可以成功创建名称为name的KEY
OK
node102.yinzhengjie.org.cn:>
node102.yinzhengjie.org.cn:> KEYS *
) "name"
node102.yinzhengjie.org.cn:>
node102.yinzhengjie.org.cn:>

5>.验证Redis的读操作

[root@node103.yinzhengjie.org.cn ~]# redis-cli -h node103.yinzhengjie.org.cn -a yinzhengjie
node103.yinzhengjie.org.cn:>
node103.yinzhengjie.org.cn:> KEYS *
(empty list or set)
node103.yinzhengjie.org.cn:>
node103.yinzhengjie.org.cn:> GET name              #当我们想要读取name字段对应的value时,发现当前节点可以帮我们路由到真正存储的该节点上去
(error) MOVED 172.30.1.102:
node103.yinzhengjie.org.cn:>
node103.yinzhengjie.org.cn:> quit
[root@node103.yinzhengjie.org.cn ~]#
[root@node103.yinzhengjie.org.cn ~]# redis-cli -h node102.yinzhengjie.org.cn -a yinzhengjie
node102.yinzhengjie.org.cn:>
node102.yinzhengjie.org.cn:> KEYS *
) "name"
node102.yinzhengjie.org.cn:>
node102.yinzhengjie.org.cn:> GET name              #按照上面的报错提示登录到指定的节点后,发现是可以读取数据的!
"Jason"
node102.yinzhengjie.org.cn:>
node102.yinzhengjie.org.cn:>

  经测试,读写操作虽然能实现,但是对于程序员来说,手动去解决这些问题简直太麻烦了,推荐大家使用Cerberus(芒果TV)的解决方案,可以帮我们轻松解决上面遇到的读写问题!

 

Redis的Cluster配置的更多相关文章

  1. redis单点、redis主从、redis哨兵sentinel,redis集群cluster配置搭建与使用

    目录 redis单点.redis主从.redis哨兵 sentinel,redis集群cluster配置搭建与使用 1 .redis 安装及配置 1.1 redis 单点 1.1.2 在命令窗口操作r ...

  2. redis cluster 配置

    #服务器192.168.56.111 192.168.56.112 192.168.56.113 计划1主2从   192.168.56.111 192.168.56.112 192.168.56.1 ...

  3. laravel redis sentinel 和 redis cluster 配置

    laravel redis sentinel配置: 'redis' => [ 'cluster' => false, 'options' => [ 'replication' =&g ...

  4. redis的主从复制配置

    redis的主从复制配置 一.     原理 Redis的主从复制功能非常强大,一个master可以拥有多个slave,而一个slave又可以拥有多个slave,如此下去,形成了强大的多级服务器集群架 ...

  5. Redis进阶实践之十一 Redis的Cluster集群搭建

    一.引言      本文档只对Redis的Cluster集群做简单的介绍,并没有对分布式系统的详细概念做深入的探讨.本文只是提供了有关如何设置集群.测试和操作集群的说明,而不涉及Redis集群规范中涵 ...

  6. Redis进阶实践之十二 Redis的Cluster集群动态扩容

    一.引言     上一篇文章我们一步一步的教大家搭建了Redis的Cluster集群环境,形成了3个主节点和3个从节点的Cluster的环境.当然,大家可以使用 Cluster info 命令查看Cl ...

  7. Redis集群配置(linux)

     *弄了一天,有问题直接问我.qq:137416943   1.redis集群的配置和简单使用   Redis集群配置 0.首先要配置环境: 0.1 安装c++ yum install gcc-c++ ...

  8. docker+redis安装与配置,主从+哨兵模式

    docker+redis安装与配置 docker安装redis并且使用redis挂载的配置启动 1.拉取镜像 docker pull redis:3.2 2.准备准备挂载的目录和配置文件 首先在/do ...

  9. Redis实战总结-配置、持久化、复制

    Redis的配置主要放置在redis.conf,可以通过修改配置文件实现Redis许多特性,比如复制,持久化,集群等. redis.conf部分配置详解 # 启动redis,显示加载配置redis.c ...

随机推荐

  1. sqlalchemy外键的一些东西

    sqlalchemy中让MySQL支持中文字符 engine = create_engine("mysql+pymysql://root:mysql8@localhost/mysqltest ...

  2. MySQL常用日期时间函数

    日期和时间函数: MySQL服务器中的三种时区设置: ①系统时区---保存在系统变量system_time_zone ②服务器时区---保存在全局系统变量global.time_zone ③每个客户端 ...

  3. 21 python 初学(json pickle shelve)

    json: # _author: lily # _date: 2019/1/19 import json my_dict = {'name': 'lily', 'age': 18} f = open( ...

  4. robot中简单的使用键盘按键,和对象无关

    参考链接: https://blog.csdn.net/smallsmallmouse/article/details/78689675 1.在python中的代码 from selenium imp ...

  5. 日志切割之Logrotate

    1.关于日志切割 日志文件包含了关于系统中发生的事件的有用信息,在排障过程中或者系统性能分析时经常被用到.对于忙碌的服务器,日志文件大小会增长极快,服务器会很快消耗磁盘空间,这成了个问题.除此之外,处 ...

  6. Lightoj 1128 - Greatest Parent

    Gate 倍增模板,在一个严格小根堆中,给定$x,y$,求$x$的祖先中$≥y$的最高点. 注意清零 #include<cstdio> #include<iostream> # ...

  7. SpringBoot 数据篇之使用JDBC

    SpringBootTutorial :: Data :: Jdbc 简介 API execute update query 实战 配置数据源 完整示例 引申和引用 简介 Spring Data 包含 ...

  8. vue 项目设置实现通过本地手机访问

    1.查询ip地址 win+R打开cmd 窗口 输入ipconfig 找到IPV4地址   192.168.x.xxx 2.在vue项目config文件夹中的index.js,将localhost换为i ...

  9. 深入剖析Redis系列:Redis数据结构与全局命令概述

    前言 Redis 提供了 5 种数据结构.理解每种数据结构的特点,对于 Redis 的 开发运维 非常重要,同时掌握 Redis 的 单线程命令处理 机制,会使 数据结构 和 命令 的选择事半功倍. ...

  10. 基于Flask 实现Web微信登陆

    网页版微信登陆网址 https://login.wx.qq.com/ 获取微信登陆的二维码 在浏览器中访问登陆接口 https://login.wx.qq.com/ 我们查找二维码的图片可以看到 其中 ...