redis下载:
https://download.redis.io/releases/redis-4.0.9.tar.gz
编译安装
wget http://download.redis.io/releases/redis-4.0.9.tar.gz
tar xzf redis-4.0.9.tar.gz -C /usr/redis/redis-cluster
cd /usr/redis/redis-cluster/redis-4.0.9/
make && make install PREFIX=/usr/redis/redis-cluster/redis-4.0.9/
vi /etc/profile
追加如下
export REDIS_HOME=/usr/redis/redis-cluster/redis-4.0.10
export PATH=$REDIS_HOME/bin:$PATH
source /etc/profile
创建节点配置文件
针对端口创建一个实例配置文件,如:redis-6381.conf
daemonize yes
tcp-backlog 511
timeout 0
bind 192.168.2.173
tcp-keepalive 0
loglevel notice
databases 16
dir /var/redis-date/
slave-serve-stale-data yes
slave-read-only yes
repl-disable-tcp-nodelay yes
slave-priority 100
appendonly yes
appendfsync everysec
no-appendfsync-on-rewrite yes
auto-aof-rewrite-min-size 64mb
lua-time-limit 5000
logfile "/var/log/redis-6381.log"
cluster-enabled yes
#添加密码
requirepass BL.epec2021
masterauth BL.epec2021
cluster-node-timeout 15000
cluster-migration-barrier 1
slowlog-log-slower-than 10000
slowlog-max-len 128
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-entries 512
list-max-ziplist-value 64
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
port 6381
maxmemory 1500m
# volatile-lru -> remove the key with an expire set using an LRU algorithm
# allkeys-lru -> remove any key accordingly to the LRU algorithm
# volatile-random -> remove a random key with an expire set
# allkeys-random -> remove a random key, any key
# volatile-ttl -> remove the key with the nearest expire time (minor TTL)
# noeviction -> don't expire at all, just return an error on write operations
maxmemory-policy allkeys-lru
appendfilename "appendonly-6381.aof"
dbfilename dump-6381.rdb
cluster-config-file nodes-6381.conf
auto-aof-rewrite-percentage 80-100
集群部署ruby环境问题
Centos默认支持ruby到2.0.0,可gem 安装redis需要最低是2.2.2,解决办法是 先安装rvm,再把ruby版本提升至2.4.0,解决方法如下
yum install gcc-c++ ruby ruby-devel rubygems rpm-build
gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
curl -sSL https://get.rvm.io | bash -s stable
source /etc/profile.d/rvm.sh
rvm list known
rvm install 2.4.6
gem install redis
ruby -v
添加其他实例配置文件
其他实例文件添加
cd /usr/redis/redis-cluster
cp redis-6381.conf redis-6382.conf
cp redis-6381.conf redis-6383.conf
cp redis-6381.conf redis-6384.conf
cp redis-6381.conf redis-6385.conf
cp redis-6381.conf redis-6386.conf
替换字符
sed -i 's/6381/6382/' redis-6382.conf
sed -i 's/6381/6383/' redis-6383.conf
sed -i 's/6381/6384/' redis-6384.conf
sed -i 's/6381/6385/' redis-6385.conf
sed -i 's/6381/6386/' redis-6386.conf
启动集群
cd /usr/redis/redis-cluster
./redis-4.0.9/src/redis-server redis-6381.conf
./redis-4.0.9/src/redis-server redis-6382.conf
./redis-4.0.9/src/redis-server redis-6383.conf
./redis-4.0.9/src/redis-server redis-6384.conf
./redis-4.0.9/src/redis-server redis-6385.conf
./redis-4.0.9/src/redis-server redis-6386.conf
[root@epec-01 redis-cluster]# ./redis-4.0.9/src/redis-trib.rb create --replicas 1 192.168.2.173:6381 192.168.2.173:6382 192.168.2.173:6383 192.168.2.173:6384 192.168.2.173:6385 192.168.2.173:6386
错误解决1:
>>> Creating cluster
[ERR] Sorry, can't connect to node 192.168.2.173:6381
解决方法:在client.rb中添加密码
[root@epec-01 redis-cluster]# find / -name client.rb
/usr/share/ruby/xmlrpc/client.rb
/usr/local/rvm/rubies/ruby-2.4.6/lib/ruby/gems/2.4.0/gems/xmlrpc-0.2.1/lib/xmlrpc/client.rb
/usr/local/rvm/src/ruby-2.4.6/gems/xmlrpc-0.2.1/lib/xmlrpc/client.rb
/usr/local/rvm/gems/ruby-2.4.6/gems/redis-4.2.5/lib/redis/client.rb
[root@epec-01 redis-cluster]# vim /usr/local/rvm/gems/ruby-2.4.6/gems/redis-4.2.5/lib/redis/client.rb
停止redis-server服务
killall redis-server
rm -fr /var/redis-date/*
配置自启动
vim /etc/init.d/redisc
#!/bin/sh
# chkconfig: 2345 80 90
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.
REDISPORT1=6381
REDISPORT2=6382
REDISPORT3=6383
REDISPORT4=6384
REDISPORT5=6385
REDISPORT6=6386
EXEC=/usr/redis/redis-cluster/redis-4.0.9/bin/redis-server
CLIEXEC=/usr/redis/redis-cluster/redis-4.0.9/bin/redis-cli
PIDFILE=/var/run/redis_${REDISPORT1}.pid
CONF1="/usr/redis/redis-cluster/redis-${REDISPORT1}.conf"
CONF2="/usr/redis/redis-cluster/redis-${REDISPORT2}.conf"
CONF3="/usr/redis/redis-cluster/redis-${REDISPORT3}.conf"
CONF4="/usr/redis/redis-cluster/redis-${REDISPORT4}.conf"
CONF5="/usr/redis/redis-cluster/redis-${REDISPORT5}.conf"
CONF6="/usr/redis/redis-cluster/redis-${REDISPORT6}.conf"
case "$1" in
start)
if [ -f $PIDFILE ]
then
echo "$PIDFILE exists, process is already running or crashed"
else
echo "Starting Redis cluster server..."
$EXEC $CONF1 &
$EXEC $CONF2 &
$EXEC $CONF3 &
$EXEC $CONF4 &
$EXEC $CONF5 &
$EXEC $CONF6 &
echo "启动成功..."
fi
;;
stop)
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE does not exist, process is not running"
else
PID=$(cat $PIDFILE)
echo "Stopping ..."
$CLIEXEC -p $REDISPORT1 shutdown
$CLIEXEC -p $REDISPORT2 shutdown
$CLIEXEC -p $REDISPORT3 shutdown
$CLIEXEC -p $REDISPORT4 shutdown
$CLIEXEC -p $REDISPORT5 shutdown
$CLIEXEC -p $REDISPORT6 shutdown
while [ -x /proc/${PID} ]
do
echo "Waiting for Redis cluster to shutdown ..."
sleep 1
done
echo "Redis cluster stopped"
fi
;;
*)
echo "Please use start or stop as first argument"
;;
esac
#设置权限
chmod +x /etc/init.d/redisc
注册为系统服务
chkconfig --add redisc
以后可以使用命令来控制Redis的启动和关闭
启动:service redisc start
关闭:service redisc stop
配置为开机自启动
chkconfig redisc on