当前位置:主页 > 查看内容

Redis基础必备

发布时间:2021-07-25 00:00| 位朋友查看

简介:redis 全称 remote dictionary server 存储系统 RDBMS关系型数据库: MYSQL?/?SQLSERVER NOSQL非关系型数据库: REDIS/MEMCACHED/MongoDB/HBase NEWSQL:?NewSQL是对所有新型可扩展、高性能数据库的简称NewSQL能够结合传统关系型数据库和NoSQL的优势 且容易横向……

redis 全称
remote dictionary server

存储系统

RDBMS关系型数据库: MYSQL?/?SQLSERVER

NOSQL非关系型数据库: REDIS/MEMCACHED/MongoDB/HBase

NEWSQL:?NewSQL是对所有新型可扩展、高性能数据库的简称NewSQL能够结合传统关系型数据库和NoSQL的优势 且容易横向扩展

redis与memcached的区别?

如果简单地比较Redis与Memcached的区别 大多数都会得到以下观点
1 Redis不仅仅支持简单的k/v key、value 类型的数据 同时还提供list set hash等数据结构的存储。
2 Redis支持数据的备份 即master-slave模式的数据备份 redis通过SENTINEL哨兵来进行主从的切换

redis在3.0 开始支持redis cluster 集群 但是可能并不完善

3 Redis支持数据的持久化 可以将内存中的数据保持在磁盘中 重启的时候可以再次加载进行使用。
在Redis中 并不是所有的数据都一直存储在内存中的。这是和Memcached相比一个最大的区别 我个人是这么认为的

另外 redis单线程 memcached多线程 可以使用多核cpu

?

Redis组件

1.?redis-server?redis的服务

2.?redis-cli?redis的客户端

command?line?interface?

3.?redis-benchmark 压力测试工具

4.?redis-check-dump? ?redis-check-aof 检查redi持久化工具 ?

corrupted?RDB/AOF file?utilities?

redis 持久化

Redis只会缓存所有的key的信息 如果Redis发现内存的使用量超过了某一个阀值 将触发swap的操作 Redis根据“swappability age*log(size_in_memory)”计算出哪些key对应的value需要swap到磁盘。然后再将这些key对应的value持久化到磁盘中 同时在内存中清除。这种特性使得Redis可以保持超过其机器本身内存大小的数据。当然 机器本身的内存必须要能够保持所有的key 毕竟这些数据是不会进行swap操作的。

同时由于Redis将内存中的数据swap到磁盘中的时候 提供服务的主线程和进行swap操作的子线程会共享这部分内存 所以如果更新需要swap的数据 Redis将阻塞这个操作 直到子线程完成swap操作后才可以进行修改。

?

Redis 并发相关介绍

1 Million small key →?string?value?pairs?use?~100MB of memory

Single threaded – but cpu should not to be the?bottleneck

Average?linux?system?can?deliver even?500K request?persecond 50w并发

如果较好的硬件经过测试支持百万的并发是完全没有问题的

Redis持久化的两种方式(Redis Persistence):

参考官网

https://redis.io/topics/persistence

?

1.?Snapshotting快照RDB

?

The RDB persistence performs point-in-time snapshots of your dataset at specified intervals.

RDB持久性执行时间点快照的数据集在指定的时间间隔。二进制格式显示为dump.rdb

2.AOF(append only file )

the AOF persistence logs every write operation received by the server

If you wish, you can disable persistence at all, if you want your data to just exist as long as the server is running.

?

The general indication is that you should use both persistence methods if you want a degree of data safety comparable to what PostgreSQL can provide you.

·一般的迹象是,您应该同时使用这两种持久性方法如果你想一定程度的数据安全与PostgreSQL能给你的一样

If you care a lot about your data, but still can live with a few minutes of data loss in case of disasters, you can simply use RDB alone.

·如果你非常关心你的数据,但仍然可以忍受几分钟的数据丢失在灾害的情况下,您可以简单地使用RDB孤单。

There are many users using AOF alone, but we discourage it since to have an RDB snapshot from time to time is a great idea for doing database backups, for faster restarts, and in the event of bugs in the AOF engine.

