准备两台虚拟机
主IP:192.168.1.129
备主:192.168.1.252
两台虚拟机分别安装redis包
[root@bogon ~]# cd /usr/local/src/
[root@bogon src]# rz
[root@bogon src]# tar -zxvf redis-3.2.8.tar.gz -C /data/server/ //解压至/data/server/下
[root@bogon src]# cd /data/server/redis-3.2.8
[root@localhost redis-3.2.8]# make
[root@localhost redis-3.2.8]# make install
[root@localhost ~]# cd /data/server/redis-3.2.8/src/
[root@localhost src]# cp redis-server redis-cli redis-sentinel redis-benchmark redis-check-aof redis-check-rdb /usr/local/bin/ //复制执行文件到/usr/local/bin下
[root@bogon server]# mkdir cluster //创建目录
[root@bogon server]# cd cluster/
[root@bogon cluster]# mkdir 6379 6380
[root@bogon cluster]# cd ..
[root@bogon server]# cd redis-3.2.8/
[root@bogon redis-3.2.8]# cp redis.conf /data/server/cluster/6379
[root@bogon redis-3.2.8]# cp redis.conf /data/server/cluster/6380
[root@bogon redis-3.2.8]# cp sentinel.conf /data/server/cluster/6380 //主机不需要哨兵
[root@bogon 6379]# vim redis.conf //修改主配置文件,只需修改如下内容
bind 192.168.1.129
protected-mode yes
port 6379
daemonize yes
pidfile "/var/run/redis_6379.pid"
slave-priority 25
配置6380下从配置文件 ip为192.168.1.252中的6380目录的redis.conf修改内容相同
[root@bogon 6380]# vim redis.conf //修改如下内容即可
bind 192.168.1.129
protected-mode yes
port 6380
daemonize yes
supervised no
pidfile "/var/run/redis_6380.pid"
slaveof 192.168.1.129 6379 //添加此行
dir "/data/server/redis-3.2.8/cluster/6380"
slave-priority 100
修改从sentinel配置文件
[root@bogon 6380]# vim sentinel.conf //修改如下内容即可
protected-mode no
daemonize yes //此行需添加
port 26379
dir "/tmp"
sentinel monitor mymaster 192.168.1.129 6379 2
192.168.1.252所有哨兵配置同上,备主6379有哨兵配置,如上sentine配置文件相同
[root@localhost /data/server]#mkdir cluster
[root@localhost /data/server/cluster]#mkdir 6379 6380
[root@localhost /data/server]#cd redis-3.2.8/
[root@bogon redis-3.2.8]# cp redis.conf /data/server/cluster/6379
[root@bogon redis-3.2.8]# cp redis.conf /data/server/cluster/6380
[root@bogon redis-3.2.8]# cp sentinel.conf /data/server/cluster/6380
[root@bogon redis-3.2.8]# cp sentinel.conf /data/server/cluster/6379 //备主需要哨兵配置文件
修改6379主配置文件
[root@localhost /data/server/cluster/6379]#vim redis.conf //修改内容如下
bind 192.168.1.252 //本机IP
protected-mode yes
port 6379
daemonize yes
pidfile "/var/run/redis_6379.pid"
slaveof 192.168.1.129 6379 //主机
slave-priority 25
从配置与哨兵配置都同192.168.1.129相同
[root@localhost server]# cd cluster/6379
[root@localhost 6379]# redis-server redis.conf
[root@localhost 6379]# cd ../6380/
[root@localhost 6380]# redis-server redis.conf
[root@localhost 6380]# redis-sentinel sentinel.conf
192.168.1.252备主开启方式相同
进行验证
[root@localhost /data/server/cluster/6380]#redis-cli -h 192.168.1.129
192.168.1.129:6379> info replication
# Replication
role:master
connected_slaves:3
slave0:ip=192.168.1.129,port=6380,state=online,offset=271996,lag=1
slave1:ip=192.168.1.252,port=6379,state=online,offset=271996,lag=1
slave2:ip=192.168.1.252,port=6380,state=online,offset=271996,lag=1
master_repl_offset:271996
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:271995
杀掉主机
[root@localhost /data/server/cluster/6380]#ps -aux |grep redis
[root@localhost 6380]# kill 7077
[root@localhost 6380]# redis-cli -h 192.168.1.252
192.168.1.252:6379> //备主192.168.1.252接替工作
搭建完成!