標籤:redis cluster
redis是個分布式緩衝,與傳統資料庫最大的優勢,在於它的“分布式”上。
分布式的優勢:
容易實現容量的擴充
資料的均等分布
很好的高可用性
redis 和memcached是分布式緩衝的兩款流行方案,他們之間的對比
|
redis |
memcached |
主從功能
|
Replication 支援 主備自動切換
|
本身不支援,可以通過用戶端自己實現
|
索引值一致性
|
雜湊槽
|
一致性雜湊
|
叢集
|
服務端支援(但是beta版) unstable
|
由用戶端實現
|
工具支援
|
提供內建的工具(用戶端redis-cli,check-rdb...)
|
較弱
|
資料存放區
|
支援
|
不支援,重啟資料丟失
|
本章主要內容
安裝一個三主的redis叢集環境。
準備
由於本人沒有接觸過ruby語言,在安裝之前做功課時,需要安裝ruby環境等,心理上有了點小負擔,擔心遇到麻煩。後來,不出所料遇到了點麻煩。最後,還是安裝成功了,回頭想想,沒有什麼可擔心的,無外乎失敗了重裝,真是年紀越大,膽子越小啊。我採用的方式,“兵來將擋,水來土掩”,有錯就解決錯,一步步安裝。藉助網路的力量,逐個把問題擊破。
安裝ruby環境
1.安裝ruby
遇到的問題
未安裝ruby
版本過低
未安裝rubygem
gem源出錯
請參考
http://blog.csdn.net/c77_cn/article/details/38227797
2. 編譯redis cluster
從https://github.com/antirez/redis/branches下載unstable版本。
編譯make
此時,在/usr/local/src/redis-unstable/src 目錄下產生了支援cluster的redis-server
3.建立叢集環境
1.建立目錄cp /usr/local/redismkdir cluster-testcd cluster-test/mkdir 7000mkdir 7001mkdir 70022.複製redis-server cp /usr/local/src/redis-unstable/src/redis-server /usr/local/redis/cluster-test/7001 cp /usr/local/src/redis-unstable/src/redis-server /usr/local/redis/cluster-test/7000 cp /usr/local/src/redis-unstable/src/redis-server /usr/local/redis/cluster-test/7002 #叢集管理工具(為了方便,將他複製到合適位置) cp /usr/local/src/redis-unstable/src/redis-trib.rb /usr/local/redis/cluster-test
3.建立3個cluster 節點對應的redis.conf檔案
[[email protected] cluster-test]# vi 7000/redis.conf port 7000cluster-enabled yescluster-config-file nodes.confcluster-node-timeout 5000appendonly yesdaemonize yeslogfile "redis7000.log"#這是最簡單的叢集參數
其他7001/redis.conf,7002.conf檔案, 僅僅port和logfile參數不同,其他一樣。
此時 7000, 7001,7002目錄下,只有2個檔案,分別是 redis-server,redis.conf
4. 啟動(分別啟動3個主節點)
cd 7000 ./redis-server ./redis.conf cd ../7001/ ./redis-server ./redis.conf cd ../7002 ./redis-server ./redis.conf
對應某個節點日誌
26361:M 10 Sep 21:13:22.054 * No cluster configuration found, I‘m 7f242cf2c7ce5d84103638c5017f2204e1509f4c
_._
_.-``__ ‘‘-._
_.-`` `. `_. ‘‘-._ Redis 3.2.3 (00000000/0) 32 bit
.-`` .-```. ```\/ _.,_ ‘‘-._
( ‘ , .-` | `, ) Running in cluster mode
|`-._`-...-` __...-.``-._|‘` _.-‘| Port: 7001
| `-._ `._ / _.-‘ | PID: 26361
`-._ `-._ `-./ _.-‘ _.-‘
|`-._`-._ `-.__.-‘ _.-‘_.-‘|
| `-._`-._ _.-‘_.-‘ | http://redis.io
`-._ `-._`-.__.-‘_.-‘ _.-‘
|`-._`-._ `-.__.-‘ _.-‘_.-‘|
| `-._`-._ _.-‘_.-‘ |
`-._ `-._`-.__.-‘_.-‘ _.-‘
`-._ `-.__.-‘ _.-‘
`-._ _.-‘
`-.__.-‘
26361:M 10 Sep 21:13:22.075 # Server started, Redis version 3.2.3
26361:M 10 Sep 21:13:22.075 * DB loaded from append only file: 0.000 seconds
26361:M 10 Sep 21:13:22.076 # I have keys for unassigned slot 5659. Taking responsibility for it.
26361:M 10 Sep 21:13:22.076 # I have keys for unassigned slot 9980. Taking responsibility for it.
26361:M 10 Sep 21:13:22.080 * The server is now ready to accept connections on port 7001
5. 建立叢集
./redis-trib.rb create 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002
[[email protected] cluster-test]# ./redis-trib.rb create 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002
>>> Creating cluster
>>> Performing hash slots allocation on 3 nodes...
Using 3 masters:
127.0.0.1:7000
127.0.0.1:7001
127.0.0.1:7002
M: 5ad201ebdf0553690295dcd06f89f15750e73989 127.0.0.1:7000
slots:0-5460 (5461 slots) master
M: 7f242cf2c7ce5d84103638c5017f2204e1509f4c 127.0.0.1:7001
slots:5461-10922 (5462 slots) master
M: 8ea7b0976e4848653435e4e77af311a6ade330a8 127.0.0.1:7002
slots:10923-16383 (5461 slots) master
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:7000)
M: 5ad201ebdf0553690295dcd06f89f15750e73989 127.0.0.1:7000
slots:0-5460 (5461 slots) master
0 additional replica(s)
M: 8ea7b0976e4848653435e4e77af311a6ade330a8 127.0.0.1:7002
slots:10923-16383 (5461 slots) master
0 additional replica(s)
M: 7f242cf2c7ce5d84103638c5017f2204e1509f4c 127.0.0.1:7001
slots:5461-10922 (5462 slots) master
0 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
這時候主節點日誌
26536:M 10 Sep 21:25:32.360 * DB loaded from append only file: 0.000 seconds
26536:M 10 Sep 21:25:32.361 * The server is now ready to accept connections on port 7000
26536:M 10 Sep 21:27:54.651 # configEpoch set to 1 via CLUSTER SET-CONFIG-EPOCH
26536:M 10 Sep 21:27:54.660 # IP address for this node updated to 127.0.0.1
26536:M 10 Sep 21:27:59.619 # Cluster state changed: ok
6.測試
[[email protected] cluster-test]# /usr/local/redis/bin/redis-cli -c -p 7000127.0.0.1:7000> keys *(empty list or set)127.0.0.1:7000> set title "cluster1"OK127.0.0.1:7000> set title1 "cluster1"OK127.0.0.1:7000> set title2 "cluster1"-> Redirected to slot [14045] located at 127.0.0.1:7002OK127.0.0.1:7002> set title5 "cluster4"-> Redirected to slot [1594] located at 127.0.0.1:7000OK127.0.0.1:7000> set title3 "cluster4"-> Redirected to slot [9980] located at 127.0.0.1:7001OK127.0.0.1:7001> get title-> Redirected to slot [2217] located at 127.0.0.1:7000"cluster1"127.0.0.1:7000> get title2-> Redirected to slot [14045] located at 127.0.0.1:7002"cluster8111"127.0.0.1:7002> get title3-> Redirected to slot [9980] located at 127.0.0.1:7001"cluster4"#節點間來回重新導向
到了這裡,環境算是搭建好了。當然,這是最簡單的叢集了。以後隨著演練的深入,也會對叢集進行深入探討。
可能遇到的錯誤(是第二次建立叢集時報錯)
ERR Slot 2217 is already busy (Redis::CommandError)
(in all nodes did FLUSHALL and then CLUSTER RESET SOFT)
參考資源
http://blog.csdn.net/c77_cn/article/details/38227797
http://redis.io/topics/cluster-tutorial
http://redisdoc.com/topic/cluster-tutorial.html
本文出自 “簡單” 部落格,請務必保留此出處http://dba10g.blog.51cto.com/764602/1851458
redis演練(8) redis Cluster 叢集環境安裝