redis6.2.5 sentinel集群 + keepalived 主从自动切换和高可用

淡淡的烟草味﹌ 2022-09-09 00:17 220阅读 0赞

环境


























ip 服务 端口
100.200.100.111 redis redis-sentinel 6379 26379
100.200.100.112 redis redis-sentinel 6379 26379
100.200.100.113 redis redis-sentinel 6379 26379

下载安装redis

  1. yum install -y gcc-c++ make tcl
  2. wget http://download.redis.io/releases/redis-6.2.5.tar.gz
  3. tar zxvf redis-6.2.5.tar.gz
  4. cd redis-6.2.5
  5. make

部署redis

操作3台机器
100.200.100.111:

  1. [root@JUMPSER-100-200-100-111 jumpserver-installer-v2.13.1]# egrep -v "^#|^$" /etc/redis.conf
  2. bind 0.0.0.0
  3. protected-mode no
  4. port 6379
  5. tcp-backlog 511
  6. timeout 0
  7. tcp-keepalive 300
  8. daemonize yes
  9. pidfile "/var/run/redis.pid"
  10. loglevel notice
  11. logfile "/var/log/redis/redis.log"
  12. databases 16
  13. always-show-logo no
  14. set-proc-title yes
  15. proc-title-template "{title} {listen-addr} {server-mode}"
  16. stop-writes-on-bgsave-error yes
  17. rdbcompression yes
  18. rdbchecksum yes
  19. dbfilename "dump.rdb"
  20. rdb-del-sync-files no
  21. dir "/var/lib/redis"
  22. replicaof 10.20.100.113 6379
  23. masterauth "123456"
  24. replica-serve-stale-data yes
  25. replica-read-only yes
  26. repl-diskless-sync no
  27. repl-diskless-sync-delay 5
  28. repl-diskless-load disabled
  29. repl-ping-replica-period 10
  30. repl-timeout 60
  31. repl-disable-tcp-nodelay no
  32. replica-priority 100
  33. acllog-max-len 128
  34. requirepass "123456"
  35. maxmemory 4gb
  36. maxmemory-policy allkeys-lru
  37. lazyfree-lazy-eviction no
  38. lazyfree-lazy-expire no
  39. lazyfree-lazy-server-del no
  40. replica-lazy-flush no
  41. lazyfree-lazy-user-del no
  42. lazyfree-lazy-user-flush no
  43. oom-score-adj no
  44. oom-score-adj-values 0 200 800
  45. disable-thp yes
  46. appendonly yes
  47. appendfilename "appendonly.aof"
  48. appendfsync everysec
  49. no-appendfsync-on-rewrite no
  50. auto-aof-rewrite-percentage 100
  51. auto-aof-rewrite-min-size 64mb
  52. aof-load-truncated yes
  53. aof-use-rdb-preamble yes
  54. lua-time-limit 5000
  55. slowlog-log-slower-than 10000
  56. slowlog-max-len 128
  57. latency-monitor-threshold 0
  58. notify-keyspace-events ""
  59. hash-max-ziplist-entries 512
  60. hash-max-ziplist-value 64
  61. list-max-ziplist-size -2
  62. list-compress-depth 0
  63. set-max-intset-entries 512
  64. zset-max-ziplist-entries 128
  65. zset-max-ziplist-value 64
  66. hll-sparse-max-bytes 3000
  67. stream-node-max-bytes 4kb
  68. stream-node-max-entries 100
  69. activerehashing yes
  70. client-output-buffer-limit normal 0 0 0
  71. client-output-buffer-limit replica 256mb 64mb 60
  72. client-output-buffer-limit pubsub 32mb 8mb 60
  73. hz 10
  74. dynamic-hz yes
  75. aof-rewrite-incremental-fsync yes
  76. rdb-save-incremental-fsync yes
  77. jemalloc-bg-thread yes
  78. save 3600 1
  79. save 300 100
  80. save 60 10000

