Redis集群


1:安装redis cluster 以及依赖
  1.安装zlib
       

wget:http://zlib.net/zlib-1.2.8.tar.gz
            tar -zxvf zlib-1.2.8.tar.gz
            cd zlib-1.2.8
            ./configure
            make
            make install

 

   2.安装redis
     

wget:http://download.redis.io/redis-stable.tar.gz
            tar -zxvf redis-stable.tar.gz
           cd redis-stable
          make
          make install

 

  3.安装ruby
           

 gem sources -a  http://ruby.taobao.org/            
            yum install rubygems
            gem install redis

 
2:配置redis
  1. 创建节点目录 
           为了区分master 和slave ,slave比master 多1000
    

mkdir /server/cluster/6380 6381 6382 7380 7381 7382

 

通用配置
 
    

mkdir -p /server/server/cluster/redis-common.conf

 

    daemonize yes
    #守护进程运行(后台运行)
   

 tcp-backlog 511  
    timeout 0  
    tcp-keepalive 0  
    loglevel notice  
    databases 16  
    dir /opt/redis/data  
    slave-serve-stale-data yes  

 
    #slave只读 
   

 slave-read-only yes  

 
    #not use default 
   

 repl-disable-tcp-nodelay yes  
    slave-priority 100  

 
    #打开aof持久化 
    

appendonly yes  

 
    #每秒一次aof写 
    

appendfsync everysec  

 
    #关闭在aof rewrite的时候对新的写操作进行fsync 
   

 no-appendfsync-on-rewrite yes  
    auto-aof-rewrite-min-size 64mb  
    lua-time-limit 5000  

 
    #打开redis集群 
   

 cluster-enabled yes  

 
    #节点互连超时的阀值 
    

cluster-node-timeout 15000  
    cluster-migration-barrier 1  
    slowlog-log-slower-than 10000  
    slowlog-max-len 128  
    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  
    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

 

特殊配置
           记得更改port

      

 vim /server/cluster/6380/redis.conf
 vim /server/cluster/6381/redis.conf
 vim /server/cluster/6382/redis.conf
 vim /serve/cluster/7380/redis.conf
 vim /server/cluster/7381/redis.conf
 vim /server/cluster/6382/redis.conf

 
#包含通用配置 

include /server/cluster/redis-common.conf  

 
#监听tcp端口 

port 6379  

 
#最大可用内存 

maxmemory 100m 

 
#内存耗尽时采用的淘汰策略: 

# volatile-lru -> remove the key with an expire set using an LRU algorithm  
# allkeys-lru -> remove any key accordingly to the LRU algorithm  
# volatile-random -> remove a random key with an expire set  
# allkeys-random -> remove a random key, any key  
# volatile-ttl -> remove the key with the nearest expire time (minor TTL)  
# noeviction -> don't expire at all, just return an error on write operations  
maxmemory-policy allkeys-lru  

 
#aof存储文件 

appendfilename "appendonly-6379.aof"  

 
#不开启rdb存储,只用于添加slave过程 

dbfilename dump-6379.rdb  

 
#cluster配置文件(启动自动生成) 

cluster-config-file nodes-6379.conf  

 
#部署在同一机器的redis实例,把auto-aof-rewrite搓开,因为cluster环境下内存占用基本一致. 
#防止同意机器下瞬间fork所有redis进程做aof rewrite,占用大量内存(ps:cluster必须开启aof) 

auto-aof-rewrite-percentage 80-100

 

2:创建集群
         1.启动redis服务

    redis-server /server/cluster/6380/redis.conf    
    redis-server /server/cluster/6381/redis.conf
    redis-server /server/cluster/6382/redis.conf
    redis-server /server/cluster/7380/redis.conf
    redis-server /server/cluster/7381/redis.conf
    redis-server /server/cluster/7382/redis.conf

 
        2.创建集群

    

 cd /server/server/redis-stable/src
     ./redis-trib.rb create --replicas 1 host:6380 host:6381 host:6382 host:7380.....

 
     #这里注意redis官方翻译过来的集群教程,使用的是127.0.0.1:prot 实际很坑爹,
     #如果你使用的是java并且使用JedisCluster API来访问
     #会报错:too many redis cluster redirection。
     #比如我的本机ip为123.133.122.141,那么就应该使用123.133.122.141:prot
     #而非127.0.0.1 :port
  命令的意义如下:
给定 redis-trib.rb 程序的命令是 create , 这表示我们希望创建一个新的集群。
选项 --replicas 1 表示我们希望为集群中的每个主节点创建一个从节点。
之后跟着的其他参数则是实例的地址列表, 我们希望程序使用这些地址所指示的实例来创建新集群。
     以上命令的意思就是让 redis-trib 程序创建一个包含三个主节点和三个从节点的集群。
    redis-trib 会打印出一份预想中的配置给你看, 如果你觉得没问题的话, 就可以输入 yes ,redis-trib 就会将这份配置应用到集群当中。
      查看集群信息

redis-cli -p 6380 -c
127.0.0.1:6380>cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_sent:19161
cluster_stats_messages_received:19161

 
可以看到当前cluster_state 是ok的


     使用./redis-trib.rb check host port  检查状态

    

./redis-trib.rb check 115.28.138.150:6380
    Connecting to node 115.28.138.150:6380: OK
    Connecting to node 115.28.138.150:7380: OK
    Connecting to node 115.28.138.150:7382: OK
    Connecting to node 115.28.138.150:7381: OK
    Connecting to node 115.28.138.150:6382: OK
    Connecting to node 115.28.138.150:6381: OK
    >>> Performing Cluster Check (using node 115.28.138.150:6380)
    M: 3046c8e6efbcea5c77e90483d97b928c5e4fbe05 115.28.138.150:6380
       slots:0-5460 (5461 slots) master
       1 additional replica(s)
    S: 2f045aaecb9addea7ae8ef636e3a5415686889e4 115.28.138.150:7380
       slots: (0 slots) slave
       replicates 3046c8e6efbcea5c77e90483d97b928c5e4fbe05
    S: c09ffe456b44abedd7ba811aaad470b3f7a36ea5 115.28.138.150:7382
       slots: (0 slots) slave
       replicates ed7200bf1cdacc18661d70c39270995582559037
    S: 1866624d3330f6491d5c57e269811694bb061759 115.28.138.150:7381
       slots: (0 slots) slave
       replicates fb5c50e062d0e770aa534315fec1aa11488c228a
    M: ed7200bf1cdacc18661d70c39270995582559037 115.28.138.150:6382
       slots:10923-16383 (5461 slots) master
       1 additional replica(s)
    M: fb5c50e062d0e770aa534315fec1aa11488c228a 115.28.138.150:6381
       slots:5461-10922 (5462 slots) master
       1 additional replica(s)
    [OK] All nodes agree about slots configuration.
    >>> Check for open slots...
    >>> Check slots coverage...
    [OK] All 16384 slots covered.

 

 

你可能感兴趣的:(redis)