Centos7 搭建Redis Sentinel集群

环境准备

echo 512 > /proc/sys/net/core/somaxconn ;\
echo vm.overcommit_memory=1 >> /etc/sysctl.conf ;\
sysctl -p ;\
echo 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' >> /etc/rc.local ;\
chmod +x /etc/rc.d/rc.local ;\
useradd -s  /sbin/nologin redis ;\
mkdir -p /var/lib/redis/run ;\
mkdir -p /var/lib/redis/logs ;\
mkdir -p /var/lib/redis/data/26379 ;\
mkdir -p /var/lib/redis/data/6379 ;\
touch /var/lib/redis/logs/6379.log ;\
touch /var/lib/redis/logs/6379.log ;\
firewall-cmd --add-port=26379/tcp --permanent ;\
firewall-cmd --add-port=6379/tcp --permanent ;\
firewall-cmd --reload ;\
firewall-cmd --list-all

编译安装

tar xf redis-4.0.11.tar.gz;\
cd redis-4.0.11;\
yum install gcc make -y;\
make && make install;\
mkdir /etc/redis;\
cp redis.conf /etc/redis/;\
chown -R redis. /etc/redis;\
chown -R redis. /var/lib/redis

修改node配置

#修改数据目录
sed -i '/dir ./c\dir /var/lib/redis/data/6379' /etc/redis/redis.conf

#更改日志目录
sed -i '/logfile /c\logfile "/var/lib/redis/logs/6379.log"' /etc/redis/redis.conf

#访问master需要的密码
sed -i '/masterauth /c\masterauth Redis_dev_2018' /etc/redis/redis.conf

#自身密码
sed -i '/requirepass /c\requirepass Redis_dev_2018' /etc/redis/redis.conf

#注释掉bind
sed -i '/bind 127.0.0.1/c\#bind 127.0.0.1' /etc/redis/redis.conf

添加node节点服务

tee /etc/systemd/system/redis.service << 'EOF'
[Unit]
Description=Redis Server
After=network.target
[Service]
Type=simple
PIDFile=/var/lib/redis/run/6379.pid
User=redis
Group=redis
LimitNOFILE=100000
ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf
ExecStop=/usr/local/bin/redis-cli -p 6379 shutdown
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF

启动node

systemctl daemon-reload ;\
systemctl enable redis ;\
systemctl start redis ;\
systemctl status redis


添加sentinel配置文件

tee /etc/redis/sentinel.conf << 'EOF'
port 26379
dir "/var/lib/redis/data/26379"
protected-mode no
daemonize no
logfile "/var/lib/redis/logs/26379.log"

sentinel monitor dev 192.168.1.151 6379 2
sentinel down-after-milliseconds dev 20000
sentinel failover-timeout dev 60000
sentinel auth-pass dev Redis_dev_2018
EOF

chown -R redis. /etc/redis

添加sentinel服务

tee /etc/systemd/system/redis-sentinel.service << 'EOF'
[Unit]
Description=Redis sentinel Server
After=network.target
[Service]
Type=simple
PIDFile=/var/lib/redis/run/26379.pid
User=redis
Group=redis
LimitNOFILE=100000
ExecStart=/usr/local/bin/redis-server /etc/redis/sentinel.conf --sentinel
ExecStop=/usr/local/bin/redis-cli -p 26379 shutdown
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF

启动sentinel

systemctl daemon-reload ;\
systemctl enable redis-sentinel ;\
systemctl start redis-sentinel ;\
systemctl status redis-sentinel

维护Slave关系

#添加slave-42
redis-cli -h 192.168.1.152

auth Redis_dev_2018

slaveof 192.168.1.151 6379


#添加slave-43
redis-cli -h 192.168.1.153

auth Redis_dev_2018

slaveof 192.168.1.151 6379

常用命令

#查看slave信息
redis-cli -p 26379

sentinel slaves dev
    原文作者:Hanson1330
    原文地址: https://www.jianshu.com/p/5d5b68b9a35c
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