1. 程式人生 > >linux上搭建redis偽叢集

linux上搭建redis偽叢集

本文使用的linux為Centos6.4版本,redis版本為redis-3.0.0,假設您已經在Linux上安裝了redis單機版,安裝目錄為/usr/local/redis下,redis-3.0.0.tar.gz解壓的目錄在/usr/local,本文只是演示怎麼搭建叢集
一、搭建叢集前需要的環境
搭建叢集需要使用到官方提供的ruby指令碼。
需要安裝ruby的環境。
安裝ruby:
yum install ruby
yum install rubygems
安裝ruby的包:(redis-3.0.0.gem需要另外下載上傳到linux上):
gem install redis-3.0.0.gem
二、搭建的步驟
第一步:建立6個redis例項,埠號從7001~7006
首先要將單機版的安裝好的redis安裝路徑上bin目錄複製一份到/usr/local/redis-cluster/redis01下
在/usr/local目錄下新建redis-cluster資料夾:
mkdir redis-cluster
cd到/usr/local/redis,複製bin:
cp -r bin ../redis-cluster/redis01
複製完cd到redis01資料夾下,編輯redis.conf檔案:
vi redis.conf
修改原埠號6379為7001
這裡寫圖片描述


去掉cluster-enable前面的註釋。
這裡寫圖片描述
修改完畢儲存redis.conf
然後將redis01複製6份
這裡寫圖片描述
分別修改redis02-redis06下面的redis-conf檔案,將埠號分別改為7002,7003,7004,7005,7006儲存即可
第二步:把建立叢集的ruby指令碼複製到redis-cluster目錄下
ruby指令碼的位置在redis的解壓路徑下的src下
cd到redis的解壓路徑的src下,複製redis-trib.rb:
這裡寫圖片描述
第三步:啟動6個redis例項
可以寫一個sh指令碼,在redis-cluster檔案下新建startall.sh檔案
這裡寫圖片描述

然後執行指令碼:
這裡寫圖片描述
檢視程序,可知啟動成功
第四步:建立叢集
在redis-cluster下執行命令:
./redis-trib.rb create –replicas 1 192.168.11.133:7001 192.168.11.133:7002 192.168.11.133:7003 192.168.11.133:7004 192.168.11.133:7005 192.168.11.133:7006
其中192.168.11.133為自己linux的host
執行顯示如下:

>>> Creating cluster
Connecting to node 192.168.11
.133:7001: OK Connecting to node 192.168.11.133:7002: OK Connecting to node 192.168.11.133:7003: OK Connecting to node 192.168.11.133:7004: OK Connecting to node 192.168.11.133:7005: OK Connecting to node 192.168.11.133:7006: OK >>> Performing hash slots allocation on 6 nodes... Using 3 masters: 192.168.11.133:7001 192.168.11.133:7002 192.168.11.133:7003 Adding replica 192.168.11.133:7004 to 192.168.11.133:7001 Adding replica 192.168.11.133:7005 to 192.168.11.133:7002 Adding replica 192.168.11.133:7006 to 192.168.11.133:7003 M: 5a8523db7e12ca600dc82901ced06741b3010076 192.168.11.133:7001 slots:0-5460 (5461 slots) master M: bf6f0929044db485dea9b565bb51e0c917d20a53 192.168.11.133:7002 slots:5461-10922 (5462 slots) master M: c5e334dc4a53f655cb98fa3c3bdef8a808a693ca 192.168.11.133:7003 slots:10923-16383 (5461 slots) master S: 2a61b87b49e5b1c84092918fa2467dd70fec115f 192.168.11.133:7004 replicates 5a8523db7e12ca600dc82901ced06741b3010076 S: 14848b8c813766387cfd77229bd2d1ffd6ac8d65 192.168.11.133:7005 replicates bf6f0929044db485dea9b565bb51e0c917d20a53 S: 3192cbe437fe67bbde9062f59d5a77dabcd0d632 192.168.11.133:7006 replicates c5e334dc4a53f655cb98fa3c3bdef8a808a693ca 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.11.133:7001) M: 5a8523db7e12ca600dc82901ced06741b3010076 192.168.11.133:7001 slots:0-5460 (5461 slots) master M: bf6f0929044db485dea9b565bb51e0c917d20a53 192.168.11.133:7002 slots:5461-10922 (5462 slots) master M: c5e334dc4a53f655cb98fa3c3bdef8a808a693ca 192.168.11.133:7003 slots:10923-16383 (5461 slots) master M: 2a61b87b49e5b1c84092918fa2467dd70fec115f 192.168.11.133:7004 slots: (0 slots) master replicates 5a8523db7e12ca600dc82901ced06741b3010076 M: 14848b8c813766387cfd77229bd2d1ffd6ac8d65 192.168.11.133:7005 slots: (0 slots) master replicates bf6f0929044db485dea9b565bb51e0c917d20a53 M: 3192cbe437fe67bbde9062f59d5a77dabcd0d632 192.168.11.133:7006 slots: (0 slots) master replicates c5e334dc4a53f655cb98fa3c3bdef8a808a693ca [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.

到此redis偽叢集搭建完畢
三、測試叢集

[root@localhost redis-cluster]# redis01/redis-cli -h 192.168.11.133 -p 7002 -c
[[email protected] redis-cluster]# redis01/redis-cli -h 192.168.11.133 -p 7002 -c
192.168.11.133:7002> set a 100
-> Redirected to slot [15495] located at 192.168.11.133:7003
OK
192.168.11.133:7003> 

四、關閉redis
同樣類似啟動寫好指令碼
這裡寫圖片描述