准备一台机器,系统版本为CentOS7。
部署redis
1、下载软件包
# wget http://download.redis.io/releases/redis-3.2.8.tar.gz
2、解压
# tar xvf redis-3.2.8.tar.gz -C /usr/local/
# cd /usr/local/
# ln -sv redis-3.2.8/ redis
3、编译
# yum groupinstall -y "Development Tools"
# cd redis
# make
4、为多实例redis配置启动环境
# mkdir -pv /home/redis-cluster/{9000,9001,9002}
# cp src/{redis-server,redis-sentinel} /home/redis-cluster/9000/
# cp src/{redis-server,redis-sentinel} /home/redis-cluster/9001/
# cp src/{redis-server,redis-sentinel} /home/redis-cluster/9002/
# cp redis.conf sentinel.conf /home/redis-cluster/9000/
# cp redis.conf sentinel.conf /home/redis-cluster/9001/
# cp redis.conf sentinel.conf /home/redis-cluster/9002/
5、修改配置文件
# vim /home/redis-cluster/9000/redis.conf
bind 172.60.0.170
protected-mode no
port 9000
daemonize yes
appendonly yes
# cp /home/redis-cluster/9000/redis.conf /home/redis-cluster/9001/redis.conf
# cp /home/redis-cluster/9000/redis.conf /home/redis-cluster/9002/redis.conf
# vim /home/redis-cluster/9001/redis.conf
port 9001
slaveof 172.60.0.170 9000
# vim /home/redis-cluster/9002/redis.conf
port 9002
slaveof 172.60.0.170 9000
6、启动三个redis实例
# cd /home/redis-cluster/9000/
# ./redis-server redis.conf
# cd ../9001/
# ./redis-server redis.conf
# cd ../9002/
# ./redis-server redis.conf
# ps -ef | grep redis
root 23565 1 0 03:29 ? 00:00:03 ./redis-server 172.60.0.170:9000
root 23569 1 0 03:29 ? 00:00:02 ./redis-server 172.60.0.170:9001
root 23575 1 0 03:29 ? 00:00:02 ./redis-server 172.60.0.170:9002
7、验证,查看主从状态
# /usr/local/redis/src/redis-cli -h 172.60.0.170 -p 9000
172.60.0.170:9000> set name keith
OK
172.60.0.170:9000> get name
"keith"
172.60.0.170:9000> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=172.60.0.170,port=9001,state=online,offset=99,lag=1
slave1:ip=172.60.0.170,port=9002,state=online,offset=99,lag=0
master_repl_offset:99
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:98
部署sentinel
1、部署实例一
# cd /home/redis-cluster/9000
# vim sentinel.conf
protected-mode no
port 29000
daemonize yes
dir /home/redis-cluster/9000/logs
sentinel monitor mymaster 172.60.0.170 9000 2
# mkdir logs
# ./redis-sentinel sentinel.conf
# cp sentinel.conf ../9001/
# cp sentinel.conf ../9002/
2、部署实例二
# cd ../9001/
port 29001
dir "/home/redis-cluster/9001/logs"
# mkdir logs
# ./redis-sentinel sentinel.conf
3、部署实例三
# cd ../9002/
# vim sentinel.conf
port 29002
dir "/home/redis-cluster/9002/logs"
# mkdir logs
# ./redis-sentinel sentinel.conf
# ps -ef | grep redis
root 23565 1 0 03:29 ? 00:00:03 ./redis-server 172.60.0.170:9000
root 23569 1 0 03:29 ? 00:00:03 ./redis-server 172.60.0.170:9001
root 23575 1 0 03:29 ? 00:00:03 ./redis-server 172.60.0.170:9002
root 23923 1 0 03:43 ? 00:00:04 ./redis-sentinel *:29000 [sentinel]
root 23927 1 0 03:44 ? 00:00:04 ./redis-sentinel *:29001 [sentinel]
root 23965 1 0 03:44 ? 00:00:04 ./redis-sentinel *:29002 [sentinel]
4、验证
# /usr/local/redis/src/redis-cli -h 172.60.0.170 -p 29000
172.60.0.170:29000> sentinel masters
1) 1) "name"
2) "mymaster"
3) "ip"
4) "172.60.0.170"
5) "port"
6) "9000"
7) "runid"
8) "3236841c634359db724f73d4833b003683efa446"
9) "flags"
10) "master"
11) "link-pending-commands"
12) "0"
13) "link-refcount"
14) "1"
15) "last-ping-sent"
16) "0"
17) "last-ok-ping-reply"
18) "65"
19) "last-ping-reply"
20) "65"
21) "down-after-milliseconds"
22) "30000"
23) "info-refresh"
24) "4288"
25) "role-reported"
26) "master"
27) "role-reported-time"
28) "2885298"
29) "config-epoch"
30) "0"
31) "num-slaves"
32) "2"
33) "num-other-sentinels"
34) "2"
35) "quorum"
36) "2"
37) "failover-timeout"
38) "180000"
39) "parallel-syncs"
40) "1"
172.60.0.170:29000> sentinel slaves mymaster
1) 1) "name"
2) "172.60.0.170:9002"
3) "ip"
4) "172.60.0.170"
5) "port"
6) "9002"
7) "runid"
8) "0e741da8193158b565113ec84c6384284d11b0bd"
9) "flags"
10) "slave"
11) "link-pending-commands"
12) "0"
13) "link-refcount"
14) "1"
15) "last-ping-sent"
16) "0"
17) "last-ok-ping-reply"
18) "133"
19) "last-ping-reply"
20) "133"
21) "down-after-milliseconds"
22) "30000"
23) "info-refresh"
24) "4338"
25) "role-reported"
26) "slave"
27) "role-reported-time"
28) "2915465"
29) "master-link-down-time"
30) "0"
31) "master-link-status"
32) "ok"
33) "master-host"
34) "172.60.0.170"
35) "master-port"
36) "9000"
37) "slave-priority"
38) "100"
39) "slave-repl-offset"
40) "632552"
2) 1) "name"
2) "172.60.0.170:9001"
3) "ip"
4) "172.60.0.170"
5) "port"
6) "9001"
7) "runid"
8) "4a500eedd644d89c565773d5e42faf4c2872295e"
9) "flags"
10) "slave"
11) "link-pending-commands"
12) "0"
13) "link-refcount"
14) "1"
15) "last-ping-sent"
16) "0"
17) "last-ok-ping-reply"
18) "133"
19) "last-ping-reply"
20) "133"
21) "down-after-milliseconds"
22) "30000"
23) "info-refresh"
24) "4338"
25) "role-reported"
26) "slave"
27) "role-reported-time"
28) "2915465"
29) "master-link-down-time"
30) "0"
31) "master-link-status"
32) "ok"
33) "master-host"
34) "172.60.0.170"
35) "master-port"
36) "9000"
37) "slave-priority"
38) "100"
39) "slave-repl-offset"
40) "632552"