下面是开发环境、测试环境,单台服务器搭建redis-sentinel集群

Redis端口:6379、6380、6381

Sentinel端口:16379、16380、16381

直接拿下面的配置文件就可以跑起来。

安装redis4.0

  1. wget http://download.redis.io/releases/redis-4.0.10.tar.gz
  2. tar xvf redis-4.0.10.tar.gz -C /app
  3. cd /app/redis-4.0.10
  4. make MALLOC=libc
  5. cd src
  6. make install

进入/app/redis-4.0.10,原有的redis.conf、sentinel.conf文件不要动,添加集群所需的文件redis1.conf、redis2.conf、redis3.conf、sentinel1.conf、sentinel2.conf、sentinel3.conf,各个文件配置如下。

redis1.conf

  1. bind 127.0.0.1
  2. protected-mode yes
  3. port 6379
  4. daemonize no
  5. supervised systemd
  6. pidfile /var/run/redis_6379.pid
  7. loglevel notice
  8. logfile "/app/redis-data/redis-master/redis.log"
  9. databases 16
  10. #always-show-logo yes
  11. save 900 1
  12. save 300 10
  13. save 60 10000
  14. stop-writes-on-bgsave-error yes
  15. rdbcompression yes
  16. rdbchecksum yes
  17. dbfilename dump.rdb
  18. dir "/app/redis-data/redis-master"
  19. #replica-serve-stale-data yes
  20. #replica-read-only yes
  21. repl-diskless-sync no
  22. repl-diskless-sync-delay 5
  23. repl-disable-tcp-nodelay no
  24. #replica-priority 100
  25. #lazyfree-lazy-eviction no
  26. #lazyfree-lazy-expire no
  27. #lazyfree-lazy-server-del no
  28. #replica-lazy-flush no
  29. appendonly no
  30. appendfilename "appendonly.aof"
  31. appendfsync everysec
  32. no-appendfsync-on-rewrite no
  33. auto-aof-rewrite-percentage 100
  34. auto-aof-rewrite-min-size 64mb
  35. aof-load-truncated yes
  36. #aof-use-rdb-preamble yes
  37. lua-time-limit 5000
  38. slowlog-log-slower-than 10000
  39. slowlog-max-len 128
  40. latency-monitor-threshold 0
  41. notify-keyspace-events ""
  42. hash-max-ziplist-entries 512
  43. hash-max-ziplist-value 64
  44. list-max-ziplist-size -2
  45. list-compress-depth 0
  46. set-max-intset-entries 512
  47. zset-max-ziplist-entries 128
  48. zset-max-ziplist-value 64
  49. hll-sparse-max-bytes 3000
  50. #stream-node-max-bytes 4096
  51. #stream-node-max-entries 100
  52. activerehashing yes
  53. client-output-buffer-limit normal 0 0 0
  54. #client-output-buffer-limit replica 256mb 64mb 60
  55. client-output-buffer-limit pubsub 32mb 8mb 60
  56. hz 10
  57. #dynamic-hz yes
  58. aof-rewrite-incremental-fsync yes
  59. #rdb-save-incremental-fsync yes
  60. masterauth 123456
  61. requirepass 123456

redis2.conf

  1. bind 127.0.0.1
  2. protected-mode yes
  3. port 6380
  4. daemonize yes
  5. supervised systemd
  6. pidfile /var/run/redis_6380.pid
  7. loglevel notice
  8. logfile "/app/redis-data/redis-slave1/redis.log"
  9. databases 16
  10. save 900 1
  11. save 300 10
  12. save 60 10000
  13. stop-writes-on-bgsave-error yes
  14. rdbcompression yes
  15. rdbchecksum yes
  16. dbfilename dump.rdb
  17. dir "/app/redis-data/redis-slave1"
  18. repl-diskless-sync no
  19. repl-diskless-sync-delay 5
  20. repl-disable-tcp-nodelay no
  21. appendonly no
  22. appendfilename "appendonly.aof"
  23. appendfsync everysec
  24. no-appendfsync-on-rewrite no
  25. auto-aof-rewrite-percentage 100
  26. auto-aof-rewrite-min-size 64mb
  27. aof-load-truncated yes
  28. lua-time-limit 5000
  29. slowlog-log-slower-than 10000
  30. slowlog-max-len 128
  31. latency-monitor-threshold 0
  32. notify-keyspace-events ""
  33. hash-max-ziplist-entries 512
  34. hash-max-ziplist-value 64
  35. list-max-ziplist-size -2
  36. list-compress-depth 0
  37. set-max-intset-entries 512
  38. zset-max-ziplist-entries 128
  39. zset-max-ziplist-value 64
  40. hll-sparse-max-bytes 3000
  41. activerehashing yes
  42. client-output-buffer-limit normal 0 0 0
  43. client-output-buffer-limit pubsub 32mb 8mb 60
  44. hz 10
  45. aof-rewrite-incremental-fsync yes
  46. masterauth 123456
  47. requirepass 123456
  48. slaveof 127.0.0.1 6379

