系统环境
- 本机为单机模式部署,多机配置雷同,直接迁移到不同机器即可
- 10.0.1.101 均为本机内网IP
- 系统Ubuntu20.04
- redis版本为2023-11-30下载的官方版本
redis-stack-server-7.2.0-v6.focal.x86_64.tar.gz
下载Redis
mkdir -p ~/redis && cd ~/redis
wget -O redis-stack-server-7.2.0-v6.focal.x86_64.tar.gz "https://packages.redis.io/redis-stack/redis-stack-server-7.2.0-v6.focal.x86_64.tar.gz?_gl=1*1erwsy9*_ga*ODY3MTMwMjYzLjE2OTY5MzA3NTc.*_ga_8BKGRQKRPV*MTcwMTMxMTcyNS4yLjEuMTcwMTMxMTczOS40Ni4wLjA.*_gcl_au*NDY3ODMzMzAuMTY5NjkzMDc1Nw.."
tar -zxvf redis-stack-server-7.2.0-v6.focal.x86_64.tar.gz
cp -pr redis-stack-server-7.2.0-v6 /usr/local/redis1
cp -pr redis-stack-server-7.2.0-v6 /usr/local/redis2
cp -pr redis-stack-server-7.2.0-v6 /usr/local/redis3
配置1
redis.conf
/usr/local/redis1/bin/redis.conf
port 6379
daemonize no
protected-mode no
logfile "/usr/local/redis1/bin/redis.log"
pidfile "/usr/local/redis1/bin/redis.pid"
dir "/usr/local/redis1/bin"
maxclients 20000
sentinel.conf
/usr/local/redis1/bin/sentinel.conf
port 26379
daemonize no
protected-mode no
logfile "/usr/local/redis1/bin/sentine.log"
pidfile "/usr/local/redis1/bin/sentine.pid"
dir "/usr/local/redis1/bin"
sentinel monitor cluster1 10.0.1.101 6379 1
sentinel failover-timeout cluster1 20000
配置2
redis.conf
/usr/local/redis2/bin/redis.conf
port 6380
daemonize no
protected-mode no
logfile "/usr/local/redis2/bin/redis.log"
pidfile "/usr/local/redis2/bin/redis.pid"
dir "/usr/local/redis2/bin"
replicaof 10.0.1.101 6379
maxclients 20000
sentinel.conf
/usr/local/redis2/bin/sentinel.conf
port 26380
daemonize no
protected-mode no
logfile "/usr/local/redis2/bin/sentine.log"
pidfile "/usr/local/redis2/bin/sentine.pid"
dir "/usr/local/redis2/bin"
sentinel monitor cluster1 10.0.1.101 6379 1
sentinel failover-timeout cluster1 20000
配置3
redis.conf
/usr/local/redis3/bin/redis.conf
port 6381
daemonize no
protected-mode no
logfile "/usr/local/redis3/bin/redis.log"
pidfile "/usr/local/redis3/bin/redis.pid"
dir "/usr/local/redis3/bin"
replicaof 10.0.1.101 6379
maxclients 20000
sentinel.conf
/usr/local/redis3/bin/sentinel.conf
port 26381
daemonize no
protected-mode no
logfile "/usr/local/redis3/bin/sentine.log"
pidfile "/usr/local/redis3/bin/sentine.pid"
dir "/usr/local/redis3/bin"
sentinel monitor cluster1 10.0.1.101 6379 1
sentinel failover-timeout cluster1 20000
systemd – redis
redis1
vim /lib/systemd/system/redis1.service
[Unit]
Description=Redis1
After=network.target
[Service]
ExecStart=/usr/local/redis1/bin/redis-server /usr/local/redis1/bin/redis.conf --daemonize no
ExecStop=/usr/local/redis1/bin/redis-cli -h 127.0.0.1 -p 6379 shutdown
[Install]
WantedBy=multi-user.target
redis2
vim /lib/systemd/system/redis1.service
[Unit]
Description=Redis2
After=network.target redis1.service
[Service]
ExecStart=/usr/local/redis2/bin/redis-server /usr/local/redis2/bin/redis.conf --daemonize no
ExecStop=/usr/local/redis2/bin/redis-cli -h 127.0.0.1 -p 6379 shutdown
[Install]
WantedBy=multi-user.target
redis3
vim /lib/systemd/system/redis1.service
[Unit]
Description=Redis3
After=network.target redis2.service redis3.service
[Service]
ExecStart=/usr/local/redis3/bin/redis-server /usr/local/redis3/bin/redis.conf --daemonize no
ExecStop=/usr/local/redis3/bin/redis-cli -h 127.0.0.1 -p 6379 shutdown
[Install]
WantedBy=multi-user.target
启动redis1 2 3 进程
systemctl start redis1
systemctl start redis2
systemctl start redis3
查看端口监听情况
netstat -tunlp | grep redis
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 25259/redis-server
tcp 0 0 0.0.0.0:6381 0.0.0.0:* LISTEN 25277/redis-server
tcp 0 0 0.0.0.0:6380 0.0.0.0:* LISTEN 25266/redis-server
tcp6 0 0 :::6379 :::* LISTEN 25259/redis-server
tcp6 0 0 :::6381 :::* LISTEN 25277/redis-server
tcp6 0 0 :::6380 :::* LISTEN 25266/redis-server
systemd – sentinel
sentinel1
/lib/systemd/system/sentinel1.service
[Unit]
Description=redis-sentinel
After=network.target
[Service]
ExecStart=/usr/local/redis1/bin/redis-sentinel /usr/local/redis1/bin/sentinel.conf --daemonize no
ExecStop=/usr/local/redis1/bin/redis-cli -h 127.0.0.1 -p 26379 shutdown
[Install]
WantedBy=multi-user.target
sentinel2
/lib/systemd/system/sentinel2.service
[Unit]
Description=redis-sentinel2
After=network.target sentinel1.service
[Service]
ExecStart=/usr/local/redis2/bin/redis-sentinel /usr/local/redis2/bin/sentinel.conf --daemonize no
ExecStop=/usr/local/redis2/bin/redis-cli -h 127.0.0.1 -p 26379 shutdown
[Install]
WantedBy=multi-user.target
sentinel3
/lib/systemd/system/sentinel3.service
[Unit]
Description=redis-sentinel3
After=network.target sentinel1.service sentinel2.service
[Service]
ExecStart=/usr/local/redis3/bin/redis-sentinel /usr/local/redis3/bin/sentinel.conf --daemonize no
ExecStop=/usr/local/redis3/bin/redis-cli -h 127.0.0.1 -p 26379 shutdown
[Install]
WantedBy=multi-user.target
启动sentinel1 2 3 进程
systemctl start sentinel1.service
systemctl start sentinel2.service
systemctl start sentinel3.service
查看端口监听
netstat -tunlp | grep sentinel
netstat -tunlp | grep -i redis-sentine
tcp 0 0 0.0.0.0:26379 0.0.0.0:* LISTEN 25514/redis-sentine
tcp 0 0 0.0.0.0:26381 0.0.0.0:* LISTEN 25532/redis-sentine
tcp 0 0 0.0.0.0:26380 0.0.0.0:* LISTEN 25525/redis-sentine
tcp6 0 0 :::26379 :::* LISTEN 25514/redis-sentine
tcp6 0 0 :::26381 :::* LISTEN 25532/redis-sentine
tcp6 0 0 :::26380 :::* LISTEN 25525/redis-sentine
检查Slave和哨兵状态
redis1 6379
cd /usr/local/redis1/bin
-
connected_slaves:2 两个slave已建立
./redis-cli -h 127.0.0.1 -p 6379 info replication # Replication role:master connected_slaves:2 slave0:ip=10.0.1.101,port=6380,state=online,offset=23797,lag=1 slave1:ip=10.0.1.101,port=6381,state=online,offset=23797,lag=1 master_failover_state:no-failover master_replid:4f0921aec8cfbb6159d9c44f9520512db8ae7006 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:24059 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:24059
-
slaves=2,sentinels=3
./redis-cli -h 127.0.0.1 -p 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=cluster1,status=ok,address=10.0.1.101:6379,slaves=2,sentinels=3
redis2 6380
./redis-cli -h 127.0.0.1 -p 6380 info replication
# Replication
role:slave
master_host:10.0.1.101
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_read_repl_offset:58502
slave_repl_offset:58502
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:4f0921aec8cfbb6159d9c44f9520512db8ae7006
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:58502
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:58502
测试
关闭redis1 结果
由此可见,关闭redis1之后,redis2会变成了master接管集群
systemctl stop redis1
./redis-cli -h 127.0.0.1 -p 6379 info replication
Could not connect to Redis at 127.0.0.1:6379: Connection refused
./redis-cli -h 127.0.0.1 -p 6380 info replication
# Replication
role:master
connected_slaves:1
slave0:ip=10.0.1.101,port=6381,state=online,offset=92299,lag=0
master_failover_state:no-failover
master_replid:4e8285f2779fc4dc25d31e1cdfbdc7fdaf65877e
master_replid2:4f0921aec8cfbb6159d9c44f9520512db8ae7006
master_repl_offset:92430
second_repl_offset:91753
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:92430
恢复redis1 结果
由此可见,redis1恢复之后,redis2依然为Master
systemctl start redis1
./redis-cli -h 127.0.0.1 -p 6379 info replication
Replication
role:slave
master_host:10.0.1.101
master_port:6380
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_read_repl_offset:104851
slave_repl_offset:104851
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:4e8285f2779fc4dc25d31e1cdfbdc7fdaf65877e
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:104851
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:103912
repl_backlog_histlen:940
./redis-cli -h 127.0.0.1 -p 6380 info replication
Replication
role:master
connected_slaves:2
slave0:ip=10.0.1.101,port=6381,state=online,offset=105506,lag=0
slave1:ip=10.0.1.101,port=6379,state=online,offset=105520,lag=0
master_failover_state:no-failover
master_replid:4e8285f2779fc4dc25d31e1cdfbdc7fdaf65877e
master_replid2:4f0921aec8cfbb6159d9c44f9520512db8ae7006
master_repl_offset:105782
second_repl_offset:91753
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:105782
# 所有进程自启动
> 一切就绪后
```bash
systemctl enable redis1
systemctl enable redis2
systemctl enable redis3
systemctl enable sentinel1.service
systemctl enable sentinel2.service
systemctl enable sentinel3.service