Redis安装部署

一、来张图片镇一下文章

在这里插入图片描述
http://www.redis.cn/------->中文站点
http://doc.redisfans.com/--------->文档参考。

sentinel测试参考文章:https://blog.csdn.net/qq_33285112/article/details/78770833
redis 哨兵模式详解文章:https://www.cnblogs.com/kevingrace/p/9004460.html

  • 首先我们来了解一下redis的作者,他叫Salvatore Sanfilippo,来自意大利的西西里岛,现在居住在卡塔尼亚。目前任职于Pivotal公司。他使用的网名是antirez,如果你有兴趣,可以去他的博客逛逛,地址是 antirez.com,当然也可以去逛一下他的github,地址是http://github.com/antirez

  • Redis 是一个开源(BSD许可)的,内存中的数据结构存储系统,该数据库使用ANSI C语言编写,它可以用作数据库、缓存和消息中间件。 它支持多种类型的数据结构,如 字符串(strings), 散列(hashes), 列表(lists), 集合(sets), 有序集合(sorted sets) 与范围查询, bitmaps, hyperloglogs 和 地理空间(geospatial) 索引半径查询。

  • Redis 内置了 复制(replication),LUA脚本(Lua scripting), LRU驱动事件(LRU eviction),事务(transactions) 和不同级别的 磁盘持久化(persistence), 并通过 Redis哨兵(Sentinel)和自动 分区(Cluster)提供高可用性(high availability)。

二、Redis安装与启动

1.安装规划
在这里插入图片描述
2.节点规划

集群节点IP地址Redis端口Sentinel端口
redis-m192.168.142.1291637926379
redis-s192.168.142.1302637936379

最后成功配置启动的结果如下:
在这里插入图片描述

3.目录规划

目录规划备注
程序安装目录/usr/local/redis-5.0.0 /Redis版本为redis-5.0.0
Redis Server实例目录/usr/local/shaun/redis-16379新建子目录conf、logs、run,用于存放redis server实例的配置文件、日志文件、pid文件等
RedisSentinel实例目录/usr/local/shaun/sentinel-26379新建子目录conf、logs,用于存放sentinel实例的配置文件和日志文件
Redis数据文件存储目录/data/redis-data

4.安装Redis软件
1)先安装Ruby环境

[root@redis-m ~]# yum install ruby tcl gcc gcc-c++ make -y 
[root@redis-m ~]# curl -L get.rvm.io | bash -s stable
[root@redis-m ~]# source /usr/local/rvm/scripts/rvm
[root@redis-m ~]# rvm install 2.3.8
[root@redis-m ~]# gem install redis

这里可能会折腾比较久,我当时就是弄了好久。
报以下错误:

Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
gcc-c++-4.8.5-36.el7.x86_64.rp FAILED                                          
http://mirror.centos.org/centos-7/7.6.1810/os/x86_64/Packages/gcc-c%2B%2B-4.8.5-36.el7.x86_64.rpm: [Errno 14] curl#6 - "Could not resolve host: mirror.centos.org; Unknown error"
Trying other mirror.
Error downloading packages:
  gcc-c++-4.8.5-36.el7.x86_64: [Errno 256] No more mirrors to try.

需要安装以下:
[root@redis-m ~]# yum provides ‘*/applydeltarpm’
[root@redis-m ~]# yum install deltarpm -y
如果这条命令 curl -L get.rvm.io | bash -s stable执行不了,可以参考这个看看:https://ruby-china.org/topics/576
ruby实用指南

2)下载和解压安装包

tar -zxvf redis-5.0.0.tar.gz -C /usr/local/shaun/
cd redis-5.0.0
make # 编译出错的话,make clean ,或者重新解压编译
make test # 不是必须
make install

三、配置Redis Server

1)创建目录和文件

mkdir -p /usr/local/shaun/redis-16379
cd /usr/local/shaun/redis-16379
mkdir conf logs run
mkdir -p /data/redis-data

