• 安装环境

这里使用三台服务器,每台服务器上开启一个redis-server和redis-sentinel服务,redis-server端口为6379,redis-sentinel的端口为26379。

redis-server说明

192.168.10.3:6379  主

192.168.10.4:6379  从

192.168.10.5:6379  从

redis-sentinel说明

192.168.10.3:26379

192.168.10.4:26379

192.168.10.5:26379

目录规划:

配置文件目录:        /data/apps/conf/redis

数据目录:               /data/apps/data/redis_6379

日志目录:               /data/apps/log/redis

pid文件目录:          /data/apps/var/redis

sentinel数据目录:   /data/apps/var/sentinel

  •  安装Redis

操作主机:192.168.10.3 192.168.10.4 192.168.10.5

1、下载redis安装包

下载完毕后使用tar命令解压到当前目录。

  1. [root@localhost ~]# wget http://download.redis.io/releases/redis-3.2.8.tar.gz
  2. [root@localhost ~]# tar zxvf redis-3.2.8.tar.gz

2、编译redis

进入安装目录下,执行make命令,编译成功后会显示下面内容。

编译后在目录下生成一个src目录,里面会有编译好的执行文件

  1. [root@localhost ~]# cd redis-3.2.8
    [root@localhost redis-3.2.8]# make
    [root@localhost redis-3.2.8]# cd src && make all
    make[1]: Entering directory `/home/redis/redis-3.2.8/src' Hint: It's a good idea to run 'make test' ;) make[1]: Leaving directory `/home/redis/redis-3.2.8/src'

3、安装redis

Redis的默认安装路径是 /usr/local 目录下,进入src目录下,使用vi编辑Makefile文件,在文件中找到“PRIFIX?=/usr/local”,可以修改为你需要安装的目录,这里使用局对路径。

然后执行make install命令进行安装,安装完毕后会在安装目录下生成一些执行文件。

  1. [root@localhost src]# make install
  2. Hint: It's a good idea to run 'make test' ;)
  3. INSTALL install
  4. INSTALL install
  5. INSTALL install
  6. INSTALL install
  7. INSTALL install
  1. [root@localhost ~]# cd /usr/local/bin/
    [redis@localhost bin]$ ls
    dump.rdb redis-check-aof redis-cli redis-server
    redis-benchmark redis-check-rdb redis-sentinel

4、编辑环境变量

  1. [root@localhost ~]# vim /etc/profile.d/redis.sh
    export REDIS_HOME=/usr/local
    export PATH=${REDIS_HOME}:bin:${PATH}
  1. [root@localhost ~]# source /etc/profile

5、修改配置文件

  1. vim /data/apps/conf/redis/redis_6379.conf
  2. bind 0.0.0.0
  3. protected-mode no
  4. port
  5. tcp-backlog
  6. timeout
  7. tcp-keepalive
  8. daemonize yes
  9. supervised no
  10. pidfile "/data/apps/var/redis/redis_6379.pid"
  11. loglevel notice
  12. logfile "/data/apps/log/redis/redis_6379.log"
  13. databases
  14. save
  15. save
  16. save
  17. stop-writes-on-bgsave-error yes
  18. rdbcompression yes
  19. rdbchecksum yes
  20. dbfilename "dump.rdb"
  21. dir "/data/apps/data/redis_6379"
  22. slave-serve-stale-data yes
  23. slave-read-only yes
  24. repl-diskless-sync no
  25. repl-diskless-sync-delay
  26. repl-disable-tcp-nodelay no
  27. slave-priority
  28. appendonly no
  29. appendfilename "appendonly.aof"
  30. appendfsync everysec
  31. no-appendfsync-on-rewrite no
  32. auto-aof-rewrite-percentage
  33. auto-aof-rewrite-min-size 64mb
  34. aof-load-truncated yes
  35. lua-time-limit
  36. slowlog-log-slower-than
  37. slowlog-max-len
  38. latency-monitor-threshold
  39. notify-keyspace-events ""
  40. hash-max-ziplist-entries
  41. hash-max-ziplist-value
  42. list-max-ziplist-size -
  43. list-compress-depth
  44. set-max-intset-entries
  45. zset-max-ziplist-entries
  46. zset-max-ziplist-value
  47. hll-sparse-max-bytes
  48. activerehashing yes
  49. client-output-buffer-limit normal
  50. client-output-buffer-limit slave 256mb 64mb
  51. client-output-buffer-limit pubsub 32mb 8mb
  52. hz
  53. aof-rewrite-incremental-fsync yes
  54. # Generated by CONFIG REWRITE

6、 配置主从复制

192.168.10.3为master,其他两个为slave,只需修改从库的配置文件

操作主机:192.168.10.4、192.168.10.5

  1.  
  1. [root@localhost ~]# echo 'slaveof 192.168.10.3 6379' >>/data/apps/conf/redis_6379.conf

7 、启动redis-server

