Redis 安装并配置哨兵模式

867 阅读2分钟

Redis 安装并配置哨兵模式

1. 安装 Redis

1.1 主机环境(CentOS 7.9)

主机IP
master192.168.160.134
slave01192.168.160.135
slave02192.168.160.136

1.2 源码安装(所有主机)

#! /bin/bash
set -e
error_handler() {
    local error_code=$?
    local line_number=$1
    echo "脚本在第 $line_number 行执行时出现错误,错误码: $error_code"
    exit $error_code
}
trap 'error_handler $LINENO' ERR
# 删除编译安装的旧版本
rm -rf /usr/local/bin/redis-server
rm -rf /usr/local/bin/redis-cli
rm -rf /usr/local/bin/redis-benchmark
rm -rf /usr/local/bin/redis-check-aof
rm -rf /usr/local/bin/redis-check-rdb


echo "开始编译并安装"
if ! command -v wget > /dev/null 2>&1; then
    yum install wget -y
else
    echo "wget 已安装。"
fi

if command -v gcc > /dev/null 2>&1; then
    echo "gcc 已安装。"
else
    yum install gcc -y
fi
wget -P /opt http://download.redis.io/releases/redis-7.2.0.tar.gz 
tar -xvf /opt/redis-7.2.0.tar.gz 
cd /opt/redis-7.2.0
make && make install 

mkdir -p /usr/local/redis/log
cp /opt/redis-7.2.0/redis.conf /usr/local/redis/
sed -i '/^\s*#/d' /usr/local/redis/redis.conf
sed -i '/^\s*$/d' /usr/local/redis/redis.conf

# 添加服务
cat > /usr/lib/systemd/system/redis.service << EOF
[Unit]
Description=Redis In-Memory Data Store
After=network.target

[Service]
# 此处为了方便,直接使用 root 用户
# 正确方式应是创建 redis 用户
User=root 
Group=root
ExecStart=/usr/local/bin/redis-server /usr/local/redis/redis.conf
ExecStop=/usr/local/bin/redis-cli shutdown
Type=forking

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload

2. 哨兵模式配置

2.1 master

firewall-cmd --zone=public --add-port=6379/tcp --permanent
firewall-cmd --zone=public --add-port=26379/tcp --permanent
firewall-cmd --reload

echo  "requirepass linuxredis" >> /usr/local/redis/redis.conf
echo  "masterauth linuxredis" >> /usr/local/redis/redis.conf
sed -i.bak 's/bind 127.0.0.1 -::1/bind 0.0.0.0/' /usr/local/redis/redis.conf
sed -i 's/daemonize no/daemonize yes/' /usr/local/redis/redis.conf
sed -i 's/replica-priority 100/replica-priority 10/' /usr/local/redis/redis.conf
sed -i 's@^.*logfile .*$@logfile "/usr/local/redis/log/redis.log"@' /usr/local/redis/redis.conf

cp /opt/redis-7.2.0/sentinel.conf /usr/local/redis/
sed -i '/^\s*#/d' /usr/local/redis/sentinel.conf
sed -i '/^\s*$/d' /usr/local/redis/sentinel.conf
sed -i '/^sentinel monitor/a sentinel auth-pass mymaster linuxredis' /usr/local/redis/sentinel.conf
sed -i '/^sentinel auth-pass/a sentinel down-after-milliseconds mymaster 10000' /usr/local/redis/sentinel.conf
sed -i '0,/^sentinel monitor/s/^sentinel monitor.*/sentinel monitor mymaster 192.168.160.134 6379 2/' /usr/local/redis/sentinel.conf
sed -i 's/daemonize no/daemonize yes/' /usr/local/redis/sentinel.conf
sed -i 's@^.*logfile .*$@logfile "/usr/local/redis/log/sentinel.log"@' /usr/local/redis/sentinel.conf

cat > /usr/lib/systemd/system/redis-sentinel.service << EOF 
[Unit]
Description=Redis Sentinel
After=network.target

[Service]
ExecStart=/usr/local/bin/redis-sentinel /usr/local/redis/sentinel.conf
ExecStop=/usr/local/bin/redis-cli shutdown redis-sentinel
Type=forking
User=root
Group=root

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl start redis-sentinel
systemctl enable --now redis-sentinel

2.2 slave01 和 slave02

firewall-cmd --zone=public --add-port=6379/tcp --permanent
firewall-cmd --zone=public --add-port=26379/tcp --permanent
firewall-cmd --reload

sed -i.bak 's/bind 127.0.0.1 -::1/bind 0.0.0.0/' /usr/local/redis/redis.conf
sed -i 's@^.*logfile .*$@logfile "/usr/local/redis/log/redis.log"@' /usr/local/redis/redis.conf
echo  "requirepass linuxredis" >> /usr/local/redis/redis.conf
echo  "masterauth linuxredis" >> /usr/local/redis/redis.conf
echo "replicaof 192.168.160.134 6379" >> /usr/local/redis/redis.conf

cp /opt/redis-7.2.0/sentinel.conf /usr/local/redis/
sed -i '/^\s*#/d' /usr/local/redis/sentinel.conf
sed -i '/^\s*$/d' /usr/local/redis/sentinel.conf
# 注意这里的密码:linuxredis
sed -i '/^sentinel monitor/a sentinel auth-pass mymaster linuxredis' 
sed -i '/^sentinel auth-pass/a sentinel down-after-milliseconds mymaster 10000' 
/usr/local/redis/sentinel.conf
sed -i '0,/^sentinel monitor/s/^sentinel monitor.*/sentinel monitor mymaster 192.168.160.134 6379 2/' /usr/local/redis/sentinel.conf
sed -i 's/daemonize no/daemonize yes/' /usr/local/redis/sentinel.conf
sed -i 's@^.*logfile .*$@logfile "/usr/local/redis/log/sentinel.log"@' /usr/local/redis/sentinel.conf

cat > /usr/lib/systemd/system/redis-sentinel.service << EOF 
[Unit]
Description=Redis Sentinel
After=network.target
After=network-online.target
Wants=network-online.target
[Service]
ExecStart=/usr/local/bin/redis-sentinel /usr/local/redis/sentinel.conf
ExecStop=/usr/local/bin/redis-cli shutdown redis-sentinel
Type=forking
User=root
Group=root
[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload

systemctl enable --now redis redis-sentinel
systemctl start redis redis-sentinel && systemctl status redis redis-sentinel

2.3 检查配置结果

[root@localhost redis]# 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.160.134:6379,slaves=2,sentinels=3
127.0.0.1:26379>

2.4 修改密码

可直接修改 redis.conf 文件中的 requirepassmasterauth 配置项,并重启服务。如果重启之后未生效,可尝试删除主备节点 sentinel.conf 配置文件中“# Generated by CONFIG REWRITE” 这一行以后的配置项。

3. 写在最后

文中涉及的命令均可直接放在 shell 脚本中一键运行,如有疑惑,可复制命令去询问 AI。

参考文章:redis哨兵模式的原理及部署