用一個叫redis-trib.rb的ruby腳本。redis-trib.rb是redis官方推出的管理redis集羣的工具,集成在redis的源碼src目錄下(redis-xxx/src/)。是基於redis提供的集羣命令封裝成簡單、便捷、實用的操做工具。redis-trib.rb是redis做者用ruby完成的。因此redis集羣須要先安裝ruby環境。html
這是在線安裝步驟node
Redis-cluster集羣須要使redis-trib.rb腳原本管理集羣,redis-trib.rb腳本是使用ruby語言編寫的。redis
搭建集羣同時須要搭建6臺redis服務器,其中3臺主機、3臺從機。端口分別爲7001~7006。vim
第一步:搭建ruby環境ruby
[root@bogon bin]# yum install -y rubybash
[root@bogon bin]# yum install -y rubygems服務器
第二步:上傳ruby和redis的接口程序工具
第三步:安裝ruby和redis的接口oop
[root@bogon ~]# gem install redis-3.0.0.gemui
第四步:複製redis-trib.rb腳本到/usr/local/redis-cluster目錄下
[root@bogon ~]# mkdir /usr/local/redis-cluster
[root@bogon ~]# cp /root/redis-3.0.0/src/redis-trib.rb /usr/local/redis-cluster/
第五步:複製/usr/local/redis/bin到/usr/local/redis-cluster/下,並更名爲7001
[root@bogon redis-cluster]# cp /usr/local/redis/bin ./7001 –r
第六步:修改7001目錄下的redis.conf文件,主要修改端口和集羣配置。
port 7001
cluster-enabled yes
第七步:複製7001目錄爲7002~7006,而後修改端口
Cp 7001/ 7002 -r
Cp 7001/ 7003 -r
......
第八步:啓動7001~7006這6個redis服務器
編輯啓動腳本
cd 7001/ ./redis-server redis.conf cd .. cd 7002/ ./redis-server redis.conf cd .. cd 7003/ ./redis-server redis.conf cd .. cd 7004/ ./redis-server redis.conf cd .. cd 7005/ ./redis-server redis.conf cd .. cd 7006/ ./redis-server redis.conf cd .. |
第九步:建立集羣
[root@bogon redis-cluster]# ./redis-trib.rb create --replicas 1 192.168.242.129:7001 192.168.242.129:7002 192.168.242.129:7003 192.168.242.129:7004 192.168.242.129:7005 192.168.242.129:7006 >>> Creating cluster Connecting to node 192.168.242.129:7001: OK Connecting to node 192.168.242.129:7002: OK Connecting to node 192.168.242.129:7003: OK Connecting to node 192.168.242.129:7004: OK Connecting to node 192.168.242.129:7005: OK Connecting to node 192.168.242.129:7006: OK >>> Performing hash slots allocation on 6 nodes... Using 3 masters: 192.168.242.129:7001 192.168.242.129:7002 192.168.242.129:7003 Adding replica 192.168.242.129:7004 to 192.168.242.129:7001 Adding replica 192.168.242.129:7005 to 192.168.242.129:7002 Adding replica 192.168.242.129:7006 to 192.168.242.129:7003 M: e6e324f6e07a8791bb4680e6d9a43a8c8521d071 192.168.242.129:7001 slots:0-5460 (5461 slots) master M: 4734ef2831cc50f6ed2a311ef6e69cd7ac3e98ab 192.168.242.129:7002 slots:5461-10922 (5462 slots) master M: 7bb03b2f0f2bed96b367d4a5ec1886e63f5b4a37 192.168.242.129:7003 slots:10923-16383 (5461 slots) master S: 4679464174bb13ab556b9caf89085b9ae6e322a3 192.168.242.129:7004 replicates e6e324f6e07a8791bb4680e6d9a43a8c8521d071 S: 3dd4692316cba5b3ab458cbcf93d85ff1de4cfbe 192.168.242.129:7005 replicates 4734ef2831cc50f6ed2a311ef6e69cd7ac3e98ab S: 2285d1dbbe3f5c54b3ffeffcedd534866d8953b5 192.168.242.129:7006 replicates 7bb03b2f0f2bed96b367d4a5ec1886e63f5b4a37 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.242.129:7001) M: e6e324f6e07a8791bb4680e6d9a43a8c8521d071 192.168.242.129:7001 slots:0-5460 (5461 slots) master M: 4734ef2831cc50f6ed2a311ef6e69cd7ac3e98ab 192.168.242.129:7002 slots:5461-10922 (5462 slots) master M: 7bb03b2f0f2bed96b367d4a5ec1886e63f5b4a37 192.168.242.129:7003 slots:10923-16383 (5461 slots) master M: 4679464174bb13ab556b9caf89085b9ae6e322a3 192.168.242.129:7004 slots: (0 slots) master replicates e6e324f6e07a8791bb4680e6d9a43a8c8521d071 M: 3dd4692316cba5b3ab458cbcf93d85ff1de4cfbe 192.168.242.129:7005 slots: (0 slots) master replicates 4734ef2831cc50f6ed2a311ef6e69cd7ac3e98ab M: 2285d1dbbe3f5c54b3ffeffcedd534866d8953b5 192.168.242.129:7006 slots: (0 slots) master replicates 7bb03b2f0f2bed96b367d4a5ec1886e63f5b4a37 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. |
命令:./redis-cli –h 127.0.0.1 –p 7001 –c
注意:-c 鏈接redis集羣
[root@localhost-0723 redis]# ./redis-cli -p 7006 -c 127.0.0.1:7006> set key1 123 -> Redirected to slot [9189] located at 127.0.0.1:7002 OK 127.0.0.1:7002> |
127.0.0.1:7003> 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:3 cluster_stats_messages_sent:926 cluster_stats_messages_received:926 |
127.0.0.1:7003> cluster nodes 7a12bc730ddc939c84a156f276c446c28acf798c 127.0.0.1:7002 master - 0 1443601739754 2 connected 5461-10922 93f73d2424a796657948c660928b71edd3db881f 127.0.0.1:7003 myself,master - 0 0 3 connected 10923-16383 d8f6a0e3192c905f0aad411946f3ef9305350420 127.0.0.1:7001 master - 0 1443601741267 1 connected 0-5460 4170a68ba6b7757e914056e2857bb84c5e10950e 127.0.0.1:7006 slave 93f73d2424a796657948c660928b71edd3db881f 0 1443601739250 6 connected f79802d3da6b58ef6f9f30c903db7b2f79664e61 127.0.0.1:7004 slave d8f6a0e3192c905f0aad411946f3ef9305350420 0 1443601742277 4 connected 0bc78702413eb88eb6d7982833a6e040c6af05be 127.0.0.1:7005 slave 7a12bc730ddc939c84a156f276c446c28acf798c 0 1443601740259 5 connected 127.0.0.1:7003> |
這是離線安裝
離線步驟和在線相差不大隻是,須要離線安裝,ruby,rubygems,openssl,zlib-devel
安裝redis集羣的環境,環境安裝完了,集羣安裝是同樣的
依賴包 redis-3.2.1.gem
openssl-1.0.1s.tar.gz
rubygems-2.4.2.tgz
ruby-2.3.0.tar.gz
安裝ruby:
tar -zxvf ruby-2.3.0.tar.gz
./configure --prefix=/home/hadoop/opt/ruby
make
make install
配置環境變量
vim .bash_profile
export PATH=$JAVA_HOME/bin:$PATH:/home/hadoop/software/ruby/bin
source .bash_profile
查看版本
ruby -v
安裝rubygens
tar -zxvf rubygems-2.4.2.tgz
cd rubygems-2.4.2
ruby setup.rb
安裝 redis與ruby接口
gem install redis-3.2.1.gem
出現報錯
ERROR: Loading command: install (LoadError)
cannot load such file -- zlib
ERROR: While executing gem ... (NoMethodError)
undefined method `invoke_with_build_args' for nil:NilClass
需先安裝 zlib-devel 否則下面的make不能編譯
gem install redis-3.2.1.gem
報錯:
ERROR: While executing gem ... (Gem::Exception)
Unable to require openssl, install OpenSSL and rebuild ruby (preferred) or use non-HTTPS sources
查看解決方法 https://www.cnblogs.com/a-du/p/7767810.html 完美解決
環境安裝好 集羣安裝和在線安裝同樣