有许多用户使用AOF独自,但是我们阻止它从一个RDB快照时间进行数据库备份是一个伟大的想法,更快的重新启动,在发生错误的AOF引擎

?

在redis服务器启动用于恢复数据时 会优先使用aof

Redis安装

?

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

2.less README.md 里面有安装步骤

3.make

4.make?test?检查有无问题

5.?修整目录结构

[root localhost redis]# mkdir others

[root localhost redis]# mv 00-RELEASENOTES BUGS CONTRIBUTING COPYING INSTALL Makefile MANIFESTO README.md runtest runtest-cluster runtest-sentinel others/

?

mkdir conf

mv redis.conf conf/

mv sentinel.conf conf/

?

mkdir?bin

cd src

mv redis-server redis-cli redis-sentinel ../bin

?

?

Make?test?的一个报错

[err]: Test replication partial resync: ok psync (diskless: yes, reconnect: 1) in tests/integration/replication-psync.tcl

Expected condition [s -1 sync_partial_ok] 0 to be true ([s -1 sync_partial_ok] 0)

?

解决方式

http://www.voidcn.com/blog/chenggong2dm/article/p-6097574.html

?

修改redis配置文件

1.修改监听地址

bind ?10.0.140.84

?

2.damonsize?yes

daemonize yes|no 是否以后台daemon方式运行 # yes|no (default:no)

?

3.port?6379

?

4.snapshoting??的持久化方式

save 900 1 ?????#在 900秒内有一个key发生了变化

save 300 10 ????#在 300秒内有10个key发生了变化

save 60 10000 ??

如果取消snapshoting?的持久化方式

save?“”

?

6.?AOF持久化方式→类似于mysql的二进制日志

appendonly yes

appendfilename appendonly.aof

appendfsync everysec

?

修改两个持久化文件的位置

dbfilename dump.rdb

?

# The working directory.

# The DB will be written inside this directory, with the filename specified

# above using the dbfilename configuration directive.

# The Append Only File will also be created inside this directory.

#

# Note that you must specify a directory here, not a file name.

默认如下

dir ./

修改为

dir /data/zpy/redis/

?

7.修改redis日志文件的内容

logfile /data/zpy/redis/redis.log

?

7.redis?密码

requirepass redis

?

8.redis主从

redis?主不用做任何配置

只需要在从服务器上指定主库的ip与端口即可,启动从库就可以自动同步
slaveof masterip masterport

例如 Slaveof?10.0.0.1 6379

另外如果主库设置了密码 从库需要配置如下

masterauth master-password

?

Redis?启动命令

?

官网给出的启动命令

If you want to provide your redis.conf, you have to run it using an additional parameter (the path of the configuration file):

% cd src

% ./redis-server /path/to/redis.conf

由于我们之前更改了目录结构 所以启动方式如下

cd bin

./redis-server ../conf/redis.conf

启动后的状态
[root localhost bin]# ps -ef | grep redis

root ????25029 ????1 ?0 10:07 ? ???????00:00:00 ./redis-server 10.0.140.84:6379 ?

?

?

?

?

Redis-cli?命令行工具使用说明

[root localhost bin]#?./redis-cli --help

redis-cli 3.2.0

?

Usage: redis-cli [OPTIONS] [cmd [arg [arg ...]]]

??-h hostname ?????Server hostname (default: 127.0.0.1).

??-p port ?????????Server port (default: 6379).

??-s socket ???????Server socket (overrides hostname and port).

??-a password ?????Password to use when connecting to the server.

??-n db ???????????Database number.

??--help ????????????Output this help and exit.

??--version ?????????Output version and exit.

截取了常用的参数

Examples:

[root localhost bin]# ./redis-cli -h ?10.0.140.84 -a redis -p 6379?

10.0.140.84:6379

?

进入之后使用help

10.0.140.84:6379 help

redis-cli 3.2.0

To get help about Redis commands type:

?????? help group to get a list of commands in group

?????? help command for help on command

?????? help tab to get a list of possible help topics

?????? quit to exit

例如

help STRING

??APPEND key value

??GET key????summary: Get the value of a key

GETSET key value

help LIST

这里与运维关联性较小 不做详细介绍

