三种模式: 主从 : 高可用性,读写分离,M写S读。故障时无法自动切换,只能手动,master存储和写能力因机器收到瓶颈哨兵 : 实现故障发现、故障转移、配置中心、客户端通知,哨兵可以从字面理解。他会持续性监视主从之间的状态信息,当主从故障自动切换集群 : 去中心化分片集群,集群中有16384个哈希槽,每个redis实例负责一部分slot,集群中的所有信息通过节点数据交换而更新。卡槽只会分配给主节点,每个卡槽能存放多key。有利于Redis动态扩容、缩容,把缓存雪崩数据丢失的风险降到最小。
集群搭建 传送门: 安装包官方下载地址
主机
端口
角色
10.6.6.69
6379/6380
master/slave
10.6.6.70
6379/6380
master/slave
10.6.6.71
6379/6380
master/slave
redis是c语言写的,下载环境 1 [root@report-redis1 ~]# yum install lrzsz wget ftp vim cpp binutils glibc glibc-kernheaders glibc-common glibc-devel gcc make -y
解压&&进入目录&&编译安装 1 2 3 4 [root@report-redis1 ~]# tar -xf redis-5.0.0.tar.gz [root@report-redis1 ~]# cd redis-5.0.0 [root@report-redis1 redis-5.0.0]# make [root@report-redis1 redis-5.0.0]# make install PREFIX=/usr/local/redis
创建数据目录 1 2 3 4 5 [root@report-redis1 redis-5.0.0]# cd /usr/local/redis/ [root@report-redis1 redis]# mkdir redis-cluster [root@report-redis1 redis-cluster]# mkdir redis-63{79,80} [root@report-redis1 redis-cluster]# ls redis-6379 redis-6380
处理配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 [root@report-redis1 bin ]# mv redis.conf redis.conf.back [root@report-redis1 redis-5.0.0 ]# `grep -Ev "#|^$" redis.conf.back` > redis_6379.conf #去掉bind绑定访问ip信息 #替换项 bind 10.6 .6 .69 #关闭保护模式 protected -mode yes#对应的端口 #替换项 port 6379 #启动集群模式 cluster-enabled yes #集群节点信息文件 cluster-config-file nodes-6379. conf #节点离线的超时时间 cluster-node-timeout 5000 #设置redis访问密码 requirepass Gxx@123 #启动进程号存储位置 #替换项 pidfile /var /run/redis_6379.pid #修改为后台启动 daemonize yes #启动AOF文件 appendonly yes #指定数据文件存放位置 #替换项 dir /usr/local/redis/redis-cluster/redis-6379 tcp-backlog 511 #当客户端闲置多长时间后关闭连接,如果指定为 0,表示关闭该功能 timeout 0 #指定的值(以秒为单位)是用于发送ACK的周期 tcp-keepalive 300 #指定日志记录级别 loglevel notice #日志存储文件路径 #替换项 logfile "/usr/local/redis/redis-cluster/redis-6379/6379-redis.log" #分别表示900秒内有1个更改,300秒内有10个更改以及60秒内有10000个更改 save 900 1 save 300 10 save 60 10000 #指定存储至本地数据库时是否压缩数据,默认为 yes rdbcompression yes rdbchecksum yes #指定本地数据库文件名 dbfilename dump.rdb supervised no databases 16 always-show-logo yes stop-writes-on -bgsave-error yes 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 lazyfree-lazy-eviction no lazyfree-lazy-expire no lazyfree-lazy-server-del no replica-lazy-flush no appendonly no appendfilename "appendonly.aof" appendfsync everysec no-appendfsync-on -rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64 mb aof-load-truncated yes aof-use-rdb-preamble yes lua-time-limit 5000 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 406 stream-node-max-entries 100 activerehashing yes client-output-buffer-limit normal 0 0 0 client-output-buffer-limit replica 256 mb 64 mb 60 client-output-buffer-limit pubsub 32 mb 8 mb 60 hz 10 dynamic -hz yesaof-rewrite-incremental-fsync yes rdb-save-incremental-fsync yes
复制文件到对应路径 1 2 3 4 [root@report-redis1 redis-5.0.0 ]# cp redis_6379.conf redis_6380.conf [root@report-redis1 redis-5.0.0 ]# cp redis_6379.conf /usr/local/redis/redis-cluster/redis-6379/ [root@report-redis1 redis-5.0.0 ]# cp redis_6380.conf /usr/local/redis/redis-cluster/redis-6380/ #配置复制到所需节点对应数据目录
增加软连接 1 2 [root@report-redis1 ~]# ln -s /usr/local/redis/bin/redis-server /usr/bin/redis-server [root@report-redis1 ~]# ln -s /usr/local/redis/bin/redis-cli /usr/bin/redis-cli
启动服务 1 2 [root@report-redis1 ~]# redis-server /usr/local/redis/redis-cluster/redis-6379/redis_6379.conf [root@report-redis1 ~]# redis-server /usr/local/redis/redis-cluster/redis-6380/redis_6380.conf
查看服务状态 1 2 3 4 [root@report-redis1 ~]# ps -fe |grep redis root 13088 1 0 Jul19 ? 00:02:45 redis-server 10.6.6.69:6379 [cluster] root 13095 1 0 Jul19 ? 00:03:00 redis-server 10.6.6.69:6380 [cluster] root 25881 20782 0 14:38 pts/0 00:00:00 grep --color=auto redis
初始化集群(随便某台节点操作即可) 1 2 3 4 #--cluster-replicas 1意味着我们希望为每个创建的主服务器创建一个副本 [root@report-redis1 ~ ]# redis-cli -a Gxx@123 --cluster create --cluster-replicas 1 10.6.6.69:6379 10.6.6.69:6380 10.6.6.70:6379 10.6.6.70:6380 10.6.6.71:6379 10.6.6.71:6380 #交互式提示 yes
集群命令 查看从主关系 1 [root@report-redis1 ~]# redis-cli -a Gxx@123 -h 10.6.6.69 -p 6379 -c cluster nodes
1 2 3 4 [root@report-redis1 ~]# redis-cli -a Gxx@123 -h 10.6.6.69 -p 6379 -c cluster slots | xargs -n8 | awk '{print $3":"$4"->"$6":"$7}' | uniq 10.6.6.69:6379->10.6.6.70:6380 10.6.6.70:6379->10.6.6.71:6380 10.6.6.71:6379->10.6.6.69:6380
数据验证 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #在69节点插入数据set [root@report-redis1 ~ ]# redis-cli -a Gxx@123 -h 10.6.6.69 -p 6379 -c Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe . 10.6.6.69:6379> set ceshi 111 -> Redirected to slot [11469] located at 10.6.6.71:6379 OK 10.6.6.71:6379> exit #70节点get 查看成功 [root @report -redis1 ~]# redis -cli -a Gxx @123 -h 10.6.6.70 -p 6379 -c 10.6.6.70:6379> get ceshi -> Redirected to slot [11469] located at 10.6.6.71:6379 "111" 10.6.6.71:6379>
手动故障验证 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #查看master进程 [root@report-redis1 ~ ]# ps -fe |grep redis root 13088 1 0 Jul19 ? 00 :02 :46 redis-server 10.6 .6 .69 :6379 [cluster] root 13095 1 0 Jul19 ? 00 :03 :02 redis-server 10.6 .6 .69 :6380 [cluster] root 26558 20782 0 15 :18 pts/0 00 :00 :00 grep --color=auto redis #kill进程 [root@report-redis1 ~ ]# kill -9 13088 [root@report-redis1 ~ ]# ps -fe |grep redis root 13095 1 0 Jul19 ? 00 :03 :02 redis-server 10.6 .6 .69 :6380 [cluster] root 26568 20782 0 15 :18 pts/0 00 :00 :00 grep --color=auto redis #重新启动 [root@report-redis1 ~ ]# redis-server /usr/local/redis/redis-cluster/redis-6379/redis_6379.conf
1 2 3 4 5 #再次查看主从状态已发生改变 [root@report-redis1 redis-6379 ]# redis-cli -a Gxx@123 -h 10.6.6.69 -p 6379 -c cluster slots | xargs -n8 | awk '{print $3":"$4"->"$6":"$7}' | uniq 10.6 .6 .70 :6380 ->10.6 .6 .69 :6379 10.6 .6 .71 :6379 ->10.6 .6 .69 :6380 10.6 .6 .70 :6379 ->10.6 .6 .71 :6380