Chinaunix首页 | 论坛 | 博客
  • 博客访问: 338843
  • 博文数量: 87
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 727
  • 用 户 组: 普通用户
  • 注册时间: 2014-11-27 15:56
个人简介

开心了, 就笑;不开心了,就过会儿再笑。。。。

文章分类

全部博文(87)

文章存档

2017年(16)

2016年(28)

2015年(33)

2014年(10)

我的朋友

分类: IT职场

2015-09-24 15:17:40

前言
官网 />
Redis是一个开源,先进的key-value存储,并用于构建高性能,可扩展的Web应用程序的完美解决方案。

Redis从它的许多竞争继承来的三个主要特点:

  • Redis数据库完全在内存中,使用磁盘仅用于持久性。

  • 相比许多键值数据存储,Redis拥有一套较为丰富的数据类型。

  • Redis可以将数据复制到任意数量的从服务器

一、集群规划
1. 单个redis实例内存不超过10G
2. 根据机器配置确定单台服务器启用实例数
3. 根据可用机器列表进行规划,尽量均衡分布
4. 根据业务数据适当配置机器(尽量前期规划留好足够冗余,减少后期维护成本)

二、程序安装
1. 下载redis集群
wget
/>
2. 解压编译
 tar -xzvf redis-3.0.2.tar.gz
 cd redis-3.0.2
00-RELEASENOTES  conf          deps     Makefile   rebuildDataNodes.sh  redis-cli           redis_old.conf     run.sh           runtest-sentinel  startRedis16590.sh  syncRedis.sh
BUGS             CONTRIBUTING  INSTALL  MANIFESTO  rebuildMediaQ.sh     redis_cluster.conf  redis-trib.rb      runtest          sentinel.conf     startRedis.sh       tests
checkRedis.sh    COPYING       log      README     rebuild.sh           redis.conf          runDataCluster.sh  runtest-cluster  src               stopRedis.sh        utils

 make

3. 修改配置文件
redis持久化内容,后面详细说:
[root@hz_host152 redis-3.0.2]# grep "#save" redis_cluster.conf 
#save 900 1          #900秒内如果超过1个key被修改,则发起快照保存
#save 300 10        #300秒内如果超过10个key被修改,则发起快照保存 
#save 60 10000

[root@hz_host152 redis-3.0.2]# cat redis_cluster.conf |grep -v "#"
daemonize yes ####守护状态
pidfile /var/run/redis_19999.pid          ####实例进程文件
port 19999                                      ####端口,+10000是心跳端口,启动异常时关注这2个端口是否被占用
tcp-backlog 511
timeout 0
tcp-keepalive 0
loglevel notice
logfile "./log/19999.log"                  ####log文件名,一般加上端口识别,异常诊断及日常维护观察用
databases 16
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump_19999.rdb            ####rdb文件名,一般加上端口识别
dir ./ ####工作目录
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
repl-backlog-size 50mb                    ####备节点宕机到恢复修改内容未超过此限制不用全部拷贝rdb,只增量同步
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
cluster-enabled yes
cluster-config-file nodes-19999.conf    ####实例生成的node文件名,以端口号区分开
cluster-node-timeout 120000             ###集群实例通讯超时设置,默认15秒,修改为2分钟,集群越大该值设置越大
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

4.启动脚本
[root@host106 redis-3.0.2]# cat rebuild.sh 
sed -i 's/^save/#save/g' redis_cluster.conf
mkdir -p log

for((i=10594;i<10595;i++))
do
    echo create conf/$i
    mkdir -p conf/$i
    cp -f redis_cluster.conf  conf/$i/redis.conf

    sed -i "s/19999/$i/g"   conf/$i/redis.conf
    ./redis-cli -p $i shutdown nosave

    ./redis-server  conf/$i/redis.conf
done

sleep 5

for((i=10594;i<10595;i++))
do
   ./redis-cli -p $i dbsize
done
[root@host106 redis-3.0.2]#


b. 生成构建集群命令的脚本
[root@hz_host152 redis-3.0.2]# cat run.sh 
IPS="./redis-trib.rb create --replicas 1 "
for((j=10580;j<10585;j++))
do
    for((i=143;i<=174;i++))
    do
IPS="$IPS 192.168.1.$i:$j"
echo 192.168.1.$i:$j
    done
done
for((j=10585;j<10590;j++))
do
    for((i=155;i<=174;i++))
    do