2)拷贝一份redis.conf配置文件到conf目录下:

[root@redis-m ~]# cp /usr/local/shaun/redis-5.0.0/redis.conf /usr/local/shaun/redis-16379/conf/redis-16379.conf

3)修改主节点配置
修改主节点上的/usr/local/shaun/redis-16379/conf/redis-16379.conf文件:

# By default Redis does not run as a daemon. Use 'yes' if you need it.
daemonize yes

# When running daemonized, Redis writes a pid file in this location.
pidfile "/usr/local/shaun/redis-16379/run/redis-16379.pid"

# Accept connections on the specified port, default is 6379.
port 16379

# Specify the network interface that the service will listen on.
bind 192.168.142.129

# Specify the log file name.
logfile "/usr/local/shaun/redis-16379/logs/redis-16379.log"

# Set the number of databases.
databases 64------>默认为16不够用。

# The filename where to dump the DB
dbfilename "dump.rdb"

# The working directory. The DB file will be written inside this directory.
dir "/data/redis-data"

# Set the max number of connected clients at the same time.
maxclients 60000
protected-mode no

#若要加redis认证功能,需要加上以下配置
requirepass  redis (redis的密码)
masterauth  redis (因为redis-sentinel提供了高可用,redis会互为主从,所以两添加此项)

4)修改从节点配置
修改从节点上的/usr/local/shaun/redis-26379/conf/redis-26379.conf文件,文件内容与主节点基本一致:

# By default Redis does not run as a daemon. Use 'yes' if you need it.
daemonize yes

# When running daemonized, Redis writes a pid file in this location.
pidfile /usr/local/shaun/redis-26379/run/redis-26379.pid

# Accept connections on the specified port, default is 6379.
port 26379

# Specify the network interface that the service will listen on.
bind 192.168.142.130

# Specify the log file name.
logfile "/usr/local/shaun/redis-26379/logs/redis-26379.log"

# Set the number of databases.
databases 64

# The filename where to dump the DB
dbfilename "dump.rdb"

# The working directory. The DB file will be written inside this directory.
dir "/data/redis-data"

# Set the max number of connected clients at the same time.
maxclients 60000

# Use slaveof to make a Redis instance a copy of another Redis server. **important**
slaveof 192.168.142.129 16379

protected-mode no
#若要加redis认证功能,需要加上以下配置
requirepass  redis (redis的密码)
masterauth  redis  (因为redis-sentinel提供了高可用,redis会互为主从,所以两添加此项)

5.启动Redis Server

[root@redis-m ~]# redis-server /usr/local/shaun/redis-16379/conf/redis-16379.conf

6.停止Redis server

[root@redis-m /usr/local/shaun/redis-16379]# redis-cli -h 192.168.142.129 -a redis -p 16379 shutdown

四、配置Redis Sentinel

1.创建目录和文件
按规划创建redis sentinel实例相关目录:

mkdir -p /usr/local/shaun/sentinel-16379
cd /usr/local/shaun/sentinel-16379
mkdir conf logs

2.拷贝一份sentinel.conf配置文件到conf目录下:

cp /usr/local/shaun/redis-5.0.0 /sentinel.conf /usr/local/shuan/sentinel-16379/conf

3.修改主节点配置
修改主节点上的/usr/local/shaun/sentinel-16379/conf/sentinel.conf文件:

# The port that this sentinel instance will run on
port 26379

# 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.
sentinel monitor mymaster 192.168.142.129 16379 1

# bind ip address
bind 192.168.142.129
logfile "/usr/local/jiaxin/sentinel-16379/logs/sentinel1.log"
daemonize yes  (认证功能里sentinel守护进程运行方式提供高可用)

sentinel down-after-milliseconds mymaster 5000 (指定了Sentinel认为Redis实例已经失效所需的毫秒数。当实例超过该时间没有返回PING,或者直接返回错误,那么Sentinel将这个实例标记为主观下线。)

