redis集群

单机版

在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
image
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.101redis-63796379主节点
192.168.100.101redis-63896389从节点redis-6379
192.168.100.101redis-63806380主节点
192.168.100.101redis-63906390从节点redis-6380
192.168.100.101redis-63816381主节点
192.168.100.101redis-63916391从节点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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

四美

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值