redis+哨兵+VIP(一主一从两哨兵)
目的:实现redis高可用集群的固定IP访问。
环境:
主服务器:192.168.2.121
从服务器:192.168.2.122
一、安装redis(主从皆)
#下载安装包
wget https://download.redis.io/releases/redis-6.2.0.tar.gz
tzr -zxvf redis-6.2.0.tar.gz
mv redis-6.2.0 /usr/local/redis
cd /usr/local/redis
yum -y install gcc
make
#安装到指定目录
make PREFIX=/usr/local/redis install
二、配置主从
主节点(192.168.2.121)
cd /usr/local/redis
mkdir logs
mkdir data
vim redis.conf
bind 0.0.0.0 #设定连接IP
port 6379 #定义端口号
daemonize yes #允许后台启动
pidfile "/usr/local/redis/6379.pid" #pid文件存放位置
logfile "/usr/local/redis/logs/redis.log" #日志文件存放位置,需要创建logs文件夹
dir "/usr/local/redis/data" #数据存放位置,需要创建data文件夹
requirepass "123456" #redis密码
masterauth "123456" #主从交互密码
从节点(192.168.2.122)
cd /usr/local/redis
mkdir logs
mkdir data
vim redis.conf
bind 0.0.0.0 #设定连接IP
port 6379 #定义端口号
daemonize yes #允许后台启动
pidfile "/usr/local/redis/6379.pid" #pid文件存放位置
logfile "/usr/local/redis/logs/redis.log" #日志文件存放位置,需要创建logs文件夹
dir "/usr/local/redis/data" #数据存放位置,需要创建data文件夹
requirepass "123456" #redis密码
masterauth "123456" #主从交互密码
replicaof 192.168.2.121 6379 #代表这个节点为从节点,5.0版本之前为slaveof
三、配置哨兵(主从皆)
cd /usr/local/redis
mkdir -p sentinel/{logs,data}
vim sentinel.conf
port 26379 #默认端口
daemonize yes #允许后台启动
protected-mode no #关闭保护模式
logfile "/usr/local/redis/sentinel/logs/sentinel.log"
dir "/usr/local/redis/sentinel/data"
pidfile "/usr/local/redis/redis-sentinel.pid"
#配置选举机制,哨兵时刻监控主服务器的6379端口,后面的“1”是当有一票认为它宕机以后就开始主从切换
sentinel monitor mymaster 192.168.2.121 6379 1
#配置redis交互密码
sentinel auth-pass mymaster 123456
#表示master被当前sentinel实例认定为失效的间隔时间, 默认为 30000 改为10秒快一点
sentinel down-after-milliseconds mymaster 10000
#执行VIP漂移脚本
sentinel client-reconfig-script mymaster /usr/local/redis/notify_master.sh
四、VIP脚本
4.1编写脚本(主从皆)
cd /usr/local/redis
vim notify_master.sh
#!/bin/bash
MASTER_IP=${6}
MY_IP='192.168.2.121'
VIP='192.168.2.250'
NETMASK='24'
INTERFACE='eth0'
if [ ${MASTER_IP} = ${MY_IP} ]; then
/sbin/ip addr add ${VIP}/${NETMASK} dev ${INTERFACE}
/sbin/arping -q -c 3 -A ${VIP} -I ${INTERFACE}
exit 0
else
/sbin/ip addr del ${VIP}/${NETMASK} dev ${INTERFACE}
exit 0
fi
exit 1
4.2、当前redis的主使192.168.2.121,手动绑定VIP
ip addr add 192.168.2.250/24 dev eth0
arping -q -c 3 -A 192.168.56.250 -I eth0
4.3、添加权限(主从皆)
groupadd redis
useradd -r -g redis redis
chown -R redis:redis /usr/local/redis/notify_master.sh
chmod +x /usr/local/redis/notify_master.sh
五、启动
5.1、redis启动(先主后从)
cd /usr/local/redis/bin
./redis-server /usr/local/redis/redis.conf
5.2、哨兵启动(先主后从)
cd /usr/local/redis/bin
./redis-sentinel /usr/local/redis/sentinel.conf
六、验证
6.1、验证主从
#登录主节点
cd /usr/local/redis/bin
./redis-cli -h 192.168.2.121 -p 6379 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.2.121:6379> info replication
# Replication
role:master #节点角色
connected_slaves:1
slave0:ip=192.168.2.122,port=6379,state=online,offset=2528052,lag=1
master_failover_state:no-failover
master_replid:d47468e826000ff65e2322f818dfdee98dc928d6
master_replid2:48add2e0f45513b6a122d317cbcfaf7a44839ae5
master_repl_offset:2528193
second_repl_offset:1973916
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1748873
repl_backlog_histlen:779321
192.168.2.121:6379> set name test #存入数据,在从节点查看
OK
192.168.2.121:6379> get name
"test"
192.168.2.121:6379>
#登录从节点
./redis-cli -h 192.168.2.122 -p 6379 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.2.122:6379> info replication
# Replication
role:slave #节点角色
master_host:192.168.2.121
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:2540022
slave_priority:100
slave_read_only:1
connected_slaves:0
master_failover_state:no-failover
master_replid:d47468e826000ff65e2322f818dfdee98dc928d6
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:2540022
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1982117
repl_backlog_histlen:557906
192.168.2.122:6379> get name #数据同步成功
"test"
192.168.2.122:6379>
6.2哨兵验证
#停止主节点的redis服务
ps -ef | grep redis
root 513 1 0 13:58 ? 00:00:25 ./redis-server 0.0.0.0:6379
root 20859 6970 0 15:40 pts/0 00:00:00 grep --color=auto redis
kill -9 513
#启动主节点redis服务
cd /usr/local/redis/bin
./redis-server /usr/local/redis/redis.conf
#如“6.1主从验证”查看主从状态
6.3、VIP验证
ip a s | grep eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
inet 192.168.2.121/24 brd 192.168.2.255 scope global noprefixroute eth0
inet 192.168.2.250/24 scope global secondary eth0
#重启主节点redis服务后,在192.168.2.122上使用ip a s命令查看vip是否漂移。
|