原创作品,允许转载,转载时请务必以超链接形式标明文章
原始出处 、作者信息和本声明。否则将追究法律责任。
http://dgd2010.blog.51cto.com/1539422/1742766
目标、需求:
方案概述:
方案原理:
连接图
主机标识 | Redis1 | Redis2 | Redis3 |
IP地址 | 192.168.1.241 | 192.168.1.242 | 192.168.1.243 |
预设配置 | redis-server master keepalived master |
redis-server slave keepalived backup |
redis-server slave keepalived backup |
VIP |
配置步骤:
安装:安装redis-server、redis-sentinel、keepalived
配置:配置Redis1、Redis2、Redis3,主要是编辑各个服务的配置文件
启动:先启动redis-server、再启动redis-sentinel、最后自动keepalived
验证:模拟一台主机宕机,主机网络中断,redis-server、redis-sentinel、keepalived三个服务任何一个发生故障的情况。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
# http://download.redis.io/redis-stable.tar.gz
wget -c http:
//download
.redis.io
/releases/redis-3
.0.7.
tar
.gz
tar
zxf redis-3.0.7.
tar
.gz
cd
redis-3.0.7
make
cd
\
cp
redis-3.0.7
/src/redis-benchmark
/usr/local/sbin/
\
cp
redis-3.0.7
/src/redis-check-aof
/usr/local/sbin/
\
cp
redis-3.0.7
/src/redis-check-dump
/usr/local/sbin/
\
cp
redis-3.0.7
/src/redis-cli
/usr/local/sbin/
\
cp
redis-3.0.7
/src/redis-sentinel
/usr/local/sbin/
\
cp
redis-3.0.7
/src/redis-server
/usr/local/sbin/
mkdir
/etc/redis
mkdir
-p
/data/redis-6379/
|
1
2
3
4
5
6
7
8
9
10
11
|
# http://www.keepalived.org/documentation.html
wget -c http:
//www
.keepalived.org
/software/keepalived-1
.2.19.
tar
.gz
tar
zxf keepalived-1.2.19.
tar
.gz
cd
keepalived-1.2.19
.
/configure
--prefix=
/usr/local/keepalived
make
make
install
cp
/usr/local/keepalived/sbin/keepalived
/usr/sbin/
cp
/usr/local/keepalived/etc/sysconfig/keepalived
/etc/sysconfig/
cp
/usr/local/keepalived/etc/rc
.d
/init
.d
/keepalived
/etc/init
.d/
mkdir
/etc/keepalived
|
1
2
3
4
5
6
7
8
|
cat
>
/etc/redis/sentinel
.conf<<eof
port 26379
dir
/tmp
sentinel monitor mymaster 192.168.1.241 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
eof
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
cat
>
/etc/redis/redis-6379
.conf <<eof
# maxmemory 268435456
maxmemory 256mb
daemonize
yes
pidfile
/data/redis-6379/redis-6379
.pid
port 6379
bind 0.0.0.0
tcp-backlog 511
timeout 0
tcp-keepalive 0
loglevel notice
logfile
/data/redis-6379/redis
.log
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error
yes
rdbcompression
yes
rdbchecksum
yes
dbfilename dumpredis-6379.rdb
dir
/data/redis-6379
slave-serve-stale-data
yes
slave-
read
-only
yes
repl-diskless-
sync
no
repl-diskless-
sync
-delay 5
# repl-ping-slave-period 10
# repl-timeout 60
repl-disable-tcp-nodelay no
# repl-backlog-size 1mb
# repl-backlog-ttl 3600
slave-priority 100
# min-slaves-to-write 3
# min-slaves-max-lag 10
appendonly no
appendfilename
"appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated
yes
lua-
time
-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events
""
hash
-max-ziplist-entries 512
hash
-max-ziplist-value 64
list-max-ziplist-entries 512
list-max-ziplist-value 64
set
-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing
yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync
yes
eof
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
cat
>
/etc/redis/redis-6379
.conf <<eof
slaveof 192.168.1.241 6379
# maxmemory 268435456
maxmemory 256mb
daemonize
yes
pidfile
/data/redis-6379/redis-6379
.pid
port 6379
bind 0.0.0.0
tcp-backlog 511
timeout 0
tcp-keepalive 0
loglevel notice
logfile
/data/redis-6379/redis
.log
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error
yes
rdbcompression
yes
rdbchecksum
yes
dbfilename dumpredis-6379.rdb
dir
/data/redis-6379
slave-serve-stale-data
yes
slave-
read
-only
yes
repl-diskless-
sync
no
repl-diskless-
sync
-delay 5
# repl-ping-slave-period 10
# repl-timeout 60
repl-disable-tcp-nodelay no
# repl-backlog-size 1mb
# repl-backlog-ttl 3600
slave-priority 100
# min-slaves-to-write 3
# min-slaves-max-lag 10
appendonly no
appendfilename
"appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated
yes
lua-
time
-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events
""
hash
-max-ziplist-entries 512
hash
-max-ziplist-value 64
list-max-ziplist-entries 512
list-max-ziplist-value 64
set
-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing
yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync
yes
eof
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
! Configuration File
for
keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 10
router_id keepalivedha_1
}
vrrp_script chk_http_port {
script
"redis-cli info | grep role:master >/dev/null 2>&1"
interval 1
timeout 2
fall 2
rise 1
}
vrrp_sync_group VG_1 {
group {
VI_1
}
}
vrrp_instance VI_1 {
state BACKUP
interface eth1
#use_vmac keepalived
#vmac_xmit_base
mcast_src_ip 192.168.1.241
smtp_alert
virtual_router_id 20
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass password
}
virtual_ipaddress {
192.168.1.245
}
track_script {
chk_http_port
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
! Configuration File
for
keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 10
router_id keepalivedha_2
}
vrrp_script chk_http_port {
script
"redis-cli info | grep role:master >/dev/null 2>&1"
interval 1
timeout 2
fall 2
rise 1
}
vrrp_sync_group VG_1 {
group {
VI_1
}
}
vrrp_instance VI_1 {
state BACKUP
interface eth2
#use_vmac keepalived
#vmac_xmit_base
mcast_src_ip 192.168.1.242
smtp_alert
virtual_router_id 20
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass password
}
virtual_ipaddress {
192.168.1.245
}
track_script {
chk_http_port
}
nopreempt
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
! Configuration File
for
keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 10
router_id keepalivedha_3
}
vrrp_script chk_http_port {
script
"redis-cli info | grep role:master >/dev/null 2>&1"
interval 1
timeout 2
fall 2
rise 1
}
vrrp_sync_group VG_1 {
group {
VI_1
}
}
vrrp_instance VI_1 {
state BACKUP
interface eth1
#use_vmac keepalived
#vmac_xmit_base
mcast_src_ip 192.168.1.243
smtp_alert
virtual_router_id 20
priority 98
advert_int 1
authentication {
auth_type PASS
auth_pass password
}
virtual_ipaddress {
192.168.1.245
}
track_script {
chk_http_port
}
nopreempt
}
|
1
2
|
redis-server
/etc/redis/redis-6379
.conf
tail
/data/redis-6379/redis
.log
|
1
2
|
redis-sentinel
/etc/redis/redis-sentinel
.conf
tail
/data/redis-6379/redis-sentinel
.log
|
1
2
|
service keepalived start
tail
/var/log/messages
|