sentinel failover-timeout mymaster 18000    (如果在该时间(ms)内未能完成failover操作,则认为该failover失败)

#如redis要加用户认证功能,加以下配置
sentinel auth-pass mymaster redis (redis认证sentinel的密码)
sentinel config-epoch mymaster 0
sentinel leader-epoch mymaster 0

4.修改从节点配置
修改从节点上的/usr/local/shaun/sentinel-26379/conf/sentinel.conf文件:

# The port that this sentinel instance will run on
port 36379

# 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.
sentinel monitor mymaster 192.168.142.129 16379 1

# bind ip address
bind 192.168.142.130
logfile "/usr/local/jiaxin/sentinel-26379/logs/sentinel2.log"
daemonize yes  (认证功能里sentinel守护进程运行方式提供高可用)

sentinel down-after-milliseconds mymaster 5000 (指定了Sentinel认为Redis实例已经失效所需的毫秒数。当实例超过该时间没有返回PING,或者直接返回错误,那么Sentinel将这个实例标记为主观下线。)

sentinel failover-timeout mymaster 18000    (如果在该时间(ms)内未能完成failover操作,则认为该failover失败)

#如redis要加用户认证功能,加以下配置
sentinel auth-pass mymaster redis (redis认证sentinel的密码)
sentinel config-epoch mymaster 0
sentinel leader-epoch mymaster 0

如需了解redis-sentinel的配置文件详情,可以看看这个文章:
https://www.cnblogs.com/xuliangxing/p/7149322.html

5.启动Redis Sentinel
redis-sentinel /usr/local/shaun/sentinel-26379/conf/sentinel.conf
6.停止Redis Sentinel
kill -9 [sentinelpid]

五、主从复制验证

1.主节点测试

# 通过redis-cli访问主节点redis server
[root@redis-m ~]# redis-cli -h 192.168.142.129 -p 16379
192.168.142.129:16379> auth redis
OK
192.168.142.129:16379> set foo bar
OK
192.168.142.129:16379> get foo
"bar"
192.168.142.129:16379> exit
# 通过redis-cli访问从节点redis server
[root@redis-m ~]# redis-cli -h 192.168.142.130 -p 26379 
192.168.142.130:26379> auth redis
OK
192.168.142.130:26379> get foo
"bar"
192.168.142.130:26379> exit

2.从节点测试

[root@redis-s ~]# redis-cli -h 192.168.142.130 -p 26379 
192.168.142.130:26379> auth redis
OK
192.168.142.130:26379> set foo newbar
(error) READONLY You can't write against a read only slave.
192.168.142.130:26379> get foo
"bar"
192.168.142.130:26379> exit

结论:符合预期,主节点可读可写,从节点为只读

六、主从切换验证

1.在master上查看sentinel相关的信息。如下

[root@redis-m ~]# redis-cli -h 192.168.142.129 -p 26379 
192.168.142.129: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=192.168.142.129:16379,slaves=1,sentinels=2
192.168.142.129:26379> SENTINEL masters
1)  1) "name"
    2) "mymaster"
    3) "ip"
    4) "192.168.142.129"
    5) "port"
    6) "16379"
    7) "runid"
    8) "31b8a13c72cdb762555ae264d60ba23435231e45"
    9) "flags"
   10) "master"
   11) "link-pending-commands"
   12) "0"
   13) "link-refcount"
   14) "1"
   15) "last-ping-sent"
   16) "0"
   17) "last-ok-ping-reply"
   18) "105"
   19) "last-ping-reply"
   20) "105"
   21) "down-after-milliseconds"
   22) "5000"
   23) "info-refresh"
   24) "9873"
   25) "role-reported"
   26) "master"
   27) "role-reported-time"
   28) "271126"
   29) "config-epoch"
   30) "0"
   31) "num-slaves"
   32) "1"
   33) "num-other-sentinels"
   34) "1"
   35) "quorum"
   36) "1"
   37) "failover-timeout"
   38) "18000"
   39) "parallel-syncs"
   40) "1"
