1、安装Redis
下载自己需要的版本(我这里以5.0.10为例)
1、上传 redis-5.0.10.tar.gz
到linux虚拟机的 /opt
文件夹
[root@redis1 opt]# ls
redis-5.0.10.tar.gz rh
2、安装gcc
[root@localhost ~]# yum install -y gcc
3、解压缩
[root@localhost opt]# tar xzvf redis-5.0.10.tar.gz
4、进入到redis根目录,进行编译、安装
[root@localhost opt]# cd redis-5.0.10
[root@localhost redis-5.0.10]# make
[root@localhost redis-5.0.10]# make install
5、将 redis-5.0.10/redis.conf
复制到 /etc/redis/
目录下
[root@localhost redis-5.0.10]# mkdir -p /etc/redis/
[root@localhost redis-5.0.10]# cp redis.conf /etc/redis/
6、启动redis:通过redis-server启动redis
[root@localhost redis-5.0.10]# redis-server /etc/redis/redis.conf
6255:M 08 Jun 00:19:28.368 * Increased maximum number of open files to 10032 (it was originally set to 1024).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 5.0.10 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 6255
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
说明:redis默认监听端口6379。
2、Redis的主从复制
主从复制引言:
实际生产环境下,单机的redis服务器是无法满足实际的生产需求的
针对这些问题,redis提供了复制(replication)的功能,通过"主从(一主多从)"和"集群(多主多从)"的方式对redis的服务进行水平扩展,用多台redis服务器共同构建一个高可用的redis服务系统
搭建步骤:
1、克隆2台Redis机器,并编辑redis.conf
[root@redis1 ~]# vim /etc/redis/redis.conf
2台机器都要编辑绑定ip地址(所有机器)
bind 0.0.0.0
2、主机正常启动,并关闭防火墙
systemctl stop firewalld
redis-server /etc/redis/redis.conf
3、配置从机,编辑redis.conf
在 port 6379 后添加如下配置
slaveof 主机ip redis端口号,示例如下:
slaveof 192.168.37.151 6379
4、启动从机
redis-server /etc/redis/redis.conf
5、查看主从信息
[root@redis1 ~]# redis-cli -h 192.168.37.151 -p 6379
192.168.37.151:6379> info replication
# Replication
role:master (担任角色master)
connected_slaves:1 (从机的数量)
slave0:ip=192.168.37.152,port=6379,state=online,offset=28,lag=1(从机的信息)
master_replid:089677f59ebc7f759ba1955044631211d964723f
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:28
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:28
3、Redis的哨兵模式
哨兵模式引言:
主从复制解决了数据备份的问题,但是如果主节点宕机,仍需要运维手动进行主从切换。要在主从复制下实现故障恢复的自动化,就需要使用Redis的哨兵(Sentinel)模式
哨兵是一个独立于数据服务器的进程,用于监控redis数据服务器的状态,当主从模式下最关键的主服务器出现故障时,能够被哨兵自动的察觉。同时哨兵会在剩余的从服务器中"选举"出新的主服务器,达到自动化恢复系统服务的目的。
哨兵模式搭建:
配置3个Redis(1主2从),3个哨兵
1、配置主从复制
主机配置redis.conf:
bind 127.0.0.1
修改为:
bind 机器ip
2、从机1配置redis.conf:
bind 127.0.0.1 修改为:bind 0.0.0.0
并添加配置:slaveof 主节点ip 端口号
示例:slaveof 192.168.37.151 6379
3、从机2配置redis.conf:(我们上面已经配置好一个一主一从,现在只需要再添加一个从机即可)
bind 127.0.0.1 修改为:bind 0.0.0.0
并添加配置:slaveof 主节点ip 端口号
示例:slaveof 192.168.37.151 6379
重复执行redis-server /etc/redis/redis.conf 启动3个redis实例。
4、查看主从信息
[root@redis1 ~]# redis-cli -h 192.168.37.151 -p 6379
192.168.37.151:6379> info replication
# Replication
role:master
connected_slaves:2 (两个从机)
slave0:ip=192.168.37.152,port=6379,state=online,offset=1680,lag=0(从机1的信息)
slave1:ip=192.168.37.153,port=6379,state=online,offset=1680,lag=0(从机2的信息)
master_replid:089677f59ebc7f759ba1955044631211d964723f
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1680
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:1680
配置哨兵:
哨兵1配置sentinel.conf:
添加bind 当前机器ip
将 sentinel monitor mymaster 127.0.0.1 6379 2
修改为 sentinel monitor mymaster 主机IP192.168.37.151 6379 2
哨兵2配置sentinel.conf:
添加bind 当前机器ip
将 sentinel monitor mymaster 127.0.0.1 6379 2
修改为 sentinel monitor mymaster 主机IP192.168.37.151 6379 2
哨兵3配置sentinel.conf:
添加bind 当前机器ip
将 sentinel monitor mymaster 127.0.0.1 6379 2
修改为 sentinel monitor mymaster 主机IP192.168.37.151 6379 2
重复执行redis-sentinel /opt/redis-5.0.10/sentinel.conf 启动3个哨兵。
redis-cli -h 哨兵ip -p 26379
比如: redis-cli -h 192.168.37.151 -p 26379
执行info命令
# 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.37.151:6379,slaves=2,sentinels=3
(最下边有这条信息即成功)
意思是:主节点:地址为 192.168.37.151,端口为 6379。
从节点:共有 2 个从节点。
Sentinel 节点:共有 3 个 Sentinel 节点。
关掉主节点,然后过几秒刷新info,可以看到选举后的新的主节点信息