redis的一主二从三哨兵配置

devtools/2024/9/23 8:15:58/

redis_0">redis安装脚本

参考了:https://yunweiba.com/163.html

https://download.redis.io/releases/

在上面链接选择合适的版本

wget -P "/opt" https://download.redis.io/releases/redis-7.0.0.tar.gz

安装脚本如下:

#!/bin/bash
# 安装redis,配置设置,该脚本默认安装文件已存在,安装版本是redis-7.0.0,请自行替换对应版本
# wget -P "/opt" https://download.redis.io/releases/redis-7.0.0.tar.gz# 检查gcc是否已安装
if ! command -v gcc &> /dev/null; thenecho "gcc未安装,将使用yum安装"# 使用yum安装gccsudo yum install -y gcc
elseecho "gcc已安装"
fi# 解压Redis安装文件
tar -xzf /opt/redis-7.0.0.tar.gz -C /opt# 进入Redis目录
# shellcheck disable=SC2164
cd /opt/redis-7.0.0# 编译并安装Redis
make
sudo make install PREFIX=/usr/local/redis# 创建配置文件
#Redis基础配置,配置文件保存在 /etc/redis.conf
grep -E -v "^$|^#" /opt/redis-7.0.0/redis.conf > /etc/redis.conf
sed -i "s/bind 127.0.0.1/bind 0.0.0.0/g" /etc/redis.conf
sed -i "s/protected-mode yes/protected-mode no/g" /etc/redis.conf
sed -i "s/daemonize no/daemonize yes/g" /etc/redis.conf
sed -i 's/logfile \"\"/logfile \"\/var\/log\/redis.log\"/g' /etc/redis.conf#PATH配置
echo "export PATH=\$PATH:/usr/local/redis/bin" >>/etc/profile
source /etc/profile#启动redis服务,配置开机启动
cp /opt/redis-7.0.0/utils/redis_init_script /etc/init.d/redis
sed -i 's/\/usr\/local\/bin\/redis-server/\/usr\/local\/redis\/bin\/redis-server/g' /etc/init.d/redis
sed -i 's/CLIEXEC=\/usr\/local\/bin\/redis-cli/CLIEXEC=\/usr\/local\/redis\/bin\/redis-cli/g' /etc/init.d/redis
# shellcheck disable=SC2016
sed -i 's/\/etc\/redis\/\${REDISPORT}.conf/\/etc\/redis.conf/g' /etc/init.d/redis
chkconfig redis on
/etc/init.d/redis start#查看redis监听端口
netstat -anlp|grep redis# 以下为可选配置
# sed -i "s/pidfile \/var\/run\/redis_6379.pid/pidfile \/usr\/local\/redis\/run\/redis_6379.pid/g
# dir指定数据目录
# sed -i "s/dir \.\//dir \/usr\/local\/redis\/data/g" /opt/redis-7.0.0/conf/redis_6379.conf
# 指定log文件目录
# sed -i "s/logfile \"\"/logfile \"\/opt\/local\/redis\/logs\/redis.log\"/g" /usr/local/redis/redis.conf
# 设置密码
# sed -i "s/^# masterauth.*/masterauth ${passwd}/" /usr/local/redis/redis.conf

我们再用一个脚本,来在目标主机上批量安装

#!/bin/bash
#将redis安装脚本和安装文件发送到指定机器然后安装# 目标主机 IP 范围
network="192.168.32"
start_ip=22
end_ip=23# 获取当前日期和时间
current_date=$(date +%Y%m%d)
current_time=$(date +%H%M)# 日志文件名
log_file="${current_date}_${current_time}_redis安装.log"# 发送文件并记录结果到日志文件
function send_file() {local target_ip=$1local file_path=$2scp "$file_path" root@"$target_ip":/opt/ &>> "$log_file"# 检查发送结果并记录到日志文件if [ $? -eq 0 ]; thenecho "${file_path}文件发送成功到${target_ip}"echo "${file_path}文件发送成功到${target_ip}" >> "$log_file"elseecho "${file_path}文件发送失败到${target_ip}"echo "${file_path}文件发送失败到${target_ip}" >> "$log_file"fi
}for ip in $(seq ${start_ip} ${end_ip})
dotarget_ip=${network}.${ip}echo "正在复制文件到 ${target_ip}..."send_file "$target_ip" "/opt/redis.sh"send_file "$target_ip" "/opt/redis-7.0.0.tar.gz" &if [ $? -eq 0 ]; thenecho "文件复制成功到 ${target_ip}"elseecho "文件复制失败到 ${target_ip}"fi
done# 执行脚本,并行执行
for ip in $(seq ${start_ip} ${end_ip})
dotarget_ip=${network}.${ip}ssh root@"$target_ip" "source /opt/redis.sh" >> "$log_file" 2>&1 &
done
waitecho "运行完成。请查看日志文件 ${log_file} 获取详细结果。"

