一、环境信息

环境安装:

redis:

主:192.168.10.119 6381

从:192.168.10.119 6382

从:192.168.10.119 6383

Sentinel服务端口

192.168.10.119 26381

192.168.10.11926382

192.168.10.119 26383

二、配置主从

带密码的复制

#复制master host:6381
[root@test-zhaozheng-db conf]# redis-cli -h 192.168.20.119 -p 6382 -a intel.com
192.168.20.119:6382> SLAVEOF 192.168.20.119 6381
OK

[root@test-zhaozheng-db conf]# redis-cli -h 192.168.20.119 -p 6383 -a intel.com
192.168.20.119:6383> SLAVEOF 192.168.20.119 6381
OK

[root@test-zhaozheng-db ~]# redis-cli -h 192.168.20.119 -p 6381 -a intel.com info
...
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.20.119,port=6382,state=online,offset=322,lag=0
slave1:ip=192.168.20.119,port=6383,state=online,offset=322,lag=0

三、 配置Sentinel

配置文件下载地址:http://download.redis.io/redis-stable/sentinel.conf

[root@test-zhaozheng-db ~]# cd /data/redis/conf/
[root@test-zhaozheng-db conf]# vi sentinel-26381.conf
修改参数如下:
bind 192.168.20.119
port 26381
daemonize yes
pidfile /var/run/redis-sentinel-26381.pid
logfile "/var/log/redis/sentinel-26381.log"
#requirepass "your_password"          #sentinel密码  
dir /data/redis/sentinel/26381
sentinel monitor mymaster 192.168.20.119 6381 2
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 60000
sentinel auth-pass mymaster intel.com

[root@test-zhaozheng-db conf]# mkdir /data/redis/sentinel/{26381,26382,26383} -p
[root@test-zhaozheng-db conf]# chown -R redis. /data/redis/sentinel

#同理配置sentinel-26382.conf、sentinel-26383.conf

四、 配置sentinel服务脚本

[root@test-zhaozheng-db conf]# vi /usr/libexec/sentinel-shutdown
#!/bin/bash
#
# Wrapper to close properly redis and sentinel
test x"REDIS_DEBUG" != x && set -x

REDIS_CLI=/usr/bin/redis-cli

# Retrieve service name
SERVICE_NAME="1"
if [ -z "SERVICE_NAME" ]; then
   SERVICE_NAME=sentinel
fi

# Get the proper config file based on service name
CONFIG_FILE="/data/redis/conf/SERVICE_NAME.conf"

# Use awk to retrieve host, port from config file
HOST=`awk '/^[[:blank:]]*bind/ { print 2 }'CONFIG_FILE | tail -n1`
PORT=`awk '/^port[[:space:]]+[0-9]+/ { print 2 }'CONFIG_FILE | tail -n1`
PASS=`awk '/^[[:blank:]]*requirepass/ { print 2 }'CONFIG_FILE | tail -n1|cut -d '"' -f 2`
SOCK=`awk '/^[[:blank:]]*unixsocket\s/ { print 2 }'CONFIG_FILE | tail -n1`

# Just in case, use default host, port
HOST={HOST:-127.0.0.1}
PORT={PORT:-26380}


# Setup additional parameters
# e.g password-protected redis instances
[ -z "PASS" ] || ADDITIONAL_PARAMS="-aPASS"

# shutdown the service properly
if [ -e "SOCK" ] ; thenREDIS_CLI -s SOCKADDITIONAL_PARAMS shutdown
else
    REDIS_CLI -hHOST -p PORTADDITIONAL_PARAMS shutdown
fi


[root@test-zhaozheng-db conf]# chmod a+x /usr/libexec/sentinel-shutdown
[root@test-zhaozheng-db conf]# vi /usr/lib/systemd/system/sentinel-26381.service
[Unit]
Description=Redis persistent key-value database
After=network.target

[Service]
ExecStart=/usr/bin/redis-sentinel /data/redis/conf/sentinel-26381.conf --supervised systemd
ExecStop=/usr/libexec/sentinel-shutdown sentinel-26381
Type=notify
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target



[root@test-zhaozheng-db conf]# systemctl daemon-reload
[root@test-zhaozheng-db conf]# systemctl enable sentinel-26381


#同理配置sentinel-26382、sentinel-26383

五、 启动sentinel

[root@test-zhaozheng-db conf]# chown -R redis. /data/redis
[root@test-zhaozheng-db conf]# systemctl start sentinel-26381.service
[root@test-zhaozheng-db conf]# systemctl start sentinel-26382.service
[root@test-zhaozheng-db conf]# systemctl start sentinel-26383.service 

#端口验证
[root@test-zhaozheng-db conf]# ss -tunlp|grep 2638
tcp    LISTEN     0      511    192.168.20.119:26381                 *:*                   users:(("redis-sentinel",pid=25008,fd=6))
tcp    LISTEN     0      511    192.168.20.119:26382                 *:*                   users:(("redis-sentinel",pid=25715,fd=6))
tcp    LISTEN     0      511    192.168.20.119:26383                 *:*                   users:(("redis-sentinel",pid=25728,fd=6))

六、 验证sentinel集群

6.1 查看redis主机状态
[root@test-zhaozheng-db conf]# redis-cli -h 192.168.20.119 -p 26381 sentinel master mymaster
 1) "name"
 2) "mymaster"
 3) "ip"
 4) "192.168.20.119"
 5) "port"
 6) "6381"
 7) "runid"
 8) "6ed059d92b45ea2b5ad9ec9ceaa5d0b7e7592c76"
6.2 停止redis master 30秒
[root@test-zhaozheng-db conf]# redis-cli -h 192.168.20.119 -p 6381 -a intel.com DEBUG sleep 30

#窗口验证可以发现主master端口已经漂移
[root@test-zhaozheng-db ~]# redis-cli -h 192.168.20.119 -p 26381 sentinel master mymaster
 1) "name"
 2) "mymaster"
 3) "ip"
 4) "192.168.20.119"
 5) "port"
 6) "6383"


#进入redis实例查看主从信息
[root@test-zhaozheng-db ~]# redis-cli -h 192.168.20.119 -p 6383 -a intel.com info
...
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.20.119,port=6382,state=online,offset=124388,lag=1
slave1:ip=192.168.20.119,port=6381,state=online,offset=124531,lag=0
最后修改日期: 2020年3月31日

作者

留言

撰写回覆或留言

发布留言必须填写的电子邮件地址不会公开。