Redis-cluster集群

redis-cluster所有包

链接:https://pan.baidu.com/s/1wcAI-jFFEaIU5Ilj6Beefg 
提取码:s29i

 

一、Redis自带Cluster集群的部署与基础使用

 

1.1 Redis集群特性

redis cluster集群方式支持主从自动切换 
redis cluster集群只有一个库,单列或者主从的话又多个库 
redis cluster集群去中心化,只要通过其中一个端口连接即可 
redis cluster集群,只有一个db库,不支持多库

 

1.2 redis自带集群搭建,一般使用三主三从来构建(最少六台机器)

主机名IP端口用途
redis-master 192.168.200.116 7000 redis-master01
  192.168.200.116 7001 redis-master02
  192.168.200.116 7002 redis-master03
redis-slave 192.168.200.105 8000 redis-slave01
  192.168.200.105 8001 redis-slave02
  192.168.200.105 8002 redis-slave03
 

1.3 实验初始环境要求

两台都需要搭建redis服务

cat /etc/redhat-release

uname -r

systemctl stop firewalld

systemctl disable firewalld

setenforce 0

sestatus

image_1d13gstvhpro15bhvrink71aoh9.png-19.3kB

 

1.4 配置实例

 

redis-master上操作如下图

mkdir -p /data/redis-cluster

cd /data/redis-cluster

mkdir -p 7000 7001 7002

cp /usr/local/redis/conf/redis.conf /data/redis-cluster/7000

cp /usr/local/redis/conf/redis.conf /data/redis-cluster/7001

cp /usr/local/redis/conf/redis.conf /data/redis-cluster/7002

tree /data

image_1d13i7kvb9pds144hvbvc93dm.png-48.1kB

 

redis-slave上操作如下图

mkdir -p /data/redis-cluster

cd /data/redis-cluster

mkdir -p 8000 8001 8002

cp /usr/local/redis/conf/redis.conf /data/redis-cluster/8000

cp /usr/local/redis/conf/redis.conf /data/redis-cluster/8001

cp /usr/local/redis/conf/redis.conf /data/redis-cluster/8002

tree /data

image_1d13i8sr4rje3ct1peo1lkdh1u13.png-48.7kB

 

修改redis-master的redis.conf配置文件(三个实例都修改端口,pid,日志并开启cluster)

 
redis-master不开任何持久化配置

vim 7000/redis.conf

cat 7000/redis.conf

 
  1. cluster-enabled yes
  2. bind 0.0.0.0
  3. port 7000
  4. pidfile /data/redis-cluster/7000/redis.pid
  5. logfile "/data/redis-cluster/7000/redis.log"
  6. dir /data/redis-cluster/7000/
  7. tcp-backlog 1024
  8. timeout 0
  9. tcp-keepalive 0
  10. daemonize yes
  11. loglevel notice
  12. databases 16
  13. stop-writes-on-bgsave-error yes
  14. rdbcompression yes
  15. rdbchecksum yes
  16. dbfilename "dump.rdb"
  17. slave-serve-stale-data yes
  18. slave-read-only yes
  19. repl-diskless-sync no
  20. repl-diskless-sync-delay 5
  21. repl-disable-tcp-nodelay no
  22. slave-priority 100
  23. lazyfree-lazy-eviction no
  24. lazyfree-lazy-expire no
  25. lazyfree-lazy-server-del no
  26. slave-lazy-flush no
  27. appendonly no
  28. appendfilename "appendonly.aof"
  29. appendfsync everysec
  30. no-appendfsync-on-rewrite yes
  31. auto-aof-rewrite-percentage 100
  32. auto-aof-rewrite-min-size 64mb
  33. aof-load-truncated yes
  34. lua-time-limit 5000
  35. slowlog-log-slower-than 10000
  36. slowlog-max-len 128
  37. latency-monitor-threshold 0
  38. notify-keyspace-events ""
  39. hash-max-ziplist-entries 512
  40. hash-max-ziplist-value 64
  41. set-max-intset-entries 512
  42. zset-max-ziplist-entries 128
  43. zset-max-ziplist-value 64
  44. hll-sparse-max-bytes 3000
  45. activerehashing yes
  46. client-output-buffer-limit normal 0 0 0
  47. client-output-buffer-limit slave 256mb 64mb 60
  48. client-output-buffer-limit pubsub 32mb 8mb 60
  49. hz 10
  50. aof-rewrite-incremental-fsync yes