100.200.100.112:

  1. [root@JUMPSER-100-200-100-112 jumpserver-installer-v2.13.1]# egrep -v "^#|^$" /etc/redis.conf
  2. bind 0.0.0.0
  3. protected-mode no
  4. port 6379
  5. tcp-backlog 511
  6. timeout 0
  7. tcp-keepalive 300
  8. daemonize yes
  9. pidfile "/var/run/redis.pid"
  10. loglevel notice
  11. logfile "/var/log/redis/redis.log"
  12. databases 16
  13. always-show-logo no
  14. set-proc-title yes
  15. proc-title-template "{title} {listen-addr} {server-mode}"
  16. stop-writes-on-bgsave-error yes
  17. rdbcompression yes
  18. rdbchecksum yes
  19. dbfilename "dump.rdb"
  20. rdb-del-sync-files no
  21. dir "/var/lib/redis"
  22. masterauth "123456"
  23. replica-serve-stale-data yes
  24. replica-read-only yes
  25. repl-diskless-sync no
  26. repl-diskless-sync-delay 5
  27. repl-diskless-load disabled
  28. repl-ping-replica-period 10
  29. repl-timeout 60
  30. repl-disable-tcp-nodelay no
  31. replica-priority 100
  32. acllog-max-len 128
  33. requirepass "123456"
  34. maxmemory 4gb
  35. maxmemory-policy allkeys-lru
  36. lazyfree-lazy-eviction no
  37. lazyfree-lazy-expire no
  38. lazyfree-lazy-server-del no
  39. replica-lazy-flush no
  40. lazyfree-lazy-user-del no
  41. lazyfree-lazy-user-flush no
  42. oom-score-adj no
  43. oom-score-adj-values 0 200 800
  44. disable-thp yes
  45. appendonly yes
  46. appendfilename "appendonly.aof"
  47. appendfsync everysec
  48. no-appendfsync-on-rewrite no
  49. auto-aof-rewrite-percentage 100
  50. auto-aof-rewrite-min-size 64mb
  51. aof-load-truncated yes
  52. aof-use-rdb-preamble yes
  53. lua-time-limit 5000
  54. slowlog-log-slower-than 10000
  55. slowlog-max-len 128
  56. latency-monitor-threshold 0
  57. notify-keyspace-events ""
  58. hash-max-ziplist-entries 512
  59. hash-max-ziplist-value 64
  60. list-max-ziplist-size -2
  61. list-compress-depth 0
  62. set-max-intset-entries 512
  63. zset-max-ziplist-entries 128
  64. zset-max-ziplist-value 64
  65. hll-sparse-max-bytes 3000
  66. stream-node-max-bytes 4kb
  67. stream-node-max-entries 100
  68. activerehashing yes
  69. client-output-buffer-limit normal 0 0 0
  70. client-output-buffer-limit replica 256mb 64mb 60
  71. client-output-buffer-limit pubsub 32mb 8mb 60
  72. hz 10
  73. dynamic-hz yes
  74. aof-rewrite-incremental-fsync yes
  75. rdb-save-incremental-fsync yes
  76. jemalloc-bg-thread yes
  77. save 3600 1
  78. save 300 100
  79. save 60 10000

100.200.100.113:

  1. [root@JUMPSER-100-200-100-113 0]# egrep -v "^#|^$" /etc/redis.conf
  2. bind 0.0.0.0
  3. protected-mode no
  4. port 6379
  5. tcp-backlog 511
  6. timeout 0
  7. tcp-keepalive 300
  8. daemonize yes
  9. pidfile "/var/run/redis.pid"
  10. loglevel notice
  11. logfile "/var/log/redis/redis.log"
  12. databases 16
  13. always-show-logo no
  14. set-proc-title yes
  15. proc-title-template "{title} {listen-addr} {server-mode}"
  16. stop-writes-on-bgsave-error yes
  17. rdbcompression yes
  18. rdbchecksum yes
  19. dbfilename "dump.rdb"
  20. rdb-del-sync-files no
  21. dir "/var/lib/redis"
  22. masterauth "123456"
  23. replica-serve-stale-data yes
  24. replica-read-only yes
  25. repl-diskless-sync no
  26. repl-diskless-sync-delay 5
  27. repl-diskless-load disabled
  28. repl-ping-replica-period 10
  29. repl-timeout 60
  30. repl-disable-tcp-nodelay no
  31. replica-priority 100
  32. acllog-max-len 128
  33. requirepass "123456"
  34. maxmemory 4gb
  35. maxmemory-policy allkeys-lru
  36. lazyfree-lazy-eviction no
  37. lazyfree-lazy-expire no
  38. lazyfree-lazy-server-del no
  39. replica-lazy-flush no
  40. lazyfree-lazy-user-del no
  41. lazyfree-lazy-user-flush no
  42. oom-score-adj no
  43. oom-score-adj-values 0 200 800
  44. disable-thp yes
  45. appendonly yes
  46. appendfilename "appendonly.aof"
  47. appendfsync everysec
  48. no-appendfsync-on-rewrite no
  49. auto-aof-rewrite-percentage 100
  50. auto-aof-rewrite-min-size 64mb
  51. aof-load-truncated yes
  52. aof-use-rdb-preamble yes
  53. lua-time-limit 5000
  54. slowlog-log-slower-than 10000
  55. slowlog-max-len 128
  56. latency-monitor-threshold 0
  57. notify-keyspace-events ""
  58. hash-max-ziplist-entries 512
  59. hash-max-ziplist-value 64
  60. list-max-ziplist-size -2
  61. list-compress-depth 0
  62. set-max-intset-entries 512
  63. zset-max-ziplist-entries 128
  64. zset-max-ziplist-value 64
  65. hll-sparse-max-bytes 3000
  66. stream-node-max-bytes 4kb
  67. stream-node-max-entries 100
  68. activerehashing yes
  69. client-output-buffer-limit normal 0 0 0
  70. client-output-buffer-limit replica 256mb 64mb 60
  71. client-output-buffer-limit pubsub 32mb 8mb 60
  72. hz 10
  73. dynamic-hz yes
  74. aof-rewrite-incremental-fsync yes
  75. rdb-save-incremental-fsync yes
  76. jemalloc-bg-thread yes
  77. save 3600 1
  78. save 300 100
  79. save 60 10000