?

help server下面一些简单命令 ?

CLIENT KILL?[ip:port] [ID client-id] [TYPE normal|slave|pubsub] [ADDR ip:port] [SKIPME yes/no]

summary: Kill the connection of a client

?

列出目前连接redis-server的主机

10.0.140.84:6379 CLIENT LIST

id 2 addr 10.0.140.84:50128 fd 6 name age 518 idle 0 flags N db 0 sub 0 psub 0 multi -1 qbuf 0 qbuf-free 32768 obl 0 oll 0 omem 0 events r cmd client

?

?

?

10.0.140.84:6379[15] INFO

显示redis的基本所有信息

?

?

10.0.140.84:6379 help select?

?

??SELECT index

??summary: Change the selected database for the current connection

??since: 1.0.0

??group: connection

SELECT 0 ?使用0号数据库

SELECT 1 ?使用1号数据库

SELECT2 ??使用2号数据库

一共使用16个数据库 select 0?–15?

在同一个数据库内 key是不能重复的

我们平时把这个数据库叫名称空间

我们平时使用可以使用不同的名称空间 也可以使用单独的名称空间 但是定义键值的时候根据键值起名的时候需要注意

?

Set king 1

Set queen 2

Set king 3

Get king ?发现是覆盖的

?

对字符串的append操作

10.0.140.84:6379[15] append ?king lalalla

(integer) 9

10.0.140.84:6379[15] get king

23lalalla

?

?

?

?

?

?

?

?

redis清空数据库操作

清空数据库

清空单个名称空间/库

FLUSHDB

清空所有名称空间/库

FLUSHALL

Redis对事务的支持

事务 一组相关的操作是原子性的 要么都执行 要么都不执行

一组事务 要么成功 要么撤回。

Mysql?事务简介 http://www.runoob.com/mysql/mysql-transaction.html

?

redis通过multi??exec??watch?等命令实现事务功能

开始事务 multi命令开始事务

在事务进行过程中 其他对redis的操作都会被阻塞

Exec 一并返回

?

例子

10.0.140.84:6379[15] multi →开始执行事务

OK

10.0.140.84:6379[15] set dailiang 1

QUEUED ????????????????→不执行 而是放置在队列中

10.0.140.84:6379[15] set lalala 2

QUEUED

10.0.140.84:6379[15] set zhangsan 3

QUEUED

10.0.140.84:6379[15] get zhangsan

QUEUED

10.0.140.84:6379[15] get dailiang

QUEUED

10.0.140.84:6379[15] get lalala

QUEUED

10.0.140.84:6379[15] exec ?→执行事务 一并返回

1) OK

2) OK

3) OK

4) 3

5) 1

6) 2

?

?

Redis的复制

一个master可以有多个slave

一个slave还可以再有slave 所以支持链式复制

Master?以非阻塞方式同步数据至salve? master可以同时处理多个slave的操作

scp -r -P 20755 redis root 10.0.140.85:/app/zpy

mkdir -p /data/zpy/redis/

?

修改配置文件两处

slaveof 10.0.140.84 6379

masterauth redis

l?slave-read-only yes ?→?默认只读权限

?

也可以只修改配置文件masterauth 直接在登陆状态下

10.0.140.85:6379 ?slaveof 10.0.140.84 6379

?

检查状态

[root localhost bin]# ./redis-cli -h 10.0.140.85 -a redis -p 6379

10.0.140.85:6379 ?info Replication

# Replication

role:slave

master_host:10.0.140.84

master_port:6379

master_link_status:up

?

?

我们再增加一个从服务器 为后面的sentinel课程打下基础10.0.140.86

?

REDIS安全简介

1).禁止一些高危命令

修改?redis.conf 文件 添加

rename-command FLUSHALL

rename-command CONFIG ??

rename-command EVAL ????

来禁用远程修改?DB 文件地址

?

2).以低权限运行 Redis 服务

为?Redis 服务创建单独的用户和家目录 并且配置禁止登陆

?

3).为 Redis 添加密码验证

修改?redis.conf 文件 添加

requirepass mypassword

?

4).禁止外网访问 Redis

