安装redis;
哨兵原理介绍
Sentinel 哨兵,上其他网站很多
配置主:
redis-cli --cluster create 172.16.71.183:7001 172.16.71.183:7002 172.16.71.183:7003 --cluster-replicas 0
这里--cluster-replicas 0表示不为以上主建立从【我们自己手动添加】;
从配置
注意:从配置需要添加masterauth xxxxx 添加主的密码,否则下面的命令执行完后,任然会是失败的;
往集群添加从:
redis-cli --cluster add-node 172.16.71.183:7006 172.16.71.183:7001 --cluster-slave --cluster-master-id 18b31f22eccc86ca13bcd992776cd3987b572441 -a password
配置哨兵
哨兵的可执行文件就是redis-server,哨兵配置示例:
# Example sentinel.conf
# port <sentinel-port>
# The port that this sentinel instance will run on
port 27000
# dir <working-directory>
# Every long running process should have a well-defined working directory.
# For Redis Sentinel to chdir to /tmp at startup is the simplest thing
# for the process to don't interfere with administrative tasks such as
# unmounting filesystems.
dir /tmp
# sentinel monitor <master-name> <ip> <redis-port> <quorum>
#
# Tells Sentinel to monitor this master, and to consider it in O_DOWN
# (Objectively Down) state only if at least <quorum> sentinels agree.
#
# Note that whatever is the ODOWN quorum, a Sentinel will require to
# be elected by the majority of the known Sentinels in order to
# start a failover, so no failover can be performed in minority.
#
# Slaves are auto-discovered, so you don't need to specify slaves in
# any way. Sentinel itself will rewrite this configuration file adding
# the slaves using additional configuration options.
# Also note that the configuration file is rewritten when a
# slave is promoted to master.
#
# Note: master name should not include special characters or spaces.
# The valid charset is A-z 0-9 and the three characters ".-_".
sentinel monitor master1 127.0.0.1 7000 1
sentinel monitor master2 127.0.0.1 7004 1
sentinel monitor master3 127.0.0.1 7005 1
# sentinel down-after-milliseconds <master-name> <milliseconds>
#
# Number of milliseconds the master (or any attached slave or sentinel) should
# be unreachable (as in, not acceptable reply to PING, continuously, for the
# specified period) in order to consider it in S_DOWN state (Subjectively
# Down).
#
# Default is 30 seconds.
sentinel down-after-milliseconds master1 30000
sentinel down-after-milliseconds master2 30000
sentinel down-after-milliseconds master3 30000
# sentinel parallel-syncs <master-name> <numslaves>
#
# How many slaves we can reconfigure to point to the new slave simultaneously
# during the failover. Use a low number if you use the slaves to serve query
# to avoid that all the slaves will be unreachable at about the same
# time while performing the synchronization with the master.
sentinel parallel-syncs master1 1
sentinel parallel-syncs master2 1
sentinel parallel-syncs master3 1
# sentinel failover-timeout <master-name> <milliseconds>
#
# Specifies the failover timeout in milliseconds. It is used in many ways:
#
# - The time needed to re-start a failover after a previous failover was
# already tried against the same master by a given Sentinel, is two
# times the failover timeout.
#
# - The time needed for a slave replicating to a wrong master according
# to a Sentinel current configuration, to be forced to replicate
# with the right master, is exactly the failover timeout (counting since
# the moment a Sentinel detected the misconfiguration).
#
# - The time needed to cancel a failover that is already in progress but
# did not produced any configuration change (SLAVEOF NO ONE yet not
# acknowledged by the promoted slave).
#
# - The maximum time a failover in progress waits for all the slaves to be
# reconfigured as slaves of the new master. However even after this time
# the slaves will be reconfigured by the Sentinels anyway, but not with
# the exact parallel-syncs progression as specified.
#
# Default is 3 minutes.
sentinel failover-timeout master1 180000
sentinel failover-timeout master2 180000
sentinel failover-timeout master3 180000
其中master1 后面的就是主的ip和端口
好了后,执行redis-sentinel sentinel1.conf --sentinel即可;
如果存在问题,最好在conf中添加上logfile的配置,以便查看问题;
redis-cli命令后,可以看集群状态:
cluster nodes:列出集群所有节点;
info:查看本节点状态,可以看到是master还是slave,还有slave连接的个数【之前没配置masterauth,所以一直没连接上,但是也不报错】;另外注意:slave的配置文件中,不能把slaveof配置打开,但是要配masterauth,之前一直以为这个slaveof没开,所以masterauth也不用设置,真是坑!!!