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