修改?redis.conf 文件 添加或修改

bind 10.0.140.84

使得?Redis 服务只在当前主机可用

?

?

Redis哨兵sentinel

Redis主从有一个问题 如果我们主服务器挂掉了 slave服务器只读 这样就会出现问题

Redis采用了sentinel机制来解决这个问题

?

Redis Sentinel provides high availability for Redis. In practical terms this means that using Sentinel you can create a Redis deployment that resists without human intervention to certain kind of failures.?您可以创建一个复述,部署,抗拒某种失败而无需人工干预

Redis Sentinel also provides other collateral 并行的 ?tasks such as monitoring, notifications and acts as a configuration provider for clients.

This is the full list of Sentinel capabilities at a macroscopical level (i.e. the?big picture):

·?Monitoring. Sentinel constantly(持续的)?checks if your master and slave instances(实例)?are working as expected.

·?Notification. Sentinel can notify the system administrator, another computer programs, via an API, that something is wrong with one of the monitored Redis instances.

·?Automatic failover.(自动切换) If a master is not working as expected, Sentinel can start a failover process where a slave is promoted?to master, the other additional slaves are reconfigured to use the new master, and the applications using the Redis server informed about the new address to use when connecting.

·?Configuration provider. Sentinel acts as a source of authority for clients service discovery: clients connect to Sentinels?in order to ask for the address of the current Redis master responsible for a given service. If a failover occurs, Sentinels will report the new address.客户端配置哨兵的地址

·?You only need to specify the masters to monitor, giving to each separated master (that may have any number of slaves) a different name. There is no need to specify slaves, which are auto-discovered. Sentinel will update the configuration automatically with additional information about slaves只需告知哨兵master的地址就可以 无需告诉slave的地址 sentinel会自动发现

·?

·?Sentinels stay connected with other Sentinels in order to reciprocally 相互地check the availability of each other, and to exchange messages. However you don t need to configure a list of other Sentinel addresses in every Sentinel instance you run(你不需要在哨兵实例中配置哨兵实例的列表), as Sentinel uses the Redis instances Pub/Sub capabilities 能力in order to discover the other Sentinels that are monitoring the same masters and slaves.

·?Sentinels need to be elected leader for the failover and be authorized to proceed. This only happens with the vote of the?majority of the Sentinel processes. 所以我们最少可以用1台sentinel 或者我们可以使用3台

·?# Slaves are auto-discovered, so you don t need to specify slaves in

·?# any way. Sentinel itself will rewrite this configuration file adding

·?# the slaves using additional configuration options.

·?# Also note that the configuration file is rewritten when a

·?# slave is promoted to master.

?

Sentinel的配置

