marquee
当你的才华还撑不起你的野心的时候,你就应该静下心来学习。当你的能力还驾驭不了你的目标的时候,你就应该沉下心来历练。问问自己,想要怎样的人生

redis-cluster分布式集群

          Redis-cluster 6.0+集群搭建

1,概述

Redis集群(redis-cluster)是redis提供的分布式数据库方案,通过分片(sharding)来进行数据共享,并提供复制和故障转移功能,相比于主从复制,哨兵模式,redis集群实现了较为完善的高可用方案,解决了存储能力受单机限制,写操作无法负载的问题。

 

2,redis集群环境搭建

为了方便起见,在本搭建环境部署到同一台服务器上,共6个节点端口,3+3从节点

注意!从redis 5.0后的版本,集群管理软件redis-trib.rb被继承到redis-cli客户端工具中了

 

 

gcc版本问题避免

Redis是基于c语言开发的,安装redis需要c语言的编译环境

安装redis6最主要的一点是要用gcc5以上版本,查看gcc版本使用gcc -v好看版本

 

环境安装并升级gcc版本

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

[root@localhost ~]# yum -y install centos-release-scl

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

 

查看版本

[root@localhost ~]# gcc --version

gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44)

 

Scl 只是临时启用,退出shell后会恢复源系统的gcc版本

[root@localhost ~]# scl enable devtoolset-9 bash

 

如果需要设置永久生效执行下面的步骤

[root@localhost ~]# echo "source /opt/rh/devtoolset-9/enable" >> /etc/profile

[root@localhost ~]# source /etc/profile

[root@localhost ~]# gcc --version

gcc (GCC) 9.3.1 20200408 (Red Hat 9.3.1-2)

 

下载安装redis

6.0之前版本需要安装ruby

[root@localhost ~]# cd /opt/

[root@localhost opt]# wget https://download.redis.io/releases/redis-6.0.14.tar.gz  //国外网站

[root@localhost opt]# tar xf redis-6.0.14.tar.gz

[root@localhost opt]# mv redis-6.0.14 redis

[root@localhost opt]# cd redis

[root@localhost redis]# make MALLOC=libc

[root@localhost redis]# make PREFIX=/opt/redis-cluster  install

[root@localhost opt]# cd /opt/redis-cluster/

[root@master-cluster redis-cluster]# mkdir {conf,data,logs,pid}

编写公共redis配置文件

[root@localhost redis-cluster]# vim conf/redis-common.conf

dir ./data

#最大可用内存  
maxmemory 4000m
bind 0.0.0.0
#打开aof持久化  
appendonly yes
daemonize yes  
tcp-backlog 511  
timeout 300
tcp-keepalive 0  
slave-serve-stale-data yes  

#slave只读  
slave-read-only yes  
#not use default  
repl-disable-tcp-nodelay yes  
slave-priority 100   

#每秒一次aof写  
appendfsync everysec  

#关闭在aof rewrite的时候对新的写操作进行fsync  
no-appendfsync-on-rewrite yes  
auto-aof-rewrite-min-size 64mb  
lua-time-limit 5000  

#打开redis集群  
cluster-enabled yes  

#节点互连超时的阀值  
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  
databases 1
protected-mode no

#日志等级
loglevel notice

###日志保存路径
logfile /opt/redis-cluster/logs/redis.log
#添加密码认证
requirepass 123456
masterauth 123456

 

添加节点配置文件

[root@localhost redis-cluster]# vim conf/redis-30031.conf

include ./conf/redis-common.conf
port 30031
pidfile ./pid/30031.pid
cluster-config-file nodes-30031.conf

appendfilename "appendonly-30031.aof"
dbfilename dump-30031.rdb

maxmemory-policy noeviction
auto-aof-rewrite-percentage 100

 

  分别拷贝其他五个节点配置文件

[root@localhost redis-cluster]# cp conf/redis-30031.conf conf/redis-30032.conf

分别替换所有配置文件的配置

[root@localhost redis-cluster]# sed -i -e 's/30031/30032/g' conf/redis-30032.conf

添加环境变量

[root@localhost redis-cluster]# vim ~/.bash_profile

export PATH="$PATH:/opt/redis-cluster/bin"

[root@localhost redis-cluster]# . ~/.bash_profile  //生效环境变量

 

配置启动脚本

[root@localhost redis-cluster]# vim start.sh 

#!/bin/sh
###################
. ~/.bash_profile #
###################

redis-server ./conf/redis-30031.conf
redis-server ./conf/redis-30032.conf
redis-server ./conf/redis-30033.conf
redis-server ./conf/redis-30034.conf
redis-server ./conf/redis-30035.conf
redis-server ./conf/redis-30036.conf

 

配置停止脚本

[root@localhost redis-cluster]# vim stop.sh

#!/bin/sh
kill -9 `ps -ef | grep redis | grep -v 'grep' | awk '{print $2}'`

启动所有实例

[root@localhost redis-cluster]# sh start.sh

 

创建集群

注意!生产环境为了满足高可用性,每台机器单独部署一个redis(三主三从就相当于要六台机器),如果三台机器每台上面部署一个主一个从,就需要每台主从对应关系岔开,例如:192.168.113.135:30032这个主就要对应192.168.113.136:30032这个从

[root@localhost redis-cluster]# redis-cli -a 123456 --cluster create 192.168.113.135:30031 192.168.113.135:30033 192.168.113.135:30035 192.168.113.135:30036 192.168.113.135:30034 192.168.113.135:30032 --cluster-replicas 1  (前三台是主,一一对应后面三台从节点

查看集群状态

[root@localhost redis-cluster]# redis-cli -c -h 192.168.113.135 -p 30031 cluster info

NOAUTH Authentication required.

[root@localhost redis-cluster]# redis-cli -a 123456 -c -h 192.168.113.135 -p 30031 cluster info

Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.

cluster_state:ok

cluster_slots_assigned:16384

cluster_slots_ok:16384

cluster_slots_pfail:0

cluster_slots_fail:0

cluster_known_nodes:6

cluster_size:3

cluster_current_epoch:6

cluster_my_epoch:1

cluster_stats_messages_ping_sent:216

cluster_stats_messages_pong_sent:221

cluster_stats_messages_sent:437

cluster_stats_messages_ping_received:216

cluster_stats_messages_pong_received:216

cluster_stats_messages_meet_received:5

cluster_stats_messages_received:437

 

查看集群节点

 

 

 

 

Redis添加开机自启

[root@localhost redis-cluster]# chmod +x /etc/rc.d/rc.local

[root@localhost redis-cluster]# echo /opt/redis-cluster/start.sh > /etc/rc.d/rc.local

 

 

测试用例

[root@localhost redis-cluster]# redis-cli -a 123456 -c -h 192.168.113.135 -p 30032

Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.

192.168.113.135:30032> get name

"1"

192.168.113.135:30032> set name2 3

-> Redirected to slot [742] located at 192.168.113.135:30031OK

192.168.113.135:30031> keys *

1) "name2"

192.168.113.135:300

posted @ 2021-10-18 15:28  智杰  阅读(297)  评论(0)    收藏  举报
marquee
当你的才华还撑不起你的野心的时候,你就应该静下心来学习。当你的能力还驾驭不了你的目标的时候,你就应该沉下心来历练。问问自己,想要怎样的人生