1.主机名:
redis-01 10.20.9.183 redis-02 10.20.9.184 redis-03 10.20.9.185
2.源码安装(三台安装方式相同)
安装目录:/usr/local/
####################################################################################################################### [[email protected]-01 local]# cd /usr/local/ [[email protected]-01 local]# wget http://download.redis.io/releases/redis-3.2.6.tar.gz [root@redis-01 local]# tar -zxvf redis-3.2.6.tar.gz [[email protected]-01 local]# cd redis-3.2.6 [[email protected]-01 local]# make && make install 新建working directory,把需要用到的文件拷贝过去 cd /home/ mkdir redis mkdir redis/logs mkdir redis/data mv /usr/local/bin/redis-* /home/redis/
3.主节点配置文件(redis-6379.conf):
[root@redis-01 redis]# vim redis-6379.conf daemonize yes pidfile "/home/redis/logs/redis-6379.pid" port 6379 tcp-backlog 511 bind 0.0.0.0 timeout 0 tcp-keepalive 60 loglevel notice logfile "/home/redis/logs/redis-6379.log" databases 16 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename "dump-6379.rdb" dir "/home/redis/data" masterauth "abcdefg" slave-serve-stale-data yes slave-read-only yes repl-diskless-sync no repl-diskless-sync-delay 5 repl-disable-tcp-nodelay no repl-backlog-size 64mb slave-priority 100 requirepass "abcdefg" maxmemory 4gb maxmemory-policy noeviction appendonly yes appendfilename "appendonly-6379.aof" appendfsync everysec no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 80 auto-aof-rewrite-min-size 128mb aof-load-truncated yes lua-time-limit 5000 slowlog-log-slower-than 10000 slowlog-max-len 128 latency-monitor-threshold 0 notify-keyspace-events "" hash-max-ziplist-entries 512 hash-max-ziplist-value 64 list-max-ziplist-entries 512 list-max-ziplist-value 64 set-max-intset-entries 512 zset-max-ziplist-entries 128 zset-max-ziplist-value 64 hll-sparse-max-bytes 3000 activerehashing yes client-output-buffer-limit normal 0 0 0 client-output-buffer-limit slave 512mb 128mb 120 client-output-buffer-limit pubsub 32mb 8mb 60 hz 10 aof-rewrite-incremental-fsync yes [[email protected]-01 redis]#
4.从节点配置文件
从节点的配置文件和主基本一致,只需要在redis配置文件上添加上slaveof MASTER_IP PORT
# 设置当本机为slave服务时,设置master服务的IP地址及端口,在Redis启动时,它会自动从master进行数据同步 slaveof 10.20.9.183 6379 # 当master服务设置了密码保护时,slave需要设置服务连接master的密码 masterauth "abcdefg"
5.启动Redis实例
通过以下命令分别把Redis实例启动,启动顺序,先启动主节点,然后再启动从节点实例
cd /home/redis/ ./redis-server ./redis-6379.conf &
6.启动完一个redis实例记得先查看一下日志输出,看是否正常启动完成。如果是从节点实例启动会与主节点进行同步数据,同步成功会在日志后面有以下输出:
12484:S 15 Mar 16:50:28.425 * Connecting to MASTER 10.20.9.185:6379 12484:S 15 Mar 16:50:28.425 * MASTER <-> SLAVE sync started 12484:S 15 Mar 16:50:28.425 * Non blocking connect for SYNC fired the event. 12484:S 15 Mar 16:50:28.425 * Master replied to PING, replication can continue... 12484:S 15 Mar 16:50:28.425 * Partial resynchronization not possible (no cached master) 12484:S 15 Mar 16:50:28.427 * Full resync from master: 4b2d98696c405d540d0cd6aece20e50b10894021:22401 12484:S 15 Mar 16:50:28.480 * MASTER <-> SLAVE sync: receiving 87 bytes from master 12484:S 15 Mar 16:50:28.480 * MASTER <-> SLAVE sync: Flushing old data 12484:S 15 Mar 16:50:28.480 * MASTER <-> SLAVE sync: Loading DB in memory 12484:S 15 Mar 16:50:28.480 * MASTER <-> SLAVE sync: Finished with success
7.哨兵配置
cd /home/redis/
mkdir sentinel
然后分别在机器上建立sentinel.conf文件如下:
[root@redis-01 redis]# cat sentinel/sentinel.conf ###################################### port 26371 dir "/tmp" logfile "/home/redis/sentinel/sentinel.log" protected-mode no sentinel monitor x_master 10.20.9.183 6379 2 sentinel down-after-milliseconds x_master 15000 sentinel auth-pass x_master abcdefg sentinel config-epoch x_master 2 sentinel leader-epoch x_master 2
8.在三个服务器中以sentinel模式启动redis-server
# cd /home/redis/
#./redis-sentinel sentinel/sentinel.conf &
Redis-Sentinel命令格式
格式:sentinel <option_name> <master_name> <option_value>; #该行的意思是:监控的master的名字叫做x_master(自定义),地址为10.20.9.183 6379,行尾最后的一个2代表在sentinel集群中,多少个sentinel认为masters死了,才能真正认为该master不可用了。 sentinel monitor x_master 10.20.9.183 6379 2 #sentinel会向master发送心跳PING来确认master是否存活,如果master在“一定时间范围”内不回应PONG 或者是回复了一个错误消息,那么这个sentinel会主观地(单方面地)认为这个master已经不可用了(subjectively down, 也简称为SDOWN)。而这个down-after-milliseconds就是用来指定这个“一定时间范围”的,单位是毫秒,默认30秒。 sentinel down-after-milliseconds x_master 15000 #failover过期时间,当failover开始后,在此时间内仍然没有触发任何failover操作,当前sentinel将会认为此次failoer失败。默认180秒,即3分钟。 sentinel failover-timeout x_master 120000 #在发生failover主备切换时,这个选项指定了最多可以有多少个slave同时对新的master进行同步,这个数字越小,完成failover所需的时间就越长,但是如果这个数字越大,就意味着越多的slave因为replication而不可用。可以通过将这个值设为 1 来保证每次只有一个slave处于不能处理命令请求的状态。 sentinel parallel-syncs x_master 1 #sentinel 连接设置了密码的主和从 sentinel auth-pass x_master abcdefg
参考文档:https://blog.csdn.net/drizzt0878/article/details/73826088
原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/9198.html