redis集群搭建流程步骤很详细

之前用的版本是4.0的这次搭建用5.05版本

不同之处,就是在5.0之后的版本可以直接用客户端工具直接创建集群,不需要安装其他工具,很方便.
安装这个的时候,遇到一个问题,就是当主库挂了的时候,从库不会晋升为主库…后面排查结果
问题是主机授权masterauth参数没有添加,导致从库一直链接失败,从而不能晋升主库.

一.安装前准备

关闭SELinux

[root@localhost ~]# setenforce 0
[root@localhost ~]# echo "/usr/sbin/setenforce 0"
[root@localhost ~]# sed -i 's/^SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux 
[root@localhost ~]# sed -i 's/^SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config

设置防火墙

[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld
[root@localhost ~]#vim /etc/sysconfig/iptables
-A INPUT -p tcp -m state --state NEW -m tcp --dport 6379 -j ACCEPT
[root@localhost ~]#service iptables restart

时钟校对

[root@localhost ~]# yum -y install ntpdate
[root@localhost ~]# echo "#Clock synchronization" >> /var/spool/cron/root
[root@localhost ~]# echo "01 00 * * * /sbin/ntpdate ntp2.aliyun.com;/sbin/hwclock -w" >> /var/spool/cron/root

调整文件打开数

[root@localhost ~]# cat <<EOF > /etc/security/limits.d/20-nproc.conf 
> root soft nofile 102400
> root hard nofile 102400
> EOF

安装gcc

[root@localhost ~]#yum -y install gcc gcc-c++

二.安装red is

下载redis安装包

[root@localhost ~]#wget http://download.redis.io/releases/redis-5.0.5.tar.gz

解压

[root@localhost ~]#tar zxvf redis-5.0.5.tar.gz 
[root@localhost ~]#cd redis-5.0.5/

编译安装

[root@localhost redis-5.0.5]#make MALLOC=libc
[root@localhost redis-5.0.5]#make && make install

创建文件目录

[root@localhost redis-5.0.5]#mkdir -p /opt/redis/{db,etc,bin,logs}

将启动文件拷贝至文件目录

[root@localhost redis-5.0.5]#cd  src/
[root@localhost src]#cp mkreleasehdr.sh redis-benchmark redis-check-aof redis-server redis-cli  /opt/redis/bin

修改配置文件/opt/redis/etc/redis.conf

bind 0.0.0.0  #绑定的IP地址,0.0.0.0对所有开放
protected-mode no  #关闭保护模式,让其他主机连接
port 6379 #服务端口
tcp-backlog 511 #tcp连接等待队列长度
timeout 0
tcp-keepalive 300
daemonize yes #开启后台模式
supervised no
pidfile /var/run/redis_6379.pid #进程文件
loglevel notice
logfile /opt/redis/logs/redis.log #日志文件
databases 1 #数据库的个数,默认16,由于集群不支持切换库,所有只有一个库
always-show-logo yes
#指定在多长时间内,有多少次更新操作,就将数据同步到数据文件,可以多个条件配合
    save <seconds> <changes>
    Redis默认配置文件中提供了三个条件:
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes #后台备份出错时,是否停止写入keys到哈希槽
rdbcompression yes #数据文件是否压缩
rdbchecksum yes#数据文件执行校验
dbfilename dump-6379.rdb #指定本地数据库文件名,默认值为dump.rdb
dir /opt/redis/db/ #指定本地数据库存放目录
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
replica-priority 100
masterauth 123456 #主机授权密码
requirepass 123456 #数据库密码
maxclients 10000 #最大客户端连接数
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
appendonly no
appendfilename "appendonly.aof" #启动Redis上启用了AOF,则会加载AOF,这是具有更好的耐久性保证的文件,相当于MySQL的二进制文件
appendfsync everysec #磁盘上刷新数据,每一秒钟一次
no-appendfsync-on-rewrite no #如果您有延迟问题,请将其转换为"yes"。否则,从持久性的角度来看,设置为"no"是最安全的选择。
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb #自动重写附加文件
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
cluster-enabled yes #开启集群
cluster-config-file node1-6379.conf #集群节点配置文件,由Redis节点自动创建和更新的,非人工创建
cluster-node-timeout 15000 #集群节点超时,连接不上的时间
cluster-slave-validity-factor 10  #从节点与主节点失联多久,从节点尝试升级成主节点。如果设置成正数,则cluster-node-timeout乘以cluster-slave-validity-factor得到的时间,是从节点与主节点失联后,此从节点数据有效的最长时间,超过这个时间,从节点不会启动故障迁移
cluster-migration-barrier 1 #主节点需要的最小从节点数,只有达到这个数,主节点失败时,它从节点才会进行迁移
cluster-require-full-coverage yes  #在部分key所在的节点不可用时,如果此参数设置为"yes"(默认值), 则整个集群停止接受操作;如果此参数设置为”no”,则集群依然为可达节点上的key提供读操作。
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10 #Redis调用一个内部函数来执行许多后台任务
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes

配置启动脚本

#!/bin/sh
# chkconfig: 35 10 90           
#重要!否则chkconfig不能识别开机启动服务项;
#”35”为运行级别,”10”为启动优先级,”90”为关闭优先级
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.

REDISPORT=6379
EXEC=/opt/redis/bin/redis-server
# CLIEXEC=/usr/local/bin/redis-cli
CLIEXEC=/opt/redis/bin/redis-cli

# PIDFILE=/var/run/redis_${REDISPORT}.pid
PIDFILE=/var/run/redis_${REDISPORT}.pid
# CONF="/etc/redis/${REDISPORT}.conf"
CONF="/opt/redis/etc/redis.conf"

USER=root            #添加执行用户变量

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 ..."
                    sleep 1
                done
                echo "Redis stopped"
        fi
        ;;
    restart|force-reload)
                ${0} stop
                ${0} start
                ;;
        *)
                echo "Usage: /etc/init.d/redis-server {start|stop|restart|force-reload}" >&2
                exit 1
esac

将脚本放置/etc/init.d/redis-server

设置开机自启

[root@localhost ~]#chkconfig --add redis-server
[root@localhost ~]#chkconfig --level 35 redis-server on  #默认runlevel 35已打开

启动脚本设置执行权限

[root@localhost ~]#chmod +x /etc/init.d/redis-server

启动redis
#可通过启动脚本启动/关闭服务;或采用”service”指令

[root@localhost ~]#/etc/init.d/redis-server start

创建集群

在5.0之前该工具是由ruby语言编写的redis-trib.rb,在使用前需要安装ruby语言环境。在5.0之后redis摒弃了该工具,将搭建集群的功能合并到了redis-cli上。
这里就可以直接创建集群,不需要在安装其他工具。

创建集群需要注意,最少要6个服务,否则创建集群失败.

redis-cli --cluster create 110.110.100.35:6379 110.110.100.36:6379 110.110.100.37:6379 110.110.100.38:6379 110.10.1100.39:6379 110.10.100.40:6379 --cluster-replicas 1 -a 123456

参数说明
redis-cli 客户端工具
–cluster create 创建一个新的集群
–cluster-replicas 1 每一个主库对应一个从库
-a 数据库密码

出现下面的字样说明创建成功

[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

登陆验证

redis-cli -c -p 端口
auth 密码

删除主节点
redis-cli --cluster reshard 110.110.100.35:6379

到这里就搭建完成了

搭建过程中有的问题也有记录在异常处理

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Rio520

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

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

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

打赏作者

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

抵扣说明:

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

余额充值