Redis 3.2.3: 集群3哨兵模式

简介

Redis是一个使用ANSI C编写的开源、支持网络、基于内存、可选持久性键值对存储数据库。从2015年6月开始,Redis的开发由Redis Labs赞助,而2013年5月至2015年6月期间,其开发由Pivotal赞助。[1]在2013年5月之前,其开发由VMware赞助。[2][3]根据月度排行网站DB-Engines.com的数据显示,Redis是最流行的键值对存储数据库。[4]

前言

本篇主要介绍Redis的集群部署,采用三台机器,一主两从,三台均为哨兵。

192.168.10.6 Redis主, Redis-Sentinel

192.168.10.7 Redis从, Redis-Sentinel

192.168.10.8 Redis从, Redis-Sentinel

Redis-master(192.168.10.6上的配置)

#下载redis源码包

mkdir /data/soft/ -pv

cd /data/soft

wget http://download.redis.io/releases/redis-3.2.3.tar.gz

tar xf redis-3.2.3.tar.gz -C /usr/local/

#编译安装

cd /usr/local/

ln -sv redis-3.2.3/ redis

cd redis

yum install gcc tcl -y #安装依赖关系

cd deps/

make lua hiredis linenoise

cd ..

#make distclean all #(不用执行)

make MALLOC=libc

make test

make install

cp redis.conf redis.conf.bak

#配置文件

  1 cat > /usr/local/redis/redis.conf << "EOF"
  2 
  3 bind 0.0.0.0
  4 
  5 protected-mode no
  6 
  7 port 6379
  8 
  9 tcp-backlog 511
 10 
 11 timeout 0
 12 
 13 tcp-keepalive 300
 14 
 15 daemonize yes
 16 
 17 supervised no
 18 
 19 pidfile "/var/run/redis_6379.pid"
 20 
 21 loglevel notice
 22 
 23 logfile "/usr/local/redis/redis.log"
 24 
 25 databases 16
 26 
 27 save 900 1
 28 
 29 save 300 10
 30 
 31 save 60 10000
 32 
 33 stop-writes-on-bgsave-error yes
 34 
 35 rdbcompression yes
 36 
 37 rdbchecksum yes
 38 
 39 dbfilename "dump.rdb"
 40 
 41 dir "/usr/local/redis-3.2.3"
 42 
 43 masterauth "123456"
 44 
 45 slave-serve-stale-data yes
 46 
 47 slave-read-only yes
 48 
 49 repl-diskless-sync no
 50 
 51 repl-diskless-sync-delay 5
 52 
 53 repl-disable-tcp-nodelay no
 54 
 55 slave-priority 90
 56 
 57 requirepass "123456"
 58 
 59 appendonly no
 60 
 61 appendfilename "appendonly.aof"
 62 
 63 appendfsync everysec
 64 
 65 no-appendfsync-on-rewrite no
 66 
 67 auto-aof-rewrite-percentage 100
 68 
 69 auto-aof-rewrite-min-size 64mb
 70 
 71 aof-load-truncated yes
 72 
 73 lua-time-limit 5000
 74 
 75 slowlog-log-slower-than 10000
 76 
 77 slowlog-max-len 128
 78 
 79 latency-monitor-threshold 0
 80 
 81 notify-keyspace-events ""
 82 
 83 hash-max-ziplist-entries 512
 84 
 85 hash-max-ziplist-value 64
 86 
 87 list-max-ziplist-size -2
 88 
 89 list-compress-depth 0
 90 
 91 set-max-intset-entries 512
 92 
 93 zset-max-ziplist-entries 128
 94 
 95 zset-max-ziplist-value 64
 96 
 97 hll-sparse-max-bytes 3000
 98 
 99 activerehashing yes
100 
101 client-output-buffer-limit normal 0 0 0
102 
103 client-output-buffer-limit slave 256mb 64mb 60
104 
105 client-output-buffer-limit pubsub 32mb 8mb 60
106 
107 hz 10
108 
109 aof-rewrite-incremental-fsync yes
110 
111 EOF
redis.conf

#开机启动设置

echo "/usr/local/bin/redis-server /usr/local/redis/redis.conf" >>/etc/rc.d/rc.local

chmod +x /etc/rc.d/rc.local #必须要加执行权限, 否则不会执行

#启动服务

redis-server /usr/local/redis/redis.conf

#查看状态

redis-cli -a 123456 info replication

 

Redis-slave(192.168.10.7,192.168.10.8上的配置)

#下载redis源码包

mkdir /data/soft/ -pv

cd /data/soft

wget http://download.redis.io/releases/redis-3.2.3.tar.gz

tar xf redis-3.2.3.tar.gz -C /usr/local/

#编译安装

cd /usr/local/

ln -sv redis-3.2.3/ redis

cd redis

yum install gcc tcl -y #安装依赖关系

cd deps/

make lua hiredis linenoise

