安裝參照 Docker 搭建redis 集羣html
啓動服務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
本地客戶端查看docker
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併發測試segmentfault
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
關掉一個從服務: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 [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
容器再開啓用來生效配置。併發
換一臺機子搭建從服務器。tcp
用阿里的一個閒置服務器,這裏就不用docker了,玩不太懂,仍是乖乖yum安裝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
便可關閉。
配置中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不能寫,只能讀。
先關閉調主服務器以前建的三個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,但是性能怎麼沒有提高麼,讀也沒有提高多少啊,多是我測試的姿式不對?仍是服務器太渣。之後再深究吧。
感謝你閱讀到了這裏,這篇筆記太凌亂又因鄙人不才,本不成文章,但是放到私人筆記裏也幫助不到任何人(包括本身),請多多批評。