1、Redis 集羣環境的搭建

前言:

       csdn上第一篇博客,打算說說redis的集羣 !!!
redis在3.0版本以後增加了集羣特性,從以前的主從複製有了質的提升。


集羣環境的搭建

首先聲明一下個人的環境:mac + centos(虛擬機),環境搭建在centos上,mac作爲客戶端調用。redis的集羣需要依賴ruby環境,首先安裝 zlib 、ruby

1.zlib 的安裝

[root@localhost ~]# yum install zlib

2.ruby的安裝

[root@localhost ~]# yum install ruby

3.redis的安裝

[root@localhost software]# mkdir redis
[root@localhost software]# wget http://download.redis.io/redis-stable.tar.gz
[root@localhost software]# tar xvzf redis-stable.tar.gz
[root@localhost software]# make && make test

安裝完成以後執行如下命令:

redis:[root@localhost software]# whereis redis-cli
redis-cli: /usr/local/bin/redis-cli

查看redis的相關命令所在位置

執行:

[root@localhost software]# <strong>redis-server </strong>
13237:C 13 Aug 20:48:24.171 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
13237:M 13 Aug 20:48:24.171 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 3.0.3 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 13237
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

13237:M 13 Aug 20:48:24.172 # Server started, Redis version 3.0.3
13237:M 13 Aug 20:48:24.173 # 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.
13237:M 13 Aug 20:48:24.173 # 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.
13237:M 13 Aug 20:48:24.174 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
13237:M 13 Aug 20:48:24.174 * The server is now ready to accept connections on port 6379

表明redis安裝成功!  

接下來開始redis cluster環境的搭建 ~~~

 · 在redis目錄下/root/software/redis-3.0.3 創建my-redis-cluster目錄,在目錄下創建如下文件:

[root@localhost my-redis-cluster]# pwd
/root/software/redis-3.0.3/my-redis-cluster
[root@localhost my-redis-cluster]# ls
node-7000.conf  node-7001.conf  node-7002.conf  node-7003.conf  node-7004.conf  node-7005.conf

node-7000.conf 文件內容如下:

[root@localhost my-redis-cluster]# cat node-7000.conf 
pidfile /root/software/redis-3.0.3/pid/node7000.pid
logfile "/root/software/redis-3.0.3/logs/node-7000.log"
dir /root/software/redis-3.0.3/data/node-7000
port <span style="color:#ff0000;">7000</span>
daemonize yes
cluster-enabled <span style="color:#ff0000;"><strong>yes</strong></span>
cluster-config-file node-7000.conf
cluster-node-timeout 5000
appendonly yes

大概解釋下: 開啓集羣、監聽端口...這個文件的內容比較簡單,其他node-700x.conf 文件類似,就不一一羅列

執行如下操作,創建data目錄以及子目錄
[root@localhost my-redis-cluster]# cd ..
[root@localhost redis-3.0.3]# ls
00-RELEASENOTES  COPYING  dump.rdb  Makefile          pid         runtest           sentinel.conf  utils
BUGS             data     INSTALL   MANIFESTO         README      runtest-cluster   src
CONTRIBUTING     deps     logs      my-redis-cluster  redis.conf  runtest-sentinel  tests
[root@localhost redis-3.0.3]# cd data
[root@localhost data]# ls
<strong><span style="color:#ff0000;">node-7000  node-7001  node-7002  node-7003  node-7004  node-7005</span></strong>
下一步開始啓動這6個node 

[root@localhost my-redis-cluster]# redis-server node-7000.conf
[root@localhost my-redis-cluster]# redis-server node-7001.conf
[root@localhost my-redis-cluster]# redis-server node-7002.conf
[root@localhost my-redis-cluster]# redis-server node-7003.conf
[root@localhost my-redis-cluster]# redis-server node-7004.conf
[root@localhost my-redis-cluster]# redis-server node-7005.conf

每個Node爲守護進程形式啓動,在控制檯無打印信息,可在logs目錄下查看啓動日誌信息,注意,這裏的logs目錄爲手動創建的。

查看Node Process
[root@localhost my-redis-cluster]# ps -ef|grep redis
root     12937     1  0 20:22 ?        00:00:00 redis-server *:7000 [cluster]
root     12941     1  1 20:22 ?        00:00:00 redis-server *:7001 [cluster]
root     12945     1  1 20:22 ?        00:00:00 redis-server *:7002 [cluster]
root     12949     1  0 20:22 ?        00:00:00 redis-server *:7003 [cluster]
root     12953     1  0 20:22 ?        00:00:00 redis-server *:7004 [cluster]
root     12957     1  2 20:23 ?        00:00:00 redis-server *:7005 [cluster]