cd ..

#make distclean all #(不用执行)

make MALLOC=libc

make test

make install

#配置文件

  1 cat > /usr/local/redis/redis.conf << "EOF"
  2 
  3 bind 0.0.0.0
  4 
  5 protected-mode no
  6 
  7 port 6379
  8 
  9 tcp-backlog 511
 10 
 11 timeout 0
 12 
 13 tcp-keepalive 300
 14 
 15 daemonize yes
 16 
 17 supervised no
 18 
 19 pidfile "/var/run/redis_6379.pid"
 20 
 21 loglevel notice
 22 
 23 logfile "/usr/local/redis/redis.log"
 24 
 25 databases 16
 26 
 27 save 900 1
 28 
 29 save 300 10
 30 
 31 save 60 10000
 32 
 33 stop-writes-on-bgsave-error yes
 34 
 35 rdbcompression yes
 36 
 37 rdbchecksum yes
 38 
 39 dbfilename "dump.rdb"
 40 
 41 dir "/usr/local/redis-3.2.3"
 42 
 43 masterauth "123456"
 44 
 45 slave-serve-stale-data yes
 46 
 47 slave-read-only yes
 48 
 49 repl-diskless-sync no
 50 
 51 repl-diskless-sync-delay 5
 52 
 53 repl-disable-tcp-nodelay no
 54 
 55 slave-priority 90
 56 
 57 requirepass "123456"
 58 
 59 appendonly no
 60 
 61 appendfilename "appendonly.aof"
 62 
 63 appendfsync everysec
 64 
 65 no-appendfsync-on-rewrite no
 66 
 67 auto-aof-rewrite-percentage 100
 68 
 69 auto-aof-rewrite-min-size 64mb
 70 
 71 aof-load-truncated yes
 72 
 73 lua-time-limit 5000
 74 
 75 slowlog-log-slower-than 10000
 76 
 77 slowlog-max-len 128
 78 
 79 latency-monitor-threshold 0
 80 
 81 notify-keyspace-events ""
 82 
 83 hash-max-ziplist-entries 512
 84 
 85 hash-max-ziplist-value 64
 86 
 87 list-max-ziplist-size -2
 88 
 89 list-compress-depth 0
 90 
 91 set-max-intset-entries 512
 92 
 93 zset-max-ziplist-entries 128
 94 
 95 zset-max-ziplist-value 64
 96 
 97 hll-sparse-max-bytes 3000
 98 
 99 activerehashing yes
100 
101 client-output-buffer-limit normal 0 0 0
102 
103 client-output-buffer-limit slave 256mb 64mb 60
104 
105 client-output-buffer-limit pubsub 32mb 8mb 60
106 
107 hz 10
108 
109 aof-rewrite-incremental-fsync yes
110 
111 slaveof 192.168.10.6 6379
112 
113 EOF
redis.conf

#开机启动设置

echo "/usr/local/bin/redis-server /usr/local/redis/redis.conf" >>/etc/rc.d/rc.local

chmod +x /etc/rc.d/rc.local #必须要加执行权限, 否则不会执行

#启动服务

redis-server /usr/local/redis/redis.conf

#查看状态

redis-cli -a 123456 info replication

 

#三sentinel配置(192.168.10.6,192.168.10.7,192.168.10.8上配置)

cat > /usr/local/redis/sentinel.conf << "EOF"

daemonize yes

port 27000

sentinel monitor redis-master 192.168.10.6 6379 2

sentinel down-after-milliseconds redis-master 5000

protected-mode no

sentinel failover-timeout redis-master 900000

sentinel parallel-syncs redis-master 2

sentinel auth-pass redis-master 123456

logfile "/usr/local/redis/sentinel.log"

EOF

#开机启动设置

echo "/usr/local/bin/redis-sentinel /usr/local/redis/sentinel.conf" >>/etc/rc.d/rc.local 

chmod +x /etc/rc.d/rc.local #必须要加执行权限, 否则不会执行

#启动服务

redis-sentinel /usr/local/redis/sentinel.conf

#查看状态

redis-cli -a 123456 info replication

redis-cli -p 27000 -a 123456 info sentinel

#单sentinel配置, 注:在一主一从的环境中,部署一个sentinel节点的环境使用

cat > /usr/local/redis/sentinel.conf << "EOF"

daemonize yes

port 27000

sentinel monitor redis-master 192.168.10.6 6379 1

protected-mode no

sentinel down-after-milliseconds redis-master 5000

sentinel failover-timeout redis-master 60000

sentinel auth-pass redis-master 123456

logfile "/usr/local/redis/sentinel.log"

EOF

#转移测试

redis-cli -a 123456 shutdown (主服务器上停掉redis服务, 或pkill redis-server)

redis-cli -p 27000 -a 123456 info sentinel(查看三台redis状态转换)

原文地址:https://www.cnblogs.com/William-Guozi/p/Redis.html