单机版
在196.168.199.101搭建单机版redis
cd /usr/local/
wget http://download.redis.io/releases/redis-4.0.11.tar.gz
tar -zxvf redis-4.0.11.tar.gz
cd /usr/local/redis-4.0.11
make install PREFIX=/usr/local/redis
如果出现类似的报错
make[1]: [persist-settings] Error 2 (ignored)
说明没有安装gcc
yum -y install gcc-c++
make distclean
然后再次执行make install PREFIX=/usr/local/redis
错误解决办法:make MALLOC=libc
复制 Redis 相关命令到 /usr/local/bin 目录,这样就可以直接执行这些命令,不用写全路径
cd /usr/local/redis/bin/
cp redis-cli redis-server redis-sentinel /usr/local/bin
将 Redis 配置文件拷贝到 /usr/local
cp /data/redis/redis-4.0.11/redis.conf /usr/local/
设置后台启动
cd /usr/local
sed -i s#daemonize\ no#daemonize\ yes#g redis.conf
设置远程访问
sed -i s#bind\ 127.0.0.1#bind\ 0.0.0.0#g redis.conf
配置 Redis 日志记录
sed -i s#logfile\ \"\"#logfile\ \"/var/log/redis_6379.log\"#g redis.conf
设置 Redis 请求密码
echo "requirepass \"123456\"" >> redis.conf
启动命令
redis-server /usr/local/redis.conf
访问客户端
redis-cli -h 127.0.0.1 -p 6379 -a 123456 2>/dev/null
在 /etc/init.d 目录下添加 Redis 服务
#!/bin/sh
#chkconfig: 2345 90 10
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.
REDISPORT=6379
EXEC=/usr/local/redis/bin/redis-server
CLIEXEC=/usr/local/redis/bin/redis-cli
PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/usr/local/redis.conf"
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 -a 123456 shutdown 2>/dev/null
while [ -x /proc/${PID} ]
do
echo "Waiting for Redis to shutdown ..."
sleep 1
done
echo "Redis stopped"
fi
;;
*)
echo "Please use start or stop as first argument"
;;
esac
赋予脚本文件可执行权限:
chmod 755 /etc/init.d/redis
设为开机启动:
chkconfig --add redis #可能不需要执行
chkconfig redis on
主从复制
master:196.168.199.101 16379
slave:196.168.199.102 26379
196.168.199.101
修改端口号
sed -i s#port\ 6379#port\ 16379#g /usr/local/redis.conf
修改pid file
sed -i s#pidfile\ /var/run/redis_6379.pid#pidfile\ /var/run/redis_16379.pid#g /usr/local/redis.conf
修改logfile
sed -i s#logfile\ \"/var/log/redis_6379.log\"#logfile\ \"/var/log/redis_16379.log\"#g /usr/local/redis.conf
修改/etc/init.d/redis
sed -i s#REDISPORT=6379#REDISPORT=16379#g /etc/init.d/redis
196.168.199.102
修改端口号
sed -i s#port\ 6379#port\ 26379#g /usr/local/redis.conf
修改pid file
sed -i s#pidfile\ /var/run/redis_6379.pid#pidfile\ /var/run/redis_26379.pid#g /usr/local/redis.conf
修改logfile
sed -i s#logfile\ \"/var/log/redis_6379.log\"#logfile\ \"/var/log/redis_26379.log\"#g /usr/local/redis.conf
修改/etc/init.d/redis
sed -i s#REDISPORT=6379#REDISPORT=26379#g /etc/init.d/redis
修改从机slaveof
echo "slaveof 192.168.199.101 16379" >> /usr/local/redis.conf
修改从机masterauth
echo "masterauth 123456" >> /usr/local/redis.conf
使用info replication命令查看状态:
192.168.199.101
192.168.199.102
哨兵模式
节点规划
角色 IP地址 端口号
Redis Master 192.168.199.101 16379
Redis Slave1 192.168.199.102 26379
Redis Slave2 192.168.199.103 36379
Redis Sentinel1 192.168.199.104 16380
Redis Sentinel2 192.168.199.105 26380
Redis Sentinel3 192.168.199.106 36380
主节点:192.168.199.101 /usr/local/redis.conf
修改timeout
sed -i s#timeout\ 0#timeout\ 300#g /usr/local/redis.conf
完整配置:
daemonize yes
pidfile /var/run/redis-16379.pid
logfile /var/log/redis-16379.log
port 16379
bind 0.0.0.0
timeout 300
databases 16
masterauth 123456
requirepass 123456
从节点1:192.168.199.102 /usr/local/redis.conf
修改timeout
sed -i s#timeout\ 0#timeout\ 300#g /usr/local/redis.conf
修改logfile
sed -i s#logfile\ \"/var/log/redis_6379.log\"#logfile\ \"/var/log/redis_26379.log\"#g /usr/local/redis.conf
daemonize yes
pidfile /var/run/redis-26379.pid
logfile /var/log/redis-26379.log
port 26379
bind 0.0.0.0
timeout 300
databases 16
masterauth 123456
requirepass 123456
slaveof 127.0.0.1 16379
从节点2:192.168.199.103 /usr/local/redis.conf
修改timeout
sed -i s#timeout\ 0#timeout\ 300#g /usr/local/redis.conf
修改logfile
sed -i s#logfile\ \"/var/log/redis_6379.log\"#logfile\ \"/var/log/redis_36379.log\"#g /usr/local/redis.conf
daemonize yes
pidfile /var/run/redis-36379.pid
logfile /var/log/redis-36379.log
port 36379
bind 0.0.0.0
timeout 300
databases 16
masterauth 123456
requirepass 123456
slaveof 127.0.0.1 16379
按顺序启动192.168.199.101、192.168.199.102、192.168.199.103
service redis start
查看主节点日志
redis-cli -h 127.0.0.1 -p 16379 -a 123456 2>/dev/null
配置sentinel
在三个sentinel节点、拷贝sentinel.conf到/usr/local
cp /data/redis/redis-4.0.11/sentinel.conf /usr/local
192.168.199.104,sentinel.conf
行首去掉字符#
sed -i '/# protected-mode no/s/^# //' /usr/local/sentinel.conf
sed -i '/# protected-mode no/c\protected-mode no' /usr/local/sentinel.conf
行首添加字符#
sed -i '17/s/^/# &/' /usr/local/sentinel.conf
sed -i '/protected-mode no/s/^/#&/' /usr/local/sentinel.conf --
会把所以匹配的都换掉,有点问题
echo "bind 0.0.0.0" >> /usr/local/sentinel.conf
sed -i s#port\ 26379#port\ 16380#g /usr/local/sentinel.conf
echo "daemonize yes" >> /usr/local/sentinel.conf
echo "sentinel monitor master 192.168.199.101 16379 2" >> /usr/local/sentinel.conf
echo "sentinel down-after-milliseconds master 5000" >> /usr/local/sentinel.conf
echo "sentinel failover-timeout master 180000" >> /usr/local/sentinel.conf
echo "sentinel parallel-syncs master 1" >> /usr/local/sentinel.conf
echo "sentinel auth-pass master 123456" >> /usr/local/sentinel.conf
echo "logfile /var/log/sentinel-16380.log" >> /usr/local/sentinel.conf
protected-mode no
bind 0.0.0.0
port 16380
daemonize yes
sentinel monitor master 192.168.199.101 16379 2 #2客观下线
sentinel failover-timeout master 180000 #主观下线
sentinel parallel-syncs master 1 #主备切换,slave replication数量
sentinel auth-pass master 123456
logfile /var/log/sentinel-16380.log
192.168.199.105,sentinel.conf
protected-mode no
bind 0.0.0.0
port 26380
daemonize yes
sentinel monitor master 192.168.199.101 16379 2 #2客观下线
sentinel failover-timeout master 180000 #主观下线
sentinel parallel-syncs master 1 #主备切换,slave replication数量
sentinel auth-pass master 123456
logfile /var/log/sentinel-26380.log
192.168.199.105,sentinel.conf
protected-mode no
bind 0.0.0.0
port 36380
daemonize yes
sentinel monitor master 192.168.199.101 16379 2 #2客观下线
sentinel failover-timeout master 180000 #主观下线
sentinel parallel-syncs master 1 #主备切换,slave replication数量
sentinel auth-pass master 123456
logfile /var/log/sentinel-36380.log
按顺序启动16380、26380、36380三个节点的sentinel服务
redis-sentinel /usr/local/sentinel.conf
当有其他服务器启动 Redis Sentinel 的时候,会有这样的日志(Redis Sentinel 是会相互通信的)
停止掉192.168.199.101主节点的redis服务,使用 redis-cli 客户端命令进入 sentinel-16380 节点,查看 Redis 节点 的状态信息
redis-cli -p 16380
SENTINEL master master
可以发现 redis-36379 晋升为 新的主节点
redis cluster
节点规划
节点ip | 节点名称 | 端口号 | 是主是从 | 所属主节点 |
---|---|---|---|---|
192.168.100.101 | redis-6379 | 6379 | 主节点 | – |
192.168.100.101 | redis-6389 | 6389 | 从节点 | redis-6379 |
192.168.100.101 | redis-6380 | 6380 | 主节点 | – |
192.168.100.101 | redis-6390 | 6390 | 从节点 | redis-6380 |
192.168.100.101 | redis-6381 | 6381 | 主节点 | – |
192.168.100.101 | redis-6391 | 6391 | 从节点 | redis-6381 |
创建redis各实例目录
mkdir -p /usr/local/redis-cluster
/usr/local/redis-cluster
mkdir conf data log
mkdir -p data/redis-6379 data/redis-6389 data/redis-6380 data/redis-6390 data/redis-6381 data/redis-6391
redis-6379.conf、其他文件把端口替换成对应端口
<!--
无用
sed -i s#daemonize\ no#daemonize\ yes#g redis-6379.conf
sed -i s#dir\ ./#dir\ /usr/local/redis-cluster/data/redis-6379#g redis-6379.conf
sed -i s#pidfile\ /var/run/redis_6379.pid#pidfile\ /var/run/redis-cluster/redis-6379.pid#g redis-6379.conf
sed -i s#logfile\ \"\"#logfile\ \"/usr/local/redis-cluster/log/redis-6379.log\"#g redis-6379.conf
sed -i '/# cluster-enabled yes/s/^# //' redis-6379.conf
sed -i '/# cluster-config-file nodes-6379\.conf/s/^# //' redis-6379.conf
sed -i s#cluster-config-file\ nodes-6379\.conf#cluster-config-file\ /usr/local/redis-cluster/conf/redis-6379\.conf#g redis-6379.conf
sed -i '/# cluster-node-timeout 15000/s/^# //' redis-6379.conf
sed -i s#cluster-node-timeout\ 15000#cluster-node-timeout\ 10000#g redis-6379.conf
sed -i s#appendonly\ no#appendonly\ yes#g redis-6379.conf-->
daemonize yes
bind 127.0.0.1
dir /usr/local/redis-cluster/data/redis-6379
pidfile /var/run/redis-cluster/redis-6379.pid
logfile /usr/local/redis-cluster/log/redis-6379.log
port 6379
cluster-enabled yes
cluster-config-file /usr/local/redis-cluster/conf/node-6379.conf
cluster-node-timeout 10000
appendonly yes
拷贝redis-trib.rb
cp /data/redis/redis-4.0.11/src/redis-trib.rb /usr/local/redis-cluster
安装ruby环境
yum install ruby
安装ruby redis 环境
gem install redis
系统默认支持ruby版本为2.0.0,版本过低,需要升级版本
升级步骤
vim /etc/hosts
199.232.68.133 raw.githubusercontent.com
199.232.68.133 user-images.githubusercontent.com
199.232.68.133 avatars2.githubusercontent.com
199.232.68.133 avatars1.githubusercontent.com
curl -L get.rvm.io | bash -s stable
gpg2 --keyserver hkp://pool.sks-keyservers.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
[root@yfm01 redis-cluster]# ll /usr/local/rvm/archives/
total 1324
-rw-r--r-- 1 root root 1351382 Jan 19 12:01 rvm-1.29.12.tgz
-rw-r--r-- 1 root root 833 Jan 19 12:01 rvm-1.29.12.tgz.asc
可以看到/usr/local/rvm/archives/有一些文件了
再次执行curl -L get.rvm.io | bash -s stable
source /usr/local/rvm/scripts/rvm
rvm list known
rvm install 2.4.5
rvm use 2.4.5
rvm remove 2.0.0
查看ruby版本
[root@yfm01 redis-cluster]# ruby --version
ruby 2.4.5p335 (2018-10-18 revision 65137) [x86_64-linux]
gem install redis
./redis-trib.rb
启动redis-server
redis-server redis-6379.conf
redis-server redis-6389.conf
redis-server redis-6380.conf
redis-server redis-6390.conf
redis-server redis-6381.conf
redis-server redis-6391.conf
ps -ef | grep redis-server
redis-trib关联集群节点
./redis-trib.rb create --replicas 1 127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6389 127.0.0.1:6390 127.0.0.1:6391