当前位置:   article > 正文

一主双从redis+哨兵模式以及通过Ansible+Shell快速部署redis哨兵模式

一主双从redis+哨兵模式以及通过Ansible+Shell快速部署redis哨兵模式

目录

一.主redis配置

1.解压并进行编译安装

2.为redis创建软链接和service管理方便启动

3.修改redis的配置文件

二.配置备redis

三.主redis上查看集群状态

1.主redis登录上去查看状态并且创建数据用于验证同步性

2.备redis查看数据是否同步过来

四.模拟测试

1.主redis宕机时

2.备redis宕机时

五.redis哨兵

1.配置sentinel.conf文件

2.运行sentinel.conf文件

3.查看进程是否起来了

4.三台设备都运行起来后就停掉主redis(101)然后查看日志

5.在102和190主机上查看

6.当101恢复后的情况

六.Ansible+Shell快速部署redis一主双从哨兵模式


主从都是通过源码部署redis软件

主机信息:

主redis:192.168.2.101:6379

备redis1:192.168.2.102:6375

备redis2:192.168.2.190:6376

一.主redis配置

这里我的主redis和两台备redis版本有所不一样但影响不大

1.解压并进行编译安装

  1. [root@localhost ~]# tar redis-6.2.14.tar.gz -C /usr/local/src/
  2. [root@localhost ~]# yum install -y gcc tcl gcc-c++ make
  3. [root@localhost redis-6.2.14]# make && make install
  4. [root@localhost src]# cd /usr/local/bin/
  5. [root@localhost bin]# ll
  6. total 18940
  7. -rw-r--r-- 1 root root     93 Apr 17 20:35 dump.rdb
  8. -rwxr-xr-x 1 root root 4830656 Apr 17 20:34 redis-benchmark
  9. lrwxrwxrwx 1 root root     12 Apr 17 20:34 redis-check-aof -> redis-server
  10. lrwxrwxrwx 1 root root     12 Apr 17 20:34 redis-check-rdb -> redis-server
  11. -rwxr-xr-x 1 root root 5004760 Apr 17 20:34 redis-cli
  12. lrwxrwxrwx 1 root root     12 Apr 17 20:34 redis-sentinel -> redis-server
  13. -rwxr-xr-x 1 root root 9548208 Apr 17 20:34 redis-server

2.为redis创建软链接和service管理方便启动

  1. [root@localhost ~]# ln -s /usr/local/bin/redis-cli /root/redis-cli
  2. [root@localhost ~]# ln -s /usr/local/bin/redis-server /root/redis-server
  3. [root@localhost ~]# cat /usr/lib/systemd/system/redis.service
  4. [Unit]
  5. Description=Redis Server Manager
  6. After=network.target
  7. [Service]
  8. Type=forking
  9. ExecStart=/root/redis-server /etc/redis.conf   #在这里指定启动参数,通常不配置service也可以通过这个命令进行启动
  10. PrivateTmp=true
  11. [Install]
  12. WantedBy=multi-user.target
  13. [root@localhost ~]# systemctl daemon-reload

3.修改redis的配置文件

  1. [root@localhost ~]# cp /usr/local/src/redis-6.2.14/redis.conf /etc/redis.conf #拷贝过去
  2. [root@localhost ~]# sed -i 's/^bind.*/bind 0.0.0.0/' /etc/redis.conf
  3. [root@localhost ~]# sed -i 's/^logfile.*/logfile \/var\/log\/redis\/redis.log/' /etc/redis.conf
  4. [root@localhost ~]# sed -i 's/^port.*/port 6379/' /etc/redis.conf
  5. [root@localhost ~]# sed -i 's/^daemonize.*/daemonize yes/' /etc/redis.conf
  6. [root@localhost ~]# sed -i 's/^appendonly.*/appendonly yes/' /etc/redis.conf
  7. [root@localhost ~]# sed -i 's/^# requirepass.*/requirepass 123456/' /etc/redis.conf
  8. [root@localhost ~]# systemctl restart redis
  9. [root@localhost ~]# ./redis-cli -p 6379
  10. 127.0.0.1:6379> auth 123456
  11. OK
  12. 127.0.0.1:6379> ping
  13. PONG
  14. sed -i 's/^bind.*/bind 0.0.0.0/' /etc/redis.conf
  15. sed -i 's/^logfile.*/logfile \/var\/log\/redis\/redis.log/' /etc/redis.conf
  16. sed -i 's/^port.*/port 6379/' /etc/redis.conf
  17. sed -i 's/^daemonize.*/daemonize yes/' /etc/redis.conf
  18. sed -i 's/^appendonly.*/appendonly yes/' /etc/redis.conf
  19. sed -i 's/^# requirepass.*/requirepass 123456/' /etc/redis.conf
  20. sed -i 's/^# masterauth.*/masterauth 123456/' /etc/redis.conf