image_1d13igc061m4a62511pp1dpa16tc1g.png-77kB


vim 7001/redis.conf

cat 7001/redis.conf

image_1d13ik2pi5j8hvj1a81fl71h201t.png-77.4kB


vim 7002/redis.conf

cat 7002/redis.conf

image_1d13incn1vst1uke1pkc1os36h52a.png-78.1kB

 

修改redis-slave的redis.conf配置文件(三个实例都修改端口,pid,日志并开启cluster)

vim 8000/redis.conf

cat 8000/redis.conf

image_1d13j0puvt1p1v3v17np1ajkq013n.png-77.6kB


vim 8001/redis.conf

cat 8001/redis.conf

image_1d13j27781seds28cbm18so1q7b44.png-78.4kB


vim 8002/redis.conf

cat 8002/redis.conf

image_1d13j37qv19cu1b7v10sf5ro2e251.png-78.1kB

 

1.5 启动redis-master和slave多实例

redis-server /data/redis-cluster/7000/redis.conf

redis-server /data/redis-cluster/7001/redis.conf

redis-server /data/redis-cluster/7002/redis.conf

ss -antup | grep redis

image_1d13j6ur81ffr1dl01crhu9q92a5e.png-58.5kB


redis-server /data/redis-cluster/8000/redis.conf

redis-server /data/redis-cluster/8001/redis.conf

redis-server /data/redis-cluster/8002/redis.conf

image_1d13j83dm19s4n942se1p536d55r.png-57.9kB

 

1.6 编译安装高ruby版本,Ruby版本需要大于等于2.2.2(yum安装的不符合)

 

master和slave都进行

ls

tar xf ruby-2.2.7.tar.gz -C /usr/src/

cd /usr/src/ruby-2.2.7/

./configure && make && make install

ruby --version

image_1d13je0eh169q1t4scstnm21kcf68.png-46.8kB

image_1d13jtasi14kg1p1h1oed16ab17ip72.png-149.9kB


image_1d13jfgc49tp1lpd55h11a03cv6l.png-58.5kB

image_1d13jtto0ng51iqs1qeiokrhqs7f.png-149.5kB

 

在线安装ruby的redis扩展

[root@redis-slave ruby-2.2.7]# /usr/local/bin/gem install redis 
Fetching: redis-4.1.0.gem (100%) 
Successfully installed redis-4.1.0 
Parsing documentation for redis-4.1.0 
Installing ri documentation for redis-4.1.0 
Done installing documentation for redis after 1 seconds 
1 gem installed

image_1d13k2f8dqurn0nfvsvst6ru7s.png-26.3kB


image_1d13k2obs123e1hb91t8j1v7665e89.png-26.3kB

 

1.7 使用reids自带redis-trib.rb工具创建集群

在redis-master上面操作

 

将redis-trib.rb复制到redis/bin下面。

cd /usr/src/redis-4.0.11/src

cp redis-trib.rb /usr/local/redis/bin/

