Centos下Redis集群的搭建实现读写分离
Centos下Redis一主多从架构搭建
搭建目标:因为自己笔记本电脑配置较低的原因,模拟两台机器之间搭建一主一从的架构,主节点Redis主要用来写数据,数据写入到主节点的Redis,然后从节点就可以同步主节点的所有数据,从节点只能读取数据,禁止写入任何数据,这样就可以实现主节点写数据,从节点读数据的读写分离架构,实现思路如下
1、在两台机器分别安装Redis
具体安装步骤,参考这篇博文安装: http://www.cnblogs.com/sunny1009/articles/7866034.html
2、主从集群配置
两台机器ip:
192.168.1.140-->作为主节点
192.168.1.142-->作为从节点
(1)、在6379.conf下配置,首先在142机器6379.conf上配置,slaveof标示代表此机器代表从节点
#配置主节点的ip地址 slaveof 192.168.1.140 6379
(2)、强制读写分离,配置子节点为只读状态,会拒绝所有的写操作
slave-read-only yes
3、集群安全认证(在主节点配置)
#master上启用安全认证
requirepass admin@master
admin@master为主节点的安全口令
4、从节点开启密码认证(从节点配置)
配置与主节点安全口令一致
#从节点开启密码认证
masterauth admin@master
5、分别在主节点的从节点开启Redis对外访问
#绑定本机的ip地址 bind 192.168.1.X protected-mode no
6、启动主节点和从节点Redis进行验证
登录主节点Redis
cd /etc/init.d/
./redis_6379 start
redis-cli -h 192.168.1.140 -p 6379 -a admin@master
执行命令
info replication
可以看到所属的角色和主节点下从节点的信息
登录从节点Redis
cd /etc/init.d/
./redis_6379 start redis-cli -h 192.168.1.140 -p 6379 -a
info replication
可以看到所属的角色和对应连接的主节点信息
查看主节点和从节点下是否有缓存key
keys *
查看主节点和从节点数据都为空,没任何缓存数据
测试从节点是否能写入数据
不能写入,说明配置成功,从节点只能读数据不能写数据,如果再实现主节点写数据,将数据同步到从节点接可以实现读写分离的架构
测试往主节点写数据,查看从节点是否可以同步到从节点的数据
set key1 value1 get key1
往主节点写了一条数据,key为key1,值为value1
直接到从节点查看是否可以同步到主节点的数据
get key1
发现从节点已经同步到主节点的数据了,到此,最基本的主从架构实现读写分离搭建成功,按照以上步骤还可以配置多个从节点实现一主多从的集群架构,从而实现缓存的高性能,高可用和水平扩展;当然在大流量的情况下会出现主节点或者其他从节点宕机的情况,这些后续会慢慢一步一步解决,例如:Redis的容灾备份,雪崩,哨兵等等。。。
7、对读写分离集群QPS压测
利用Redis提供的压测工具方便快捷,具体压测步骤如下
cd /usr/local/redis-3.2.8/src ./redis-benchmark -h 192.168.1.140
由于自己的集群搭建在虚拟机下,配置:单核1G的cpu,性能很差,所以采用默认参数压测即可
-c <clients> Number of parallel connections (default 50)
-n <requests> Total number of requests (default 100000)
-d <size> Data size of SET/GET value in bytes (default 2)
====== PING_INLINE ====== 100000 requests completed in 2.51 seconds 50 parallel clients 3 bytes payload keep alive: 1 89.64% <= 1 milliseconds 98.37% <= 2 milliseconds 99.45% <= 3 milliseconds 99.59% <= 4 milliseconds 99.75% <= 5 milliseconds 99.80% <= 6 milliseconds 99.82% <= 7 milliseconds 99.85% <= 8 milliseconds 99.86% <= 9 milliseconds 99.86% <= 10 milliseconds 99.87% <= 22 milliseconds 99.89% <= 23 milliseconds 99.91% <= 30 milliseconds 99.94% <= 37 milliseconds 99.95% <= 54 milliseconds 99.95% <= 58 milliseconds 99.97% <= 59 milliseconds 100.00% <= 59 milliseconds 39824.77 requests per second ====== PING_BULK ====== 100000 requests completed in 2.80 seconds 50 parallel clients 3 bytes payload keep alive: 1 83.94% <= 1 milliseconds 96.93% <= 2 milliseconds 98.54% <= 3 milliseconds 99.04% <= 4 milliseconds 99.38% <= 5 milliseconds 99.58% <= 6 milliseconds 99.68% <= 7 milliseconds 99.73% <= 8 milliseconds 99.78% <= 10 milliseconds 99.80% <= 11 milliseconds 99.82% <= 14 milliseconds 99.83% <= 15 milliseconds 99.86% <= 16 milliseconds 99.87% <= 25 milliseconds 99.88% <= 26 milliseconds 99.94% <= 27 milliseconds 99.94% <= 45 milliseconds 99.99% <= 78 milliseconds 100.00% <= 78 milliseconds 35752.59 requests per second ====== SET ====== 100000 requests completed in 2.70 seconds 50 parallel clients 3 bytes payload keep alive: 1 86.10% <= 1 milliseconds 97.34% <= 2 milliseconds 98.72% <= 3 milliseconds 99.17% <= 4 milliseconds 99.30% <= 5 milliseconds 99.42% <= 6 milliseconds 99.47% <= 7 milliseconds 99.52% <= 8 milliseconds 99.61% <= 9 milliseconds 99.70% <= 10 milliseconds 99.79% <= 15 milliseconds 99.82% <= 22 milliseconds 99.84% <= 26 milliseconds 99.85% <= 27 milliseconds 99.88% <= 28 milliseconds 99.88% <= 29 milliseconds 99.89% <= 45 milliseconds 99.89% <= 46 milliseconds 99.93% <= 47 milliseconds 99.94% <= 52 milliseconds 99.96% <= 53 milliseconds 99.98% <= 54 milliseconds 99.98% <= 58 milliseconds 99.98% <= 59 milliseconds 100.00% <= 59 milliseconds 37009.62 requests per second ====== GET ====== 100000 requests completed in 2.67 seconds 50 parallel clients 3 bytes payload keep alive: 1 87.35% <= 1 milliseconds 97.68% <= 2 milliseconds 99.00% <= 3 milliseconds 99.26% <= 4 milliseconds 99.42% <= 5 milliseconds 99.55% <= 6 milliseconds 99.62% <= 7 milliseconds 99.69% <= 8 milliseconds 99.74% <= 16 milliseconds 99.79% <= 17 milliseconds 99.82% <= 18 milliseconds 99.83% <= 19 milliseconds 99.85% <= 20 milliseconds 99.86% <= 27 milliseconds 99.86% <= 28 milliseconds 99.91% <= 47 milliseconds 99.92% <= 48 milliseconds 99.95% <= 49 milliseconds 99.96% <= 57 milliseconds 99.96% <= 58 milliseconds 99.96% <= 70 milliseconds 99.99% <= 71 milliseconds 100.00% <= 71 milliseconds 37467.21 requests per second ====== INCR ====== 100000 requests completed in 2.65 seconds 50 parallel clients 3 bytes payload keep alive: 1 88.43% <= 1 milliseconds 97.81% <= 2 milliseconds 98.96% <= 3 milliseconds 99.33% <= 4 milliseconds 99.48% <= 5 milliseconds 99.49% <= 6 milliseconds 99.55% <= 7 milliseconds 99.57% <= 8 milliseconds 99.66% <= 9 milliseconds 99.76% <= 10 milliseconds 99.81% <= 11 milliseconds 99.83% <= 15 milliseconds 99.85% <= 16 milliseconds 99.87% <= 19 milliseconds 99.90% <= 20 milliseconds 99.92% <= 28 milliseconds 99.92% <= 44 milliseconds 99.94% <= 60 milliseconds 99.98% <= 61 milliseconds 100.00% <= 61 milliseconds 37792.89 requests per second ====== LPUSH ====== 100000 requests completed in 2.62 seconds 50 parallel clients 3 bytes payload keep alive: 1 86.97% <= 1 milliseconds 97.55% <= 2 milliseconds 99.01% <= 3 milliseconds 99.38% <= 4 milliseconds 99.47% <= 5 milliseconds 99.55% <= 6 milliseconds 99.67% <= 7 milliseconds 99.74% <= 8 milliseconds 99.75% <= 9 milliseconds 99.78% <= 10 milliseconds 99.83% <= 11 milliseconds 99.84% <= 18 milliseconds 99.86% <= 19 milliseconds 99.87% <= 24 milliseconds 99.89% <= 29 milliseconds 99.89% <= 30 milliseconds 99.89% <= 39 milliseconds 99.92% <= 45 milliseconds 99.94% <= 46 milliseconds 99.95% <= 53 milliseconds 99.97% <= 54 milliseconds 100.00% <= 54 milliseconds 38109.76 requests per second ====== RPUSH ====== 100000 requests completed in 2.89 seconds 50 parallel clients 3 bytes payload keep alive: 1 85.96% <= 1 milliseconds 97.12% <= 2 milliseconds 98.62% <= 3 milliseconds 99.02% <= 4 milliseconds 99.20% <= 5 milliseconds 99.45% <= 6 milliseconds 99.59% <= 7 milliseconds 99.67% <= 8 milliseconds 99.68% <= 9 milliseconds 99.72% <= 10 milliseconds 99.75% <= 12 milliseconds 99.82% <= 13 milliseconds 99.83% <= 17 milliseconds 99.86% <= 18 milliseconds 99.88% <= 32 milliseconds 99.89% <= 33 milliseconds 99.93% <= 52 milliseconds 99.94% <= 56 milliseconds 99.95% <= 76 milliseconds 99.97% <= 79 milliseconds 99.97% <= 80 milliseconds 100.00% <= 112 milliseconds 34650.04 requests per second ====== LPOP ====== 100000 requests completed in 2.67 seconds 50 parallel clients 3 bytes payload keep alive: 1 86.79% <= 1 milliseconds 97.77% <= 2 milliseconds 98.91% <= 3 milliseconds 99.28% <= 4 milliseconds 99.52% <= 5 milliseconds 99.66% <= 6 milliseconds 99.73% <= 7 milliseconds 99.80% <= 8 milliseconds 99.80% <= 9 milliseconds 99.83% <= 10 milliseconds 99.84% <= 11 milliseconds 99.86% <= 12 milliseconds 99.88% <= 22 milliseconds 99.88% <= 23 milliseconds 99.88% <= 24 milliseconds 99.90% <= 25 milliseconds 99.91% <= 49 milliseconds 99.92% <= 50 milliseconds 99.94% <= 51 milliseconds 99.95% <= 52 milliseconds 99.97% <= 55 milliseconds 100.00% <= 56 milliseconds 37453.18 requests per second ====== RPOP ====== 100000 requests completed in 2.54 seconds 50 parallel clients 3 bytes payload keep alive: 1 89.33% <= 1 milliseconds 98.10% <= 2 milliseconds 99.42% <= 3 milliseconds 99.61% <= 4 milliseconds 99.69% <= 5 milliseconds 99.78% <= 6 milliseconds 99.80% <= 7 milliseconds 99.82% <= 8 milliseconds 99.84% <= 9 milliseconds 99.86% <= 10 milliseconds 99.88% <= 11 milliseconds 99.89% <= 15 milliseconds 99.90% <= 17 milliseconds 99.91% <= 21 milliseconds 99.93% <= 22 milliseconds 99.94% <= 65 milliseconds 99.95% <= 77 milliseconds 99.98% <= 78 milliseconds 99.98% <= 79 milliseconds 100.00% <= 79 milliseconds 39401.10 requests per second ====== SADD ====== 100000 requests completed in 2.38 seconds 50 parallel clients 3 bytes payload keep alive: 1 89.92% <= 1 milliseconds 98.57% <= 2 milliseconds 99.48% <= 3 milliseconds 99.68% <= 4 milliseconds 99.78% <= 5 milliseconds 99.78% <= 6 milliseconds 99.79% <= 7 milliseconds 99.87% <= 8 milliseconds 99.89% <= 10 milliseconds 99.91% <= 11 milliseconds 99.91% <= 18 milliseconds 99.95% <= 21 milliseconds 99.95% <= 59 milliseconds 99.96% <= 60 milliseconds 99.99% <= 61 milliseconds 100.00% <= 61 milliseconds 41928.72 requests per second ====== SPOP ====== 100000 requests completed in 2.69 seconds 50 parallel clients 3 bytes payload keep alive: 1 85.19% <= 1 milliseconds 97.62% <= 2 milliseconds 98.98% <= 3 milliseconds 99.34% <= 4 milliseconds 99.50% <= 5 milliseconds 99.67% <= 6 milliseconds 99.73% <= 7 milliseconds 99.75% <= 8 milliseconds 99.80% <= 10 milliseconds 99.84% <= 11 milliseconds 99.88% <= 12 milliseconds 99.89% <= 34 milliseconds 99.92% <= 37 milliseconds 99.94% <= 61 milliseconds 99.96% <= 65 milliseconds 99.97% <= 66 milliseconds 100.00% <= 66 milliseconds 37133.31 requests per second ====== LPUSH (needed to benchmark LRANGE) ====== 100000 requests completed in 2.50 seconds 50 parallel clients 3 bytes payload keep alive: 1 88.30% <= 1 milliseconds 98.12% <= 2 milliseconds 99.26% <= 3 milliseconds 99.40% <= 4 milliseconds 99.52% <= 5 milliseconds 99.62% <= 6 milliseconds 99.63% <= 7 milliseconds 99.71% <= 8 milliseconds 99.80% <= 10 milliseconds 99.81% <= 13 milliseconds 99.83% <= 14 milliseconds 99.86% <= 15 milliseconds 99.86% <= 27 milliseconds 99.88% <= 28 milliseconds 99.91% <= 44 milliseconds 99.91% <= 45 milliseconds 99.93% <= 46 milliseconds 100.00% <= 46 milliseconds 39952.06 requests per second ====== LRANGE_100 (first 100 elements) ====== 100000 requests completed in 2.63 seconds 50 parallel clients 3 bytes payload keep alive: 1 88.45% <= 1 milliseconds 97.64% <= 2 milliseconds 98.84% <= 3 milliseconds 99.06% <= 4 milliseconds 99.26% <= 5 milliseconds 99.34% <= 6 milliseconds 99.41% <= 7 milliseconds 99.46% <= 8 milliseconds 99.59% <= 9 milliseconds 99.61% <= 10 milliseconds 99.65% <= 11 milliseconds 99.69% <= 12 milliseconds 99.70% <= 13 milliseconds 99.72% <= 14 milliseconds 99.75% <= 15 milliseconds 99.79% <= 16 milliseconds 99.83% <= 17 milliseconds 99.84% <= 25 milliseconds 99.86% <= 26 milliseconds 99.89% <= 27 milliseconds 99.89% <= 31 milliseconds 99.91% <= 32 milliseconds 99.93% <= 33 milliseconds 99.94% <= 38 milliseconds 99.99% <= 45 milliseconds 99.99% <= 88 milliseconds 100.00% <= 89 milliseconds 100.00% <= 89 milliseconds 38022.81 requests per second ====== LRANGE_300 (first 300 elements) ====== 100000 requests completed in 2.91 seconds 50 parallel clients 3 bytes payload keep alive: 1 84.62% <= 1 milliseconds 95.68% <= 2 milliseconds 97.95% <= 3 milliseconds 98.55% <= 4 milliseconds 98.94% <= 5 milliseconds 99.16% <= 6 milliseconds 99.34% <= 7 milliseconds 99.44% <= 8 milliseconds 99.52% <= 9 milliseconds 99.63% <= 10 milliseconds 99.65% <= 11 milliseconds 99.70% <= 12 milliseconds 99.74% <= 13 milliseconds 99.77% <= 14 milliseconds 99.79% <= 15 milliseconds 99.83% <= 16 milliseconds 99.83% <= 21 milliseconds 99.86% <= 25 milliseconds 99.90% <= 26 milliseconds 99.92% <= 48 milliseconds 99.92% <= 52 milliseconds 99.96% <= 80 milliseconds 99.98% <= 81 milliseconds 100.00% <= 81 milliseconds 34411.56 requests per second ====== LRANGE_500 (first 450 elements) ====== 100000 requests completed in 2.61 seconds 50 parallel clients 3 bytes payload keep alive: 1 87.74% <= 1 milliseconds 98.11% <= 2 milliseconds 99.33% <= 3 milliseconds 99.46% <= 4 milliseconds 99.56% <= 5 milliseconds 99.62% <= 6 milliseconds 99.69% <= 7 milliseconds 99.76% <= 8 milliseconds 99.79% <= 9 milliseconds 99.82% <= 10 milliseconds 99.83% <= 11 milliseconds 99.84% <= 12 milliseconds 99.87% <= 13 milliseconds 99.88% <= 23 milliseconds 99.89% <= 28 milliseconds 99.92% <= 29 milliseconds 99.93% <= 30 milliseconds 99.94% <= 49 milliseconds 99.94% <= 50 milliseconds 99.95% <= 51 milliseconds 99.98% <= 52 milliseconds 99.98% <= 73 milliseconds 100.00% <= 73 milliseconds 38372.98 requests per second ====== LRANGE_600 (first 600 elements) ====== 100000 requests completed in 2.61 seconds 50 parallel clients 3 bytes payload keep alive: 1 86.53% <= 1 milliseconds 97.54% <= 2 milliseconds 98.95% <= 3 milliseconds 99.41% <= 4 milliseconds 99.64% <= 5 milliseconds 99.79% <= 6 milliseconds 99.80% <= 7 milliseconds 99.85% <= 8 milliseconds 99.88% <= 10 milliseconds 99.89% <= 12 milliseconds 99.89% <= 13 milliseconds 99.91% <= 14 milliseconds 99.93% <= 15 milliseconds 99.94% <= 59 milliseconds 99.98% <= 70 milliseconds 100.00% <= 70 milliseconds 38328.86 requests per second ====== MSET (10 keys) ====== 100000 requests completed in 2.83 seconds 50 parallel clients 3 bytes payload keep alive: 1 82.10% <= 1 milliseconds 95.96% <= 2 milliseconds 98.25% <= 3 milliseconds 98.87% <= 4 milliseconds 99.25% <= 5 milliseconds 99.34% <= 6 milliseconds 99.48% <= 7 milliseconds 99.49% <= 8 milliseconds 99.57% <= 9 milliseconds 99.60% <= 10 milliseconds 99.65% <= 11 milliseconds 99.76% <= 12 milliseconds 99.77% <= 13 milliseconds 99.83% <= 14 milliseconds 99.85% <= 17 milliseconds 99.86% <= 18 milliseconds 99.88% <= 19 milliseconds 99.88% <= 24 milliseconds 99.89% <= 33 milliseconds 99.91% <= 34 milliseconds 99.94% <= 48 milliseconds 99.95% <= 52 milliseconds 99.95% <= 63 milliseconds 99.97% <= 64 milliseconds 100.00% <= 64 milliseconds 35298.27 requests per second
测试结果分析:并发模拟Redis一列列的命令执行发送请求响应的时间