1. 程式人生 > >Redis安裝與叢集部署

Redis安裝與叢集部署

安裝

當前安裝環境為:
- 系統:Ubuntu 14.04.4 LTS
- 單節:虛擬機器單節點
- 地址:192.168.0.18
- 硬體:1核1G

apt-get包管理安裝

安裝

apt-get install redis-server

遠端訪問

使用包管理工具安裝的redis預設是僅本機可訪問,不支援遠端訪問的。所以需要修改redis的配置檔案,取消繫結ip。

vi /etc/redis/redis.conf
# 註釋掉: bind 127.0.0.1這一行

原始碼編譯安裝

安裝

原始碼編譯安裝其實只要完成編譯工作,安裝也就完成了。在編譯過程中首先發現機器沒有安裝make工具,等把make裝好後發現cc命令執行失敗,所以又安裝了gcc。等這些條件都準備好後,報了一個檔案缺失的錯誤,網上查了一下,發現這是一個bug,好的是也給出瞭解決方案,這才順利的完成編譯過程。

# 原始碼下載
wget http://download.redis.io/releases/redis-3.0.6.tar.gz

# 解壓
tar xzf redis-3.0.6.tar.gz

# 編譯
cd redis-3.0.6
make MALLOC=libc
# 直接執行make有可能會報錯:fatal error: jemalloc/jemalloc.h

# 無make模組的時候:apt-get install make
# cc命令執行失敗的時候:apt-get install gcc

啟動

進入src目錄,直接執行redis-server指令碼即可以redis預設引數啟動。另外也可以編寫自定義配置檔案,然後根據配置啟動redis。

cd src
./redis-server 或者 ./redis-server [configPath]

配置叢集

環境準備

redis3.0以後開始支援叢集,並提供了相關工具。叢集工具位於原始碼src目錄下的redis-trib.rb,該指令碼是使用ruby書寫的,所以當前環境還需要安裝ruby。

安裝ruby

apt-get install ruby
gem install redis

修改配置檔案

預設配置檔案僅能開啟一個單節點redis,所以我們需要對配置檔案做一定的修改。一下是配置叢集的必須修改項。

cluster-enabled yes
cluster-config
-file nodes.conf cluster-node-timeout 5000 appendonly yes

本次安裝因為是單機多節點redis叢集,而且每個節點都以守護程序模式啟動。所以還需要修改pid檔案和埠,已經啟動模式。

daemonize yes
pidfile /var/run/redis0.pid
port 6380

啟動redis

本次叢集計劃6個redis例項:3個主節點,3個複製節點。埠分別位:6380,6381,6382,6383,6384,6385。

./redis-server ../shards/6380/shard.conf
./redis-server ../shards/6381/shard.conf
./redis-server ../shards/6382/shard.conf
./redis-server ../shards/6383/shard.conf
./redis-server ../shards/6384/shard.conf
./redis-server ../shards/6385/shard.conf

組建叢集

[email protected]:/opt/sbin/redis-3.0.6/src# ./redis-trib.rb create --replicas 1 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384 127.0.0.1:6385
./redis-trib.rb:1573: warning: key "threshold" is duplicated and overwritten on line 1573
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
127.0.0.1:6380
127.0.0.1:6381
127.0.0.1:6382
Adding replica 127.0.0.1:6383 to 127.0.0.1:6380
Adding replica 127.0.0.1:6384 to 127.0.0.1:6381
Adding replica 127.0.0.1:6385 to 127.0.0.1:6382
M: 1b85ea9b2e569906d6d6fb99ef59434690bb5314 127.0.0.1:6380
   slots:0-5460 (5461 slots) master
M: 861172b6acc35c101eb349cf550a5f270b3b4260 127.0.0.1:6381
   slots:5461-10922 (5462 slots) master
M: ac7155ff194714b67597c848d187d01c002fac91 127.0.0.1:6382
   slots:10923-16383 (5461 slots) master
S: 8f1b32c678e136c2d5abba75c6b85d6c001ae297 127.0.0.1:6383
   replicates 1b85ea9b2e569906d6d6fb99ef59434690bb5314
S: 34ac1fc6237f84d7be105da86d7fc305757a8ae3 127.0.0.1:6384
   replicates 861172b6acc35c101eb349cf550a5f270b3b4260
S: 2b5e12e4ace97e8964abc5b596b065058f879086 127.0.0.1:6385
   replicates ac7155ff194714b67597c848d187d01c002fac91
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 127.0.0.1:6380)
M: 1b85ea9b2e569906d6d6fb99ef59434690bb5314 127.0.0.1:6380
   slots:0-5460 (5461 slots) master
M: 861172b6acc35c101eb349cf550a5f270b3b4260 127.0.0.1:6381
   slots:5461-10922 (5462 slots) master
M: ac7155ff194714b67597c848d187d01c002fac91 127.0.0.1:6382
   slots:10923-16383 (5461 slots) master
M: 8f1b32c678e136c2d5abba75c6b85d6c001ae297 127.0.0.1:6383
   slots: (0 slots) master
   replicates 1b85ea9b2e569906d6d6fb99ef59434690bb5314
M: 34ac1fc6237f84d7be105da86d7fc305757a8ae3 127.0.0.1:6384
   slots: (0 slots) master
   replicates 861172b6acc35c101eb349cf550a5f270b3b4260
M: 2b5e12e4ace97e8964abc5b596b065058f879086 127.0.0.1:6385
   slots: (0 slots) master
   replicates ac7155ff194714b67597c848d187d01c002fac91
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

測試叢集

存取測試

[email protected]:~# redis-cli -c -h 192.168.0.18 -p 6380
192.168.0.18:6380> set name kim
-> Redirected to slot [5798] located at 127.0.0.1:6381
OK
127.0.0.1:6381> set bllow 192.168.0.1
-> Redirected to slot [12497] located at 127.0.0.1:6382
OK
127.0.0.1:6382> set cat tom
-> Redirected to slot [1075] located at 127.0.0.1:6380
OK