Redis下载地址:https://redis.io/download

一、单机安装

1.1 下载

  1. wget https://download.redis.io/releases/redis-5.0.10.tar.gz

1.2 解压

  1. tar -zxvf redis-5.0.10.tar.gz

1.3 编译Redis

  1. cd redis-5.0.10
  2. yum install gcc
  3. make

如果执行make时发现如下错误

  1. [root@localhost redis-5.0.10]# make
  2. cd src && make all
  3. make[1]: 进入目录“/opt/software/redis-5.0.10/src
  4. CC adlist.o
  5. In file included from adlist.c:34:0:
  6. zmalloc.h:50:31: 致命错误:jemalloc/jemalloc.h:没有那个文件或目录
  7. #include <jemalloc/jemalloc.h>
  8. ^
  9. 编译中断。
  10. make[1]: *** [adlist.o] 错误 1
  11. make[1]: 离开目录“/opt/software/redis-5.0.10/src
  12. make: *** [all] 错误 2

将make命令改为如下命令

  1. make MALLOC=libc

1.3 修改配置文件

将redis.conf文件中的如下配置修改成yes

  1. daemonize yes

1.4 启动redis

  1. [root@localhost redis-5.0.10]# src/redis-server redis.conf
  2. 2863:C 03 Nov 2020 10:37:40.106 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
  3. 2863:C 03 Nov 2020 10:37:40.106 # Redis version=5.0.10, bits=64, commit=00000000, modified=0, pid=2863, just started
  4. 2863:C 03 Nov 2020 10:37:40.106 # Configuration loade

1.5 查看redis是否启动

  1. [root@localhost redis-5.0.10]# ps -ef|grep redis
  2. root 2864 1 0 10:37 ? 00:00:00 src/redis-server 127.0.0.1:6379
  3. root 2895 1443 0 10:38 pts/0 00:00:00 grep --color=auto redis

1.6 redis-cli测试

  1. [root@localhost redis-5.0.10]# src/redis-cli
  2. 127.0.0.1:6379> ping
  3. PONG
  4. 127.0.0.1:6379> set k1 v1
  5. OK
  6. 127.0.0.1:6379> get k1
  7. "v1"

至此,Redis单机安装完毕


二、主从搭建

2.1 复制redis.conf到master目录以及slave目录

2.2 修改salve目录中的配置文件

主要修改如下的几个配置

  1. port 6380 # 修改端口
  2. pidfile /var/run/redis_6380.pid # 修改pid的文件命
  3. logfile "/opt/redis-replica/slave/6380.log"
  4. dir /opt/software/redis-replica/slave # 指定数据存放目录
  5. 3、配置主从复制
  6. replicaof 192.168.137.134 6379 # 从本机6379的redis实例复制数据,Redis 5.0之前使用slaveof
  7. replica-read-only yes # 配置从节点只读

2.3 启动主从

  1. [root@localhost redis-replica]# /opt/software/redis-5.0.10/src/redis-server master/redis.conf
  2. 3681:C 03 Nov 2020 10:53:53.894 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
  3. 3681:C 03 Nov 2020 10:53:53.894 # Redis version=5.0.10, bits=64, commit=00000000, modified=0, pid=3681, just started
  4. 3681:C 03 Nov 2020 10:53:53.894 # Configuration loaded
  5. [root@localhost redis-replica]# /opt/software/redis-5.0.10/src/redis-server master/redis.conf
  6. 3703:C 03 Nov 2020 10:54:12.227 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
  7. 3703:C 03 Nov 2020 10:54:12.227 # Redis version=5.0.10, bits=64, commit=00000000, modified=0, pid=3703, just started
  8. 3703:C 03 Nov 2020 10:54:12.227 # Configuration loaded
  9. [root@localhost redis-replica]# ps -ef|grep redis
  10. root 3704 1 0 10:54 ? 00:00:00 /opt/software/redis-5.0.10/src/redis-server 127.0.0.1:6379
  11. root 3717 1443 0 10:54 pts/0 00:00:00 grep --color=auto redis
  12. [root@localhost redis-replica]# /opt/software/redis-5.0.10/src/redis-server slave/redis.conf
  13. [root@localhost redis-replica]# ps -ef|grep redis
  14. root 3704 1 0 10:54 ? 00:00:00 /opt/software/redis-5.0.10/src/redis-server 127.0.0.1:6379
  15. root 3761 1 0 10:55 ? 00:00:00 /opt/software/redis-5.0.10/src/redis-server 127.0.0.1:6380
  16. root 3778 1443 0 10:55 pts/0 00:00:00 grep --color=auto redis

