0. 环境准备
操作系统版本:CentOS 6.9
Redis版本:redis-3.2.11
如果路径不存在,请创建,或使用自定义路径
安装包路径:/usr/local/src
Redis程序路径:/usr/local/redis
Redis配置文件路径:/usr/local/redis/conf
Redis日志文件路径:/var/log/redis
1. 安装Redis
编译安装
# 解压源码包
cd /usr/local/src
tar xzvf redis-3.2.11.tar.gz
cd redis-3.2.11
# 编译并安装
make PREFIX=/usr/local/redis install
make test # 可以不执行这步
如果编译时有如下报错时,添加参数"MALLOC=libc"
- 报错信息
zmalloc.h:50:31: error: jemalloc/jemalloc.h: No such file or directory
zmalloc.h:55:2: error: #error "Newer version of jemalloc required"
- 编译语句
make PREFIX=/usr/local/redis MALLOC=libc install
添加redis文件夹至系统环境变量
# 添加redis可执行文件所在文件夹至系统路径
vi /etc/profile
# 最后添加下面一行
export PATH=/usr/local/redis/bin:$PATH
# 即时生效环境变量
source /etc/profile
添加Redis为系统服务
# 使用Redis自带的安装脚本
cd /usr/local/src/redis-3.2.11/utils
./install_server.sh
# 下面是运行过程及结果
Welcome to the redis service installer
This script will help you easily set up a running redis server
Please select the redis port for this instance: [6379] 6378
Please select the redis config file name [/etc/redis/6378.conf] /usr/local/redis/conf/6378/6378.conf
Please select the redis log file name [/var/log/redis_6378.log] /var/log/redis/6480.log
Please select the data directory for this instance [/var/lib/redis/6378] /usr/local/redis/conf/6378
Please select the redis executable path [/usr/local/redis/bin/redis-server]
Selected config:
Port : 6378
Config file : /usr/local/redis/conf/6378/6378.conf
Log file : /var/log/redis/6480.log
Data dir : /usr/local/redis/conf/6378
Executable : /usr/local/redis/bin/redis-server
Cli Executable : /usr/local/redis/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /etc/6378.conf => /etc/init.d/redis_6378
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!
2. 创建Redis集群
集群命令
# 复制集群创建命令至Redis
cp /usr/local/src/redis/src/redis-trib.rb /usr/local/redis/bin
集群配置文件
- 若果之前没有安装Redis服务,可以手工创建配置文件。创建了跳过此步
cp /usr/local/src/redis/redis.conf /usr/local/redis/conf/6378.conf
cp /usr/local/src/redis/redis.conf /usr/local/redis/conf/6379.conf
cp /usr/local/src/redis/redis.conf /usr/local/redis/conf/6380.conf
cp /usr/local/src/redis/redis.conf /usr/local/redis/conf/6478.conf
cp /usr/local/src/redis/redis.conf /usr/local/redis/conf/6479.conf
cp /usr/local/src/redis/redis.conf /usr/local/redis/conf/6480.conf
- 修改集群配置文件
此处主要列举需要更改的配置项,其余可使用默认选项
daemonize yes
pidfile /var/run/redis-6378.pid
port 6378
protected-mode no
# 可以指定IP,如果不清楚IP来源,可以直接注释掉
# bind 127.0.0.1
logfile /var/logs/redis/redis-6378.log
dbfilename /usr/local/redis/conf/dump-6378.rdb
dir /usr/local/redis/conf
appendonly yes
appendfilename /usr/local/redis/conf/appendonly-6378.aof
cluster-enabled yes
cluster-config-file /usr/local/redis/conf/nodes-6380.conf
创建集群
- ruby环境准备
因为创建Redis集群时,需要用到Redis,确认环境中是否已安装Redis,没有则需要安装
# 安装相关包,也可使用源码安装ruby,本文使用yum方式
yum install ruby ruby-devel rubygems
gem install redis
# 本地安装包使用gem install -l redis-4.0.1.gem
- 集群创建
# 集群不能指定从redis服务,即slaveof选项无效
# 创建集群前,先不启用密码策略,即注释掉requirepass,可以将protected-mode改为no
# 集群需开启持续化,appendonly改为yes
# 选项 --replicas 1 表示集群中的每个主节点有一个从节点
# Redis集群至少3个主节点,每台主节点至少有1个从节点,即至少需要6个节点
redis-trib.rb create --replicas 1 192.168.5.100:6378 192.168.5.100:6379 192.168.5.100:6380 192.168.5.100:6478 192.168.5.100:6479 192.168.5.100:6480
- 设置密码
有两种方式,更改配置文件和命令行
# 1. 更改配置文件(需要重启服务)
# 配置文件中的更改为下列选项
# 每个节点都需更改
protected-mode yes
masterauth "your-password"
requirepass "your-password"
# 2. 命令行(无需重启服务),推荐该方式
# 每个节点都要执行一遍
redis-cli -c -p 6378
> config set protected-mode yes
> config set masterauth your-password
> config set requirepass your-password
> auth your-password
> config rewrite
> exit
密码设置完成后,添加密码验证至Redis服务
vim /etc/init.d/redis_6378
# 在第4行添加如下行,从配置文件获取密码
# "awk -F"后的顺序是单引号双引号单引号
AUTH=$(grep ^requirepass $CONF | awk -F '"' '{print $2}')
# 添加"-a"密码认证选项
$CLIEXEC -a $AUTH -p $REDISPORT shutdown # 如果设置了密码认证
完整的redis_6378
代码如下:
#!/bin/sh
#Configurations injected by install_server below....
AUTH=$(grep ^requirepass $CONF | awk -F '"' '{print $2}')
EXEC=/usr/local/redis/bin/redis-server
CLIEXEC=/usr/local/redis/bin/redis-cli
PIDFILE=/var/run/redis_6378.pid
CONF="/usr/local/redis/conf/6378/6378.conf"
REDISPORT="6279"
###############
# SysV Init Information
# chkconfig: - 58 74
# description: redis_${REDIS_PORT} is the redis daemon.
### BEGIN INIT INFO
# Provides: redis_${REDIS_PORT}
# Required-Start: \$network \$local_fs \$remote_fs
# Required-Stop: \$network \$local_fs \$remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Should-Start: \$syslog \$named
# Should-Stop: \$syslog \$named
# Short-Description: start and stop redis_${REDIS_PORT}
# Description: Redis daemon
### END INIT INFO
case "$1" in
start)
if [ -f $PIDFILE ]
then
echo "$PIDFILE exists, process is already running or crashed"
else
echo "Starting Redis server..."
$EXEC $CONF
fi
;;
stop)
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE does not exist, process is not running"
else
PID=$(cat $PIDFILE)
echo "Stopping ..."
$CLIEXEC -a $AUTH -p $REDISPORT shutdown
while [ -x /proc/${PID} ]
do
echo "Waiting for Redis to shutdown ..."
sleep 1
done
echo "Redis stopped"
fi
;;
status)
PID=$(cat $PIDFILE)
if [ ! -x /proc/${PID} ]
then
echo 'Redis is not running'
else
echo "Redis is running ($PID)"
fi
;;
restart)
$0 stop
$0 start
;;
*)
echo "Please use start, stop, restart or status as first argument"
;;
esac
3. 验证Redis集群
# 如果不在本机上,可以使用-h选项加上远程IP地址
redis-cli -c -p 6378
127.0.0.1:6378> auth your-password
127.0.0.1:6378> cluster nodes
127.0.0.1:6378> cluster info