写一个自动化安装redis哨兵模式的三节点集群
时间: 2023-04-12 13:01:34 浏览: 187
可以使用以下脚本来自动化安装redis哨兵模式的三节点集群:
```
#!/bin/bash
# 安装redis
sudo apt-get update
sudo apt-get install redis-server -y
# 配置redis
sudo sed -i 's/# bind 127.0.0.1/bind 0.0.0.0/g' /etc/redis/redis.conf
sudo sed -i 's/protected-mode yes/protected-mode no/g' /etc/redis/redis.conf
# 创建redis哨兵配置文件
sudo mkdir /etc/redis/sentinel
sudo touch /etc/redis/sentinel/sentinel.conf
sudo chmod 777 /etc/redis/sentinel/sentinel.conf
# 配置redis哨兵
echo "port 26379" >> /etc/redis/sentinel/sentinel.conf
echo "dir /tmp" >> /etc/redis/sentinel/sentinel.conf
echo "sentinel monitor mymaster 127.0.0.1 6379 2" >> /etc/redis/sentinel/sentinel.conf
echo "sentinel down-after-milliseconds mymaster 5000" >> /etc/redis/sentinel/sentinel.conf
echo "sentinel failover-timeout mymaster 60000" >> /etc/redis/sentinel/sentinel.conf
echo "sentinel parallel-syncs mymaster 1" >> /etc/redis/sentinel/sentinel.conf
echo "sentinel auth-pass mymaster password" >> /etc/redis/sentinel/sentinel.conf
# 启动redis哨兵
redis-server /etc/redis/sentinel/sentinel.conf --sentinel
# 添加redis从节点
redis-cli -p 6379 slaveof 127.0.0.1 6380
redis-cli -p 6380 slaveof 127.0.0.1 6381
# 添加redis主节点
redis-cli -p 6379 config set masterauth password
redis-cli -p 6379 config set requirepass password
redis-cli -p 6379 config rewrite
# 添加redis哨兵节点
redis-cli -p 26379 sentinel monitor mymaster 127.0.0.1 6379 2
redis-cli -p 26379 sentinel set mymaster auth-pass password
redis-cli -p 26379 sentinel set mymaster down-after-milliseconds 5000
redis-cli -p 26379 sentinel set mymaster failover-timeout 60000
redis-cli -p 26379 sentinel set mymaster parallel-syncs 1
```
这个脚本会自动安装redis,并配置redis哨兵模式的三节点集群。注意,这个脚本中的密码是示例密码,实际使用时应该替换为更安全的密码。
阅读全文