redis集群尝试

1. 使用Docker搭建redis主从复制集群

安装参照 Docker 搭建redis 集群

启动服务

docker run --name redis-master -p 6379:6379 -d redis
docker run --link redis-master:redis-master -v /data/docker/redis/redis.conf:/usr/local/etc/redis/redis.conf --name redis-slave1 -d redis redis-server /usr/local/etc/redis/redis.conf
docker run --link redis-master:redis-master -v /data/docker/redis/redis.conf:/usr/local/etc/redis/redis.conf --name redis-slave2 -d redis redis-server /usr/local/etc/redis/redis.conf
docker run --link redis-master:redis-master -v /data/docker/redis/redis.conf:/usr/local/etc/redis/redis.conf --name redis-slave3 -d redis redis-server /usr/local/etc/redis/redis.conf

本地客户端查看

D:\redis\redis-cli.exe -h xxx.xxx.xxx.xxx -p 6379
xxx.xxx.xxx.xxx:6379> info
# Replication
role:master
connected_slaves:3
slave0:ip=172.17.0.4,port=6379,state=online,offset=58278,lag=0
slave1:ip=172.17.0.5,port=6379,state=online,offset=58278,lag=0
slave2:ip=172.17.0.3,port=6379,state=online,offset=58278,lag=1
master_replid:b8a0697619cb49c9e82cdf57d404b998e7f92615
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:58278
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:58278

性能测试,10000次太慢,100并发测试

PS E:\> D:\redis\redis-benchmark.exe -h xxx.xxx.xxx.xxx -p 6379 -n 100 -q
PING_INLINE: 813.01 requests per second
PING_BULK: 813.01 requests per second
SET: 840.34 requests per second
GET: 746.27 requests per second
INCR: 819.67 requests per second
LPUSH: 724.64 requests per second
RPUSH: 793.65 requests per second
LPOP: 833.33 requests per second
RPOP: 806.45 requests per second
SADD: 806.45 requests per second
SPOP: 826.45 requests per second
LPUSH (needed to benchmark LRANGE): 819.67 requests per second
LRANGE_100 (first 100 elements): 684.93 requests per second
LRANGE_300 (first 300 elements): 49.07 requests per second
LRANGE_500 (first 450 elements): 23.69 requests per second
LRANGE_600 (first 600 elements): 22.81 requests per second
MSET (10 keys): 819.67 requests per second

关掉一个从服务:

#查看所有运行容器
$ docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
0ab06a9f786a        redis               "docker-entrypoint..."   34 minutes ago      Up 34 minutes       6379/tcp                 redis-slave3
8bfdd1cd2e60        redis               "docker-entrypoint..."   34 minutes ago      Up 34 minutes       6379/tcp                 redis-slave2
89fc61fb4804        redis               "docker-entrypoint..."   36 minutes ago      Up 36 minutes       6379/tcp                 redis-slave1
e32817e5e65c        redis               "docker-entrypoint..."   40 minutes ago      Up 6 minutes        0.0.0.0:6379->6379/tcp   redis-master
[root@VM_128_25_centos ~]# docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
0ab06a9f786a        redis               "docker-entrypoint..."   34 minutes ago      Up 34 minutes       6379/tcp                 redis-slave3
8bfdd1cd2e60        redis               "docker-entrypoint..."   34 minutes ago      Up 34 minutes       6379/tcp                 redis-slave2
89fc61fb4804        redis               "docker-entrypoint..."   36 minutes ago      Up 36 minutes       6379/tcp                 redis-slave1
e32817e5e65c        redis               "docker-entrypoint..."   40 minutes ago      Up 6 minutes        0.0.0.0:6379->6379/tcp   redis-master

#停到一个从服务,可用name也可以用id
$ docker stop redis-slave3
redis-slave3

#再次查看确实停掉了
$ docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
8bfdd1cd2e60        redis               "docker-entrypoint..."   35 minutes ago      Up 35 minutes       6379/tcp                 redis-slave2
89fc61fb4804        redis               "docker-entrypoint..."   37 minutes ago      Up 37 minutes       6379/tcp                 redis-slave1
e32817e5e65c        redis               "docker-entrypoint..."   42 minutes ago      Up 7 minutes        0.0.0.0:6379->6379/tcp   redis-master

再次测试发现区别不大, docker start redis-slave3启动刚才停掉的容器。

$  docker start redis-slave3
redis-slave3
$docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
0ab06a9f786a        redis               "docker-entrypoint..."   3 hours ago         Up 1 second         6379/tcp                 redis-slave3
8bfdd1cd2e60        redis               "docker-entrypoint..."   3 hours ago         Up 3 hours          6379/tcp                 redis-slave2
89fc61fb4804        redis               "docker-entrypoint..."   3 hours ago         Up 3 hours          6379/tcp                 redis-slave1
e32817e5e65c        redis               "docker-entrypoint..."   3 hours ago         Up About an hour    0.0.0.0:6379->6379/tcp   redis-master

看到,又有了第三个从服务redis-slave3
p.s. 修改了配置/data/docker/redis.conf后,可以关掉redis-master容器再开启用来生效配置。

换一台机子搭建从服务器。

2. 配置另一台服务器为从服务器

用阿里的一个闲置服务器,这里就不用docker了,玩不太懂,还是乖乖yum安装redis

2.1 安装配置redis

#安装
yum install redis
#启动
service redis start
#配置文件在 
/etc/redis.conf

安装redis之后,发现客户端连接无反应,配置规则并关闭防火墙后,,提醒redis受保护模式拒绝访问,配置中 daemonize no 改为yes,再次访问发现奇怪的错误在驱动器 %1 上插入软盘,redis添加密码 requirepass 123456,解决错误。

