redis主从配置及哨兵
1、主从配置
1、下载安装redis
下载安装上redis
2、修改配置文件
所有机器都需要配置
vim redis.conf
daemonize yes #后台运行
bind 0.0.0.0 #取消访问限制
protected-mode no #允许远程访问
requirepass 123456 #密码(可以不要密码)
masterauth 123456 #集群主机密码(每台都要配,主机会切换,没有密码不要这个)
slave额外配置
slaveof 192.168.2.152 6379 #主机地址(master)(可以不配然后直接在redis客户端中输入`slaveof 192.168.2.152 6379`命令指定我是谁的奴隶)
#slaveof no one #使从服务器关闭复制功能,并从从服务器转变为主服务器,原来同步所得的数据不会丢弃
3、启动redis
src目录下
./src/redis-server ./redis.conf
2、哨兵配置
1、修改配置文件
所有机器上都需要配置
vim sentinel.conf
sentinel monitor mymaster 192.168.13.139 6379 2
#当集群中有2个sentinel认为master死了时,才能真正认为该master已经不可用了。mymaster是自定义的集群名字 (slave上面写的是master的ip,master写自己ip)
sentinel auth-pass mymaster 123456
#有密码的话配置密码 主机与从机的密码需保持一致
protected-mode no
#关闭保护模式
2、启动哨兵
src目录下
./src/redis-sentinel ./sentinel.conf >/dev/null 2>&1 &
3、大佬的搭建
https://www.jianshu.com/p/5d5b68b9a35c
1、环境准备
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
2、编译安装
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
3、修改node配置
#修改数据目录
sed -i '/dir ./cdir /var/lib/redis/data/6379' /etc/redis/redis.conf
#更改日志目录
sed -i '/logfile /clogfile "/var/lib/redis/logs/6379.log"' /etc/redis/redis.conf
#访问master需要的密码
sed -i '/masterauth /cmasterauth Redis_dev_2018' /etc/redis/redis.conf
#自身密码
sed -i '/requirepass /c
equirepass 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
4、添加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
5、启动node
systemctl daemon-reload ;
systemctl enable redis ;
systemctl start redis ;
systemctl status redis
6、添加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
7、添加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
8、启动sentinel
systemctl daemon-reload ;
systemctl enable redis-sentinel ;
systemctl start redis-sentinel ;
systemctl status redis-sentinel
9、维护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
10、常用命令
#查看slave信息
redis-cli -p 26379
sentinel slaves dev