Redis下载地址:https://redis.io/download
一、单机安装
1.1 下载
wget https://download.redis.io/releases/redis-5.0.10.tar.gz
1.2 解压
tar -zxvf redis-5.0.10.tar.gz
1.3 编译Redis
cd redis-5.0.10
yum install gcc
make
如果执行make时发现如下错误
[root@localhost redis-5.0.10]# make
cd src && make all
make[1]: 进入目录“/opt/software/redis-5.0.10/src”
CC adlist.o
In file included from adlist.c:34:0:
zmalloc.h:50:31: 致命错误:jemalloc/jemalloc.h:没有那个文件或目录
#include <jemalloc/jemalloc.h>
^
编译中断。
make[1]: *** [adlist.o] 错误 1
make[1]: 离开目录“/opt/software/redis-5.0.10/src”
make: *** [all] 错误 2
将make命令改为如下命令
make MALLOC=libc
1.3 修改配置文件
将redis.conf文件中的如下配置修改成yes
daemonize yes
1.4 启动redis
[root@localhost redis-5.0.10]# src/redis-server redis.conf
2863:C 03 Nov 2020 10:37:40.106 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
2863:C 03 Nov 2020 10:37:40.106 # Redis version=5.0.10, bits=64, commit=00000000, modified=0, pid=2863, just started
2863:C 03 Nov 2020 10:37:40.106 # Configuration loade
1.5 查看redis是否启动
[root@localhost redis-5.0.10]# ps -ef|grep redis
root 2864 1 0 10:37 ? 00:00:00 src/redis-server 127.0.0.1:6379
root 2895 1443 0 10:38 pts/0 00:00:00 grep --color=auto redis
1.6 redis-cli测试
[root@localhost redis-5.0.10]# src/redis-cli
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> set k1 v1
OK
127.0.0.1:6379> get k1
"v1"
至此,Redis单机安装完毕
二、主从搭建
2.1 复制redis.conf到master目录以及slave目录
2.2 修改salve目录中的配置文件
主要修改如下的几个配置
port 6380 # 修改端口
pidfile /var/run/redis_6380.pid # 修改pid的文件命
logfile "/opt/redis-replica/slave/6380.log"
dir /opt/software/redis-replica/slave # 指定数据存放目录
3、配置主从复制
replicaof 192.168.137.134 6379 # 从本机6379的redis实例复制数据,Redis 5.0之前使用slaveof
replica-read-only yes # 配置从节点只读
2.3 启动主从
[root@localhost redis-replica]# /opt/software/redis-5.0.10/src/redis-server master/redis.conf
3681:C 03 Nov 2020 10:53:53.894 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
3681:C 03 Nov 2020 10:53:53.894 # Redis version=5.0.10, bits=64, commit=00000000, modified=0, pid=3681, just started
3681:C 03 Nov 2020 10:53:53.894 # Configuration loaded
[root@localhost redis-replica]# /opt/software/redis-5.0.10/src/redis-server master/redis.conf
3703:C 03 Nov 2020 10:54:12.227 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
3703:C 03 Nov 2020 10:54:12.227 # Redis version=5.0.10, bits=64, commit=00000000, modified=0, pid=3703, just started
3703:C 03 Nov 2020 10:54:12.227 # Configuration loaded
[root@localhost redis-replica]# ps -ef|grep redis
root 3704 1 0 10:54 ? 00:00:00 /opt/software/redis-5.0.10/src/redis-server 127.0.0.1:6379
root 3717 1443 0 10:54 pts/0 00:00:00 grep --color=auto redis
[root@localhost redis-replica]# /opt/software/redis-5.0.10/src/redis-server slave/redis.conf
[root@localhost redis-replica]# ps -ef|grep redis
root 3704 1 0 10:54 ? 00:00:00 /opt/software/redis-5.0.10/src/redis-server 127.0.0.1:6379
root 3761 1 0 10:55 ? 00:00:00 /opt/software/redis-5.0.10/src/redis-server 127.0.0.1:6380
root 3778 1443 0 10:55 pts/0 00:00:00 grep --color=auto redis
2.4 测试主从
连接两个不同的redis实例
/opt/software/redis-5.0.10/src/redis-cli -p 6379
/opt/software/redis-5.0.10/src/redis-cli -p 6380
在主节点上,进行数据的操作
127.0.0.1:6379>
127.0.0.1:6379> set k1 v1
OK
127.0.0.1:6379> set k2 v2
OK
127.0.0.1:6379> set k3 v3
OK
127.0.0.1:6379> keys *
1) "k3"
2) "k2"
3) "k1"
127.0.0.1:6379>
在从节点查看数据
# 主节点操作数据之前,数据是空的
127.0.0.1:6380> keys *
(empty list or set)
# 主节点操作之后,同步到了主节点的数据
127.0.0.1:6380> keys *
1) "k3"
2) "k1"
3) "k2"
127.0.0.1:6380>
到这,Redis的主从也搭好了
三、Redis哨兵模式搭建
哨兵模式要解决的问题:
在上述的主从架构中,假设有一天Master机器挂了,因为客户端是直接连向了Master机器,所以当master挂了之后,客户端将会直接操作失败,此时需要手动的切换,将一台slave提升为master,其他slave复制新master的数据。会相对比较麻烦。所以衍生出了哨兵模式的架构。
在哨兵架构中,客户端连接的是哨兵集群,获取主节点的信息,后续直接访问主节点进行操作。当主节点发生变化时,哨兵节点将会第一时间感受到,并将新的主节点返回给客户端。实现主从的自动切换。
具体流程大致如下
- 客户端连接哨兵节点
- 哨兵节点返回一个主节点给客户端
- 客户端通过返回的主节点信息,连接主节点,进行操作
- 当主节点发生变化时,如宕机,哨兵节点将会立即感受到,从当前的节点中,选举出一个新的主节点,推送给客户端
- 客户端接收到新的主节点信息,开始连接新的主节点,进行操作。
3.1 复制sentinel配置文件,并修改
(通过修改不同的端口以及其他信息,可以启动一个sentinel集群)cp sentinel.conf yourDir/sentinel.conf
# 修改如下的配置
port 26379
daemonize yes
pidfile "/var/run/redis-sentinel-26379.pid"
logfile "26379.log"
dir "/usr/local/redis-5.0.3/data"
# sentinel monitor <master-redis-name> <master-redis-ip> <master-redis-port> <quorum>
# quorum是一个数字,指明当有多少个sentinel认为一个master失效时(值一般为:sentinel总数/2 + 1),master才算真正失效
sentinel monitor mymaster 127.0.0.1 6379 2
3.2 启动redis-sentinel
启动完成之后,可以cat一下sentinel.conf,可以看到已经将节点的信息,写入了sentinel.conf中(这里仅配置了一点节点示意)src/redis-sentinel sentinel.conf
通过客户端连接,来查看 ```shell 127.0.0.1:26379> infoprotected-mode no
sentinel known-replica mymaster 127.0.0.1 6380
sentinel current-epoch 0
Server
redis_version:5.0.10 redis_git_sha1:00000000 redis_git_dirty:0 redis_build_id:49ef4f7add54a56f redis_mode:sentinel os:Linux 3.10.0-693.el7.x86_64 x86_64 arch_bits:64 multiplexing_api:epoll atomicvar_api:atomic-builtin gcc_version:4.8.5 process_id:20722 run_id:ab83a98144c4a9f1f9dc938b616079ed2a6bcd01 tcp_port:26379 uptime_in_seconds:365 uptime_in_days:0 hz:14 configured_hz:10 lru_clock:10548260 executable:/opt/software/redis-5.0.10/src/redis-sentinel config_file:/opt/software/redis-replica/sentinel/sentinel.conf
Clients
connected_clients:1 client_recent_max_input_buffer:2 client_recent_max_output_buffer:0 blocked_clients:0
CPU
used_cpu_sys:0.700107 used_cpu_user:0.104016 used_cpu_sys_children:0.000000 used_cpu_user_children:0.000000
Stats
total_connections_received:1 total_commands_processed:0 instantaneous_ops_per_sec:0 total_net_input_bytes:31 total_net_output_bytes:60 instantaneous_input_kbps:0.00 instantaneous_output_kbps:0.00 rejected_connections:0 sync_full:0 sync_partial_ok:0 sync_partial_err:0 expired_keys:0 expired_stale_perc:0.00 expired_time_cap_reached_count:0 evicted_keys:0 keyspace_hits:0 keyspace_misses:0 pubsub_channels:0 pubsub_patterns:0 latest_fork_usec:0 migrate_cached_sockets:0 slave_expires_tracked_keys:0 active_defrag_hits:0 active_defrag_misses:0 active_defrag_key_hits:0 active_defrag_key_misses:0
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=127.0.0.1:6379,slaves=1,sentinels=1
当redis的主节点挂了之后,哨兵将会进行重新选举出新的主节点,将这些信息,重新写入到sentinel.conf中
```shell
sentinel known-replica mymaster 192.168.137.134 6379 #代表主节点的从节点信息
sentinel known-replica mymaster 192.168.137.134 6381 #代表主节点的从节点信息
sentinel known-sentinel mymaster 192.168.137.134 26380 52d0a5d70c1f90475b4fc03b6ce7c3c56935760f #代表感知到的其它哨兵节点
sentinel known-sentinel mymaster 192.168.137.134 26381 e9f530d3882f8043f76ebb8e1686438ba8bd5ca6 #代表感知到的其它哨兵节点
至此,Sentinel架构的搭建也完成了