Rediss 部署以及哨兵模式
Redis 安装配置
Yum 官方源里没有 redis,所以采用手动源码安装
1、 上传 redis 安装包到 Linux 任意目录下,解压
wget http://download.redis.io/releases/redis-5.0.7.tar.gz
tar xvf redis-5.0.7.tar.gz
2、 安装 gcc(redis 安装依赖 C 语言环境,需要先安装 gcc)
yum install -y gcc
3、 进入解压后的 redis 目录下,执行编译操作
make MALLOC=libc
4、 执行安装命令
`make install`
5、 安装成功
Redis 配置
1、 在 redis 目录下,新建 conf 文件夹,将 redis.conf 配置文件拷贝到 conf 文件夹下,并重
命名为 6379.conf
mkdir conf
cp redis.conf ./conf/6379.conf
2、 修改 redis.conf 配置文件
daemonize 改为 yes
dbfilename 改为 dump_6379.rdb
将 bind 127.0.0.1 注释掉
protected-mode 改为 no
Redis启动与关闭
1、 先启动 server,在 conf 目录下,执行
cd redis-5.0.7/src
redis-server ../conf/6379.conf
2、 使用客户端登录 redis,默认连接的是 6379 端口的 redis 实例
redis-cli
3、关闭 redis-server
redis-cli shutdown
开启外网连接权限(0.0.0.0表示接受任意外网连接)
# By default Redis listens for connections from all the network interfaces
# available on the server. It is possible to listen to just one or multiple
# interfaces using the "bind" configuration directive, followed by one or
# more IP addresses.
#
# Examples:
#
# bind 192.168.1.100 10.0.0.1
bind 0.0.0.0
指定端口号
# Accept connections on the specified port, default is 6379.
# If port 0 is specified Redis will not listen on a TCP socket.
port 6379
开启slave节点写权限
# Note: read only slaves are not designed to be exposed to untrusted clients
# on the internet. It's just a protection layer against misuse of the instance.
# Still a read only slave exports by default all the administrative commands
# such as CONFIG, DEBUG, and so forth. To a limited extent you can improve
# security of read only slaves using 'rename-command' to shadow all the
# administrative / dangerous commands.
slave-read-only no
最后在文件末尾添加如下配置,表示当服务后台启动时,自动将某台服务器作为master,在这里我的master是175,端口号是6380
slaveof 192.168.2.47 6379
第二步:配置sentinel.conf文件(如果没有此文件,在redis文件夹下touch一个即可)
cp sentinel.conf ./conf/sentinel.conf
首先,开启哨兵后台运行权限
#Control the switch running in the background
daemonize yes
重点来了!下面是配置哨兵监控模式(重中之重)
# Note: master name should not include special characters or spaces.
# The valid charset is A-z 0-9 and the three characters ".-_".
sentinel monitor mymaster 192.168.2.47 6379 1
哨兵开启故障转移阈值(当其余某台节点中的一台有1票时,开启选举,使其为master,否则一直处于选举状态)
然后,使用scp指令将redis整个文件夹拷贝到其他一台节点上,需要改变的地方有
redis.conf中的端口号
将主服务器中的redis.conf文件末尾的slaveof 192.168.2.47 6380去掉
所有的配置完成之后,使用如下命令再三台服务器上分别开启redis服务(注意顺序:先主、后从)
redis-server ../conf/6379.conf
使用如下命令,开启哨兵监控模式(开启顺序不要紧)
src/redis-sentinel ./conf/sentinel.conf
而后,分别在两台服务器上执行如下命令,开启redis客户端
src/redis-cli -h 当前服务器ip -p redis.conf文件中的端口号
在主服务器上的客户端上执行如下命令,查看master和slaves
info replication