redis_132">redis主从配置

如上我们已经在4台虚拟机上安装了redis,计划按照如下进行1主2从3哨兵的配置

IP角色
192.168.32.21Master1
192.168.32.22Sentinel哨兵
192.168.32.23Slave1
192.168.32.24Slave2
192.168.32.25哨兵2
192.168.32.25哨兵3

参考了该链接:https://cloud.tencent.com/developer/article/2124382

主机配置192.168.32.21


#!/bin/bash
#redis主从配置master_ip="192.168.32.21"
master_pwd="Abc@1234"
start_ip=23
end_ip=24
network="192.168.32"
redis_port=6379# 配置主节点
echo "requirepass $master_pwd" >> /etc/redis.conf
echo "masterauth $master_pwd" >> /etc/redis.conf
systemctl restart redis# 开启6379端口
firewall-cmd --zone=public --add-port=$redis_port/tcp --permanent
# 重启一下防火墙服务
firewall-cmd --reload# 需要关闭防火墙,否则在哨兵模式下,切换主从会失败
systemctl stop firewalld
systemctl disable firewalld# 配置从节点
for ip in $(seq ${start_ip} ${end_ip})
dotarget_ip=${network}.${ip}echo "正在进行 ${target_ip}redis设置..."ssh root@"$target_ip" << EOFecho "replicaof $master_ip $redis_port" >> /etc/redis.confecho "requirepass $master_pwd" >> /etc/redis.confecho "masterauth $master_pwd" >> /etc/redis.confredis-cli -a Abc@1234 shutdown/etc/init.d/redis startsystemctl stop firewalldsystemctl disable firewalld
EOF
done

查看master状态,并set一个key值

[root@node21 ~]# redis-cli
127.0.0.1:6379> auth Abc@1234
OK
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.32.23,port=6379,state=online,offset=210,lag=0
slave1:ip=192.168.32.24,port=6379,state=online,offset=210,lag=1
master_failover_state:no-failover
master_replid:0717a944c0adb0a6a290b2f9ae4d242f44b288f3
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:210
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:210
127.0.0.1:6379> set myname kayotin
OK

查看salve状态,在salve上成功get到了key值,说明同步成功了。

[root@node23 ~]# redis-cli
127.0.0.1:6379> auth Abc@1234
OK
127.0.0.1:6379> get myname
"kayotin"

redis_227">redis哨兵配置

简单来说哨兵就是用来监控各机器的状态,如果master挂掉了,就在哨兵中选举一个当做新的master。redis的哨兵,最少需要配置3台。我们在25,26ip的两台机器上用同样方式配置了哨兵。

哨兵配置脚本如下:

#!/bin/bash
#redis哨兵配置master_ip="192.168.32.21"
sentinel_ip="192.168.32.22"
redis_port=6379
sentinel_port=26379
redis_password="Abc@1234"# 在哨兵节点上配置sentinel
ssh root@$sentinel_ip << EOF# 安装redis时默认设置了开机启动,所以先关闭服务chkconfig redis offredis-cli shutdown# 将哨兵的配置文件复制一份在/etc下面grep -E -v "^$|^#" /opt/redis-7.0.0/sentinel.conf > /etc/sentinel.conf# 修改配置文件# 后台启动服务sed -i "s/daemonize no/daemonize yes/g" /etc/sentinel.conf# 设置logsed -i "s/logfile \"\"/logfile \/var\/log\/sentinel.log/g" /etc/sentinel.conf# 指定监控的mater 的ip,默认2是指有2台哨兵认为master死了就切换sed -i "s/monitor mymaster 127.0.0.1 6379 2/monitor mymaster $master_ip $redis_port 2/g" /etc/sentinel.conf# 默认是30000ms无响应,就认为挂了,我们设置为10s,方便测试sed -i "s/down-after-milliseconds mymaster 30000/down-after-milliseconds mymaster 10000/g" /etc/sentinel.conf# 设置密码echo "sentinel auth-pass mymaster $redis_password" >> /etc/sentinel.conf#echo "bind 0.0.0.0" >> /etc/sentinel.conf# 防火墙开放端口firewall-cmd --zone=public --add-port=$sentinel_port/tcp --permanentfirewall-cmd --reload# 启动哨兵redis-sentinel /etc/sentinel.confEOFecho "Redis哨兵配置完成"

查看哨兵状态,可以看到已经监听到1台master和2个Slave,并且有3个哨兵。

[root@node25 ~]# redis-cli -p 26379
127.0.0.1:26379> info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_tilt_since_seconds:-1
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=192.168.32.21:6379,slaves=2,sentinels=3

容灾演练

我们已经配置了1个master加上2个salve,还有3个哨兵。现在模拟把21上的服务关掉,然后去哨兵那边查看状态,可以看到master的状态已经是down了

