您的位置:首页 > 数据库 > Redis

搭建基于 centos 的 Redis 哨兵模式 (4.0.6)

2017-12-24 10:48 543 查看
linux版本是Centos 7.4.x。Redis官方下载tar.gz官方网站:https://redis.io/download.

环境架构:

1)master redis server: bind 10.70.27.10

2) slave redis server: bind 10.70.27.8

step: 1. 建议把主和从的redis服务器的防火墙都关闭(主和从redis服务器上)。

登录到主和从的redis服务器,执行下面的操作.

# systemctl stop firewalld.service 

# systemctl disable firewalld.service 

step 2: 安装gcc编译器(主redis服务器上)

登录到主redis服务器,执行下面的操作:

#yum install gcc-c++

# gcc --version

gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-16)

Copyright (C) 2015 Free Software Foundation, Inc.

This is free software; see the source for copying conditions.  There is NO

warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

step
3. 下载redis源码(主redis服务器上)

wget http://download.redis.io/releases/redis-4.0.6.tar.gz[/code]   
然后解压: 

tar xzf redis-4.0.6.tar.gz
 ; 
cd
redis-4.0.6
 

Step 4: 编译redis(主redis服务器上)

安装目录: /opt/app/redis4
用户: root

$ make PREFIX=/opt/app/redis4 install  

当你看到下面的文字,恭喜您。编译完成。 

Hint: It’s a good idea to run ‘make test’ ;)

安装完成后, 可以看到/opt/app/redis4/ 目录下有一个 bin 目录, bin 目录里就是 redis 的命令脚本:

step 5: 修改redis 配置文件

配置文件 redis.conf和sentinel.conf就在
redis-4.0.6
 目录下。

主服务器的配置文件:

1) 主服务器:vim redis.conf 

找到:bind 127.0.0.1(修改成:bind 10.70.27.10 记得修改成自己ip,远程才能访问。) 

找到:protected-mode yes(设置成:protected-mode no;保护模式关闭,如果你不关闭保护模式,启动哨兵的时候,无法正常运行。还有个解决办法就是你设置密码,但是一般都不设置redis的密码。麻烦,我每次连接还得输入密码。在部署中,可以设置密码。) 

找到:daemonize no(设置成:daemonize yes,标示后台启动。) 

然后把 redis.conf  拷贝到 /opt/app/redis4/conf (没有则建一个目录)

step 6 redis 开始运行

1:在主服务器上的操作:

# /opt/app/redis4/bin/redis-server /opt/app/redis4/conf/redis.conf
 

2: 在从服务器上的操作:

 1. 把主服务器上的/opt/app/redis4整个目录
拷贝到 从服务器上的  /opt/app/redis4(没有则事先建立该目录)上。

2. 修改redis.conf
 

1) vim
redis.conf ,改成如下信息。

bind 10.70.27.8 127.0.0.1

slaveof 10.70.27.8 6379

2) 然后启动从redis

# /opt/app/redis4/bin/redis-server /opt/app/redis4/conf/redis.conf
 

3)检验运行状况

在主或者从redis服务器上,

# /opt/app/redis4/bin/redis-cli

>info

然后输入info,看看结果

如果上面没有啥问题,则继续。

Step 7. redis 作为systemctl的服务来启动

把上面的主,从redis的redis.conf文件按如下修改。

#bind127.0.0.1去掉,不能bind到127.0.0.1

daemonize yes

protected-mode no

logfile/opt/app/redis4/log/redis.log

pidfile /opt/app/redis4/redis_6379.pid

 vi /etc/systemd/system/redis.service

[Unit]
Description=Redis persistent key-value database
After=network.target

[Service]
ExecStart=/opt/app/redis4/bin/redis-server /opt/app/redis4/conf/redis.conf --daemonize no
ExecStop=/opt/app/redis4/bin/redis-shutdown
User=root
Group=root

[Install]
WantedBy=multi-user.target


这里自己要写一个shell程序redis-shutdown,内容如下:

cat  /opt/app/redis4/bin/redis-shutdown

#!/bin/bash
# Wrapper to close properly redis and sentinel
test x"$REDIS_DEBUG" != x && set -x
REDIS_CLI=/opt/app/redis4/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="/opt/app/redis4/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 '/^[[: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


然后启动redis.service

systemctl daemon-reload

systemctl enable redis.service

systemctl start redis

systemctl status redis

cd /opt/app/redis4/log,   打开 redis.log来看看redis的状况。

//check redis status

/opt/app/redis4/bin/redis-cli -h 10.70.27.8

10.70.27.8:6379> auth <your password>

OK

10.70.27.8:6379> info

Step 8:  哨兵模式的配置

在完成上面的配置后,redis提供了哨兵HA模式,下面继续哨兵HA模式安装。

选择一个主机来安装哨兵,本文中选择主redis server。

1.  把上面的 redis4 目录copy为  redis4_sentinel 

2.  把
redis4_sentinel/conf 目录下的sentinel.conf 改成  redis4_sentinel.conf,然后修改里面的内容

bind 10.70.27.8

daemonize yes

protected-mode no

port 26379

dir "/opt/app/redis4_sentinel"

logfile "/opt/app/redis4_sentinel/log/sentinel.log"

sentinel monitor mymaster 10.70.27.8 6379 1

sentinel down-after-milliseconds mymaster 20000

sentinel failover-timeout mymaster 120000

3 . 采用服务化来启动sentinel

vi /etc/systemd/system/redis-sentinel.service

[Install]
WantedBy=multi-user.target

[Unit]
Description=Redis persistent key-value database
After=network.target

[Service]
ExecStart=/opt/app/redis4_sentinel/bin/redis-sentinel /opt/app/redis4_sentinel/conf/redis-sentinel.conf --daemonize no
ExecStop=/opt/app/redis4_sentinel/bin/redis-shutdown redis-sentinel
User=root
Group=root

[Install]
WantedBy=multi-user.target


新建一个启动shell程序,如下:

 cat /opt/app/redis4_sentinel/bin/redis-shutdown

#!/bin/bash
# Wrapper to close properly redis and sentinel
test x"$REDIS_DEBUG" != x && set -x
REDIS_CLI=/opt/app/redis4_sentinel/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="/opt/app/redis4_sentinel/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 '/^[[: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


然后

systemctl enable redis-sentinel.service

systemctl start redis-sentinel

systemctl status redis-sentinel

在这里看日志: /opt/app/redis4_sentinel/log/sentinel.log

//check sentinel status

/opt/app/redis4_sentinel/bin/redis-cli -h 10.70.27.8 -p 26379

10.70.27.8:26379> info

本文安装到此结束。

另外在centOS上,也可以用 yum install redis来安装,这个比较省事,但是源代码版本比较低。

注意:master通过
requirepass
设置自身的密码,不提供密码无法连接到这个master。

slave通过
masterauth
来设置访问master时的密码。

当使用了sentinel时,由于一个master可能会变成一个slave,一个slave也可能会变成master,所以需要在master 和slave 的配置文件中同时设置上述两个配置项,才能多次切换,否则就有可能只能切换一次。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: