server3(192.168.34.4)是主库,server4(192.168.34.5)是从库
[root@server3 ~]# tar zxf redis-5.0.3.tar.gz
[root@server3 ~]# cd redis-5.0.3
[root@server3 redis-5.0.3]# yum install gcc -y
[root@server3 redis-5.0.3]# make && make install ##make可能出现错误,是因为解压过程中可能会出现错误,可以删除重新解压
[root@server3 redis-5.0.3]# cd utils/
[root@server3 utils]# ./install_server.sh ##初始化
Welcome to the redis service installer
This script will help you easily set up a running redis server
Please select the redis port for this instance: [6379]
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf]
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log]
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379]
Selected default - /var/lib/redis/6379
Please select the redis executable path [/usr/local/bin/redis-server]
Selected config:
Port : 6379
Config file : /etc/redis/6379.conf
Log file : /var/log/redis_6379.log
Data dir : /var/lib/redis/6379
Executable : /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Starting Redis server...
Installation successful!
[root@server3 utils]# netstat -antuple ##查看端口只有内部能使用
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State User Inode PID/Program name
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN
[root@server3 utils]# vim /etc/redis/6379.conf
70 bind 0.0.0.0 # 监听本机所有端口
187 databases 16 # 默认数据库为16个
293 # masterauth <master-password> # 设定redis的认证,因为我们这是内网网段进行操作,所以此处不用设定
457 min-replicas-to-write 1
[root@server3 utils]# /etc/init.d/redis_6379 restart ##重启服务
Stopping ...
Redis stopped
Starting Redis server...
[root@server4 ~]# tar zxf redis-5.0.3.tar.gz
[root@server4 ~]# cd redis-5.0.3
[root@server4 redis-5.0.3]# yum install gcc -y
[root@server4 redis-5.0.3]# make && make install
[root@server4 redis-5.0.3]# ls
00-RELEASENOTES deps README.md runtest-sentinel utils
BUGS INSTALL redis.conf sentinel.conf
CONTRIBUTING Makefile runtest src
COPYING MANIFESTO runtest-cluster tests
[root@server4 redis-5.0.3]# cd utils/
[root@server4 utils]# ./install_server.sh
Welcome to the redis service installer
This script will help you easily set up a running redis server
Please select the redis port for this instance: [6379]
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf]
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log]
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379]
Selected default - /var/lib/redis/6379
Please select the redis executable path [/usr/local/bin/redis-server]
Selected config:
Port : 6379
Config file : /etc/redis/6379.conf
Log file : /var/log/redis_6379.log
Data dir : /var/lib/redis/6379
Executable : /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!
[root@server4 utils]# vim /etc/redis/6379.conf
bind 0.0.0.0
slaveof 172.25.34.4 6379 ##设置master
[root@server4 utils]# /etc/init.d/redis_6379 restart
Stopping ...
Redis stopped
Starting Redis server...
[root@server4 utils]# netstat -antuple
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State User Inode PID/Program name
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN
[root@server3 utils]# redis-cli
127.0.0.1:6379> set name su
OK
127.0.0.1:6379> get name
"su"
[root@server4 utils]# redis-cli
127.0.0.1:6379> get name
"su"
127.0.0.1:6379> set age 18 ##备库默认只能读取不能写
\(error) READONLY You can't write against a read only replica.
在来一台备库server5(192.168.34.6)
[root@server3 ~]# scp -r redis-5.0.3 root@172.25.34.6:/root/
[root@server5 ~]# cd redis-5.0.3/
[root@server5 redis-5.0.3]# make install && make install
make[1]: Entering directory `/root/redis-5.0.3/src'
Hint: It's a good idea to run 'make test' ;)
INSTALL install
INSTALL install
INSTALL install
INSTALL install
INSTALL install
make[1]: Leaving directory `/root/redis-5.0.3/src'
[root@server5 redis-5.0.3]# cd utils/
[root@server5 utils]# ./install_server.sh
Welcome to the redis service installer
This script will help you easily set up a running redis server
Please select the redis port for this instance: [6379]
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf]
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log]
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379]
Selected default - /var/lib/redis/6379
Please select the redis executable path [/usr/local/bin/redis-server]
Selected config:
Port : 6379
Config file : /etc/redis/6379.conf
Log file : /var/log/redis_6379.log
Data dir : /var/lib/redis/6379
Executable : /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!
[root@server5 utils]# vim /etc/redis/6379.conf
bind 0.0.0.0
slaveof 172.25.34.4 6379
[root@server5 utils]# /etc/init.d/redis_6379 restart
Stopping ...
Redis stopped
Starting Redis server...
[root@server3 redis-5.0.3]# vi sentinel.conf
16 protected-mode no # 去掉保护模式
84 sentinel monitor mymaster 172.25.78.12 6379 2 # 指定要监控的master,2表示将这个主服务器判断为失效至少需要 2 个 Sentinel 同意
113 sentinel down-after-milliseconds mymaster 10000 # Sentinel 认为服务器已经断线所需的毫秒数即服务多长时间开始下线
121 sentinel parallel-syncs mymaster 1 # 在执行故障转移时,最多可以有多少个从服务器同时对新的主服务器进行同步, 这个数字越小, 完成故障转移所需的时间就越长
146 sentinel failover-timeout mymaster 180000 # 三分钟之内还没有完成故障切换,表示超时
[root@server3 redis-5.0.3]# cp sentinel.conf /etc/redis/
——————
[root@server3 redis-5.0.3]# scp sentinel.conf root@172.25.34.5:/etc/redis/
sentinel.conf 100% 9711 9.5KB/s 00:00
[root@server3 redis-5.0.3]# scp sentinel.conf root@172.25.34.6:/etc/redis/
sentinel.conf 100% 9711 9.5KB/s 00:00
注:改完这个文件之后千万不能重启
开启监控
[root@server3 redis]# redis-server /etc/redis/sentinel.conf --sentinel
11300:X 26 Oct 2019 16:06:05.421 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
11300:X 26 Oct 2019 16:06:05.421 # Redis version=5.0.3, bits=64, commit=00000000, modified=0, pid=11300, just started
11300:X 26 Oct 2019 16:06:05.421 # Configuration loaded
11300:X 26 Oct 2019 16:06:05.424 * Increased maximum number of open files to 10032 (it was originally set to 1024).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 5.0.3 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in sentinel mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 26379
| `-._ `._ / _.-' | PID: 11300
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
11300:X 26 Oct 2019 16:06:05.427 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
11300:X 26 Oct 2019 16:06:05.437 # Sentinel ID is f4e3b9ba514c6cdea98e555c046679e55ab6249d
11300:X 26 Oct 2019 16:06:05.437 # +monitor master mymaster 172.25.34.4 6379 quorum 2
11300:X 26 Oct 2019 16:06:05.440 * +slave slave 172.25.34.5:6379 172.25.34.5 6379 @ mymaster 172.25.34.4 6379
11300:X 26 Oct 2019 16:06:05.449 * +slave slave 172.25.34.6:6379 172.25.34.6 6379 @ mymaster 172.25.34.4 6379
[kiosk@foundation34 redis]$ ssh root@172.25.34.4
root@172.25.34.4's password:
Last login: Sat Oct 26 09:00:16 2019 from 172.25.34.250
[root@server3 ~]# redis-cli
127.0.0.1:6379> info
# Replication
role:master
connected_slaves:2
slave0:ip=172.25.34.5,port=6379,state=online,offset=11052,lag=1
slave1:ip=172.25.34.6,port=6379,state=online,offset=11052,lag=1
[root@server3 ~]# netstat -antuple
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State User Inode PID/Program name
tcp 0 0 0.0.0.0:26379 0.0.0.0:* LISTEN 0 249173 11300/redis-server
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN
[root@server3 ~]# redis-cli -p 26379
127.0.0.1:26379> info
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=172.25.34.4:6379,slaves=2,sentinels=3
[root@server3 ~]# redis-cli ##宕掉主库
127.0.0.1:6379> shutdown
在监控哨兵的地方可以看到,主库进行了切换
7658:X 26 Oct 2019 16:11:32.177 # +switch-master mymaster 172.25.34.4 6379 172.25.34.5 6379
7658:X 26 Oct 2019 16:11:32.178 * +slave slave 172.25.34.6:6379 172.25.34.6 6379 @ mymaster 172.25.34.5 6379
7658:X 26 Oct 2019 16:11:32.178 * +slave slave 172.25.34.4:6379 172.25.34.4 6379 @ mymaster 172.25.34.5 6379
7658:X 26 Oct 2019 16:11:42.219 # +sdown slave 172.25.34.4:6379 172.25.34.4 6379 @ mymaster 172.25.34.5 6379
[kiosk@foundation34 redis]$ ssh root@172.25.34.5
[root@server4 ~]# redis-cli
127.0.0.1:6379> info
# Replication
role:master
connected_slaves:1
slave0:ip=172.25.34.6,port=6379,state=online,offset=144738,lag=0
可以看到server4变为了主库
[root@server3 redis]# redis-server /etc/redis/sentinel.conf --sentinel &
[root@server4 redis]# redis-server /etc/redis/sentinel.conf --sentinel &
[root@server5 redis]# redis-server /etc/redis/sentinel.conf --sentinel &