IPS="$IPS 192.168.1.$i:$j"
echo 192.168.1.$i:$j
    done
    for((i=143;i<=154;i++))
    do
IPS="$IPS 192.168.1.$i:$j"
echo 192.168.1.$i:$j
    done
done
echo $IPS
7. 启动redis各实例
[root@hz_host166 redis-3.0.2]# sh ./rebuild.sh
create conf/10580
... ...
Could not connect to Redis at 127.0.0.1:10589: Connection refused
(integer) 0
... ...
(integer) 0
Could not connect to Redis at 127.0.0.1:10585: Connection refused
(integer) 0
... ...

[root@hz_host166 redis-3.0.2]# ps -ef |grep redis|grep 1058|sort -k9
root      5638     1  0 Jul16 ?        00:05:05 ./redis-server *:10580 [cluster]    
root      5646     1  0 Jul16 ?        00:05:08 ./redis-server *:10581 [cluster]    
root      5655     1  0 Jul16 ?        00:04:51 ./redis-server *:10582 [cluster]    
root      5663     1  0 Jul16 ?        00:05:23 ./redis-server *:10583 [cluster]    
root      5671     1  0 Jul16 ?        00:05:09 ./redis-server *:10584 [cluster]    
root      5678     1  0 Jul16 ?        00:04:36 ./redis-server *:10585 [cluster]    
root      5688     1  0 Jul16 ?        00:04:37 ./redis-server *:10586 [cluster]    
root      5696     1  0 Jul16 ?        00:04:36 ./redis-server *:10587 [cluster]    
root      5705     1  0 Jul16 ?        00:04:31 ./redis-server *:10588 [cluster]    
root      5715     1  0 Jul16 ?        00:04:28 ./redis-server *:10589 [cluster]    


####注意事项:redis进程在,但是端口被占用
[root@hz_host166 redis-3.0.2]# cat log/10585.log
27651:M 13 Jul 17:52:05.791 * No cluster configuration found, I'm eba6621c4656873b26899f437fd450bf619b560f
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 3.0.2 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in cluster mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 10585
 |    `-._   `._    /     _.-'    |     PID: 27651
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               


27651:M 13 Jul 17:52:05.795 # Server started, Redis version 3.0.2
27651:M 13 Jul 17:52:05.795 # 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.
27651:M 13 Jul 17:52:05.795 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
27651:M 13 Jul 17:52:05.795 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
27651:M 13 Jul 17:52:05.795 * The server is now ready to accept connections on port 10585


i. 推荐配置
sysctl vm.overcommit_memory=1


ii. 端口连接其他redis的处理
[root@hz_host146 redis-3.0.2]# netstat -ntp |grep 10582
tcp        0      0 192.168.1.146:10582         192.168.1.149:14591         ESTABLISHED 30741/Dmap-media-ba 
You have mail in /var/spool/mail/root
[root@hz_host146 redis-3.0.2]# ./redis-cli -h  192.168.1.149 -p 14591 client kill 192.168.1.146:10582 
OK
[root@hz_host146 redis-3.0.2]# netstat -ntp |grep 10582

[root@hz_host146 redis-3.0.2]# ./redis-server ./conf/10582/redis.conf 

####检测信息启动的redis是否正常
[root@hz_host146 redis-3.0.2]# ./redis-cli -p 10582 dbsize
(integer) 0


####到此确认节点正常


8. 构建集群
i. 完整构建一主一备集群
生成创建集群脚本的命令
 sh ./run.sh
 
 将命令拷贝执行
 ./redis-trib.rb create --replicas 1 192.168.1.143:10580 192.168.1.144:10580 192.168.1.145:10580 
... ...
 /usr/bin/env: ruby: No such file or directory
[root@hz_host174 redis-3.0.2]# yum install ruby




./redis-trib.rb:24:in `require': no such file to load -- rubygems (LoadError)
from ./redis-trib.rb:24
[root@hz_host152 redis-3.0.2]#  yum install rubygems 

/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require': no such file to load -- redis (LoadError)
from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
from ./redis-trib.rb:25
[root@hz_host152 redis-3.0.2]#  gem install redis-3.1.0.gem 
Successfully installed redis-3.1.0
1 gem installed
Installing ri documentation for redis-3.1.0...
Installing RDoc documentation for redis-3.1.0...

执行sh run.sh  生成的命令,中途有输入yes确认节点,然后等待.....创建完成。


阅读(1291) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~