一、部署包下载
直接从官网下载安装包,编译二进制;也可以从机器拷贝现有的
wget https://download.redis.io/releases/redis-6.2.9.tar.gz
# 解压
tar xvf redis-6.2.9.tar.gz
# 进行编译
cd redis-6.2.9/
make && make install
# 确认相关编译的二进制;
ls -al /usr/local/bin | grep redis
二、配置准备
测试机器
| 172.28.112.75 |
|---|
| 172.28.112.66 |
| 172.28.112.87 |
redis.conf
线上服务密码待修改
# 原始默认的配置文件,配置项多,比较复杂
cd redis-6.2.9
mkdir /etc/redis/ && cp redis.conf /etc/redis/redis.conf.bak
cat << EOF > /etc/redis/redis.conf
port 6379
bind 0.0.0.0
protected-mode no
requirepass "oyltest"
logfile "/var/log/redis/redis.log"
dbfilename "dump.rdb"
dir "/media/disk1/redis"
maxclients 100000
maxmemory 15gb
timeout 86400
tcp-keepalive 50
masterauth "oyltest"
# 从节点需要下列配置项
replicaof 172.28.112.75 6379
replica-read-only yes
EOF
sentinel.conf
集群名字
# 原始默认的配置文件,配置项多,比较复杂
cd redis-6.2.9
cp sentinel.conf /etc/redis/sentinel.conf.bak
cat << EOF > /etc/redis/sentinel.conf
port 26379
protected-mode no
dir "/media/disk1/redis"
maxclients 100000
sentinel monitor oylcluster 172.28.112.75 6379 2
sentinel auth-pass oylcluster oyltest
sentinel down-after-milliseconds oylcluster 7000
sentinel failover-timeout oylcluster 30000
logfile "/var/log/redis/sentinel.log"
EOF
redis-shutdown
准备redis服务退出脚步
vim /usr/local/bin/redis-shutdown
#!/bin/bash
#
# Wrapper to close properly redis and sentinel
test x"$REDIS_DEBUG" != x && set -x
REDIS_CLI=/usr/local/bin/redis-cli
# Retrieve service name
SERVICE_NAME="$1"
if [ -z "$SERVICE_NAME" ]; then
SERVICE_NAME=redis
fi
# Get the proper config file based on service name
CONFIG_FILE="/etc/redis/$SERVICE_NAME.conf"
# Use awk to retrieve host, port from config file
HOST=`awk '/^[[:blank:]]*bind/ { print $2 }' $CONFIG_FILE | tail -n1`
PORT=`awk '/^[[:blank:]]*port/ { print $2 }' $CONFIG_FILE | tail -n1`
PASS=`awk '/^[[:blank:]]*requirepass/ { print $2 }' $CONFIG_FILE | tail -n1`
SOCK=`awk '/^[[:blank:]]*unixsocket\s/ { print $2 }' $CONFIG_FILE | tail -n1`
# Just in case, use default host, port
HOST=${HOST:-127.0.0.1}
if [ "$SERVICE_NAME" = redis ]; then
PORT=${PORT:-6379}
else
PORT=${PORT:-26739}
fi
# Setup additional parameters
# e.g password-protected redis instances
[ -z "$PASS" ] || ADDITIONAL_PARAMS="-a $PASS"
# shutdown the service properly
if [ -e "$SOCK" ] ; then
$REDIS_CLI -s $SOCK $ADDITIONAL_PARAMS shutdown
else
$REDIS_CLI -h $HOST -p $PORT $ADDITIONAL_PARAMS shutdown
fi
# 添加可执行权限
chmod +x /usr/local/bin/redis-shutdown
redis.service
# Redis 服务
cat << EOF > /etc/systemd/system/redis.service
[Unit]
Description=Redis persistent key-value database
After=network.target
After=network-online.target
Wants=network-online.target
[Service]
ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf --supervised systemd
ExecStop=/usr/local/bin/redis-shutdown
Type=notify
[Install]
WantedBy=multi-user.target
EOF
sentinel.service
# sentinel 服务
cat << EOF > /etc/systemd/system/redis-sentinel.service
[Unit]
Description=Redis persistent key-value database
After=network.target
After=network-online.target
Wants=network-online.target
[Service]
ExecStart=/usr/local/bin/redis-sentinel /etc/redis/sentinel.conf --supervised systemd
ExecStop=/usr/local/bin/redis-shutdown redis-sentinel
Type=notify
[Install]
WantedBy=multi-user.target
EOF
三、服务启动
redis部署&启动
systemctl daemon-reload
systemctl enable redis
mkdir -p /var/log/redis
mkdir -p /media/disk1/redis
systemctl stop redis
systemctl start redis
systemctl status redis
tail -f /var/log/redis/redis.log
sentinel部署&启动
# systemctl daemon-reload
systemctl enable redis-sentinel.service
systemctl stop redis-sentinel.service
systemctl start redis-sentinel.service
systemctl status redis-sentinel.service
tail -f /var/log/redis/sentinel.log
四、服务验证
集群状态、客户端连接
# /usr/local/bin/redis-cli -h 127.0.0.1
127.0.0.1:6379> AUTH oyltest
OK
127.0.0.1:6379> info
集群高可用,确认自动切换主从; 拉起后,故障节点自动切换为REPLICA角色
# 主节点
systemctl stop redis.service
systemctl start redis.service