Redis 哨兵模式
| 服务器IP | 主机角色 |
|---|---|
| 192.168.10.13 | Redis Master |
| 192.168.10.14 | Redis Slave1 |
| 192.168.10.15 | Redis Slave2 |
Redis Master
-
下载 redis 安装包
- 服务器支持访问互联网
- 服务器不支持访问互联网
wget https://pdpublic.mingdao.com/private-deployment/offline/common/redis-8.0.4-glibc2.17-amd64.tar.gz# redis 安装包文件下载链接,下载完成后上传到部署服务器https://pdpublic.mingdao.com/private-deployment/offline/common/redis-8.0.4-glibc2.17-amd64.tar.gz -
解压到安装目录
tar -zxvf redis-8.0.4-glibc2.17-amd64.tar.gzmv redis-8.0.4-glibc2.17-amd64 /usr/local/redis -
调整内核参数
echo 'net.core.somaxconn = 32768' >> /etc/sysctl.d/99-sysctl.confecho 'vm.overcommit_memory = 1' >> /etc/sysctl.d/99-sysctl.confsysctl -p -
创建 redis 数据目录
mkdir /data/redis -
修改配置文件
cat > /usr/local/redis/redis.conf <<'EOF'bind 0.0.0.0protected-mode yesport 6379tcp-backlog 511timeout 0tcp-keepalive 300daemonize nosupervised nopidfile /usr/local/redis/redis.pidloglevel noticelogfile /usr/local/redis/redis.logdatabases 16save 900 1save 300 10save 60 100000stop-writes-on-bgsave-error yesrdbcompression yesrdbchecksum yesdbfilename dump.rdbdir /data/redisslave-serve-stale-data yesslave-read-only yesrepl-diskless-sync norepl-diskless-sync-delay 5repl-disable-tcp-nodelay noslave-priority 100lua-time-limit 5000slowlog-log-slower-than 10000slowlog-max-len 128latency-monitor-threshold 0notify-keyspace-events ""hash-max-ziplist-entries 512hash-max-ziplist-value 64list-max-ziplist-size -2list-compress-depth 0set-max-intset-entries 512zset-max-ziplist-entries 128zset-max-ziplist-value 64hll-sparse-max-bytes 3000activerehashing yesclient-output-buffer-limit normal 0 0 0client-output-buffer-limit slave 256mb 64mb 60client-output-buffer-limit pubsub 0 0 0hz 10requirepass 123456masterauth 123456maxmemory 16gbmaxmemory-policy allkeys-lrumaxclients 100000rename-command KEYS ""EOF- redis 认证与主从认证的密码为
123456,实际部署必须修改为强口令 - 若密码中包含特殊字符,仅允许
-或_,禁止使用@ ! # &等字符,以避免兼容性问题
- redis 认证与主从认证的密码为
-
创建 redis 用户授权
useradd -U -M -s /sbin/nologin redischown -R redis:redis /usr/local/redis/ /data/redis -
配置 systemd 管理
cat > /etc/systemd/system/redis.service <<'EOF'[Unit]Description=Redis[Service]User=redisGroup=redisTasksMax=infinityLimitNOFILE=102400LimitNPROC=infinityLimitCORE=0ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/redis.confExecStop=/usr/bin/kill $MAINPIDRestart=on-failure[Install]WantedBy=multi-user.targetEOF -
启动 redis 服务并加入开机自启动
systemctl start redissystemctl enable redis
Redis Slave-01
-
下载 redis 安装包
- 服务器支持访问互联网
- 服务器不支持访问互联网
wget https://pdpublic.mingdao.com/private-deployment/offline/common/redis-8.0.4-glibc2.17-amd64.tar.gz# redis 安装包文件下载链接,下载完成后上传到部署服务器https://pdpublic.mingdao.com/private-deployment/offline/common/redis-8.0.4-glibc2.17-amd64.tar.gz -
解压到安装目录
tar -zxvf redis-8.0.4-glibc2.17-amd64.tar.gzmv redis-8.0.4-glibc2.17-amd64 /usr/local/redis -
调整内核参数
echo 'net.core.somaxconn = 32768' >> /etc/sysctl.d/99-sysctl.confecho 'vm.overcommit_memory = 1' >> /etc/sysctl.d/99-sysctl.confsysctl -p -
创建 redis 数据目录
mkdir /data/redis -
修改配置文件
cat > /usr/local/redis/redis.conf <<'EOF'bind 0.0.0.0protected-mode yesport 6379tcp-backlog 511timeout 0tcp-keepalive 300daemonize nosupervised nopidfile /usr/local/redis/redis.pidloglevel noticelogfile /usr/local/redis/redis.logdatabases 16save 900 1save 300 10save 60 100000stop-writes-on-bgsave-error yesrdbcompression yesrdbchecksum yesdbfilename dump.rdbdir /data/redisslave-serve-stale-data yesslave-read-only yesrepl-diskless-sync norepl-diskless-sync-delay 5repl-disable-tcp-nodelay noslave-priority 100lua-time-limit 5000slowlog-log-slower-than 10000slowlog-max-len 128latency-monitor-threshold 0notify-keyspace-events ""hash-max-ziplist-entries 512hash-max-ziplist-value 64list-max-ziplist-size -2list-compress-depth 0set-max-intset-entries 512zset-max-ziplist-entries 128zset-max-ziplist-value 64hll-sparse-max-bytes 3000activerehashing yesclient-output-buffer-limit normal 0 0 0client-output-buffer-limit slave 256mb 64mb 60client-output-buffer-limit pubsub 0 0 0hz 10requirepass 123456masterauth 123456maxmemory 16gbmaxmemory-policy allkeys-lrumaxclients 100000rename-command KEYS ""slaveof 192.168.1.13 6379EOF- redis 认证与主从认证的密码为
123456,实际部署必须修改为强口令 - 若密码中包含特殊字符,仅允许
-或_,禁止使用@ ! # &等字符,以避免兼容性问题 - slaveof 参数注意在实际部署过程中进行替换 master ip地址
- redis 认证与主从认证的密码为
-
创建 redis 用户授权
useradd -U -M -s /sbin/nologin redischown -R redis:redis /usr/local/redis/ /data/redis -
配置 systemd 管理
cat > /etc/systemd/system/redis.service <<'EOF'[Unit]Description=Redis[Service]User=redisGroup=redisTasksMax=infinityLimitNOFILE=102400LimitNPROC=infinityLimitCORE=0ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/redis.confExecStop=/usr/bin/kill $MAINPIDRestart=on-failure[Install]WantedBy=multi-user.targetEOF -
启动 redis 服务并加入开机自启动
systemctl start redissystemctl enable redis
Redis Slave-02
-
下载 redis 安装包
- 服务器支持访问互联网
- 服务器不支持访问互联网
wget https://pdpublic.mingdao.com/private-deployment/offline/common/redis-8.0.4-glibc2.17-amd64.tar.gz# redis 安装包文件下载链接,下载完成后上传 到部署服务器https://pdpublic.mingdao.com/private-deployment/offline/common/redis-8.0.4-glibc2.17-amd64.tar.gz -
解压到安装目录
tar -zxvf redis-8.0.4-glibc2.17-amd64.tar.gzmv redis-8.0.4-glibc2.17-amd64 /usr/local/redis -
调整内核参数
echo 'net.core.somaxconn = 32768' >> /etc/sysctl.d/99-sysctl.confecho 'vm.overcommit_memory = 1' >> /etc/sysctl.d/99-sysctl.confsysctl -p -
创建 redis 数据目录
mkdir /data/redis -
修改配置文件
cat > /usr/local/redis/redis.conf <<EOFbind 0.0.0.0protected-mode yesport 6379tcp-backlog 511timeout 0tcp-keepalive 300daemonize nosupervised nopidfile /usr/local/redis/redis.pidloglevel noticelogfile /usr/local/redis/redis.logdatabases 16save 900 1save 300 10save 60 100000stop-writes-on-bgsave-error yesrdbcompression yesrdbchecksum yesdbfilename dump.rdbdir /data/redisslave-serve-stale-data yesslave-read-only yesrepl-diskless-sync norepl-diskless-sync-delay 5repl-disable-tcp-nodelay noslave-priority 100lua-time-limit 5000slowlog-log-slower-than 10000slowlog-max-len 128latency-monitor-threshold 0notify-keyspace-events ""hash-max-ziplist-entries 512hash-max-ziplist-value 64list-max-ziplist-size -2list-compress-depth 0set-max-intset-entries 512zset-max-ziplist-entries 128zset-max-ziplist-value 64hll-sparse-max-bytes 3000activerehashing yesclient-output-buffer-limit normal 0 0 0client-output-buffer-limit slave 256mb 64mb 60client-output-buffer-limit pubsub 0 0 0hz 10requirepass 123456masterauth 123456maxmemory 16gbmaxmemory-policy allkeys-lrumaxclients 100000rename-command KEYS ""slaveof 192.168.1.13 6379EOF- redis 认证与主从认证的密码为
123456,实际部署必须修改为强口令 - 若密码中包含特殊字符,仅允许
-或_,禁止使用@ ! # &等字符,以避免兼容性问题 - slaveof 参数注意在实际部署过程中进行替换 master ip地址
- redis 认证与主从认证的密码为
-
创建 redis 用户授权
useradd -U -M -s /sbin/nologin redischown -R redis:redis /usr/local/redis/ /data/redis -
配置 systemd 管理
cat > /etc/systemd/system/redis.service <<'EOF'[Unit]Description=Redis[Service]User=redisGroup=redisTasksMax=infinityLimitNOFILE=102400LimitNPROC=infinityLimitCORE=0ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/redis.confExecStop=/usr/bin/kill $MAINPIDRestart=on-failure[Install]WantedBy=multi-user.targetEOF -
启动 redis 服务并加入开机自启动
systemctl start redissystemctl enable redis
检查各节点主从同步状态
/usr/local/redis/bin/redis-cli -a 123456 info replication
哨兵配置
三台 Redis 服务器都操作
-
配置 sentinel 配置文件
cat > /usr/local/redis/sentinel.conf <<'EOF'port 26379daemonize yessentinel deny-scripts-reconfig yessentinel monitor mymaster 192.168.10.13 6379 2sentinel auth-pass mymaster 123456sentinel down-after-milliseconds mymaster 5000sentinel failover-timeout mymaster 30000pidfile "/usr/local/redis/redis-sentinel.pid"logfile "/usr/local/redis/redis-sentinel.log"dir "/data/redis/sentinel"maxclients 100000EOF- 注意将 192.168.10.13 替换为实际当前 Redis Master 节点 IP
- 123456 为示例密码,部署时请改为与 Redis 节点一致的强密码
- 若密码中包含特殊字符,仅允许
-或_,禁止使用@ ! # &等字符,以避免兼容性问题
-
创建目录并授权 redis 相关目录权限
mkdir -p /data/redis/sentinelchown -R redis:redis /data/redis /usr/local/redis -
配置 systemd 管理
cat > /etc/systemd/system/sentinel.service <<'EOF'[Unit]Description=Redis-sentinel[Service]User=redisGroup=redisType=forkingTasksMax=infinityLimitNOFILE=102400LimitNPROC=infinityLimitCORE=0ExecStart=/usr/local/redis/bin/redis-sentinel /usr/local/redis/sentinel.confExecStop=/usr/bin/kill $MAINPIDRestart=on-failure[Install]WantedBy=multi-user.targetEOF -
启动哨兵
systemctl start sentinelsystemctl enable sentinel
检查状态
-
查看 Sentinel 状态
/usr/local/redis/bin/redis-cli -a 123456 -p 26379 info sentinel -
获取当前 Master 信息
/usr/local/redis/bin/redis-cli -a 123456 -p 26379 SENTINEL get-master-addr-by-name mymaster -
查看已监控的从库
/usr/local/redis/bin/redis-cli -a 123456 -p 26379 SENTINEL slaves mymaster