二.配置备redis

和上面同样的操作,解压编译、创建软链接、拷贝并修改redis.conf文件,然后在主redis上拷贝service文件到两台备redis

  1. [root@localhost ~]# scp /usr/lib/systemd/system/redis.service root@192.168.2.102:/usr/lib/systemd/system/
  2. [root@localhost ~]# scp /usr/lib/systemd/system/redis.service root@192.168.2.190:/usr/lib/systemd/system/
  3. 两台备redis重载文件
  4. systemctl daemon-reload

注意两台备redis在修改redis.conf时还要多修改几个参数,因为主redis的配置了密码要求,并且这里选择了更改端口

  1. [root@localhost ~]# sed -i 's/^bind.*/bind 0.0.0.0/' /etc/redis.conf
  2. [root@localhost ~]# sed -i 's/^logfile.*/logfile \/var\/log\/redis\/redis.log/' /etc/redis.conf
  3. [root@localhost ~]# sed -i 's/^port.*/port 6375/' /etc/redis.conf
  4. [root@localhost ~]# sed -i 's/^daemonize.*/daemonize yes/' /etc/redis.conf
  5. [root@localhost ~]# sed -i 's/^appendonly.*/appendonly yes/' /etc/redis.conf
  6. [root@localhost ~]# sed -i 's/^requirepass.*/requirepass 123456/' /etc/redis.conf
  7. [root@localhost ~]# sed -i 's/replicaof.*/replicaof 192.168.2.101 6379/' redis.conf #指定你要绑定的主redis的IP和端口
  8. [root@localhost ~]# sed -i 's/masterauth.*/masterauth 123456/' redis.conf   #指定主redis的密码
  9. [root@localhost ~]# systemctl restart redis

三.主redis上查看集群状态

1.主redis登录上去查看状态并且创建数据用于验证同步性

  1. [root@localhost ~]# ./redis-cli -p 6379
  2. 127.0.0.1:6379> auth 123456
  3. OK
  4. 127.0.0.1:6379> info replication
  5. # Replication
  6. role:master
  7. connected_slaves:2
  8. slave0:ip=192.168.2.102,port=6375,state=online,offset=2002,lag=0
  9. slave1:ip=192.168.2.190,port=6376,state=online,offset=2002,lag=0
  10. master_failover_state:no-failover
  11. master_replid:2cb4965520a60a53417fb26183178d5a6800dfe3
  12. master_replid2:0000000000000000000000000000000000000000
  13. master_repl_offset:2002
  14. second_repl_offset:-1
  15. repl_backlog_active:1
  16. repl_backlog_size:1048576
  17. repl_backlog_first_byte_offset:1
  18. repl_backlog_histlen:2002
  19. 127.0.0.1:6379>
  20. 127.0.0.1:6379> keys *
  21. 1) "lilxinjin"
  22. 2) "key6"
  23. 3) "sulibao"
  24. 4) "key7"
  25. 5) "niexincheng"

2.备redis查看数据是否同步过来

  1. [root@localhost ~]# ./redis-cli -p 6375
  2. 127.0.0.1:6375> ping
  3. PONG
  4. 127.0.0.1:6375> keys *
  5. 1) "niexincheng"
  6. 2) "sulibao"
  7. 3) "lilxinjin"
  8. 4) "key6"
  9. 5) "key7"
  10. [root@localhost ~]# ./redis-cli -p 6376
  11. 127.0.0.1:6376> keys *
  12. 1) "niexincheng"
  13. 2) "sulibao"
  14. 3) "key6"
  15. 4) "lilxinjin"
  16. 5) "key7"

四.模拟测试

1.主redis宕机时

主机宕机时,备redis仍然可以提供读的操作,但是不能写和删除

  1. #主机宕机后备redis仍然是slave
  2. 127.0.0.1:6375> info replication
  3. # Replication
  4. role:slave
  5. master_host:192.168.2.101
  6. master_port:6379
  7. master_link_status:down
  8. master_last_io_seconds_ago:-1
  9. master_sync_in_progress:0
  10. slave_read_repl_offset:2352
  11. slave_repl_offset:2352
  12. master_link_down_since_seconds:15
  13. slave_priority:100
  14. slave_read_only:1
  15. replica_announced:1
  16. connected_slaves:0
  17. master_failover_state:no-failover
  18. master_replid:2cb4965520a60a53417fb26183178d5a6800dfe3
  19. master_replid2:0000000000000000000000000000000000000000
  20. master_repl_offset:2352
  21. second_repl_offset:-1
  22. repl_backlog_active:1
  23. repl_backlog_size:1048576
  24. repl_backlog_first_byte_offset:1
  25. repl_backlog_histlen:2352
  26. 127.0.0.1:6376> info replication
  27. # Replication
  28. role:slave
  29. master_host:192.168.2.101
  30. master_port:6379
  31. master_link_status:down
  32. master_last_io_seconds_ago:-1
  33. master_sync_in_progress:0
  34. slave_read_repl_offset:2352
  35. slave_repl_offset:2352
  36. master_link_down_since_seconds:39
  37. slave_priority:100
  38. slave_read_only:1
  39. replica_announced:1
  40. connected_slaves:0
  41. master_failover_state:no-failover
  42. master_replid:2cb4965520a60a53417fb26183178d5a6800dfe3
  43. master_replid2:0000000000000000000000000000000000000000
  44. master_repl_offset:2352
  45. second_repl_offset:-1
  46. repl_backlog_active:1
  47. repl_backlog_size:1048576
  48. repl_backlog_first_byte_offset:1
  49. repl_backlog_histlen:2352