客户端再次访问redis-cli.exe -h yyy.yyy.yyy.yyy -p 6379 -a 123456,访问成功。

可以用service redis restart重启,但是无法用service redis stop停止,可能是有密码的原因,可以进客户端然后shutdown掉

$ redis-cli -a 123456  
127.0.0.1:6379> SHUTDOWN  
not connected> exit  

也可以直接用redis-cli -p 6380 -a 123456 shutdown删除,这里带-p是因为我改了端口,默认为6379则不用-p,如果没密码的话,那也不用-a直接redis-cli shutdown即可关闭。

2.2 配置从服务器

配置中slaveof <masterip> <masterport>改为主服务器ip
slaveof xxx.xxx.xxx.xxx 6379
试了好久无法获取到master的值,查看redis日志

37358 22528:S 03 May 18:08:23.521 * MASTER <-> SLAVE sync: Loading DB in memory
37359 22528:S 03 May 18:08:23.521 # Can't handle RDB format version 8
37360 22528:S 03 May 18:08:23.521 # Failed trying to load the MASTER synchronization DB from disk

查看两个redis版本

从服务器直接安装的redis,查看很简单

$redis-server --version
Redis server v=3.2.11 sha=00000000:0 malloc=jemalloc-3.6.0 bits=64 build=6ad59

主服务器docker下版本查看

$docker run redis redis-server --version
Redis server v=4.0.9 sha=00000000:0 malloc=jemalloc-4.0.3 bits=64 build=22c6ee

是不一样,怎么办呢,搜了几个,都是删除rdb文件,我删了也不行。再安装一次redis吧。

从服务器再次安装redis 4.0.9,安装并再次配置后客户端连接会有错误
Error: Server closed the connection,配置更改注释bind也没用,发现没有指定文件,再次启动redis
./src/redis-server /root/redis-4.0.9/redis.conf
,可以连接并可访问主服务器数据。

试验一下,xxx为主服务器IP,yyy为从服务器IP:

PS E:\> D:\redis\redis-cli.exe -h xxx.xxx.xxx.xxx -p 6379
xxx.xxx.xxx.xxx:6379> set day 3
OK

PS E:\> D:\redis\redis-cli.exe -h yyy.yyy.yyy.yyy -p 6379 -a 123456
yyy.yyy.yyy.yyy:6379> get day
"3"
yyy.yyy.yyy.yyy:6379> set day2 111
(error) READONLY You can't write against a read only slave.
yyy.yyy.yyy.yyy:6379>

可以看到从服务器slave不能写,只能读。

3. 性能测试

先关闭调主服务器之前建的三个slave,

$ docker stop redis-slave1
redis-slave
$ docker stop redis-slave2
redis-slave2
$ docker stop redis-slave3
redis-slave3

开始测试,
没有使用从服务器的slave时,

PS E:\> D:\redis\redis-benchmark.exe -h xxx.xxx.xxx.xxx -p 6379 -n 1000 -q
PING_INLINE: 1204.82 requests per second
PING_BULK: 1191.90 requests per second
SET: 1219.51 requests per second
GET: 1213.59 requests per second
INCR: 1209.19 requests per second
LPUSH: 1210.65 requests per second
RPUSH: 1183.43 requests per second
LPOP: 1197.60 requests per second
RPOP: 1222.49 requests per second
SADD: 1144.16 requests per second
SPOP: 1210.65 requests per second
LPUSH (needed to benchmark LRANGE): 1215.07 requests per second
LRANGE_100 (first 100 elements): 102.64 requests per second
LRANGE_300 (first 300 elements): 29.06 requests per second
LRANGE_500 (first 450 elements): 25.27 requests per second
LRANGE_600 (first 600 elements): 15.88 requests per second
MSET (10 keys): 1201.92 requests per second

启动从服务器,使用从服务器slave

PS E:\> D:\redis\redis-benchmark.exe -h xxx.xxx.xxx.xxx -p 6379 -n 1000 -q
PING_INLINE: 1184.83 requests per second
PING_BULK: 1207.73 requests per second
SET: 1203.37 requests per second
GET: 1156.07 requests per second
INCR: 1184.83 requests per second
LPUSH: 1203.37 requests per second
RPUSH: 1201.92 requests per second
LPOP: 1223.99 requests per second
RPOP: 1200.48 requests per second
SADD: 1179.25 requests per second
SPOP: 1132.50 requests per second
LPUSH (needed to benchmark LRANGE): 1190.48 requests per second
LRANGE_100 (first 100 elements): 103.00 requests per second
LRANGE_300 (first 300 elements): 30.63 requests per second
LRANGE_500 (first 450 elements): 26.52 requests per second
LRANGE_600 (first 600 elements): 14.22 requests per second
MSET (10 keys): 1233.05 requests per second

查看主服务器信息

info
# Replication
role:master
connected_slaves:1
slave0:ip=yyy.yyy.yyy.yyy,port=6379,state=online,offset=814862,lag=1
master_replid:600f93df98b7eff6799a0e0cdb6c6d85fd401793
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:814862
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:814862

看到slave0就是从服务器的slave,可是性能怎么没有提升么,读也没有提升多少啊,可能是我测试的姿势不对?还是服务器太渣。以后再深究吧。

感谢你阅读到了这里,这篇笔记太凌乱又因鄙人不才,本不成文章,可是放到私人笔记里也帮助不到任何人(包括自己),请多多批评。

其他参考

posted @ 2018-05-03 19:29  姜小豆  阅读(640)  评论(0编辑  收藏  举报