Redis的哨兵集群是建立在主备集群之上的自动主备切换集群。
首先,要有一个主备集群。假设有3台电脑分别是MASTER、SLAVE1、SLAVE2。分别在这3台电脑上安装好Redis,并在SLAVE1、SLAVE2上设置为从MASTER同步(replicaof ***),那么它们就组成了主备集群,但是如果主节点出现故障了,从节点并不能提升为主节点,依然不能写入。为了解决这个问题,就要用到哨兵了。
Redis Sentinel(哨兵)是个单独的程序,用来监控主备集群的状态,如果出现故障,则进行主备切换,当前的主节点可以从哨兵查询。哨兵启动时,只需要配置被监控集群的主节点,就可以自动获取整个集群信息。
哨兵自身也可以组成集群,在3台电脑上,分别安装哨兵,配置是一样的。他们通过被监控的集群,可以自动相互识别。这样,要获取Redis主备集群的主节点,就可以从任意一个哨兵那里获取。
机器准备
3台腾讯云Ubuntu主机,分别是:10.0.16.12(主节点)、10.0.16.7(从节点1)、10.0.16.16(从节点2)
手动安装
下面,说明一下Redis哨兵集群的安装步骤:
主节点
【步骤1】安装 redis、redis-sentinel
代码语言:javascript复制curl -fsSL https://packages.redis.io/gpg | sudo gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/redis.list
sudo apt-get update
sudo apt-get install redis redis-sentinel -y
【步骤2】配置IP、密码、哨兵监控对象(10.0.16.12)
代码语言:javascript复制# config redis-server
sudo sed -i "s/bind 127.0.0.1 -::1/bind 10.0.16.12 127.0.0.1 -::1/" /etc/redis/redis.conf
sudo sed -i "s/# requirepass foobared/requirepass 123456/" /etc/redis/redis.conf
sudo sed -i "s/# masterauth <master-password>/masterauth 123456/" /etc/redis/redis.conf
sudo systemctl restart redis-server
# config redis-sentinel
sudo sed -i "s/bind 127.0.0.1 ::1/bind 10.0.16.12 127.0.0.1 ::1/" /etc/redis/sentinel.conf
sudo sed -i "s/sentinel monitor mymaster 127.0.0.1 6379 2/sentinel monitor mymaster 10.0.16.12 6379 2/" /etc/redis/sentinel.conf
sudo sed -i "s/# sentinel auth-pass <master-name> <password>/sentinel auth-pass mymaster 123456/" /etc/redis/sentinel.conf
sudo systemctl restart redis-sentinel
从节点1:
【步骤1】安装 redis、redis-sentinel,同上
【步骤2】配置IP、密码、哨兵监控对象,并设为从主节点同步
代码语言:javascript复制# config redis-server
sudo sed -i "s/bind 127.0.0.1 -::1/bind 10.0.16.7 127.0.0.1 -::1/" /etc/redis/redis.conf
sudo sed -i "s/# requirepass foobared/requirepass 123456/" /etc/redis/redis.conf
sudo sed -i "s/# masterauth <master-password>/masterauth 123456/" /etc/redis/redis.conf
sudo systemctl restart redis-server
# config redis-sentinel
sudo sed -i "s/bind 127.0.0.1 ::1/bind 10.0.16.7 127.0.0.1 ::1/" /etc/redis/sentinel.conf
sudo sed -i "s/sentinel monitor mymaster 127.0.0.1 6379 2/sentinel monitor mymaster 10.0.16.12 6379 2/" /etc/redis/sentinel.conf
sudo sed -i "s/# sentinel auth-pass <master-name> <password>/sentinel auth-pass mymaster 123456/" /etc/redis/sentinel.conf
sudo systemctl restart redis-sentinel
# config replication
redis-cli -a $REDIS_PASSWORD replicaof 10.0.16.12 6379
查看Redis主备集群信息和哨兵集群信息
代码语言:javascript复制> redis-cli -a $REDIS_PASSWORD info replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:slave
master_host:10.0.16.12
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_read_repl_offset:428
slave_repl_offset:428
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:08a2e095a9b7fcc5f080453330510fe61e58adca
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:428
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:428
> redis-cli -p 26379 info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=10.0.16.12:6379,slaves=1,sentinels=2
从节点2:
【步骤1】安装 redis、redis-sentinel,同上
【步骤2】配置IP、密码、哨兵监控对象,并设为从主节点同步
代码语言:javascript复制# config redis-server
sudo sed -i "s/bind 127.0.0.1 -::1/bind 10.0.16.16 127.0.0.1 -::1/" /etc/redis/redis.conf
sudo sed -i "s/# requirepass foobared/requirepass 123456/" /etc/redis/redis.conf
sudo sed -i "s/# masterauth <master-password>/masterauth 123456/" /etc/redis/redis.conf
sudo systemctl restart redis-server
# config redis-sentinel
sudo sed -i "s/bind 127.0.0.1 ::1/bind 10.0.16.16 127.0.0.1 ::1/" /etc/redis/sentinel.conf
sudo sed -i "s/sentinel monitor mymaster 127.0.0.1 6379 2/sentinel monitor mymaster 10.0.16.12 6379 2/" /etc/redis/sentinel.conf
sudo sed -i "s/# sentinel auth-pass <master-name> <password>/sentinel auth-pass mymaster 123456/" /etc/redis/sentinel.conf
sudo systemctl restart redis-sentinel
# config replication
redis-cli -a $REDIS_PASSWORD replicaof 10.0.16.12 6379
查看Redis主备集群信息、哨兵集群信息、主节点信息
代码语言:javascript复制> redis-cli -a $REDIS_PASSWORD info replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:slave
master_host:10.0.16.12
master_port:6379
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_read_repl_offset:2908
slave_repl_offset:2908
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:08a2e095a9b7fcc5f080453330510fe61e58adca
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:2908
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2347
repl_backlog_histlen:562
> redis-cli -p 26379 info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=10.0.16.12:6379,slaves=2,sentinels=3
> redis-cli -p 26379 sentinel get-master-addr-by-name mymaster
1) "10.0.16.12"
2) "6379"
这样,就安装好了!
流水线安装
可以将以上步骤编排为流水线实现自动安装部署。流水线我已经在 Y20持续部署 系统上编排好了,地址在 部署Redis哨兵集群(Ubuntu),可以进去查看步骤、变量,并在自己的项目中创建流水线。
下面演示一下流水线的运行