这里使用的环境是 阿里云的服务器(SUSE)Linux版本.
这里要使用服务器的外网ip,假设我这里的外网ip为:172.19.105.188
| ip| port | 配置文件| 持久化数据
| ------------- |:------------- -----
|172.19.105.188 | 7001 (master) | /home/redis/redis-cluster/7001/redis.conf | /home/redis/redis-cluster/7001/data
| 172.19.105.188 | 7002 (slave1) | /home/redis/redis-cluster/7002/redis.conf| /home/redis/redis-cluster/7001/data
| 172.19.105.188 | 7003 (slave2)| /home/redis/redis-cluster/7003/redis.conf| /home/redis/redis-cluster/7001/data
说明 | ip | 端口 |
---|---|---|
哨兵1(sentinel1) | 172.19.105.188 | 27001 |
哨兵2(sentinel2) | 172.19.105.188 | 27002 |
哨兵3(sentinel3) | 172.19.105.188 | 27003 |
我这里使用的是redis版本是: redis-4.0.6.tar.gz.
使用下列命令,下载压缩包
-- 下载压缩包
# wget http://download.redis.io/releases/redis-4.0.6.tar.gz
-- 解压缩
tar -zxvf redis-4.0.6.tar.gz
-- 进入解压后的redis目录,编译和安装
# make
-- 安装
# make install PREFIX=/home/redis/redis-4.0.6
-- 创建目录,并分别创建 7001 7002 7003三个目录,每个目录下,创建 data temp 目录
# mkdir redis-cluster
-- 切换路径到安装好的redis目录中,发现只有一个bin目录,里面有redis的操作命令
# cp bin ../redis-cluster/7001 -r
# cp bin ../redis-cluster/7002 -r
# cp bin ../redis-cluster/7003 -r
# 复制解压后的redis中的redis.conf 和 sentinel.conf文件到 7001 7002 7003目录中.
到此,配置前的准备工作完毕,接下来,就要进行主从配置,哨兵配置.
修改 redis.conf文件
主redis 7001目录:
bind 0.0.0.0
protected-mode no
port 7001
daemonize yes
pidfile /var/run/redis_7001.pid
dir /home/redis/redis-cluster/7001/data
slave-read-only yes
slave-priority 100
appendonly yes
appendfilename "appendonly_7001.aof"
appendfsync everysec
从slave1 7002:
bind 0.0.0.0
protected-mode no
port 7002
daemonize yes
pidfile /var/run/redis_7001.pid
dir /home/redis/redis-cluster/7001/data
slave-read-only yes
slaveof 172.19.105.188 7001
slave-priority 90
appendonly yes
appendfilename "appendonly_7001.aof"
appendfsync everysec
相比 master 增加了
slaveof 172.19.105.188 7001
slave-priority 90 (将权重降为90)
从slave2配置与slave1的配置相同,唯一变化的是:
slave-priority 80
到此,主从配置结束,master既可以读,也可以写,而 从服务器是只可以读,不可写的.
protected-mode no
port 27001
dir "/home/redis/redis-cluster/7001/temp"
sentinel monitor redis1 172.19.105.188 7001 2
sentinel down-after-milliseconds redis1 10000
sentinel failover-timeout redis1 60000
protected-mode no
port 27002
dir "/home/redis/redis-cluster/7001/temp"
sentinel monitor redis1 172.19.105.188 7001 2
sentinel down-after-milliseconds redis1 10000
sentinel failover-timeout redis1 60000
protected-mode no
port 27003
dir "/home/redis/redis-cluster/7001/temp"
sentinel monitor redis1 172.19.105.188 7001 2
sentinel down-after-milliseconds redis1 10000
sentinel failover-timeout redis1 60000
哨兵配置完成.
./redis-server ../redis.conf
./redis-server ../sentinel.conf --sentinel
在bin目录下,查看 主从信息:
redis@iZuf67l40rbz5veggwpb5xZ:/home/redis/redis-cluster/7001/bin> ./redis-cli -h 172.19.105.188 -p 7001 info Replication
# Replication
role:master
connected_slaves:2
slave0:ip=172.19.105.188,port=7002,state=online,offset=3668038,lag=0
slave1:ip=172.19.105.188,port=7003,state=online,offset=3668177,lag=0
master_replid:ecf0e258f9ccc0c3de2e1d1599e914511555d05c
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:3668177
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2619602
repl_backlog_histlen:1048576
查看 7002 信息:
redis@iZuf67l40rbz5veggwpb5xZ:/home/redis/redis-cluster/7001/bin> ./redis-cli -h 172.19.105.188 -p 7002 info Replication
# Replication
role:slave
master_host:172.19.105.188
master_port:7001
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:3687207
slave_priority:90
slave_read_only:1
connected_slaves:0
master_replid:ecf0e258f9ccc0c3de2e1d1599e914511555d05c
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:3687207
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2638632
repl_backlog_histlen:1048576
查看slave2的信息:
redis@iZuf67l40rbz5veggwpb5xZ:/home/redis/redis-cluster/7001/bin> ./redis-cli -h 172.19.105.188 -p 7003 info Replication
# Replication
role:slave
master_host:172.19.105.188
master_port:7001
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:3696868
slave_priority:80
slave_read_only:1
connected_slaves:0
master_replid:ecf0e258f9ccc0c3de2e1d1599e914511555d05c
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:3696868
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2648293
repl_backlog_histlen:1048576
redis@iZuf67l40rbz5veggwpb5xZ:/home/redis/redis-cluster/7001/bin> ./redis-cli -h 172.19.105.188 -p 7001
172.19.105.188:7001> set k1 v1
OK
172.19.105.188:7001> get k1
"v1"
172.19.105.188:7001>
redis@iZuf67l40rbz5veggwpb5xZ:/home/redis/redis-cluster/7001/bin> ./redis-cli -h 172.19.105.188 -p 7002
172.19.105.188:7002> set k2 v2
(error) READONLY You can't write against a read only slave.
172.19.105.188:7002> get k1
"v1"
172.19.105.188:7002>
redis@iZuf67l40rbz5veggwpb5xZ:/home/redis/redis-cluster/7001/bin> ./redis-cli -h 172.19.105.188 -p 7003
172.19.105.188:7003> set k2 v2
(error) READONLY You can't write against a read only slave.
172.19.105.188:7003> get k1
"v1"
172.19.105.188:7003>
这时,我们把7001的服务停掉,按照我们配置的 “slave-priority"的值,值小的7003(slave2)将成为新的master.7002成为7003的slave.
然后,我们再将 7001的redis服务起来,发现7001并没有成为master,而是成为了 7003的slave2,这也就是我们常说的"反客为主”.这里大家可以自己动手试一下,本文就不进行描述了.