192.168.142.129:26379> SENTINEL get-master-addr-by-name mymaster
   1) "192.168.142.129"
   2) "16379"
192.168.142.129:26379> exit

2.在slave上查看相关的sentinel信息。

#查看sentinel的信息。
[root@redis-s ~]# redis-cli -h 192.168.142.130 -p 36379 
192.168.142.130:36379> SENTINEL masters
1)  1) "name"
    2) "mymaster"
    3) "ip"
    4) "192.168.142.129"
    5) "port"
    6) "26379"
    7) "runid"
    8) "a4dd0d76ba133cb32094eeeb100e56cb5c022808"
    9) "flags"
   10) "master"
   11) "link-pending-commands"
   12) "0"
   13) "link-refcount"
   14) "1"
   15) "last-ping-sent"
   16) "0"
   17) "last-ok-ping-reply"
   18) "983"
   19) "last-ping-reply"
   20) "983"
   21) "down-after-milliseconds"
   22) "5000"
   23) "info-refresh"
   24) "3010"
   25) "role-reported"
   26) "slave"
   27) "role-reported-time"
   28) "741443"
   29) "config-epoch"
   30) "0"
   31) "num-slaves"
   32) "0"
   33) "num-other-sentinels"
   34) "1"
   35) "quorum"
   36) "1"
   37) "failover-timeout"
   38) "18000"
   39) "parallel-syncs"
   40) "1"
192.168.142.130:36379> SENTINEL get-master-addr-by-name mymaster
1) "192.168.142.129"
2) "26379" 
192.168.142.130:36379> exit

3.模拟主机down掉,kill掉它。

##在主上面登录sentinel查看。
[root@redis-m ~]# redis-cli -h 192.168.142.129 -p 26379 
192.168.142.129:26379> SENTINEL slaves mymaster
1)  1) "name"
    2) "192.168.142.129:16379"
    3) "ip"
    4) "192.168.142.129"
    5) "port"
    6) "16379"
    7) "runid"
    8) ""
    9) "flags"
   10) "s_down,slave,disconnected"
   11) "link-pending-commands"
   12) "2"
   13) "link-refcount"
   14) "1"
   15) "last-ping-sent"
   16) "71646"
   17) "last-ok-ping-reply"
   18) "71646"
   19) "last-ping-reply"
   20) "71646"
   21) "s-down-time"
   22) "66629"
   23) "down-after-milliseconds"
   24) "5000"
   25) "info-refresh"
   26) "1564122037878"
   27) "role-reported"
   28) "slave"
   29) "role-reported-time"
   30) "71646"
   31) "master-link-down-time"
   32) "0"
   33) "master-link-status"
   34) "err"
   35) "master-host"
   36) "?"
   37) "master-port"
   38) "0"
   39) "slave-priority"
   40) "100"
   41) "slave-repl-offset"
   42) "0"
192.168.142.129: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=192.168.142.130:26379,slaves=1,sentinels=2
192.168.142.129:26379> exit

查看日志:

[root@redis-m /usr/local/shaun/sentinel-16379]# cat logs/sentinel1.log
8375:X 26 Jul 14:19:25.040 # +new-epoch 23
8375:X 26 Jul 14:19:25.041 # +vote-for-leader 8e8cabfec9e74abf23eea64fccd4dba3772035a7 23
8375:X 26 Jul 14:19:25.133 # +sdown master mymaster 192.168.142.129 16379
8375:X 26 Jul 14:19:25.133 # +odown master mymaster 192.168.142.129 16379 #quorum 1/1
8375:X 26 Jul 14:19:25.134 # Next failover delay: I will not start a failover before Fri Jul 26 14:20:01 2019
8375:X 26 Jul 14:19:26.231 # +config-update-from sentinel 8e8cabfec9e74abf23eea64fccd4dba3772035a7 192.168.142.130 36379 @ mymaster 192.168.142.129 16379
8375:X 26 Jul 14:19:26.232 # +switch-master mymaster 192.168.142.129 16379 192.168.142.130 26379
8375:X 26 Jul 14:19:26.232 * +slave slave 192.168.142.129:16379 192.168.142.129 16379 @ mymaster 192.168.142.130 26379
8375:X 26 Jul 14:19:31.249 # +sdown slave 192.168.142.129:16379 192.168.142.129 16379 @ mymaster 192.168.142.130 26379
8375:X 26 Jul 14:23:00.613 # -sdown slave 192.168.142.129:16379 192.168.142.129 16379 @ mymaster 192.168.142.130 26379
8375:X 26 Jul 14:23:10.580 * +convert-to-slave slave 192.168.142.129:16379 192.168.142.129 16379 @ mymaster 192.168.142.130 26379