Sentinel当前最新的稳定版本称为Sentinel 2(与之前的Sentinel 1区分开来 。随着redis2.8的安装包一起发行。安装完Redis2.8后 可以在redis2.8/src/里面找到Redis-sentinel的启动程序。

配置文件 /app/zpy/redis/conf/sentinel.conf

二进制文件 /app/zpy/redis/bin/redis-sentinel

?

运行sentinel有两种方式

第一种 redis-sentinel /path/to/sentinel.conf

第二种 redis-server /path/to/sentinel.conf --sentinel

以上两种方式 都必须指定一个sentinel的配置文件sentinel.conf 如果不指定 将无法启动sentinel。

sentinel默认监听26379端口 所以运行前必须确定该端口没有被别的进程占用。

修改sentinel的配置文件

1.port 26379

2.sentinel monitor mymaster 10.0.140.87?6379 2

sentinel monitor master-name ip redis-port quorum

说明 Tells Sentinel to monitor this master, and to consider it in O_DOWN

(Objectively Down) state only if at least quorum sentinels agree.

Mymaster?其实你可以理解为一个别名 可以随便起 但是注意格式

# Note: master name should not include special characters or spaces.

# The valid charset is A-z 0-9 and the three characters .-_ .

如果你有两个sentinel 那么就可以改为1个

如果你有三个sentinel 那么就可以改为2个

尽可能使用奇数个 所以我们这里安装三个sentinel

?

?

3.?sentinel down-after-milliseconds mymaster 6000??默认30000

sentinel down-after-milliseconds master-name milliseconds

# Number of milliseconds (毫秒)0the master (or any attached slave or sentinel) should?be unreachable (as in, not acceptable reply to PING, continuously, for the?specified period) in order to consider it in S_DOWN state (Subjectively??Down)主观下线.

# Default is 30 seconds.

说明

subjective主观下线 如果一个sentinel认为redis-master?down了 那他就主观认为它down了 需要征求其他sentinel的意见

objecive客观下线 多个sentinel协商后做出判断 认为某节点下线

?

4.sentinel failover-timeout mymaster 80000? 默认180000

sentinel failover-timeout master-name milliseconds

当redis主出现问题 sentinel经过投票提升slave 180秒后如果不切换成功认为失败

?

?

5.?如果redis配置了密码

sentinel auth-pass mymaster redis

?

7.?sentinel parallel-syncs mymaster 1 并行同步

?

8.?其他添加项

daemonize yes ??#即默认以后台程序方式运行

protected-mode no ??# 非保护模式运行

logfile /data/zpy/redis/sentinel.log ?#修改生成默认日志文件位置

?

整理配置文件

egrep -v #|^$ sentinel.conf a.conf

port 26379

dir /tmp

daemonize yes

protected-mode no

logfile /data/zpy/redis/sentinel.log

sentinel monitor mymaster 10.0.140.84 6379 2

sentinel down-after-milliseconds mymaster 5000

sentinel failover-timeout mymaster 60000

sentinel auth-pass mymaster redis

?

启动sentinel

cd /app/zpy/redis/bin/

./redis-sentinel ../conf/sentinel.conf

Ss -tnl ?查看26379 端口

./redis-cli ?-h 10.0.150.43 -p 26379

Sentinel简单命令

10.0.140.84:26379 info sentinel

# Sentinel

sentinel_masters:1

sentinel_tilt:0

sentinel_running_scripts:0

sentinel_scripts_queue_length:0

sentinel_simulate_failure_flags:0

master0:name mymaster,status ok,address 10.0.140.84:6379,slaves 2,sentinels 3

?

?

10.0.140.84:26379 sentinel masters ?获取监控的所有masters信息

1) name

????2) mymaster

????3) ip

????4) 10.0.140.84

????5) port

????6) 6379

7) runid

?

10.0.140.84:26379 SENTINEL sentinels mymaster 显示这个主服务器所有的从节点

?

sentinel切换实验

杀掉redis-master?

再去看上述几条命令的返回结果

Redis-cluster

3.0以后开始支持cluster

分布式数据库 通过分片机制进行数据分布 clustering内的每个节点仅持有数据库的一部分数据

Redis cluster每一个节点都可以作为客户端的接入节点

每个节点只持有一部分数据 但每个节点都有全局视角

例子 ?比如有10000个key 一共5台redis节点 每台都可以被客户端接入 但是每台可能只存储2000个key 每台都有一个类似index索引的东西记录了这10000个key是如何分布的

?

我们来看一下官网的解释

Redis Cluster provides a way to run a Redis installation where data is?automatically sharded across multiple Redis nodes.

Redis Cluster also provides?some degree of availability during partitions, that is in practical terms the ability to continue the operations when some nodes fail or are not able to communicate. However the cluster stops to operate in the event of larger failures (for example when the majority of masters are unavailable).

So in practical terms, what you get with Redis Cluster?

·?The ability to?automatically split your dataset among multiple nodes.

·?The ability to?continue operations when a subset of the nodes are experiencing failures?or are unable to communicate with the rest of the cluster.

官网关于cluster?port的说明

Redis Cluster TCP ports

Every Redis Cluster node requires two TCP connections open. The normal Redis TCP port used to serve clients, for example 6379, plus the port obtained by adding 10000 to the data port, so 16379 in the example.

This second?high?port is used for the Cluster bus 集群总线 , that is a node-to-node communication channel using a binary protocol. The Cluster bus is used by nodes for?failure detection, configuration update, failover authorization?and so forth. Clients should never try to communicate?with the cluster bus port, but always with the normal Redis command port, however make sure you open both ports in your firewall, otherwise Redis cluster nodes will be not able to communicate.

