redis集群搭建
节点规划
准备了3台机子,每台机子设置两个redis几点,规划如下
192.168.0.131:7001(master)
192.168.0.131:7002(slave)
192.168.0.132:7001(master)
192.168.0.132:7002(slave)
192.168.0.133:7001(master)
192.168.0.133:7002(slave)
安装步骤
安装ruby
1、下载与上传
下载ruby安装包https://cache.ruby-lang.org/pub/ruby/2.3/ruby-2.3.1.tar.gz
使用scp命令把下载好的文件上传到3个主机中(安装包我下载到/Users/demondevil/mydatas/softs/ruby/路径中)
scp /Users/demondevil/mydatas/softs/ruby/ruby-2.3.1.tar.gz root@192.168.0.131:/home/softs
scp /Users/demondevil/mydatas/softs/ruby/ruby-2.3.1.tar.gz root@192.168.0.132:/home/softs
scp /Users/demondevil/mydatas/softs/ruby/ruby-2.3.1.tar.gz root@192.168.0.133:/home/softs
2、安装
登陆主机
ssh root@192.168.0.131
解压
cd /home/softs/
tar -zxvf ruby-2.3.1.tar.gz
配置
cd ruby-2.3.1
./configure --prefix=/usr/local/ruby
编译与安装
make
make install
设置环境变量
vim /etc/profile
在最后面添加一行
export PATH=/usr/local/ruby/bin:$PATH
使配置起作用
source /etc/profile
查看是否配置成功
echo $PATH
安装redis-3.0.7.gem
方法一
下载安装redis-3.0.7.gem,安装目的是使得ruby可以与redis集群通信,这样子就可以使用 redis-trib.rb工具管理集群了
下载
cd /home/softs/
wget --no-check-certificate https://rubygems.global.ssl.fastly.net/gems/redis-3.0.7.gem
安装
gem install -l redis-3.0.7.gem
报错
cannot load such file – zlib
下载安装zlib
wget http://down1.chinaunix.net/distfiles/zlib-1.2.7.tar.gz
tar -zxvf zlib-1.2.7.tar.gz
cd zlib-1.2.7
./configure
make
make install
再次运行gem install -l redis-3.0.7.gem还是报相同的错误,解决方法
进入ruby源码文件夹
cd /home/softs/ruby-2.3.1
安装ruby自身提供的zlib包
cd ext/zlib
ruby ./extconf.rb
make
make install
回到redis-3.0.7.gem当前目录
再次运行gem install -l redis-3.0.7.gem成功
方法二
gem install redis
执行报错
Unable to require openssl, install OpenSSL and rebuild ruby (preferred) or use non-HTTPS sources
解决方法
yum -y install openssl-devel
yum -y install openssl
进入ruby 源码目录下的ext/openssl 目录
ruby extconf.rb
将ruby 源码目录下的include 目录软链接到 / 目录下:
ln -s /home/softs/ruby-2.3.1/include/ /
make
make install
再次执行gem install redis成功
如果有报源的错误则进行gem源更换,方法如下
gem使用的是https://rubygems.org/,境外源,国内无法访问
解决办法:
更换gem源为https://ruby.taobao.org/,淘宝源
查看当前使用源
gem source –l
结果显示
*** CURRENT SOURCES ***
https://rubygems.org/
gem source -a https://ruby.taobao.org/ #添加淘宝源
gem source -r https://rubygems.org/ #删除境外源
192.168.0.131和192.168.0.133使用的是方法一
192.168.0.132使用的是方法二
安装redis
安装
下载安装包
http://download.redis.io/releases/redis-3.0.7.tar.gz
上传至主机
scp /Users/demondevil/mydatas/softs/redis/redis-3.0.7.tar.gz root@192.168.0.131:/home/softs
scp /Users/demondevil/mydatas/softs/redis/redis-3.0.7.tar.gz root@192.168.0.132:/home/softs
scp /Users/demondevil/mydatas/softs/redis/redis-3.0.7.tar.gz root@192.168.0.133:/home/softs
编译与安装
cd /home/softs
tar -zxvf redis-3.0.7.tar.gz
cd redis-3.0.7
make
make install
配置
对192.168.0.131主机进行如下操作
cd /home/softs/redis-3.0.7/src
cp redis-cli /usr/local/bin/
cp redis-server /usr/local/bin/
cp redis-trib.rb /usr/local/bin/
cd /home/softs/
mkdir redis-cluster
cd redis-cluster
mkdir data
mkdir log
创建并编辑7001端口的redis命令脚本,用于启动停止等
vim redis_7001
redis_7001文件内容如下
#!/bin/sh
#Configurations injected by install_server below....
EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli
PIDFILE=/home/softs/redis-cluster/data/redis-7001.pid
CONF="/home/softs/redis-cluster/redis_7001.conf"
REDISPORT="7001"
###############
# SysV Init Information
# chkconfig: - 58 74
# description: redis_6379 is the redis daemon.
### BEGIN INIT INFO
# Provides: redis_7001
# 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_7001
# 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 -p $REDISPORT shutdown
while [ -x /proc/${PID} ]
do
echo "Waiting for Redis to shutdown ..."