redis3.conf

  1. bind 127.0.0.1
  2. protected-mode yes
  3. port 6381
  4. daemonize yes
  5. supervised systemd
  6. pidfile /var/run/redis_6381.pid
  7. loglevel notice
  8. logfile "/app/redis-data/redis-slave2/redis.log"
  9. databases 16
  10. save 900 1
  11. save 300 10
  12. save 60 10000
  13. stop-writes-on-bgsave-error yes
  14. rdbcompression yes
  15. rdbchecksum yes
  16. dbfilename dump.rdb
  17. dir "/app/redis-data/redis-slave2"
  18. repl-diskless-sync no
  19. repl-diskless-sync-delay 5
  20. repl-disable-tcp-nodelay no
  21. appendonly no
  22. appendfilename "appendonly.aof"
  23. appendfsync everysec
  24. no-appendfsync-on-rewrite no
  25. auto-aof-rewrite-percentage 100
  26. auto-aof-rewrite-min-size 64mb
  27. aof-load-truncated yes
  28. lua-time-limit 5000
  29. slowlog-log-slower-than 10000
  30. slowlog-max-len 128
  31. latency-monitor-threshold 0
  32. notify-keyspace-events ""
  33. hash-max-ziplist-entries 512
  34. hash-max-ziplist-value 64
  35. list-max-ziplist-size -2
  36. list-compress-depth 0
  37. set-max-intset-entries 512
  38. zset-max-ziplist-entries 128
  39. zset-max-ziplist-value 64
  40. hll-sparse-max-bytes 3000
  41. activerehashing yes
  42. client-output-buffer-limit normal 0 0 0
  43. client-output-buffer-limit pubsub 32mb 8mb 60
  44. hz 10
  45. aof-rewrite-incremental-fsync yes
  46. masterauth 123456
  47. requirepass 123456
  48. slaveof 127.0.0.1 6379

下面是sentinel的配置文件

sentinel1.conf

  1. port 16379
  2. daemonize yes
  3. bind 172.11.26.205
  4. logfile "/app/redis-data/redis-master/redis-sentinel.log"
  5. sentinel myid 454634552802f3864f3477883933e7aa803e4808
  6. sentinel monitor mymaster 172.11.26.205 6379 2
  7. sentinel failover-timeout mymaster 10000
  8. sentinel auth-pass mymaster Bluesea#redis?123
  9. protected-mode yes
  10. sentinel config-epoch mymaster 0
  11. # Generated by CONFIG REWRITE
  12. dir "/root"
  13. sentinel leader-epoch mymaster 377
  14. sentinel known-slave mymaster 172.11.26.205 6381
  15. sentinel known-slave mymaster 172.11.26.205 6380
  16. sentinel known-sentinel mymaster 172.11.26.205 26381 962a9cf6afa20726baf3d97d326ce87d0469c4ae
  17. sentinel known-sentinel mymaster 172.11.26.205 26380 1e48ed185e5e514510029c6dad49320b793d41b6
  18. sentinel current-epoch 377

sentinel2.conf

  1. port 16380
  2. daemonize yes
  3. bind 172.11.26.205
  4. logfile "/app/redis-data/redis-slave1/redis-sentinel.log"
  5. sentinel myid 1e48ed185e5e514510029c6dad49320b793d41b6
  6. sentinel monitor mymaster 172.11.26.205 6379 2
  7. sentinel failover-timeout mymaster 10000
  8. protected-mode yes
  9. sentinel auth-pass mymaster Bluesea#redis?123
  10. # Generated by CONFIG REWRITE
  11. dir "/root"
  12. sentinel config-epoch mymaster 0
  13. sentinel leader-epoch mymaster 381
  14. sentinel known-slave mymaster 172.11.26.205 6381
  15. sentinel known-slave mymaster 172.11.26.205 6380
  16. sentinel known-sentinel mymaster 172.11.26.205 26381 962a9cf6afa20726baf3d97d326ce87d0469c4ae
  17. sentinel known-sentinel mymaster 172.11.26.205 26379 454634552802f3864f3477883933e7aa803e4808
  18. sentinel current-epoch 381

sentinel3.conf

  1. port 16381
  2. daemonize yes
  3. bind 172.11.26.205
  4. logfile "/app/redis-data/redis-slave2/redis-sentinel.log"
  5. sentinel myid 962a9cf6afa20726baf3d97d326ce87d0469c4ae
  6. sentinel monitor mymaster 172.11.26.205 6379 2
  7. sentinel failover-timeout mymaster 10000
  8. protected-mode yes
  9. sentinel auth-pass mymaster Bluesea#redis?123
  10. # Generated by CONFIG REWRITE
  11. dir "/root"
  12. sentinel config-epoch mymaster 0
  13. sentinel leader-epoch mymaster 383
  14. sentinel known-slave mymaster 172.11.26.205 6380
  15. sentinel known-slave mymaster 172.11.26.205 6381
  16. sentinel known-sentinel mymaster 172.11.26.205 26379 454634552802f3864f3477883933e7aa803e4808
  17. sentinel known-sentinel mymaster 172.11.26.205 26380 1e48ed185e5e514510029c6dad49320b793d41b6
  18. sentinel current-epoch 383

redis启动脚本—-redis.sh

  1. #!/bin/bash
  2. nohup /app/redis-4.0.10/src/redis-server /app/redis-4.0.10/redis1.conf >output 2>&1 &
  3. nohup /app/redis-4.0.10/src/redis-server /app/redis-4.0.10/redis2.conf >output 2>&1 &
  4. nohup /app/redis-4.0.10/src/redis-server /app/redis-4.0.10/redis3.conf >output 2>&1 &

sentinel启动脚本—-sentinel.sh

  1. #!/bin/bash
  2. nohup /app/redis-4.0.10/src/redis-sentinel /app/redis-4.0.10/sentinel1.conf > output 2>&1 &
  3. nohup /app/redis-4.0.10/src/redis-sentinel /app/redis-4.0.10/sentinel2.conf > output 2>&1 &
  4. nohup /app/redis-4.0.10/src/redis-sentinel /app/redis-4.0.10/sentinel3.conf > output 2>&1 &