操作主机:所有主机

  1. [root@localhost ~]# redis-server /data/apps/conf/redis/redis_6379.conf

8、检查

  1. [root@localhost ~]# redis-cli -h 192.168.10.3 -p 6379
    127.0.0.1:6379> info

# Replication
role:master
connected_slaves:2
slave0:ip=192.168.10.5,port=6379,state=online,offset=14874,lag=1
slave1:ip=192.168.10.4,port=6379,state=online,offset=15160,lag=1
master_repl_offset:15303
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:15302

# 出现上面的结果说明主从复制成功

  • 部署redis-sentinel

操作主机:所有主机,每个主机的配置文件相同

1、配置文件

  1. [root@localhost ~]# vim /data/apps/config/redis/redis-sentinel-.conf
  2. port
  3. daemonize yes
  4. protected-mode no
  5. dir "/data/apps/var/sentinel"
  6. pidfile "/data/apps/var/redis/redis-sentinel.pid"
  7. logfile "/data/apps/log/redis/redis-sentinel.log"
  8. sentinel monitor redis_master 192.168.10.3
  9. sentinel down-after-milliseconds redis_master
  10. sentinel failover-timeout redis_master

2、启动sentinel

  1. [root@localhost ~]# redis-sentinel /data/apps/conf/redis/redis-sentinel-.conf

3、检查

  1. [root@localhost ~]# ss -lnt|grep 26379
  1. [root@localhost ~]# redis-cli -p 26379
    127.0.0.1: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=redis_master,status=ok,address=192.168.10.3:6379,slaves=2,sentinels=3

# 出现上面的情况说明配置成功

  • 部署Haproxy

1、安装Haproxy

  1. #下载
  2. wget http://fossies.org/linux/misc/haproxy-1.8.12.tar.gz
  3. #解压
  4. tar -zxvf haproxy-1.8..tar.gz
  5. cd haproxy-1.8.
  6. #安装
  7. make TARGET=linux2628 ARCH=x86_64 PREFIX=/usr/local/haproxy
  8. make install PREFIX=/usr/local/haproxy
  9. #参数说明
  10. TARGET=linux26 #内核版本,使用uname -r查看内核,如:2.6.-.el5,此时该参数就为linux26;kernel 大于2..28的用:TARGET=linux2628
  11. ARCH=x86_64 #系统位数
  12. PREFIX=/usr/local/haprpxy #/usr/local/haprpxy为haprpxy安装路径

2、配置Haproxy

  1. vim /usr/local/haproxy/haproxy.cfg
  2. #---------------------------------------------------------------------
  3. # Global settings
  4. #---------------------------------------------------------------------
  5. global
  6. # to have these messages end up in /var/log/haproxy.log you will
  7. # need to:
  8. #
  9. # ) configure syslog to accept network log events. This is done
  10. # by adding the '-r' option to the SYSLOGD_OPTIONS in
  11. # /etc/sysconfig/syslog
  12. #
  13. # ) configure local2 events to go to the /var/log/haproxy.log
  14. # file. A line like the following can be added to
  15. # /etc/sysconfig/syslog
  16. #
  17. # local2.* /var/log/haproxy.log
  18. #
  19. log 127.0.0.1 local2
  20. chroot /usr/local/haproxy
  21. pidfile /usr/local/haproxy/haproxy.pid
  22. maxconn
  23. user haproxy
  24. group haproxy
  25. daemon
  26. # turn on stats unix socket
  27. stats socket /var/lib/haproxy/stats
  28. #---------------------------------------------------------------------
  29. # common defaults that all the 'listen' and 'backend' sections will
  30. # use if not designated in their block
  31. #---------------------------------------------------------------------
  32. defaults
  33. log global
  34. mode tcp
  35. retries
  36. option redispatch
  37. maxconn
  38. timeout connect 600s
  39. timeout client 600s
  40. timeout server 600s
  41. listen stats
  42. bind 0.0.0.0:
  43. mode http
  44. stats uri /haproxy-status
  45. stats auth admin:admin
  46. stats hide-version
  47. stats refresh 30s
  48. frontend redis16379
  49. bind :
  50. default_backend redis_16379_backend
  51. backend redis_16379_backend
  52. option tcp-check
  53. tcp-check send PING\r\n
  54. tcp-check expect string +PONG
  55. tcp-check send INFO\ REPLICATION\r\n
  56. tcp-check expect string role:master
  57. tcp-check send INFO\ REPLICATION\r\n
  58. tcp-check expect rstring connected_slaves:[-]
  59. tcp-check send QUIT\r\n
  60. tcp-check expect string +OK
  61. server redis1_6379 192.168.10.3: check inter 1s
  62. server redis2_6379 192.168.10.4: check inter 1s
  63. server redis3_6379 192.168.10.5: check inter 1s

3、检查

  1. [root@localhost ~]# redis-cli -p set a
  2. OK
  3. [root@localhost ~]# redis-cli -p set get a
  4. OK
  • 总结

