save 900 1 #15分钟1条 save 300 1000 #5分钟1000条 save 60 10000 #1分钟1万条
假设距离上一次保存后的52秒写了9999条,那么就会全部丢了
可以使用AOF,有点类似mysql的二进制的日志,redis写一条记一条。
redis集群搭建步骤
1.安装软件
[root@mastera0 redis]# cp redis-3.0.7.tar.gz /usr/local [root@mastera0 redis]# cd /usr/local/ [root@mastera0 local]# ls bin etc games include lib lib64 libexec redis-3.0.7.tar.gz sbin share src [root@mastera0 local]# tar -xf redis-3.0.7.tar.gz [root@mastera0 local]# ls bin etc games include lib lib64 libexec redis-3.0.7 redis-3.0.7.tar.gz sbin share src
port 6380 pidfile /var/run/redis6380.pid dir /app/redis_cluster/node6380 cluster-enabled yes cluster-config-file node6380.conf cluster-node-timeout 5000
[root@mastera0 redis]# vim create_redis_conf.sh #!/bin/bash for i in `seq $1 $2` do cat > $3/redis$i.conf << ENDF daemonize no pidfile /var/run/redis$i.pid port $i tcp-backlog 511 timeout 0 tcp-keepalive 0 loglevel notice logfile "" databases 16 save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename dump.rdb dir /app/redis_cluster/node$i slave-serve-stale-data yes slave-read-only yes repl-diskless-sync no repl-diskless-sync-delay 5 repl-disable-tcp-nodelay no slave-priority 100 appendonly no appendfilename "appendonly.aof" appendfsync everysec no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb aof-load-truncated 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-entries 512 list-max-ziplist-value 64 set-max-intset-entries 512 zset-max-ziplist-entries 128 zset-max-ziplist-value 64 hll-sparse-max-bytes 3000 activerehashing yes client-output-buffer-limit normal 0 0 0 client-output-buffer-limit slave 256mb 64mb 60 client-output-buffer-limit pubsub 32mb 8mb 60 hz 10 aof-rewrite-incremental-fsync yes cluster-enabled yes cluster-config-file node$i.conf cluster-node-timeout 5000 ENDF done
[root@mastera0 redis]# bash create_redis_conf.sh 6380 6384 /app/redis_cluster/conf [root@mastera0 redis]# ll /app/redis_cluster/conf total 24 -rw-r--r--. 1 root root 1232 Dec 17 12:57 redis6380.conf -rw-r--r--. 1 root root 1232 Dec 17 12:57 redis6381.conf -rw-r--r--. 1 root root 1232 Dec 17 12:57 redis6382.conf -rw-r--r--. 1 root root 1232 Dec 17 12:57 redis6383.conf -rw-r--r--. 1 root root 1232 Dec 17 12:57 redis6384.conf -rw-r--r--. 1 root root 1232 Dec 17 13:27 redis6385.conf
[root@mastera0 redis]# pwd /usr/local/redis [root@mastera0 redis]# cd /app/redis_cluster/ [root@mastera0 redis_cluster]# ls conf node6380 node6381 node6382 node6383 node6384
3.启动redis服务
[root@mastera0 redis_cluster]# redis-server /app/redis_cluster/conf/node6380.conf 29837:M 17 Dec 13:02:36.216 * Increased maximum number of open files to 10032 (it was originally set to 1024). 29837:M 17 Dec 13:02:36.226 * No cluster configuration found, I'm d3609c8b68dcc4884501d14ec7803eef6223513b _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 3.0.7 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in cluster mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 6380 | `-._ `._ / _.-' | PID: 29837 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | http://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-'
29837:M 17 Dec 13:02:36.422 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128. 29837:M 17 Dec 13:02:36.422 # Server started, Redis version 3.0.7 29837:M 17 Dec 13:02:36.422 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect. 29837:M 17 Dec 13:02:36.423 * The server is now ready to accept connections on port 6380 ^C29837:signal-handler (1481950975) Received SIGINT scheduling shutdown... 29837:M 17 Dec 13:02:55.685 # User requested shutdown... 29837:M 17 Dec 13:02:55.686 * Saving the final RDB snapshot before exiting. 29837:M 17 Dec 13:02:55.850 * DB saved on disk 29837:M 17 Dec 13:02:55.850 # Redis is now ready to exit, bye bye...
[root@mastera0 redis_cluster]# vim /etc/sysctl.conf vm.overcommit_memory = 1 [root@mastera0 redis_cluster]# sysctl -a
[root@mastera0 redis_cluster]# redis-server /app/redis_cluster/conf/node6380.conf 29861:M 17 Dec 13:04:39.478 * Increased maximum number of open files to 10032 (it was originally set to 1024). 29861:M 17 Dec 13:04:39.479 * Node configuration loaded, I'm d3609c8b68dcc4884501d14ec7803eef6223513b _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 3.0.7 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in cluster mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 6380 | `-._ `._ / _.-' | PID: 29861 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | http://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-'
29861:M 17 Dec 13:04:39.480 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128. 29861:M 17 Dec 13:04:39.480 # Server started, Redis version 3.0.7 29861:M 17 Dec 13:04:39.480 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect. 29861:M 17 Dec 13:04:39.480 * DB loaded from disk: 0.000 seconds 29861:M 17 Dec 13:04:39.480 * The server is now ready to accept connections on port 6380
测试1台成功后,同时启动6台
4.安装ruby rubygems
目的是使用gem工具安装redis.gem
ruby,rhel6中自带,而rubygems没有,需要下载
[root@mastera0 redis_cluster]# cd [root@mastera0 ~]# ls anaconda-ks.cfg Downloads Music rubygems-1.3.7-5.el6.noarch.rpm Desktop install.log Pictures Templates Documents install.log.syslog Public Videos [root@mastera0 ~]# yum install -y ruby [root@mastera0 ~]# rpm -ivh rubygems-1.3.7-5.el6.noarch.rpm warning: rubygems-1.3.7-5.el6.noarch.rpm: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY error: Failed dependencies: ruby-rdoc is needed by rubygems-1.3.7-5.el6.noarch [root@mastera0 ~]# yum localinstall -y rubygems-1.3.7-5.el6.noarch.rpm
[root@mastera0 ~]# cp /mnt/lesson9/redis/redis-3.0.7.gem ~ [root@mastera0 ~]# ls anaconda-ks.cfg install.log Public Videos Desktop install.log.syslog redis-3.0.7.gem Documents Music rubygems-1.3.7-5.el6.noarch.rpm Downloads Pictures Templates [root@mastera0 ~]# gem install redis-3.0.7.gem Successfully installed redis-3.0.7 1 gem installed Installing ri documentation for redis-3.0.7... Installing RDoc documentation for redis-3.0.7...
5.启动reids集群
至少 3 master
此处写了一个小脚本来实现多个实例启动关闭
[root@mastera0 redis]# cat redis_multi_ctl.sh #!/bin/bash redis_conf_dir="/app/redis_cluster/conf" redis_data_dir="/app/redis_cluster" ALL_start() { for i in `seq 6380 6385` do redis-server $redis_conf_dir/redis$i.conf &> $redis_data_dir/redis$i.log & done }
ALL_stop() { for i in `ps -ef|grep redis|grep cluster |awk '{print $2}'` do kill -9 $i done }
[root@mastera0 redis]# redis-cli -c -p 6380 127.0.0.1:6380> set name booboo -> Redirected to slot [5798] located at 127.0.0.1:6381 OK 127.0.0.1:6381> set age 99 -> Redirected to slot [741] located at 127.0.0.1:6380 OK 127.0.0.1:6380> set addr shagnhai -> Redirected to slot [12790] located at 127.0.0.1:6382 OK 127.0.0.1:6382> set tel 10086 -> Redirected to slot [7485] located at 127.0.0.1:6381 OK 127.0.0.1:6381> get name "booboo" 127.0.0.1:6381> get tel "10086" 127.0.0.1:6381> get age -> Redirected to slot [741] located at 127.0.0.1:6380 "99" 127.0.0.1:6380> get addr -> Redirected to slot [12790] located at 127.0.0.1:6382 "shagnhai" 127.0.0.1:6382> exit [root@mastera0 redis]# redis-cli -p 6380 127.0.0.1:6380> get name (error) MOVED 5798 127.0.0.1:6381 127.0.0.1:6380> get age "99" 127.0.0.1:6380> get addr (error) MOVED 12790 127.0.0.1:6382 127.0.0.1:6380> get tel (error) MOVED 7485 127.0.0.1:6381 127.0.0.1:6380> exit
8.哈希槽的配置
redis-cli
集群命令
CLUSTER INFO 打印集群的信息
CLUSTER NODES 列出集群当前已知的所有节点( node ),以及这些节点的相关信息。
节点
CLUSTER MEET 将 ip 和 port 所指定的节点添加到集群当中,让它成为集群的一份子。
[root@mastera0 redis]# redis-cli -p 6380 cluster slots (empty list or set)
0-16383 (16383是15 * 2^10)
通过cluster addslots来为节点分配槽
如果平均分配,则为每个node为5461个槽
0-5460
5461-10922
10923-16383
我有三个master,分配如下:
节点
node6380
6382
6384
槽范围
0-15999
16000-16200
16201-16383
槽数量
16000
200
182
[root@mastera0 redis]# for i in `seq 0 16000`;do redis-cli -p 6380 cluster addslots $i;done [root@mastera0 redis]# for i in `seq 16001 16200`;do redis-cli -p 6382 cluster addslots $i;done [root@mastera0 redis]# for i in `seq 16201 16383`;do redis-cli -p 6384 cluster addslots $i;done
[root@mastera0 redis]# redis-cli -c -p 6384 127.0.0.1:6384> get 1 -> Redirected to slot [9842] located at 127.0.0.1:6380 "1" 127.0.0.1:6380> get 2 "2" 127.0.0.1:6380> get 3 "3" 127.0.0.1:6380> get 5 "5" 127.0.0.1:6380> get 100 "100" 127.0.0.1:6380> get 1000 "1000" 127.0.0.1:6380> get 10000 "10000" 127.0.0.1:6380> exit
redis数据迁移
实验目标:将9842槽(6380实例)中的数据key 1迁移到6382实例中
通过dbsize来判断数据是否丢失
数据迁移之前的总大小为30000
[root@mastera0 redis]# for i in 6380 6382 6384;do redis-cli -p $i dbsize;done|awk 'BEGIN{sum=0};{sum=$1+sum};END{print sum}' 30000
查看要迁移的数据在哪个槽上
1.get的时候返回信息
[root@mastera0 redis]# redis-cli -c -p 6384 127.0.0.1:6384> get 1 -> Redirected to slot [9842] located at 127.0.0.1:6380 "1"