The command port and cluster bus port offset is fixed and?is always 10000.

?

If you don t open both TCP ports, your cluster will not work as expected.

The cluster bus uses a different, binary protocol, for node to node data exchange, which is more suited to exchange information between nodes using little bandwidth and processing time.

?

?

Redis Cluster data sharding 分片 redis?cluster的数据分片机制

?

Redis Cluster does not use consistent 一致的 ?hashing, but a different form of sharding 分片 ?where every key is conceptually (概念性的)part of what we call an?hash slot.

翻译说明 Redis?cluster?不会使用常用的哈希算法 而是提出了分片机制 这种分片机制把一个key叫做一个hash?slot 哈希槽位

?

There are 16384 hash slots in Redis Cluster, and to compute what is the hash slot of a given key, we simply take the CRC16 of the key modulo 16384.

Every node in a Redis Cluster is responsible for a subset of the hash slots, so for example you may have a cluster with 3 nodes, where:

翻译说明

这里有16384个hash槽位在redis?cluster里 为了计算出一个key的hash槽位 我们使用CRC16校验机制

每一个节点都是16384的子集 例如

·?Node A contains hash slots from 0 to 5500.

·?Node B contains hash slots from 5501 to 11000.

·?Node C contains hash slots from 11001 to 16383.

This allows to add and remove nodes in the cluster easily. For example if I want to add a new node D, I need to move some hash slot from nodes A, B, C to D. Similarly 类似的 ?if I want to remove node A from the cluster I can just move the hash slots served by A to B and C. When the node A will be empty I can remove it from the cluster completely.

翻译说明 这样增加或者减少节点就变得容易了 例如如果我想增加一个D节点 我需要从node?A?B C 里面移除一部分hash?slot到D 类似的 如果我想移除A从集群中 我只需要把A的hash?slot?移动到B C .当A空了的时候 我就可以把它从集群移除了

?

Because moving hash slots from a node to another does not require to stop operations, adding and removing nodes, or changing the percentage of hash slots hold by nodes, does not require any downtime.

翻译说明

因为从一个节点移除hash?slot到另一个节点并不需要停机操作 增加或者移除节点和改变某一个节点拥有hash?slot的比例都不需要停机

?

?

Redis Cluster master-slave model

In order to remain available when a subset of master nodes are failing or are not able to communicate with the majority of nodes, Redis Cluster uses a master-slave model where every hash slot has from 1 (the master itself) to N replicas (N-1 additional slaves nodes).

翻译说明 为了保证少数redis节点宕机或者不能与多数节点通讯时整个cluster还可以正常使用 redis-cluster使用了master-slave模式 每一个hash?slot?具有N个副本 n-1个 slave?node

?

In our example cluster with nodes A, B, C, if node B fails the cluster is not able to continue, since we no longer have a way to serve hash slots in the range 5501-11000.

However when the cluster is created (or at a latter time) we add a slave node to every master, so that the final cluster is composed of A, B, C that are masters nodes, and A1, B1, C1 that are slaves nodes, the system is able to continue if node B fails.

Node B1 replicates B, and B fails, the cluster will promote node B1 as the new master and will continue to operate correctly.

However note that if nodes B and B1 fail at the same time Redis Cluster is not able to continue to operate.

翻译说明

在我们上面的实验里如果NODEB故障了 cluster就不能继续工作了 因为5501-1000这个范围的hash-slot就丢失了

但是当创建集群的时候我们为每个master-node添加一个slave A B C是master?node? A1 B1 C1 是slave?node? 如果B?挂掉了 仍然没有问题

Node?B1 复制B 如果B挂掉了 系统会提升B1为新的master

但是如果B和B1都挂掉了 那就不行了

?

Redis Cluster consistency guarantees

Redis集群一致性的保证

?

Redis Cluster is not able to guarantee strong consistency 一致性 .

In practical terms this means that under certain conditions it is possible that Redis Cluster will lose writes

that were acknowledged 承认 by the system to the client.

The first reason why Redis Cluster can lose writes is because it uses asynchronous 异步 replication.

This means that during writes the following happens:

Your client writes to the master B.