当主redis恢复后,他仍然还是master,读写操作也正常,数据也都还在

  1. [root@localhost ~]# systemctl start redis
  2. [root@localhost ~]# ./redis-cli -p 6379
  3. 127.0.0.1:6379> auth 123456
  4. OK
  5. 127.0.0.1:6379> info replication
  6. # Replication
  7. role:master
  8. connected_slaves:2
  9. slave0:ip=192.168.2.190,port=6376,state=online,offset=28,lag=1
  10. slave1:ip=192.168.2.102,port=6375,state=online,offset=28,lag=0
  11. master_failover_state:no-failover
  12. master_replid:5c009c69f5e98bc4580fb4090b7a7032795744d4
  13. master_replid2:0000000000000000000000000000000000000000
  14. master_repl_offset:28
  15. second_repl_offset:-1
  16. repl_backlog_active:1
  17. repl_backlog_size:1048576
  18. repl_backlog_first_byte_offset:1
  19. repl_backlog_histlen:28

2.备redis宕机时

备redis宕机后,我们这里是在配置文件中指定了和主redis的绑定关系的,所以他宕机后恢复时的身份仍然是slave,数据都还在。但是如果只是在命令行指定的绑定关系,那么他这个设备上的身份应该是恢复为master了然后数据也没了。使用 slaveof no one 可以将从机变为主机

五.redis哨兵

基于上面的环境进行配置

1.配置sentinel.conf文件

101、102、190主机都配置如下,这个配置文件在sentinel生效后会生成额外内容,并且在master产生漂移过后所监控的主机和端口也会变化

  1. [root@localhost bin]# pwd
  2. /usr/local/bin
  3. [root@localhost bin]# cat sentinel.conf
  4. daemonize yes
  5. logfile "/var/log/redis/sentinel.log"
  6. sentinel monitor redis01 192.168.2.101 6379 2
  7. sentinel down-after-milliseconds redis01 10000
  8. sentinel failover-timeout redis01 60000
  9. sentinel parallel-syncs redis01 2
  10. sentinel auth-pass redis01 123456

2.运行sentinel.conf文件

  1. #三台设备分别以主->从依次运行
  2. redis-sentinel sentinel.conf &

3.查看进程是否起来了

  1. [root@localhost bin]# ps -ef | grep sentinel
  2. root     22449     1 0 11:12 ?       00:00:08 redis-sentinel *:26379 [sentinel]
  3. root     22462 21745 0 11:13 pts/3   00:00:00 tail -f /var/log/redis/sentinel.log

4.三台设备都运行起来后就停掉主redis(101)然后查看日志

  1. #主设备上
  2. systemctl stop redis
  3. #主设备上的sentinel.log内容
  4. 22449:X 22 Apr 2024 11:16:52.859 * +failover-state-wait-promotion slave 192.168.2.102:6375 192.168.2.102 6375 @ redis01 192.168.2.101 6379
  5. 22449:X 22 Apr 2024 11:16:53.588 # +promoted-slave slave 192.168.2.102:6375 192.168.2.102 6375 @ redis01 192.168.2.101 6379
  6. 22449:X 22 Apr 2024 11:16:53.589 # +failover-state-reconf-slaves master redis01 192.168.2.101 6379
  7. 22449:X 22 Apr 2024 11:16:53.688 * +slave-reconf-sent slave 192.168.2.190:6376 192.168.2.190 6376 @ redis01 192.168.2.101 6379
  8. 22449:X 22 Apr 2024 11:16:54.608 * +slave-reconf-inprog slave 192.168.2.190:6376 192.168.2.190 6376 @ redis01 192.168.2.101 6379
  9. 22449:X 22 Apr 2024 11:16:54.814 # -odown master redis01 192.168.2.101 6379
  10. 22449:X 22 Apr 2024 11:17:53.591 # +failover-end-for-timeout master redis01 192.168.2.101 6379
  11. 22449:X 22 Apr 2024 11:17:53.591 # +failover-end master redis01 192.168.2.101 6379
  12. 22449:X 22 Apr 2024 11:17:53.591 * +slave-reconf-sent-be slave 192.168.2.190:6376 192.168.2.190 6376 @ redis01 192.168.2.101 6379
  13. 22449:X 22 Apr 2024 11:17:53.591 # +switch-master redis01 192.168.2.101 6379 192.168.2.102 6375   #这里表示已经将master切换到102主机上
  14. 22449:X 22 Apr 2024 11:17:53.592 * +slave slave 192.168.2.190:6376 192.168.2.190 6376 @ redis01 192.168.2.102 6375
  15. 22449:X 22 Apr 2024 11:17:53.592 * +slave slave 192.168.2.101:6379 192.168.2.101 6379 @ redis01 192.168.2.102 6375
  16. 22449:X 22 Apr 2024 11:18:03.629 # +sdown slave 192.168.2.101:6379 192.168.2.101 6379 @ redis01 192.168.2.102 6375