2.4 测试主从

连接两个不同的redis实例

  1. /opt/software/redis-5.0.10/src/redis-cli -p 6379
  2. /opt/software/redis-5.0.10/src/redis-cli -p 6380

在主节点上,进行数据的操作

  1. 127.0.0.1:6379>
  2. 127.0.0.1:6379> set k1 v1
  3. OK
  4. 127.0.0.1:6379> set k2 v2
  5. OK
  6. 127.0.0.1:6379> set k3 v3
  7. OK
  8. 127.0.0.1:6379> keys *
  9. 1) "k3"
  10. 2) "k2"
  11. 3) "k1"
  12. 127.0.0.1:6379>

在从节点查看数据

  1. # 主节点操作数据之前,数据是空的
  2. 127.0.0.1:6380> keys *
  3. (empty list or set)
  4. # 主节点操作之后,同步到了主节点的数据
  5. 127.0.0.1:6380> keys *
  6. 1) "k3"
  7. 2) "k1"
  8. 3) "k2"
  9. 127.0.0.1:6380>

到这,Redis的主从也搭好了


三、Redis哨兵模式搭建

哨兵模式要解决的问题:
在上述的主从架构中,假设有一天Master机器挂了,因为客户端是直接连向了Master机器,所以当master挂了之后,客户端将会直接操作失败,此时需要手动的切换,将一台slave提升为master,其他slave复制新master的数据。会相对比较麻烦。所以衍生出了哨兵模式的架构。
在哨兵架构中,客户端连接的是哨兵集群,获取主节点的信息,后续直接访问主节点进行操作。当主节点发生变化时,哨兵节点将会第一时间感受到,并将新的主节点返回给客户端。实现主从的自动切换。
具体流程大致如下

  1. 客户端连接哨兵节点
  2. 哨兵节点返回一个主节点给客户端
  3. 客户端通过返回的主节点信息,连接主节点,进行操作
  4. 当主节点发生变化时,如宕机,哨兵节点将会立即感受到,从当前的节点中,选举出一个新的主节点,推送给客户端
  5. 客户端接收到新的主节点信息,开始连接新的主节点,进行操作。

    3.1 复制sentinel配置文件,并修改

    (通过修改不同的端口以及其他信息,可以启动一个sentinel集群)
    1. cp sentinel.conf yourDir/sentinel.conf
    2. # 修改如下的配置
    3. port 26379
    4. daemonize yes
    5. pidfile "/var/run/redis-sentinel-26379.pid"
    6. logfile "26379.log"
    7. dir "/usr/local/redis-5.0.3/data"
    8. # sentinel monitor <master-redis-name> <master-redis-ip> <master-redis-port> <quorum>
    9. # quorum是一个数字,指明当有多少个sentinel认为一个master失效时(值一般为:sentinel总数/2 + 1),master才算真正失效
    10. sentinel monitor mymaster 127.0.0.1 6379 2

    3.2 启动redis-sentinel

    1. src/redis-sentinel sentinel.conf
    启动完成之后,可以cat一下sentinel.conf,可以看到已经将节点的信息,写入了sentinel.conf中(这里仅配置了一点节点示意)
    1. protected-mode no
    2. sentinel known-replica mymaster 127.0.0.1 6380
    3. sentinel current-epoch 0
    通过客户端连接,来查看 ```shell 127.0.0.1:26379> info

    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

  1. redis的主节点挂了之后,哨兵将会进行重新选举出新的主节点,将这些信息,重新写入到sentinel.conf
  2. ```shell
  3. sentinel known-replica mymaster 192.168.137.134 6379 #代表主节点的从节点信息
  4. sentinel known-replica mymaster 192.168.137.134 6381 #代表主节点的从节点信息
  5. sentinel known-sentinel mymaster 192.168.137.134 26380 52d0a5d70c1f90475b4fc03b6ce7c3c56935760f #代表感知到的其它哨兵节点
  6. sentinel known-sentinel mymaster 192.168.137.134 26381 e9f530d3882f8043f76ebb8e1686438ba8bd5ca6 #代表感知到的其它哨兵节点

至此,Sentinel架构的搭建也完成了