ln -s /usr/local/redis/bin/* /usr/local/bin/ --->报错是之前添加的

image_1d13kra4c1lthq411bpjlla1q9u8m.png-42.1kB

 

创建集群

redis-trib.rb create 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002

image_1d13kt2ck91tmlb1gbhovc1sod93.png-105.9kB

 

1.8 查看集群的信息

redis-cli -p 7000 cluster nodes

cat /data/redis-cluster/7000/nodes.conf

image_1d13kuaisei016ci1u616cq19oa9g.png-56kB

 

1.9 使用redis-cli去操作集群,需要加入-c参数,-c表示用集群的方式查看建值

redis-cli -c: Enable cluster mode (follow-ASK and -MOVED redirections)

redis-cli -c -p 7000

set name1 yunjisuan

set name2 benet

image_1d13l1jpl1tr5maunjjpjdca09t.png-25.8kB

 

1.10 去中心化,随意一个入口

 

在master上操作

redis-cli -c -p 7001 get name1

redis-cli -c -p 7002 get name2

image_1d13l39q96orhaj1icdet81id3aa.png-15.6kB

 

在slave上操作

redis-cli -h 192.168.200.116 -c -p 7000 get name1 --->被拒绝

redis-cli -h 192.168.200.116 -p 7002 get name1 --->访问成功,指定7002端口

image_1d13l4jkg44i30e1kj23ri59an.png-24.2kB

为什么远程方式访问redis-cluster时候被拒绝了?因为我们创建集群的时候是以127.0.0.1的本地IP方式创建的,因此只有本地访问cluster集群才能发挥作用,远程访问7002端口之所以能成功,因为数据本身就在7002端口的redis上,不加-c也能访问成功。

 

1.11 cluster集群的重建

 

在master上操作

ls /data/redis-cluster/7000

ls /data/redis-cluster/7001

ls /data/redis-cluster/7002

image_1d13lbf99fog1f8r1d7ipjg1lf8bn.png-22.2kB

 

删除cluster集群配置文件

rm -rf /data/redis-cluster/7000/nodes.conf

rm -rf /data/redis-cluster/7001/nodes.conf

rm -rf /data/redis-cluster/7002/nodes.conf

redis-cli -p 7000 shutdown

redis-cli -p 7001 shutdown

redis-cli -p 7002 shutdown

image_1d13lhadmec3157prpt1qcvgaqc4.png-29.3kB

 

启动redis-server

redis-server /data/redis-cluster/7000/redis.conf

redis-server /data/redis-cluster/7001/redis.conf

redis-server /data/redis-cluster/7002/redis.conf

ss -antup | grep redis

image_1d13ljak3iksc63mihhon79ch.png-58.4kB

 

重新创建redis-cluster集群

redis-trib.rb create 192.168.200.116:7000 192.168.200.116:7001 192.168.200.116:7002

ps -ef | grep cluster | grep -v grep

image_1d13lnkt41uo31uur19cmrtu99idb.png-126.8kB

 

1.12 在redis-slave上进行远程连接cluster集群测试

redis-cli -h 192.168.200.116 -c -p 7000 set name yunjisuan

redis-cli -h 192.168.200.116 -c -p 7001 get name

redis-cli -h 192.168.200.116 -c -p 7001

get name

set name2 xxxx

get name2

image_1d13lsjj817261lcf23gun0l80e8.png-37.6kB

 

1.13 批量导入数据观察key的集群分布情况

for line in `seq -w 10000`;do redis-cli -h 192.168.200.116 -p 7000 -c set key3_${line} value_${line};done

image_1d13m1emb1f6o1dq0sbk1ai2kshel.png-35.1kB

 

1.14 分析cluster集群key的节点分布情况

redis-cli -h 192.168.200.116 -p 7000 info keyspace

redis-cli -h 192.168.200.116 -p 7001 info keyspace

redis-cli -h 192.168.200.116 -p 7002 info keyspace

image_1d0md41aa17ch108u1mhs15tr1433aq.png-27.6kB

 

二、Redis Cluster集群的故障自动切换

 

2.1 将redis-master的配置文件拷贝到redis-slave

cd /data/redis-cluster

scp 7000/redis.conf root@192.168.200.137:/data/redis-cluster/8000

scp 7000/redis.conf root@192.168.200.137:/data/redis-cluster/8001

scp 7000/redis.conf root@192.168.200.137:/data/redis-cluster/8002

修改配置文件为对应的8000、8001、8002端口,过程省略(上文提前修改了)

 

2.2 启动redis-slave上所有的从库

image_1d13m7nej1dkkdqc1f72im01kjmf2.png-44.1kB

 

2.3 redis-cluster集群从库的添加

redis-master上操作

 

添加第一组主从

redis-trib.rb add-node --slave 192.168.200.105:8000 192.168.200.116:7000

image_1d13mcivghtd14lumua1l9astqff.png-80kB

 

添加第二组主从

redis-trib.rb add-node --slave 192.168.200.105:8001 192.168.200.116:7001

image_1d13me3761io81f0d1meu1isd11njfs.png-90.1kB

 

添加第三组主从

redis-trib.rb add-node --slave 192.168.200.105:8002 192.168.200.116:7002

image_1d13mekq4v2ehfc1dcqvem1hv9gp.png-100.9kB

 

2.4 查看集群所有节点的信息

redis-cli -p 7000 cluster nodes

image_1d13mfom51872gnv1buk1nfkhpnh6.png-54.5kB

 

2.5 redis-cluster集群slave从库的读写测试

redis-cli -h 192.168.200.105 -c -p 8000

set name 666

exit

redis-cli -h 192.168.200.105 -c -p 8000 get name

redis-cli -h 192.168.200.105 -c -p 8001 get name

redis-cli -h 192.168.200.105 -c -p 8002 get name

redis-cli -h 192.168.200.116 -c -p 7002 get name

redis-cli -h 192.168.200.116 -c -p 7001 get name

redis-cli -h 192.168.200.116 -c -p 7000 get name

通过测试,发现redis的cluster集群,不论是主库还是从库都可以进行set和get.因此,在使用中就没有必要都去主库了.

image_1d13mnq0h1dd1rq93qr1f7f137bhj.png-52.6kB

 

2.6 查看主从cluster集群key的分布情况

redis-cli -h 192.168.200.105 -c -p 8002 info keyspace --->从库

redis-cli -h 192.168.200.105 -c -p 8001 info keyspace

redis-cli -h 192.168.200.105 -c -p 8000 info keyspace

redis-cli -h 192.168.200.116 -c -p 7002 info keyspace --->主库

redis-cli -h 192.168.200.116 -c -p 7001 info keyspace

redis-cli -h 192.168.200.116 -c -p 7000 info keyspace

image_1d13ms6rl1umanev156hcd110gui0.png-71.3kB

 

2.7 redis集群的主从自动切换,主库挂掉后,从自动变成主

手动切换主从命令cluster failover

 

手动down掉了7000端口的redis-server。从信息得知,7000端口挂了,8000端口被切换为了master

redis-cli -h 192.168.200.116 -p 7000 shutdown

redis-cli -h 192.168.200.105 -p 8000 cluster nodes

image_1d13n3o1o1rjl1l3h1dkl1cn51t8eid.png-62.4kB

 

重新启动7000端口的server,再次查看

redis-server /data/redis-cluster/7000/redis.conf --->重新启动7000端口的server

image_1d13n6fg2sn510t718ebsf0mm3ja.png-10.3kB

 

在从库上查看状态

redis-cli -h 192.168.200.105 -p 8000 cluster nodes --->发现原先的主变成了从

image_1d13nag5e1v1r1cp4na910olalfjn.png-56kB

 

手动将redis-server 7000端口重新切换成主库

redis-cli -h 192.168.200.116 -c -p 7000 cluster failover

redis-cli -h 192.168.200.105 -p 8000 cluster nodes --->发现7000端口又变成了主

image_1d13ncnjq18stovh14h1duc2ank4.png-63.3kB

 

三、使用Python操作Redis Cluster集群

 

3.1 Python安装扩展用来操作redis集群

 
  1. [root@redis-master ~]# yum -y install epel-release
  2. [root@redis-master ~]# yum -y install python2-pip
  3. [root@redis-master ~]# pip install redis-py-cluster
 

3.2 操作集群的代码

 
  1. [root@redis-master ~]# mkdir -p /server/scripts
  2. [root@redis-master ~]# cd /server/scripts
  3. [root@redis-master scripts]# vim redis_cluster.py
  4. [root@redis-master scripts]# cat redis_cluster.py
  5. # -*- coding:utf-8 -*-
  6. from rediscluster import StrictRedisCluster
  7. redis_nodes = [
  8. {'host':'192.168.200.116','port':7000},
  9. {'host':'192.168.200.116','port':7001},
  10. {'host':'192.168.200.116','port':7002},
  11. {'host':'192.168.200.105','port':8000},
  12. {'host':'192.168.200.105','port':8001},
  13. {'host':'192.168.200.105','port':8002}
  14. ]
  15. redis_conn = StrictRedisCluster(startup_nodes=redis_nodes)
  16. redis_conn.set('key_test','values_test')
  17. print(redis_conn.get('key_test'))
  18. [root@redis-master scripts]# python redis_cluster.py
  19. values_test
  20. [root@redis-master scripts]# redis-cli -c -p 7002 get key_test
  21. "values_test"

主:如果其中一个节点挂了,不影响功能的使用

 

四、分析Redis的所有key和key的大小

 

4.1 pip安装rdbtools分析工具

 
  1. [root@redis-master scripts]# pip install rdbtools
  2. [root@redis-master scripts]# which rdb
  3. /usr/bin/rdb
 

4.2分析key及key的大小

运维需求,根据dump.rdb文件分析key和key的大小

 
    1. [root@redis-master ~]# rdb -c memory /data/redis-cluster/7000/dump.rdb > /root/memory.csv
    2. [root@redis-master ~]# cat /root/memory.csv | head
    3. database,type,key,size_in_bytes,encoding,num_elements,len_largest_element,expiry
    4. 0,string,key3_03130,72,string,11,11,
    5. 0,string,key3_04725,72,string,11,11,
    6. 0,string,key3_07347,72,string,11,11,
    7. 0,string,key3_06980,72,string,11,11,
    8. 0,string,key3_07297,72,string,11,11,
    9. 0,string,key3_02168,72,string,11,11,
    10. 0,string,key3_04336,72,string,11,11,
    11. 0,string,key3_00710,72,string,11,11,
    12. 0,string,key3_06607,72,string,11,11,
    13. #分析key的大小
    14. [root@redis-master ~]# cat /root/memory.csv | tr ',' ' ' | sort -k4rn | head
    15. 0 string key3_00002 72 string 11 11
    16. 0 string key3_00003 72 string 11 11
    17. 0 string key3_00006 72 string 11 11
    18. 0 string key3_00007 72 string 11 11
    19. 0 string key3_00010 72 string 11 11
    20. 0 string key3_00014 72 string 11 11
    21. 0 string key3_00018 72 string 11 11
    22. 0 string key3_00020 72 string 11 11
    23. 0 string key3_00021 72 string 11 11
    24. 0 string key3_00024 72 string 11 11
    25. [root@redis-master ~]# cat /root/memory.csv | tr ',' ' ' | sort -k4n | head
    26. database type key size_in_bytes encoding num_elements len_largest_element expiry
    27. 0 string name2 56 string 4 4
    28. 0 string key3_00002 72 string 11 11
    29. 0 string key3_00003 72 string 11 11
    30. 0 string key3_00006 72 string 11 11
    31. 0 string key3_00007 72 string 11 11
    32. 0 string key3_00010 72 string 11 11
    33. 0 string key3_00014 72 string 11 11
    34. 0 string key3_00018 72 string 11 11
    35. 0 string key3_00020 72 string 11 11
posted @ 2019-07-24 15:23  L1n  阅读(1381)  评论(0编辑  收藏  举报