部署redis-sentinel

100.200.100.111、112、113: 使用同一个配置

  1. [root@JUMPSER-100-200-100-111 jumpserver-installer-v2.13.1]# egrep -v "^#|^$" /etc/redis-sentinel.conf
  2. protected-mode no
  3. port 26379
  4. daemonize yes
  5. pidfile "/var/run/redis-sentinel.pid"
  6. logfile "/var/log/redis/sentinel.log"
  7. dir "/tmp"
  8. sentinel monitor mymaster 100.200.100.113 6379 2
  9. sentinel down-after-milliseconds mymaster 5000
  10. sentinel auth-pass mymaster 123456
  11. acllog-max-len 128
  12. sentinel deny-scripts-reconfig yes
  13. sentinel resolve-hostnames no
  14. sentinel announce-hostnames no

启动redis和redis-sentinel

配置自启动项

  1. [root@JUMPSER-100-200-100-113 0]# cat /etc/systemd/system/redis.service
  2. [Unit]
  3. Description=Redis persistent key-value database
  4. After=network.target
  5. After=network-online.target
  6. Wants=network-online.target
  7. [Service]
  8. Type=forking
  9. ExecStart=/usr/local/redis/bin/redis-server /etc/redis.conf
  10. PIDFile=/var/run/redis.pid
  11. ExecReload=/bin/kill -s HUP $MAINPID
  12. ExecStop=/bin/kill -s QUIT $MAINPID
  13. [Install]
  14. WantedBy=multi-user.target
  15. [root@JUMPSER-100-200-100-113 0]# cat /etc/systemd/system/redis-sentinel.service
  16. [Unit]
  17. Description=Redis Sentinel
  18. After=network.target
  19. After=network-online.target
  20. Wants=network-online.target
  21. [Service]
  22. Type=forking
  23. ExecStart=/usr/local/redis/bin/redis-sentinel /etc/redis-sentinel.conf
  24. PIDFile=/var/run/redis-sentinel.pid
  25. ExecReload=/bin/kill -s HUP $MAINPID
  26. ExecStop=/bin/kill -s QUIT $MAINPID
  27. [Install]
  28. WantedBy=multi-user.target

开机自启动

  1. systemctl enable redis redis-sentinel

启动redis

  1. systemctl start redis redis-sentinel

验证集群

  1. redis-cli -h 127.0.0.1 -a 123456 info Replication
  2. # Replication
  3. role:master
  4. connected_slaves:2
  5. slave0:ip=100.200.100.111,port=6379,state=online,offset=244414,lag=1
  6. slave1:ip=100.200.100.112,port=6379,state=online,offset=244414,lag=0
  7. master_failover_state:no-failover
  8. master_replid:b7f646c4f8e0d4f1d036115f24c903406cb3ca7e50
  9. master_replid2:fef7c063c9500ec000709ab51dd03d6a8cc88fd1f9
  10. master_repl_offset:244414
  11. second_repl_offset:21938
  12. repl_backlog_active:1
  13. repl_backlog_size:1048576
  14. repl_backlog_first_byte_offset:17657
  15. repl_backlog_histlen:226758

