redis的一主二从三哨兵配置

redis安装脚本

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

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

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

bash 复制代码
wget -P "/opt" https://download.redis.io/releases/redis-7.0.0.tar.gz

安装脚本如下:

bash 复制代码
#!/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; then
    echo "gcc未安装,将使用yum安装"

    # 使用yum安装gcc
    sudo yum install -y gcc
else
    echo "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

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

bash 复制代码
#!/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=$1
    local file_path=$2

    scp "$file_path" root@"$target_ip":/opt/ &>> "$log_file"
    # 检查发送结果并记录到日志文件
    if [ $? -eq 0 ]; then
        echo "${file_path}文件发送成功到${target_ip}"
        echo "${file_path}文件发送成功到${target_ip}" >> "$log_file"
    else
        echo "${file_path}文件发送失败到${target_ip}"
        echo "${file_path}文件发送失败到${target_ip}" >> "$log_file"
    fi
}

for ip in $(seq ${start_ip} ${end_ip})
do
    target_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 ]; then
        echo "文件复制成功到 ${target_ip}"
    else
        echo "文件复制失败到 ${target_ip}"
    fi
done

# 执行脚本,并行执行
for ip in $(seq ${start_ip} ${end_ip})
do
    target_ip=${network}.${ip}
    ssh root@"$target_ip" "source /opt/redis.sh" >> "$log_file" 2>&1 &
done
wait

echo "运行完成。请查看日志文件 ${log_file} 获取详细结果。"

redis主从配置

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

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

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

主机配置192.168.32.21

bash 复制代码
#!/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})
do
    target_ip=${network}.${ip}
    echo "正在进行 ${target_ip} 的redis设置..."
    ssh root@"$target_ip" << EOF
        echo "replicaof $master_ip $redis_port" >> /etc/redis.conf
        echo "requirepass $master_pwd" >> /etc/redis.conf
        echo "masterauth $master_pwd" >> /etc/redis.conf
        redis-cli -a Abc@1234 shutdown
        /etc/init.d/redis start
        systemctl stop firewalld
        systemctl disable firewalld
EOF
done

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

bash 复制代码
[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值,说明同步成功了。

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

redis哨兵配置

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

哨兵配置脚本如下:

bash 复制代码
#!/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 off
    redis-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
    # 设置log
    sed -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 --permanent
    firewall-cmd --reload

    # 启动哨兵
    redis-sentinel /etc/sentinel.conf

EOF

echo "Redis哨兵配置完成"

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

bash 复制代码
[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了

bash 复制代码
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了。

bash 复制代码
# 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了

bash 复制代码
[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

相关推荐
ignativs amor2 小时前
Python redis 安装和使用介绍
开发语言·redis·python
NCU_wander2 小时前
五种数据库特性对比(Redis/Mysql/SQLite/ES/MongoDB)
数据库·redis·mysql
Jacky-YY2 小时前
教你如何在Java中操作Redis
java·开发语言·redis
fensnote4 小时前
QTableView使用QSortFilterProxyModel后行号错乱
数据库
huapiaoy7 小时前
Redis的一些通用指令
数据库·redis·缓存
A_cot7 小时前
深入了解 Maven 和 Redis
java·redis·maven
Lojarro7 小时前
后端-navicat查找语句(单表与多表)
数据库·mysql
月泪同学7 小时前
数据库面试题整理
数据库·mysql·面试
程序员学习随笔9 小时前
PostgreSQL技术内幕11:PostgreSQL事务原理解析-MVCC
数据库·postgresql