执行如下命令:git
➜ ~ mkdir redisSentinel ➜ ~ cd redisSentinel ➜ redisSentinel mkdir master ➜ redisSentinel cp /usr/local/redis-3.2.8/redis.conf master/redis.conf
此时主节点目录已经建立好了,而后,配置主节点配置:redis
port 8000 daemonize yes
就简单修改这两个配置好了。 而后自动这个主节点:测试
➜ redisSentinel cd master ➜ master redis-server redis.conf
检查一下:spa
➜ master redis-cli -p 8000 ping PONG
主节点就行了,接下来建立从节点。命令行
➜ master cd .. ➜ redisSentinel mkdir slave
拷贝配置文件到从节点中:code
➜ redisSentinel cp master/redis.conf slave/redis-8001.conf ➜ redisSentinel cp master/redis.conf slave/redis-8002.conf
这里咱们将两个从节点的端口定为 8001 和 8002,而后修改配置。server
port 800X damonnize yes slaveof 127.0.0.1 8000
加入了一个 slaveof 配置,代表该节点的主节点是 127.0.0.1:8000.blog
而后启动:ip
➜ redisSentinel cd slave ➜ slave redis-server redis-8001.conf ➜ slave redis-server redis-8002.conf
如今主从节点已经关联好了,如今经过 info 命令看看是否成功。部署
➜ slave redis-cli -p 8000 info replication # Replication role:master connected_slaves:2 slave0:ip=127.0.0.1,port=8001,state=online,offset=505,lag=1 slave1:ip=127.0.0.1,port=8002,state=online,offset=505,lag=1 master_repl_offset:505 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:2 repl_backlog_histlen:504
进入了 8000 主节点中查看,看见 role 是 master,而且有 2 个从节点,8001 和 8002. 成功!
接着开始部署哨兵节点.
首先建立配置文件:
➜ slave cd .. ➜ redisSentinel mkdir sentinel ➜ redisSentinel cd sentinel ➜ sentinel cp /usr/local/redis-3.2.8/redis.conf redis-sentinel-26379.conf ➜ sentinel cp /usr/local/redis-3.2.8/redis.conf redis-sentinel-26380.conf ➜ sentinel cp /usr/local/redis-3.2.8/redis.conf redis-sentinel-26381.conf
哨兵节点的默认端口是 26739。咱们这里弄了 3 个哨兵节点。
而后修改配置文件:
port 26379 daemonize yes sentinel monitor mymaster 127.0.0.1 8000 2
这里比较关键的是:sentinel monitor mymaster 127.0.0.1 8000 2
,这个配置表示该哨兵节点须要监控 8000 这个主节点, 2 表明着判断主节点失败至少须要 2 个 Sentinel 节点赞成。
启动 Sentinel 节点:
➜ sentinel redis-sentinel redis-sentinel-26379.conf ➜ sentinel redis-sentinel redis-sentinel-26380.conf ➜ sentinel redis-sentinel redis-sentinel-26381.conf
启动成功后,看看哨兵的相关信息:
➜ sentinel redis-cli -p 26380 info sentinel # 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=127.0.0.1:8000,slaves=2,sentinels=3
经过进入 26380 节点,使用 info Sentinel 命令,看到有个 master 节点,名称是 mymaster,地址是咱们刚刚配置的 8000, 从节点有 2 个,哨兵有 3 个。
此时一个高可用的 Redis 集群就搭建好了。
固然若是是生产环境,全部实例建议部署在不一样的机器上。
部署后的拓扑图以下:
先在命令行往主节点写入一条数据:
➜ cachecloud git:(master) ✗ redis-cli -p 8000 set "hello" "world" OK
Jedis 代码测试:
public static void main(String[] args) { Set<String> sentinelSet = new HashSet<>(); sentinelSet.add("127.0.0.1:26379"); sentinelSet.add("127.0.0.1:26380"); sentinelSet.add("127.0.0.1:26381"); String masterName = "mymaster"; JedisSentinelPool sentinelPool = new JedisSentinelPool(masterName, sentinelSet, new GenericObjectPoolConfig(), 10000, 10000, null, Protocol.DEFAULT_DATABASE); System.out.println(sentinelPool.getResource().get("hello")); }
结果:
world
成功!!!
Sentinel 节点实际上就是个特殊的 Redis 节点。 回头看看搭建过程:
就好啦。