准备两台虚拟机
192.168.1.38
192.168.1.238
安装环境
[root@bogon src]# yum -y install gcc lrzsz vim
[root@bogon src]# rz 上传至虚拟机
[root@bogon src]# ls
redis-3.2.8.tar.gz
安装Redis
[root@bogon src]# mkdir -p /data/server
[root@bogon src]# tar zxf redis-3.2.8.tar.gz -C /data/server/
[root@bogon src]# cd /data/server/redis-3.2.8/
[root@bogon redis-3.2.8]# make
[root@bogon redis-3.2.8]# cd src/
[root@bogon src]# make install
新建目录
[root@bogon src]# cd ..
[root@bogon redis-3.2.8]# mkdir redis_cluster/
[root@bogon redis-3.2.8]# cd redis_cluster/
[root@bogon redis_cluster]# mkdir 6379 6380
在主机192.168.1.38中,复制配置文件
[root@bogon redis_cluster]# vim 6379/redis.conf
bind 192.168.1.38
protected-mode yes
port 6379
daemonize yes
supervised no
pidfile "/var/run/redis_6379.pid"
dir ./
slave-priority 25
[root@bogon redis_cluster]# vim 6380/redis.conf
bind 192.168.1.38
protected-mode yes
port 6380
daemonize yes
supervised no
pidfile "/var/run/redis_6380.pid"
slaveof 192.168.1.38 6379
slave-priority 100
[root@bogon redis_cluster]# vim 6380/sentinel.conf
protected-mode no
daemonize yes
port 26379
dir "/tmp"
sentinel monitor mymaster 192.168.1.38 6379 2
在192.168.1.238中,复制配置文件
[root@localhost redis_cluster]# cp ../redis.conf 6379/
[root@localhost redis_cluster]# cp ../redis.conf 6380/
[root@localhost redis_cluster]# cp ../sentinel.conf 6380/
[root@localhost redis_cluster]# cp ../sentinel.conf 6379/
[root@localhost redis_cluster]# vim 6379/redis.conf
bind 192.168.1.238
protected-mode yes
port 6379
daemonize yes
pidfile "/var/run/redis_6379.pid"
slaveof 192.168.1.38 6379
slave-priority 25
[root@localhost redis_cluster]# vim 6379/sentinel.conf
protected-mode no
daemonize yes
port 26379
dir "/tmp"
sentinel monitor mymaster 192.168.1.38 6379 2
[root@localhost redis_cluster]# vim 6380/redis.conf
bind 192.168.1.238
protected-mode yes
port 6380
daemonize yes
supervised no
pidfile "/var/run/redis_6380.pid"
slaveof 192.168.1.38 6379
slave-priority 100
[root@localhost redis_cluster]# vim 6380/sentinel.conf
protected-mode no
daemonize yes
port 26379
dir "/tmp"
sentinel monitor mymaster 192.168.1.38 6379 2
开启所有的服务和哨兵,在192.168.1.38中
[root@bogon 6380]# cd ../6379/
[root@bogon 6379]# redis-server redis.conf
[root@bogon 6379]# cd ../6380/
[root@bogon 6380]# redis-server redis.conf
[root@bogon 6380]# redis-sentinel sentinel.conf
在192.168.1.238中
[root@localhost 6380]# cd ../6379/
[root@localhost 6379]# redis-server redis.conf
[root@localhost 6379]# redis-sentinel sentinel.conf
[root@localhost 6379]# cd ../6380/
[root@localhost 6380]# redis-server redis.conf
[root@localhost 6380]# redis-sentinel sentinel.conf
关闭防火墙,降低沙盒
[root@bogon 6380]# systemctl stop firewalld
[root@bogon 6380]# setenforce 0
验证
[root@localhost 6380]# redis-cli -h 192.168.1.38
192.168.1.38:6379> info replication
#Replication
role:master
connected_slaves:3
slave0:ip=192.168.1.38,port=6380,state=online,offset=52800,lag=1
slave1:ip=192.168.1.238,port=6380,state=online,offset=52941,lag=1
slave2:ip=192.168.1.238,port=6379,state=online,offset=52800,lag=1
master_repl_offset:52941
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:52940
现在我们杀掉主机
[root@localhost 6380]# ps -aux |grep redis
root 8466 0.3 0.4 136920 8068 ? Sl 12:06 0:40redis-sentinel *:26379 [sentinel]
root 9381 0.4 0.4 136920 7616 ? Ssl 14:46 0:03redis-server 192.168.1.38:6380
root 9385 0.4 0.5 138968 9724 ? Ssl 14:46 0:03redis-server 192.168.1.38:6379
root 9426 0.0 0.0 112648 960 pts/2 S+ 14:59 0:00 grep --color=auto redis
[root@localhost 6380]# kill 9385
我们发现备主接替主机工作
[root@localhost 6379]# redis-cli -h 192.168.1.238
192.168.1.238:6379> info replication
#Replication
role:master
connected_slaves:2
slave0:ip=192.168.1.38,port=6380,state=online,offset=470,lag=1
slave1:ip=192.168.1.238,port=6380,state=online,offset=484,lag=0
master_repl_offset:484
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:483
192.168.1.238:6379>
杀掉备主,并开启主机
[root@localhost 6379]# kill 12296
[root@localhost 6379]# redis-server redis.conf
发现从机接替工作
[root@localhost 6379]# redis-cli -h 192.168.1.238 -p 6380
192.168.1.238:6380> info replication
#Replication
role:master
connected_slaves:2
slave0:ip=192.168.1.38,port=6380,state=online,offset=11298,lag=1
slave1:ip=192.168.1.38,port=6379,state=online,offset=11439,lag=0
master_repl_offset:11439
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:11438