赞
踩
目录
4.三台设备都运行起来后就停掉主redis(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版本有所不一样但影响不大
- [root@localhost ~]# tar redis-6.2.14.tar.gz -C /usr/local/src/
- [root@localhost ~]# yum install -y gcc tcl gcc-c++ make
- [root@localhost redis-6.2.14]# make && make install
- [root@localhost src]# cd /usr/local/bin/
- [root@localhost bin]# ll
- total 18940
- -rw-r--r-- 1 root root 93 Apr 17 20:35 dump.rdb
- -rwxr-xr-x 1 root root 4830656 Apr 17 20:34 redis-benchmark
- lrwxrwxrwx 1 root root 12 Apr 17 20:34 redis-check-aof -> redis-server
- lrwxrwxrwx 1 root root 12 Apr 17 20:34 redis-check-rdb -> redis-server
- -rwxr-xr-x 1 root root 5004760 Apr 17 20:34 redis-cli
- lrwxrwxrwx 1 root root 12 Apr 17 20:34 redis-sentinel -> redis-server
- -rwxr-xr-x 1 root root 9548208 Apr 17 20:34 redis-server
- [root@localhost ~]# ln -s /usr/local/bin/redis-cli /root/redis-cli
- [root@localhost ~]# ln -s /usr/local/bin/redis-server /root/redis-server
- [root@localhost ~]# cat /usr/lib/systemd/system/redis.service
- [Unit]
- Description=Redis Server Manager
- After=network.target
- [Service]
- Type=forking
- ExecStart=/root/redis-server /etc/redis.conf #在这里指定启动参数,通常不配置service也可以通过这个命令进行启动
- PrivateTmp=true
-
- [Install]
- WantedBy=multi-user.target
- [root@localhost ~]# systemctl daemon-reload
- [root@localhost ~]# cp /usr/local/src/redis-6.2.14/redis.conf /etc/redis.conf #拷贝过去
- [root@localhost ~]# sed -i 's/^bind.*/bind 0.0.0.0/' /etc/redis.conf
- [root@localhost ~]# sed -i 's/^logfile.*/logfile \/var\/log\/redis\/redis.log/' /etc/redis.conf
- [root@localhost ~]# sed -i 's/^port.*/port 6379/' /etc/redis.conf
- [root@localhost ~]# sed -i 's/^daemonize.*/daemonize yes/' /etc/redis.conf
- [root@localhost ~]# sed -i 's/^appendonly.*/appendonly yes/' /etc/redis.conf
- [root@localhost ~]# sed -i 's/^# requirepass.*/requirepass 123456/' /etc/redis.conf
- [root@localhost ~]# systemctl restart redis
- [root@localhost ~]# ./redis-cli -p 6379
- 127.0.0.1:6379> auth 123456
- OK
- 127.0.0.1:6379> ping
- PONG
- sed -i 's/^bind.*/bind 0.0.0.0/' /etc/redis.conf
- sed -i 's/^logfile.*/logfile \/var\/log\/redis\/redis.log/' /etc/redis.conf
- sed -i 's/^port.*/port 6379/' /etc/redis.conf
- sed -i 's/^daemonize.*/daemonize yes/' /etc/redis.conf
- sed -i 's/^appendonly.*/appendonly yes/' /etc/redis.conf
- sed -i 's/^# requirepass.*/requirepass 123456/' /etc/redis.conf
- sed -i 's/^# masterauth.*/masterauth 123456/' /etc/redis.conf
和上面同样的操作,解压编译、创建软链接、拷贝并修改redis.conf文件,然后在主redis上拷贝service文件到两台备redis
- [root@localhost ~]# scp /usr/lib/systemd/system/redis.service root@192.168.2.102:/usr/lib/systemd/system/
- [root@localhost ~]# scp /usr/lib/systemd/system/redis.service root@192.168.2.190:/usr/lib/systemd/system/
- 两台备redis重载文件
- systemctl daemon-reload
注意两台备redis在修改redis.conf时还要多修改几个参数,因为主redis的配置了密码要求,并且这里选择了更改端口
- [root@localhost ~]# sed -i 's/^bind.*/bind 0.0.0.0/' /etc/redis.conf
- [root@localhost ~]# sed -i 's/^logfile.*/logfile \/var\/log\/redis\/redis.log/' /etc/redis.conf
- [root@localhost ~]# sed -i 's/^port.*/port 6375/' /etc/redis.conf
- [root@localhost ~]# sed -i 's/^daemonize.*/daemonize yes/' /etc/redis.conf
- [root@localhost ~]# sed -i 's/^appendonly.*/appendonly yes/' /etc/redis.conf
- [root@localhost ~]# sed -i 's/^requirepass.*/requirepass 123456/' /etc/redis.conf
- [root@localhost ~]# sed -i 's/replicaof.*/replicaof 192.168.2.101 6379/' redis.conf #指定你要绑定的主redis的IP和端口
- [root@localhost ~]# sed -i 's/masterauth.*/masterauth 123456/' redis.conf #指定主redis的密码
- [root@localhost ~]# systemctl restart redis
- [root@localhost ~]# ./redis-cli -p 6379
- 127.0.0.1:6379> auth 123456
- OK
- 127.0.0.1:6379> info replication
- # Replication
- role:master
- connected_slaves:2
- slave0:ip=192.168.2.102,port=6375,state=online,offset=2002,lag=0
- slave1:ip=192.168.2.190,port=6376,state=online,offset=2002,lag=0
- master_failover_state:no-failover
- master_replid:2cb4965520a60a53417fb26183178d5a6800dfe3
- master_replid2:0000000000000000000000000000000000000000
- master_repl_offset:2002
- second_repl_offset:-1
- repl_backlog_active:1
- repl_backlog_size:1048576
- repl_backlog_first_byte_offset:1
- repl_backlog_histlen:2002
- 127.0.0.1:6379>
- 127.0.0.1:6379> keys *
- 1) "lilxinjin"
- 2) "key6"
- 3) "sulibao"
- 4) "key7"
- 5) "niexincheng"
- [root@localhost ~]# ./redis-cli -p 6375
- 127.0.0.1:6375> ping
- PONG
- 127.0.0.1:6375> keys *
- 1) "niexincheng"
- 2) "sulibao"
- 3) "lilxinjin"
- 4) "key6"
- 5) "key7"
- [root@localhost ~]# ./redis-cli -p 6376
- 127.0.0.1:6376> keys *
- 1) "niexincheng"
- 2) "sulibao"
- 3) "key6"
- 4) "lilxinjin"
- 5) "key7"
主机宕机时,备redis仍然可以提供读的操作,但是不能写和删除
- #主机宕机后备redis仍然是slave
- 127.0.0.1:6375> info replication
- # Replication
- role:slave
- master_host:192.168.2.101
- master_port:6379
- master_link_status:down
- master_last_io_seconds_ago:-1
- master_sync_in_progress:0
- slave_read_repl_offset:2352
- slave_repl_offset:2352
- master_link_down_since_seconds:15
- slave_priority:100
- slave_read_only:1
- replica_announced:1
- connected_slaves:0
- master_failover_state:no-failover
- master_replid:2cb4965520a60a53417fb26183178d5a6800dfe3
- master_replid2:0000000000000000000000000000000000000000
- master_repl_offset:2352
- second_repl_offset:-1
- repl_backlog_active:1
- repl_backlog_size:1048576
- repl_backlog_first_byte_offset:1
- repl_backlog_histlen:2352
-
-
- 127.0.0.1:6376> info replication
- # Replication
- role:slave
- master_host:192.168.2.101
- master_port:6379
- master_link_status:down
- master_last_io_seconds_ago:-1
- master_sync_in_progress:0
- slave_read_repl_offset:2352
- slave_repl_offset:2352
- master_link_down_since_seconds:39
- slave_priority:100
- slave_read_only:1
- replica_announced:1
- connected_slaves:0
- master_failover_state:no-failover
- master_replid:2cb4965520a60a53417fb26183178d5a6800dfe3
- master_replid2:0000000000000000000000000000000000000000
- master_repl_offset:2352
- second_repl_offset:-1
- repl_backlog_active:1
- repl_backlog_size:1048576
- repl_backlog_first_byte_offset:1
- repl_backlog_histlen:2352
当主redis恢复后,他仍然还是master,读写操作也正常,数据也都还在
- [root@localhost ~]# systemctl start redis
- [root@localhost ~]# ./redis-cli -p 6379
- 127.0.0.1:6379> auth 123456
- OK
- 127.0.0.1:6379> info replication
- # Replication
- role:master
- connected_slaves:2
- slave0:ip=192.168.2.190,port=6376,state=online,offset=28,lag=1
- slave1:ip=192.168.2.102,port=6375,state=online,offset=28,lag=0
- master_failover_state:no-failover
- master_replid:5c009c69f5e98bc4580fb4090b7a7032795744d4
- master_replid2:0000000000000000000000000000000000000000
- master_repl_offset:28
- second_repl_offset:-1
- repl_backlog_active:1
- repl_backlog_size:1048576
- repl_backlog_first_byte_offset:1
- repl_backlog_histlen:28
备redis宕机后,我们这里是在配置文件中指定了和主redis的绑定关系的,所以他宕机后恢复时的身份仍然是slave,数据都还在。但是如果只是在命令行指定的绑定关系,那么他这个设备上的身份应该是恢复为master了然后数据也没了。使用 slaveof no one 可以将从机变为主机
基于上面的环境进行配置
101、102、190主机都配置如下,这个配置文件在sentinel生效后会生成额外内容,并且在master产生漂移过后所监控的主机和端口也会变化
- [root@localhost bin]# pwd
- /usr/local/bin
- [root@localhost bin]# cat sentinel.conf
- daemonize yes
- logfile "/var/log/redis/sentinel.log"
- sentinel monitor redis01 192.168.2.101 6379 2
- sentinel down-after-milliseconds redis01 10000
- sentinel failover-timeout redis01 60000
- sentinel parallel-syncs redis01 2
- sentinel auth-pass redis01 123456
- #三台设备分别以主->从依次运行
- redis-sentinel sentinel.conf &
- [root@localhost bin]# ps -ef | grep sentinel
- root 22449 1 0 11:12 ? 00:00:08 redis-sentinel *:26379 [sentinel]
- root 22462 21745 0 11:13 pts/3 00:00:00 tail -f /var/log/redis/sentinel.log
- #主设备上
- systemctl stop redis
-
- #主设备上的sentinel.log内容
- 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
- 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
- 22449:X 22 Apr 2024 11:16:53.589 # +failover-state-reconf-slaves master redis01 192.168.2.101 6379
- 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
- 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
- 22449:X 22 Apr 2024 11:16:54.814 # -odown master redis01 192.168.2.101 6379
- 22449:X 22 Apr 2024 11:17:53.591 # +failover-end-for-timeout master redis01 192.168.2.101 6379
- 22449:X 22 Apr 2024 11:17:53.591 # +failover-end master redis01 192.168.2.101 6379
- 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
- 22449:X 22 Apr 2024 11:17:53.591 # +switch-master redis01 192.168.2.101 6379 192.168.2.102 6375 #这里表示已经将master切换到102主机上
- 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
- 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
- 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
如下为102主机的redis,我使用的是6375端口,190使用的6376端口
- 127.0.0.1:6375> info replication
- # Replication
- role:master
- connected_slaves:0
- master_failover_state:no-failover
- master_replid:eecf88568f1ffefcdc315f73427f371d0496c878
- master_replid2:321f7c50f38e8c9dd042bb88080a381d0baef84d
- master_repl_offset:605938
- second_repl_offset:44936
- repl_backlog_active:1
- repl_backlog_size:1048576
- repl_backlog_first_byte_offset:1
- repl_backlog_histlen:605938
-
- 127.0.0.1:6376> info replication
- # Replication
- role:slave
- master_host:192.168.2.102
- master_port:6375
- master_link_status:down
- master_last_io_seconds_ago:-1
- master_sync_in_progress:0
- slave_read_repl_offset:44935
- slave_repl_offset:44935
- master_link_down_since_seconds:2743
- slave_priority:100
- slave_read_only:1
- replica_announced:1
- connected_slaves:0
- master_failover_state:no-failover
- master_replid:321f7c50f38e8c9dd042bb88080a381d0baef84d
- master_replid2:0000000000000000000000000000000000000000
- master_repl_offset:44935
- second_repl_offset:-1
- repl_backlog_active:1
- repl_backlog_size:1048576
- repl_backlog_first_byte_offset:43
- repl_backlog_histlen:44893
他重新进来后是作为slave身份了,现在的集群中的master就是102主机了
- 127.0.0.1:6379> info replication
- # Replication
- role:slave
- master_host:192.168.2.102
- master_port:6375
- master_link_status:down
- master_last_io_seconds_ago:-1
- master_sync_in_progress:0
- slave_read_repl_offset:0
- slave_repl_offset:0
- master_link_down_since_seconds:-1
- slave_priority:100
- slave_read_only:1
- replica_announced:1
- connected_slaves:0
- master_failover_state:no-failover
- master_replid:f9eec4b7a919560dbce7aa6117067414d9a891de
- master_replid2:0000000000000000000000000000000000000000
- master_repl_offset:0
- second_repl_offset:-1
- repl_backlog_active:0
- repl_backlog_size:1048576
- repl_backlog_first_byte_offset:0
- repl_backlog_histlen:0
主要都在master上做,文件有点长,很多需要改进的地方,按需修改吧,文件内部的主机、密码、目录、文件名等参数请自行修改。
先执行redis.sh,把自己的环境部好,然后通过ansible-playbook redis-backup.yaml来为两台从redis部署环境,于2024/4/22日成功快速搭建,redis版本为7.0.15,Ansible版本为2.9.27,如有疑问请于CSDN私聊或发送邮箱至sulibao2003@163.com,脚本文件和剧本文件放在百度/迅雷网盘了。
- 分享文件:Ansible+Shell-redis
- 链接:https://pan.xunlei.com/s/VNw5EfaHp-_r1h9Ud5e9vjAdA1#
- 提取码:47xf
- 复制这段内容后打开手机迅雷App,查看更方便
- 链接:https://pan.baidu.com/s/12TYqW8GWEAH0qt_Pm6MJWA
- 提取码:tdbe
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。