关闭master节点,看某个slave节点是否成为master,然后在启动刚才关闭的节点,看是否加入到集群。(步骤省略,各位看官自行测试吧 ~~)

安装keepalived

  1. yum install -y keepalived

配置keepalived
100.200.100.111:

  1. [root@JUMPSER-100-200-100-111 jumpserver-installer-v2.13.1]# cat /etc/keepalived/keepalived.conf
  2. ! Configuration File for keepalived
  3. global_defs {
  4. router_id redis-111
  5. }
  6. vrrp_script chk_redis {
  7. script "/usr/bin/redis-cli -h 127.0.0.1 -a 123456 info|grep role:master >/dev/null 2>&1"
  8. interval 1
  9. }
  10. vrrp_instance redis {
  11. state BACKUP
  12. interface eth0
  13. virtual_router_id 79 #保持一致
  14. priority 98 #优先级
  15. nopreempt
  16. advert_int 1
  17. authentication {
  18. auth_type PASS
  19. auth_pass redis
  20. }
  21. virtual_ipaddress {
  22. 100.200.100.114
  23. }
  24. track_script {
  25. chk_redis
  26. }
  27. }

100.200.100.112:

  1. [root@JUMPSER-100-200-100-112 jumpserver-installer-v2.13.1]# cat /etc/keepalived/keepalived.conf
  2. ! Configuration File for keepalived
  3. global_defs {
  4. router_id redis-112
  5. }
  6. vrrp_script chk_redis {
  7. script "/usr/bin/redis-cli -h 127.0.0.1 -a 123456 info|grep role:master >/dev/null 2>&1"
  8. interval 1
  9. }
  10. vrrp_instance redis {
  11. state BACKUP
  12. interface eth0
  13. virtual_router_id 79 #保持一致
  14. priority 99 #优先级
  15. nopreempt
  16. advert_int 1
  17. authentication {
  18. auth_type PASS
  19. auth_pass redis
  20. }
  21. virtual_ipaddress {
  22. 100.200.100.114
  23. }
  24. track_script {
  25. chk_redis
  26. }
  27. }

100.200.100.113:

  1. [root@JUMPSER-100-200-100-113 ~]# cat /etc/keepalived/keepalived.conf
  2. ! Configuration File for keepalived
  3. global_defs {
  4. router_id redis-113
  5. }
  6. vrrp_script chk_redis {
  7. script "/usr/bin/redis-cli -h 127.0.0.1 -a 123456 info|grep role:master >/dev/null 2>&1"
  8. interval 1
  9. }
  10. vrrp_instance redis {
  11. state MASTER
  12. interface eth0
  13. virtual_router_id 79
  14. priority 100
  15. nopreempt
  16. advert_int 1
  17. authentication {
  18. auth_type PASS
  19. auth_pass redis
  20. }
  21. virtual_ipaddress {
  22. 100.200.100.114
  23. }
  24. track_script {
  25. chk_redis
  26. }
  27. }

启动keepalived

三台操作

  1. systemctl enable keepalived
  2. systemctl start keepalived

验证集群

  1. [root@JUMPSER-100-200-100-111 jumpserver-installer-v2.13.1]# ip a
  2. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
  3. link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  4. inet 127.0.0.1/8 scope host lo
  5. valid_lft forever preferred_lft forever
  6. 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
  7. link/ether 52:54:00:67:a0:b1 brd ff:ff:ff:ff:ff:ff
  8. inet 100.200.100.111/21 brd 100.200.103.255 scope global noprefixroute eth0
  9. valid_lft forever preferred_lft forever
  10. inet 100.200.100.114/32 scope global eth0

首先关闭vip漂移到的节点,查看vip状态是否移除,使用以下命令
/usr/bin/redis-cli -h 127.0.0.1 -a 123456 info|grep role:master 查看master在那个节点上,同时查看vip是否存在这个节点上,有的话成功。 以上操作多刷几次!!

发表评论

表情:
评论列表 (有 0 条评论,220人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Redis

    本文使用的是redis5.0.7版本 一、主从复制 > 单个Redis如果因为某种原因宕机的话,可能会导致Redis服务不可用,可以使用主从复制实现一主多从,主节点负责

    相关 redis主从节点自动切换

    前言 最近在将redis作为数据库使用(redis中存放大量数据)的项目中,偶然发现redis的主从节点发生了变化,但是没有任务报错,redis集群的各节点也没有fail