從上發現Node都成功啓動,但此時每個Node都是相互獨立的,需要講各個Node join cluster

        Nodes Join Cluster   節點加入進去。 進入redis下的src目錄,執行如下命令

./redis-trib.rb create --replicas 1 192.168.1.103:7000 192.168.1.103:7001 192.168.1.103:7002 192.168.1.103:7003 192.168.1.103:7004 192.168.1.103:7005 
Note:這裏不要使用localhost或者127.0.0.1,192.168.1.103是centos所在的ip地址,如果使用localhost或127.0.0.1,在mac上(也就是局域網內)是沒有辦法獲取redis cluster的鏈接信息的,因爲在cluster
中,使用的是加入集羣的ip去通信的。
replicas 1 表示爲每個master添加一個slave

執行完以上命令後,得到如下信息:


[root@localhost src]# ./redis-trib.rb create --replicas 1 192.168.1.103:7000 192.168.1.103:7001 192.168.1.103:7002 192.168.1.103:7003 192.168.1.103:7004 192.168.1.103:7005 
>>> Creating cluster
Connecting to node 192.168.1.103:7000: OK
Connecting to node 192.168.1.103:7001: OK
Connecting to node 192.168.1.103:7002: OK
Connecting to node 192.168.1.103:7003: OK
Connecting to node 192.168.1.103:7004: OK
Connecting to node 192.168.1.103:7005: OK
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.1.103:7000
192.168.1.103:7001
192.168.1.103:7002
Adding replica 192.168.1.103:7003 to 192.168.1.103:7000
Adding replica 192.168.1.103:7004 to 192.168.1.103:7001
Adding replica 192.168.1.103:7005 to 192.168.1.103:7002
<strong><span style="color:#ff0000;">M</span></strong>: <span style="background-color: rgb(51, 255, 255);">4bc092eb4731152d15172b065c74c7a795fe6304</span> 192.168.1.103:7000
   slots:<span style="color:#ff0000;"><strong>0-5460</strong></span> (5461 slots) <strong><span style="color:#ff0000;">master</span></strong>
M: f37ec54101536425ce8798e041ad75a582d7e153 192.168.1.103:7001
   slots:5461-10922 (5462 slots) master
M: 7b0ca3978858454051ad572aa816eec450f31a53 192.168.1.103:7002
   slots:10923-16383 (5461 slots) master
<strong><span style="color:#ff0000;">S</span></strong>: 778e649f47fa98f6d1f6b1f1043812c6685dc4a8 192.168.1.103:7003
   <strong><span style="color:#ff0000;">replicates</span></strong> <span style="background-color: rgb(102, 255, 255);">4bc092eb4731152d15172b065c74c7a795fe6304</span>
S: 907feee1b665554cadc64921c7fcb8c05b8a5ab6 192.168.1.103:7004
   replicates f37ec54101536425ce8798e041ad75a582d7e153
S: b2bea8ede402e2112cced7d7cea52127f18edef2 192.168.1.103:7005
   replicates 7b0ca3978858454051ad572aa816eec450f31a53
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join...
>>> Performing Cluster Check (using node 192.168.1.103:7000)
M: 4bc092eb4731152d15172b065c74c7a795fe6304 192.168.1.103:7000
   slots:0-5460 (5461 slots) master
M: f37ec54101536425ce8798e041ad75a582d7e153 192.168.1.103:7001
   slots:5461-10922 (5462 slots) master
M: 7b0ca3978858454051ad572aa816eec450f31a53 192.168.1.103:7002
   slots:10923-16383 (5461 slots) master
M: 778e649f47fa98f6d1f6b1f1043812c6685dc4a8 192.168.1.103:7003
   slots: (0 slots) master
   replicates 4bc092eb4731152d15172b065c74c7a795fe6304
M: 907feee1b665554cadc64921c7fcb8c05b8a5ab6 192.168.1.103:7004
   slots: (0 slots) master
   replicates f37ec54101536425ce8798e041ad75a582d7e153
M: b2bea8ede402e2112cced7d7cea52127f18edef2 192.168.1.103:7005
   slots: (0 slots) master
   replicates 7b0ca3978858454051ad572aa816eec450f31a53
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

以上的輸出包含了很多的信息,在後面的文章當中會講到,這裏你只要能看出redis cluster已成功構建即可。


至此,redis的集羣環境搭建完成 ~~~




轉載請註明
http://blog.csdn.net/liaokailin/article/details/47988617


歡迎關注,您的肯定是對我最大的支持




















      
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章