127.0.0.1:26379> info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_tilt_since_seconds:-1
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=odown,address=192.168.32.21:6379,slaves=2,sentinels=3

我们设置的是10s后切换,等待约15秒后,可以看到24已经被设定为新的master了。而且因为21挂掉了,这时只剩下一个Slave了。

# Replication
role:master
connected_slaves:1
slave0:ip=192.168.32.23,port=6379,state=online,offset=248180,lag=0
master_failover_state:no-failover
master_replid:a862378e4faf0017f0a51d8b817e02c26d33d91e
master_replid2:5f56d68aa8046f176bcdffe5b04cdc21c7bbedb2
master_repl_offset:248180
second_repl_offset:220622
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:102087
repl_backlog_histlen:146094

重新启动21上的redis,可以看到它已经变成一个slave了

[root@node21 ~]# /etc/init.d/redis start
Starting Redis server...
[root@node21 ~]# redis-cli -a Abc@1234
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:192.168.32.24
master_port:6379
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_read_repl_offset:517881
slave_repl_offset:517881
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:a862378e4faf0017f0a51d8b817e02c26d33d91e
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:517881
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:293313
repl_backlog_histlen:224569

哨兵的切换日志,可以参考该链接https://blog.csdn.net/miaomiao19971215/article/details/108567837


http://www.ppmy.cn/devtools/113850.html

相关文章

音视频直播应用场景探讨之RTMP推流还是GB28181接入?

技术背景 好多开发者跟我们沟通音视频解决方案的时候&#xff0c;不清楚什么时候用RTMP推送模块&#xff0c;什么时候用GB28181设备接入模块&#xff0c;也不清楚二者差异化。实际上&#xff0c;RTMP推流和GB28181接入模块&#xff0c;在很多方面存在差异&#xff0c;如应用领…

实时系统资源监测:AutoPowerOptionsOK确保电脑性能与节能兼备

科技赋予生活翅膀&#xff0c;让我们在快节奏中翱翔&#xff0c;协调工作与梦想&#xff0c;让每一个梦想都有机会照进现实&#xff0c;绽放光彩——科技的进步不仅推动了社会的发展&#xff0c;也极大地改善了人们的日常生活。在众多科技成果中&#xff0c;电脑作为信息处理的…

基于web的 BBS论坛管理系统设计与实现

博主介绍&#xff1a;专注于Java .net php phython 小程序 等诸多技术领域和毕业项目实战、企业信息化系统建设&#xff0c;从业十五余年开发设计教学工作 ☆☆☆ 精彩专栏推荐订阅☆☆☆☆☆不然下次找不到哟 我的博客空间发布了1000毕设题目 方便大家学习使用 感兴趣的可以…

老旧电力系统安全隐患增加 该如何预防电气线路老化等因素引发的电气火灾呢?

为应对我国电气火灾事故频发的挑战&#xff0c;安科瑞电气股份有限公司开发了AcrelCloud-6000安全用电管理云平台。这一平台依托移动互联网和云计算技术&#xff0c;结合物联网传感器&#xff0c;将办公楼、学校、医院、工厂、体育场馆、宾馆及福利院等人员密集场所的电气安全数…

PHP 数组排序类型介绍

在PHP中&#xff0c;数组排序是一项常见且重要的操作&#xff0c;它允许开发者根据一定的规则对数组中的元素进行排序。PHP提供了多种数组排序函数&#xff0c;以适应不同的排序需求。这些函数包括基本的升序和降序排序&#xff0c;以及基于特定键值、自定义排序逻辑等的复杂排…

Unity 中切换编辑器的语言

在 Unity 中切换编辑器的语言&#xff08;如切换到中文&#xff09;&#xff0c;可以按照以下步骤进行&#xff1a; 1. 打开 Unity Hub 启动 Unity Hub。 2. 修改编辑器语言设置 在 Unity Hub 的右上角&#xff0c;点击你的 头像图标 或 账户名称&#xff0c;然后选择 Prefer…

字节秋招前端一面-9.1

视频面试1h JS有哪些数据类型检测数据类型的方式diff算法&#xff0c;是否使用过&#xff08;手写&#xff09;开发中如何解决跨域问题cors跨域问题怎么解决样式中平常用什么布局比较多flex布局是什么伪类选择器有哪些promise是什么开发中如何解决安全性问题 手撕代码&#xf…

线性代数之QR分解和SVD分解

文章目录 1.QR分解Schmidt正交化Householder变换QR分解的应用 2. 求矩阵特征值、特征向量的基本方法3.SVD分解SVD分解的应用 参考文献 1.QR分解 矩阵的正交分解又称为QR分解&#xff0c;是将矩阵分解为一个正交矩阵Q和一个上三角矩阵R的乘积的形式。 任意实数方阵A&#xff0c…