查看从上面的;

#下面是之前查看的master的address。
[root@redis-s ~]# redis-cli -h 192.168.142.130 -p 36379 
192.168.142.130:36379> SENTINEL get-master-addr-by-name mymaster
1) "192.168.142.129"
2) "16379"

#后面kill掉主后查看的master address。
192.168.142.130:36379> SENTINEL get-master-addr-by-name mymaster
1) "192.168.142.130"
2) "26379"

192.168.142.130:36379> 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=192.168.142.130:26379,slaves=1,sentinels=2
192.168.142.130:36379> exit

查看sentinel的日志。(请忽略时间,因为我的机子没有做时间同步,所以主从时间出现错乱)

12733:X 26 Jul 12:34:49.118 # +new-epoch 23
12733:X 26 Jul 12:34:49.118 # +try-failover master mymaster 192.168.142.129 16379
12733:X 26 Jul 12:34:49.121 # +vote-for-leader 8e8cabfec9e74abf23eea64fccd4dba3772035a7 23
12733:X 26 Jul 12:34:49.124 # be3568f31bd9d146d88620f457c6098df53e3ba4 voted for 8e8cabfec9e74abf23eea64fccd4dba3772035a7 23
12733:X 26 Jul 12:34:49.177 # +elected-leader master mymaster 192.168.142.129 16379
12733:X 26 Jul 12:34:49.177 # +failover-state-select-slave master mymaster 192.168.142.129 16379
12733:X 26 Jul 12:34:49.244 # +selected-slave slave 192.168.142.130:26379 192.168.142.130 26379 @ mymaster 192.168.142.129 16379
12733:X 26 Jul 12:34:49.244 * +failover-state-send-slaveof-noone slave 192.168.142.130:26379 192.168.142.130 26379 @ mymaster 192.168.142.129 16379
12733:X 26 Jul 12:34:49.316 * +failover-state-wait-promotion slave 192.168.142.130:26379 192.168.142.130 26379 @ mymaster 192.168.142.129 16379
12733:X 26 Jul 12:34:50.255 # +promoted-slave slave 192.168.142.130:26379 192.168.142.130 26379 @ mymaster 192.168.142.129 16379
12733:X 26 Jul 12:34:50.256 # +failover-state-reconf-slaves master mymaster 192.168.142.129 16379
12733:X 26 Jul 12:34:50.309 # +failover-end master mymaster 192.168.142.129 16379
12733:X 26 Jul 12:34:50.309 # +switch-master mymaster 192.168.142.129 16379 192.168.142.130 26379
12733:X 26 Jul 12:34:50.310 * +slave slave 192.168.142.129:16379 192.168.142.129 16379 @ mymaster 192.168.142.130 26379
12733:X 26 Jul 12:34:55.318 # +sdown slave 192.168.142.129:16379 192.168.142.129 16379 @ mymaster 192.168.142.130 26379
12733:X 26 Jul 12:38:25.182 # -sdown slave 192.168.142.129:16379 192.168.142.129 16379 @ mymaster 192.168.142.130 26379

本期的分享到此结束,谢谢大家!有什么问题可以一起探讨一下。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值