The master B replies 回复 OK to your client.

The master B propagates 传播 the write to its slaves B1, B2 and B3.

As you can see B does not wait for an acknowledge 确认 from B1, B2, B3 before replying to the client,

since this would be a prohibitive 禁止 latency 潜在 penalty 处罚 for Redis, so if your client writes something, B acknowledges the write,

but crashes 崩溃了 before being able to send the write to its slaves, one of the slaves (that did not receive the write) can be promoted to master, losing the write forever.

This is very similar to what happe

总结 这个是无法避免的 并且只会有很小概率发生

?

?

?

redis-cluster选举:容错

(1)领着选举过程是集群中所有master参与,如果半数以上master节点与故障节点通信超过(cluster-node-timeout),认为该节点故障 自动触发故障转移操作.

(2):什么时候整个集群不可用(cluster_state:fail)??

? ? a:如果集群任意master挂掉,且当前master没有slave.集群进入fail状态,也可以理解成集群的slot映射[0-16383]不完成时进入fail状态. ps : redis-3.0.0.rc1加入cluster-require-full-coverage参数,默认关闭,打开集群兼容部分失败.

? ? b:如果集群超过半数以上master挂掉 无论是否有slave集群进入fail状态.

? ps:当集群不可用时,所有对集群的操作做都不可用 收到((error) CLUSTERDOWN The cluster is down)错误

?

Redis Cluster configuration parameters

Cp?redis.conf ?redis-cluster.conf

1.?cluster-enabled?yes??

2.?cluster-config-file?nodes-6379.conf?# 建议以nodes-端口号的形式命名 方便辨识 ??

3.?cluster-node-timeout?15000??

4.?cluster-slave-validity-factor?10??

5.?cluster-migration-barrier?1??

6.?cluster-require-full-coverage?yes

?

?

cluster-enabled yes ???集群开关 默认是不开启集群模式。

cluster-config-file nodes-6379.conf ?集群配置文件的名称 每个节点都有一个集群相关的配置文件 持久化保存集群的信息。这个文件并不需要手动配置 这个配置文件有?Redis生成并更新 每个Redis集群节点需要一个单独的配置文件 请确保与实例运行的系统中配置文件名称不冲突。

cluster-node-timeout 15000 节点互连超时的阀值。集群节点超时毫秒数。即节点与集群其他节点断开多长时间将被认定为超时。建议稍微大一点

cluster-slave-validity-factor 10 在进行故障转移的时候 全部slave都会请求申请为master 但是有些slave可能与master断开连接一段时间了 导致数据过于陈旧 这样的slave不应该被提升为master。该参数就是用来判断slave节点与master断线的时间是否过长。判断方法是 比较slave断开连接的时间和(node-timeout * slave-validity-factor) repl-ping-slave-period如果节点超时时间为三十秒, 并且slave-validity-factor为10,假设默认的repl-ping-slave-period是10秒 即如果超过310秒slave将不会尝试进行故障转移

cluster-migration-barrier 1 master的slave数量大于该值 slave才能迁移到其他孤立master上 如这个参数若被设为2 那么只有当一个主节点拥有2个可工作的从节点时 它的一个从节点才会尝试迁移。

cluster-require-full-coverage yes 默认情况下 集群全部的slot有节点负责 集群状态才为ok 才能提供服务。设置为no 可以在slot没有全部分配的时候提供服务。不建议打开该配置 这样会造成分区的时候 小分区的master一直在接受写请求 而造成很长时间数据不一致。

?

?

?

Yum install ruby ?→?启动集群的时候需要ruby

yum?install?rubygems?

gem source -l 查看当前ruby源

gem sources --remove https://rubygems.org/

gem sources --remove http://rubygems.org/

gem sources -a https://ruby.taobao.org/

gem install redis --version 3.2.0

?

redis-trib 位于 Redis 源码的 src 文件夹中 它是一个 Ruby 程序 这个程序通过向实例发送特殊命令来完成创建新集群 检查集群 或者对集群进行重新分片 reshared 等工作。这里通过create命令来创建集群 指定replicas 1 即每一个主实例有一个从实例。redis-trib 会打印出一份预想中的配置给你看 如果你觉得没问题的话 就可以输入 yes redis-trib 就会将这份配置应用到集群当中,让各个节点开始互相通讯

