• 安装环境

这里使用三台服务器,每台服务器上开启一个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命令解压到当前目录。

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

2、编译redis

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

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

[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命令进行安装,安装完毕后会在安装目录下生成一些执行文件。

[root@localhost src]# make install
Hint: It's a good idea to run 'make test' ;)
INSTALL install
INSTALL install
INSTALL install
INSTALL install
INSTALL install
[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、编辑环境变量

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

5、修改配置文件

vim /data/apps/conf/redis/redis_6379.conf
bind 0.0.0.0
protected-mode no
port
tcp-backlog
timeout
tcp-keepalive
daemonize yes
supervised no
pidfile "/data/apps/var/redis/redis_6379.pid"
loglevel notice
logfile "/data/apps/log/redis/redis_6379.log"
databases
save
save
save
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename "dump.rdb"
dir "/data/apps/data/redis_6379"
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay
repl-disable-tcp-nodelay no
slave-priority
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit
slowlog-log-slower-than
slowlog-max-len
latency-monitor-threshold
notify-keyspace-events ""
hash-max-ziplist-entries
hash-max-ziplist-value
list-max-ziplist-size -
list-compress-depth
set-max-intset-entries
zset-max-ziplist-entries
zset-max-ziplist-value
hll-sparse-max-bytes
activerehashing yes
client-output-buffer-limit normal
client-output-buffer-limit slave 256mb 64mb
client-output-buffer-limit pubsub 32mb 8mb
hz
aof-rewrite-incremental-fsync yes
# Generated by CONFIG REWRITE

6、 配置主从复制

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

操作主机:192.168.10.4、192.168.10.5


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

7 、启动redis-server

操作主机:所有主机

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

8、检查

[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、配置文件

[root@localhost ~]# vim /data/apps/config/redis/redis-sentinel-.conf
port
daemonize yes
protected-mode no
dir "/data/apps/var/sentinel"
pidfile "/data/apps/var/redis/redis-sentinel.pid"
logfile "/data/apps/log/redis/redis-sentinel.log"
sentinel monitor redis_master 192.168.10.3
sentinel down-after-milliseconds redis_master
sentinel failover-timeout redis_master

2、启动sentinel

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

3、检查

[root@localhost ~]# ss -lnt|grep 26379
[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

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

2、配置Haproxy

vim /usr/local/haproxy/haproxy.cfg
#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
# to have these messages end up in /var/log/haproxy.log you will
# need to:
#
# ) configure syslog to accept network log events. This is done
# by adding the '-r' option to the SYSLOGD_OPTIONS in
# /etc/sysconfig/syslog
#
# ) configure local2 events to go to the /var/log/haproxy.log
# file. A line like the following can be added to
# /etc/sysconfig/syslog
#
# local2.* /var/log/haproxy.log
#
log 127.0.0.1 local2
chroot /usr/local/haproxy
pidfile /usr/local/haproxy/haproxy.pid
maxconn
user haproxy
group haproxy
daemon
# turn on stats unix socket
stats socket /var/lib/haproxy/stats
#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
log global
mode tcp
retries
option redispatch
maxconn
timeout connect 600s
timeout client 600s
timeout server 600s
listen stats
bind 0.0.0.0:
mode http
stats uri /haproxy-status
stats auth admin:admin
stats hide-version
stats refresh 30s
frontend redis16379
bind :
default_backend redis_16379_backend
backend redis_16379_backend
option tcp-check
tcp-check send PING\r\n
tcp-check expect string +PONG
tcp-check send INFO\ REPLICATION\r\n
tcp-check expect string role:master
tcp-check send INFO\ REPLICATION\r\n
tcp-check expect rstring connected_slaves:[-]
tcp-check send QUIT\r\n
tcp-check expect string +OK
server redis1_6379 192.168.10.3: check inter 1s
server redis2_6379 192.168.10.4: check inter 1s
server redis3_6379 192.168.10.5: check inter 1s

3、检查

[root@localhost ~]# redis-cli -p  set a
OK
[root@localhost ~]# redis-cli -p set get a
OK
  • 总结

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

最新文章

  1. angular监听
  2. STL 跨模块 调用 异常 解决
  3. Sql导出数据报错-->SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatasource' 的访问
  4. (转)RSA算法原理
  5. HDU 2066 一个人的旅行【Dijkstra 】
  6. iOS安全——代码混淆&反编译
  7. JDK版本错误:Unsupported major.minor version 51.0
  8. Sogou搜狗搜索引擎登录网站 - Blog透视镜
  9. 使用apache daemon让java程序在unix系统上以服务方式运行
  10. python 日期 & 时间
  11. WdatePicker设置时间区间时,对开始时间和结束时间限制
  12. python字符串常用的方法解析
  13. HTML文档命名规则
  14. 使用C++对物理网卡/虚拟网卡进行识别(包含内外网筛选)
  15. NOI-1.3-05-计算分数的浮点数值-double要注意
  16. Google's Machine Learning Crash Course #01# Introducing ML & Framing & Fundamental terminology
  17. hdu1233 还是畅通工程 最小生成树
  18. 编写Android程序Eclipse连不上手机。
  19. java web项目中引入spring
  20. C# 实例化类的执行顺序

热门文章

  1. window上安装kafka(单机)
  2. php安全开发(1)文件包含漏洞
  3. linux blast
  4. Golang安装与命令
  5. Postman中x-www-form-urlencoded请求K-V的ajax实现
  6. word中一页中添加两种不同的页码
  7. HIT2019春软件构造->正则表达式语法
  8. jsp页面输出当前时间
  9. [luogu P2633] Count on a tree
  10. C#异步(下)