5.在102和190主机上查看

如下为102主机的redis,我使用的是6375端口,190使用的6376端口

  1. 127.0.0.1:6375> info replication
  2. # Replication
  3. role:master
  4. connected_slaves:0
  5. master_failover_state:no-failover
  6. master_replid:eecf88568f1ffefcdc315f73427f371d0496c878
  7. master_replid2:321f7c50f38e8c9dd042bb88080a381d0baef84d
  8. master_repl_offset:605938
  9. second_repl_offset:44936
  10. repl_backlog_active:1
  11. repl_backlog_size:1048576
  12. repl_backlog_first_byte_offset:1
  13. repl_backlog_histlen:605938
  14. 127.0.0.1:6376> info replication
  15. # Replication
  16. role:slave
  17. master_host:192.168.2.102
  18. master_port:6375
  19. master_link_status:down
  20. master_last_io_seconds_ago:-1
  21. master_sync_in_progress:0
  22. slave_read_repl_offset:44935
  23. slave_repl_offset:44935
  24. master_link_down_since_seconds:2743
  25. slave_priority:100
  26. slave_read_only:1
  27. replica_announced:1
  28. connected_slaves:0
  29. master_failover_state:no-failover
  30. master_replid:321f7c50f38e8c9dd042bb88080a381d0baef84d
  31. master_replid2:0000000000000000000000000000000000000000
  32. master_repl_offset:44935
  33. second_repl_offset:-1
  34. repl_backlog_active:1
  35. repl_backlog_size:1048576
  36. repl_backlog_first_byte_offset:43
  37. repl_backlog_histlen:44893

6.当101恢复后的情况

他重新进来后是作为slave身份了,现在的集群中的master就是102主机了

  1. 127.0.0.1:6379> info replication
  2. # Replication
  3. role:slave
  4. master_host:192.168.2.102
  5. master_port:6375
  6. master_link_status:down
  7. master_last_io_seconds_ago:-1
  8. master_sync_in_progress:0
  9. slave_read_repl_offset:0
  10. slave_repl_offset:0
  11. master_link_down_since_seconds:-1
  12. slave_priority:100
  13. slave_read_only:1
  14. replica_announced:1
  15. connected_slaves:0
  16. master_failover_state:no-failover
  17. master_replid:f9eec4b7a919560dbce7aa6117067414d9a891de
  18. master_replid2:0000000000000000000000000000000000000000
  19. master_repl_offset:0
  20. second_repl_offset:-1
  21. repl_backlog_active:0
  22. repl_backlog_size:1048576
  23. repl_backlog_first_byte_offset:0
  24. repl_backlog_histlen:0

六.Ansible+Shell快速部署redis一主双从哨兵模式

主要都在master上做,文件有点长,很多需要改进的地方,按需修改吧,文件内部的主机、密码、目录、文件名等参数请自行修改。

先执行redis.sh,把自己的环境部好,然后通过ansible-playbook redis-backup.yaml来为两台从redis部署环境,于2024/4/22日成功快速搭建,redis版本为7.0.15,Ansible版本为2.9.27,如有疑问请于CSDN私聊或发送邮箱至sulibao2003@163.com,脚本文件和剧本文件放在百度/迅雷网盘了。

  1. 分享文件:Ansible+Shell-redis
  2. 链接:https://pan.xunlei.com/s/VNw5EfaHp-_r1h9Ud5e9vjAdA1#
  3. 提取码:47xf
  4. 复制这段内容后打开手机迅雷App,查看更方便
  1. 链接:https://pan.baidu.com/s/12TYqW8GWEAH0qt_Pm6MJWA
  2. 提取码:tdbe
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/496623
推荐阅读
相关标签
  

闽ICP备14008679号