上面的内容可以解决redis高可用的问题,但是Haproxy还是单点。如果服务器是物理机,Haproxy可以通过Keepalived解决单点问题,实现高可用;但是服务器是ECS的情况下不能使用Keepalived。

Redis高可用方案----Redis主从+Sentinel+Haproxy的更多相关文章

  1. Windows版本redis高可用方案探究

    目录 Windows版本redis高可用方案探究 前言 搭建redis主从 配置主redis-28380 配置从redis-23381 配置从redis-23382 将redis部署为服务 启动red ...

  2. Redis高可用方案-哨兵与集群

    Redis高可用方案 一.名词解释   二.主从复制 Redis主从复制模式可以将主节点的数据同步给从节点,从而保障当主节点不可达的情况下,从节点可以作为 后备顶上来,并且可以保障数据尽量不丢失(主从 ...

  3. Redis高可用之哨兵模式Sentinel配置与启动(五)

    0.Redis目录结构 1)Redis介绍及部署在CentOS7上(一) 2)Redis指令与数据结构(二) 3)Redis客户端连接以及持久化数据(三) 4)Redis高可用之主从复制实践(四) 5 ...

  4. 深入理解Redis高可用方案-Sentinel

    Redis Sentinel是Redis的高可用方案.是Redis 2.8中正式引入的. 在之前的主从复制方案中,如果主节点出现问题,需要手动将一个从节点升级为主节点,然后将其它从节点指向新的主节点, ...

  5. redis高可用、redis集群、redis缓存优化

    今日内容概要 redis高可用 redis集群 redis缓存优化 内容详细 1.redis高可用 # 主从复制存在的问题: 1 主从复制,主节点发生故障,需要做故障转移,可以手动转移:让其中一个sl ...

  6. Redis高可用方案哨兵机制------ 配置文件sentinel.conf详解

    Redis的哨兵机制是官方推荐的一种高可用(HA)方案,我们在使用Redis的主从结构时,如果主节点挂掉,这时是不能自动进行主备切换和通知客户端主节点下线的. Redis-Sentinel机制主要用三 ...

  7. 理解redis高可用方案

    *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...

  8. redis高可用之REDIS SENTINEL

    1. Redis主从配置 1.1. 设置主从复制 Master <= Salve 10.24.6.5:6379 <= 10.24.6.7:6379 1.2.   取消主从复制 1.3.   ...

  9. 容器化redis高可用方案

    偶然看到一个GITHUB项目,提供了一套Docker Compose下的redis Sentinel方案. 项目地址https://github.com/AliyunContainerService/ ...

随机推荐

  1. suse源

    zypper addrepo -f http://mirrors.vbi.vt.edu/mirrors/linux/opensuse/discontinued/distribution/11.4/re ...

  2. wordpress-基础插件,常用函数

    一,插件制作 1.首先在plugin文件夹下创建一个php文件,我以制作一个banner插件为例,把以下代码拷贝到php文件中 <?php add_action("init" ...

  3. [数据结构] 2.2 Huffman树

    注:本文原创,转载请注明出处,本人保留对未注明出处行为的责任追究. 1.Huffman树是什么 Huffman树也称为哈夫曼编码,是一种编码方式,常用于协议的制定,以节省传输空间. A - F字母,出 ...

  4. 【转】RTP学习笔记

    转自:https://www.cnblogs.com/yoyotl/p/5650101.html 一.定义 实时传输协议(Real- time Transport Protocol,RTP)是在Int ...

  5. 怎么将GitHub上的项目下载到本地,并运行

    第一步:首页的有项目的地址才能下载 第二步:使用git 下载  命令:git clone 项目地址 第三步:npm install  下载依赖 第四步:npm run dev 运行项目

  6. 多重if-else语句

    C语言自学之多重if-else语句 Dome : 某游戏对不同等级的积分的玩家赋予不同的荣誉称号,其对应关系如下: 积分>=10000分为钻石玩家 积分>=5000并且<10000为 ...

  7. springboot 默认异常处理

    SpringBoot默认有自定义异常处理的体系,在做SpringBoot项目的时候,如果是抛出了运行时异常,springBoot并会对异常进行处理,返回如下异常信息: { "timestam ...

  8. phpquerylist 抓取数据详解

    参考文档 https://doc.querylist.cc/site/index/doc

  9. 【1】HTTP协议和Socket接口区别

    内容提要: 1.网络七层模型 2.什么是HTTP协议 3.什么是Socket接口 1.网络七层模型 第一层:物理层 为设备之间的信息提供传输提供可靠环境,那么这个环境是什么呢? 如:同轴电缆,插头,接 ...

  10. python数据结构与算法之算法和算法分析

    1.问题.问题实例.算法的概念区分. 一个例子说明一下: 问题:判断一个正整数N是否为素数   #问题是需要解决的一个需求 问题实例:判断1314是否为素数? #问题实例是该问题的一个具体例子 算法: ...