?

启动redis-cluster

./redis-server ../conf/redis-cluster.conf

root ?????4379 ????1 ?0 15:51 ? ???????00:00:00 ./redis-server 10.0.140.84:6379 [cluster]

?

./redis-trib.rb create --replicas 1 10.0.140.84:6379 10.0.140.85:6379 10.0.140.86:6379 10.0.140.87:6379 10.0.140.88:6379 10.0.140.89:6379

[root localhost src]# ./redis-trib.rb create --replicas 1 10.0.140.84:6379 10.0.140.85:6379 10.0.140.86:6379 10.0.140.87:6379 10.0.140.88:6379 10.0.140.89:6379

Creating cluster

Performing hash slots allocation on 6 nodes...

Using 3 masters:

10.0.140.89:6379

10.0.140.88:6379

10.0.140.87:6379

Adding replica 10.0.140.86:6379 to 10.0.140.89:6379

Adding replica 10.0.140.85:6379 to 10.0.140.88:6379

Adding replica 10.0.140.84:6379 to 10.0.140.87:6379

S: 5200ed3e64ea63b354e2f54ed48c48b174430862 10.0.140.84:6379

???replicates 32736fccc69843f94a9a9338cf361873e8822bd2

S: 487b4f58f7eceaaeaef37f15812896cc7bd36123 10.0.140.85:6379

???replicates 496670b6782ed55944516242523123aa21884999

S: e3b71ae77c71d1169212dac7abcbf58ba8bf911d 10.0.140.86:6379

???replicates 182114a1fd4018cd5b3096c32f08e8fea4495201

M: 32736fccc69843f94a9a9338cf361873e8822bd2 10.0.140.87:6379

???slots:5287,9511,10439,10923-16383 (5464 slots) master

M: 496670b6782ed55944516242523123aa21884999 10.0.140.88:6379

???slots:5287,5461-10922,15929 (5464 slots) master

M: 182114a1fd4018cd5b3096c32f08e8fea4495201 10.0.140.89:6379

???slots:0-5460,9511,10439,15929 (5464 slots) master

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 10.0.140.84:6379)

M: cf63a791c8f94a2be4c851ba9b61c51b899c74e6 10.0.140.84:6379

???slots: (0 slots) master

???replicates 2053b9f7a15d58afb3aa4de8ad0fd702344c6314

M: cd5bcb50bb93d7a42a1bc14be2dce85d6618309f 10.0.140.85:6379

???slots: (0 slots) master

???replicates 44c036a03fa207d98e1c1eb65c8aae17da44d642

M: c585237f96a0ebfde4f5e184561e9e32c6f6c37a 10.0.140.86:6379

???slots: (0 slots) master

???replicates 2b0cebfb35a51e3d38005a9545b15fa9c8d34d5f

M: 2053b9f7a15d58afb3aa4de8ad0fd702344c6314 10.0.140.87:6379

???slots:10923-16383 (5461 slots) master

M: 44c036a03fa207d98e1c1eb65c8aae17da44d642 10.0.140.88:6379

???slots:5461-10922 (5462 slots) master

M: 2b0cebfb35a51e3d38005a9545b15fa9c8d34d5f 10.0.140.89:6379

???slots:0-5460 (5461 slots) master

[OK] All nodes agree about slots configuration.

Check for open slots...

Check slots coverage...

[OK] All 16384 slots covered.

?

10.0.140.84:6379 set dailiang 333

- Redirected to slot [15929] located at 10.0.140.87:6379

OK

10.0.140.87:6379 get dailiang

333

10.0.140.87:6379


?

为节点分配slot redis-trib.rb?reshard?192.168.72.100:7006??

?

?


本文转自网络,原文链接:https://developer.aliyun.com/article/785708
本站部分内容转载于网络,版权归原作者所有,转载之目的在于传播更多优秀技术内容,如有侵权请联系QQ/微信:153890879删除,谢谢!

推荐图文

  • 周排行
  • 月排行
  • 总排行

随机推荐