redis集羣的搭建:html
下面咱們來進行學習下redis3.2.4如何搭建集羣。node
集權配置:至少三個mastergit
第一步:建立一個文件夾redis-cluster,而後在其下面分別建立6個文件以下:redis
(1)mkdir -p /usr/local/redis-clustersql
(2)mkdir 700一、mkdir 700二、mkdir 700三、mkdir 700四、mkdir 700五、mkdir 7006vim
第二步:把以前的redis.conf配置文件分別copy到700*下,進行修改各個文件內存,也就是對700*下的每個copy的redis.conf文件進行修改!以下:api
(1)daemonize yesruby
(2)port 700*(分別對每一個機器的端口進行設置)app
(3)bind 192.168.1.171(必需要綁定當前機器的ip,否則會無限悲劇下去啊,深坑勿入!!!)nosql
(4)dir /usr/local/redis-cluster/700*/(指定數據文件存放位置,必需要指定不一樣的目錄位置,否則會丟失數據,深坑勿入!!!)
(5)cluster-enabled yes(啓動集羣模式,開始玩耍)
(6)cluster-config-file nodes700*.conf(這裏700x最好和port對應上)
(7)cluster-node-timeout 5000
(8)appendonly yes
(9)requirepass 123456(設置redis鏈接密碼,全部的配置文件的密碼是同樣的,感受在集羣前配置密碼是否是更好一點???這裏暫時看成一個遺留問題吧)
第三步:把修改後的配置文件,分別copy到各個文件夾下,注意每一個文件要修改端口號,而且nodes文件也要不相同!!
vi下全局替換:%s/7001/7002/g 全局替換7001成7002
第四步:因爲redis集羣須要使用ruby命令,因此咱們須要安裝ruby
(1) yum install ruby
(2)yum install rubygems
(3)gem install redis(安裝redis和ruby的接口)
第五步:分別啓動6個redis實例,而後檢查是否啓動成功
(1)/usr/local/redis/bin/redis-server /usr/local/redis-cluster/700*/redis.conf
(2)ps -el | grep redis 查看是否啓動成功
第六步:首先到redis3.0的安裝目錄下,而後執行redis-trib.rb指令
(1)cd /usr/local/redis3.0/src
(2)./redis-trib.rb create --replicas 1 192.168.1.171:7001 192.168.1.171:7002 192.168.1.171:7003 192.168.1.171:7004 192.168.1.171:7005 192.168.1.171:7006
其中
create 表示 建立集羣的意思;
1 表示主節點與從節點的比例,這裏是1,也就是說6臺redis服務,有3臺是主節點,3臺是從節點;
執行輸出打印信息:
1 >>> Creating cluster 2 >>> Performing hash slots allocation on 6 nodes... 3 Using 3 masters: 4 192.168.1.171:7001 5 192.168.1.171:7002 6 192.168.1.171:7003 7 Adding replica 192.168.1.171:7004 to 192.168.1.171:7001 8 Adding replica 192.168.1.171:7005 to 192.168.1.171:7002 9 Adding replica 192.168.1.171:7006 to 192.168.1.171:7003 10 M: 201503eb5f050814a56ae6a5c8623a0bf774fd0f 192.168.1.171:7001 11 slots:0-5460 (5461 slots) master 12 M: 18cbda8addae41f73c184cfd67e26414184a5498 192.168.1.171:7002 13 slots:5461-10922 (5462 slots) master 14 M: d4a9a5b02611b4d8e7e4150f485b966f7de1c26b 192.168.1.171:7003 15 slots:10923-16383 (5461 slots) master 16 S: 91a6d479718ca5a23bdb007f6f55088e0d979707 192.168.1.171:7004 17 replicates 201503eb5f050814a56ae6a5c8623a0bf774fd0f 18 S: e014b1f935c0fc4ee0860b4e2200c960619e1fa6 192.168.1.171:7005 19 replicates 18cbda8addae41f73c184cfd67e26414184a5498 20 S: 1dfc3c8f170506216ecf3b53841d681e07f3c81e 192.168.1.171:7006 21 replicates d4a9a5b02611b4d8e7e4150f485b966f7de1c26b 22 Can I set the above configuration? (type 'yes' to accept):
從輸出結果來看,從節點沒有"槽",由於從節點沒有寫權限
注意事項:
1.若是是使用redis-trib.rb工具構建集羣,集羣構建完成前不要配置密碼,集羣構建完畢再經過config set + config rewrite命令逐個機器設置密碼
2.若是對集羣設置密碼,那麼requirepass和masterauth都須要設置,不然發生主從切換時,就會遇到受權問題,能夠模擬並觀察日誌
3.各個節點的密碼都必須一致,不然Redirected就會失敗
1 config set masterauth 123456 2 config set requirepass 123456 3 config rewrite
設置密碼後,redis-trib.rb check 192.168.1.171:7001 報錯鏈接不上實例,解決方法:
vim /usr/lib/ruby/gems/1.8/gems/redis-3.3.3/lib/redis/client.rb
1 class Redis 2 class Client 3 4 DEFAULTS = { 5 :url => lambda { ENV["REDIS_URL"] }, 6 :scheme => "redis", 7 :host => "127.0.0.1", 8 :port => 6379, 9 :path => nil, 10 :timeout => 5.0, 11 :password => nil, 12 :db => 0, 13 :driver => nil, 14 :id => nil, 15 :tcp_keepalive => 0, 16 :reconnect_attempts => 1, 17 :inherit_socket => false 18 }
修改下默認密碼就能夠了,好比:password => "123456",
第七步:到此爲止咱們集羣搭建成功!進行驗證:
(1)鏈接任意一個客戶端便可:./redis-cli -c -h -p(-c表示集羣模式,指定ip地址和端口號)如:/usr/local/redis -c -h 192.168.1.171 -p 700*
(2)進行驗證:cluster info(查看集羣信息)、cluster nodes(查看節點列表)
(3)進行數據操做驗證
(4)關閉集羣則須要逐個進行關閉,使用命令:
/usr/local/redis/bin/redis-cli -c -h 192.168.171 -p 700* shutdown
鏈接192.168.1.171:7001,輸入info,查看主從信息以下:
1 [root@iZ11wt0u6kbZ yucong]# /usr/local/redis/bin/redis-cli -c -h 192.168.1.171 -p 7001 2 192.168.1.171:7001> info 3 # Server 4 redis_version:3.2.4 5 redis_git_sha1:00000000 6 redis_git_dirty:0 7 redis_build_id:6b401482e8c31daa 8 redis_mode:cluster 9 os:Linux 2.6.32-573.7.1.el6.x86_64 x86_64 10 arch_bits:64 11 multiplexing_api:epoll 12 gcc_version:4.4.7 13 process_id:20352 14 run_id:057db549bba87f39bdf7e2a9483cf3ecc6b49867 15 tcp_port:7001 16 uptime_in_seconds:3722 17 uptime_in_days:0 18 hz:10 19 lru_clock:9975830 20 executable:/usr/local/redis/bin/redis-server 21 config_file:/usr/local/redis-cluster/7001/redis.conf 22 23 # Clients 24 connected_clients:1 25 client_longest_output_list:0 26 client_biggest_input_buf:0 27 blocked_clients:0 28 29 # Memory 30 used_memory:2418328 31 used_memory_human:2.31M 32 used_memory_rss:10010624 33 used_memory_rss_human:9.55M 34 used_memory_peak:2521056 35 used_memory_peak_human:2.40M 36 total_system_memory:4018704384 37 total_system_memory_human:3.74G 38 used_memory_lua:37888 39 used_memory_lua_human:37.00K 40 maxmemory:0 41 maxmemory_human:0B 42 maxmemory_policy:noeviction 43 mem_fragmentation_ratio:4.14 44 mem_allocator:jemalloc-4.0.3 45 46 # Persistence 47 loading:0 48 rdb_changes_since_last_save:0 49 rdb_bgsave_in_progress:0 50 rdb_last_save_time:1486367331 51 rdb_last_bgsave_status:ok 52 rdb_last_bgsave_time_sec:0 53 rdb_current_bgsave_time_sec:-1 54 aof_enabled:1 55 aof_rewrite_in_progress:0 56 aof_rewrite_scheduled:0 57 aof_last_rewrite_time_sec:-1 58 aof_current_rewrite_time_sec:-1 59 aof_last_bgrewrite_status:ok 60 aof_last_write_status:ok 61 aof_current_size:0 62 aof_base_size:0 63 aof_pending_rewrite:0 64 aof_buffer_length:0 65 aof_rewrite_buffer_length:0 66 aof_pending_bio_fsync:0 67 aof_delayed_fsync:0 68 69 # Stats 70 total_connections_received:8 71 total_commands_processed:3539 72 instantaneous_ops_per_sec:1 73 total_net_input_bytes:183131 74 total_net_output_bytes:5986856 75 instantaneous_input_kbps:0.04 76 instantaneous_output_kbps:0.00 77 rejected_connections:0 78 sync_full:1 79 sync_partial_ok:0 80 sync_partial_err:0 81 expired_keys:0 82 evicted_keys:0 83 keyspace_hits:0 84 keyspace_misses:0 85 pubsub_channels:0 86 pubsub_patterns:0 87 latest_fork_usec:722 88 migrate_cached_sockets:0 89 90 # Replication 91 role:master 92 connected_slaves:1 93 slave0:ip=192.168.1.171,port=7004,state=online,offset=4901,lag=0 94 master_repl_offset:4901 95 repl_backlog_active:1 96 repl_backlog_size:1048576 97 repl_backlog_first_byte_offset:2 98 repl_backlog_histlen:4900 99 100 # CPU 101 used_cpu_sys:2.21 102 used_cpu_user:1.65 103 used_cpu_sys_children:0.00 104 used_cpu_user_children:0.00 105 106 # Cluster 107 cluster_enabled:1 108 109 # Keyspace 110 192.168.1.171:7001>
再輸入cluster nodes,查看集權信息以下:
1 192.168.1.171:7001> cluster nodes 2 d4a9a5b02611b4d8e7e4150f485b966f7de1c26b 192.168.1.171:7003 master - 0 1486371253432 3 connected 10923-16383 3 e014b1f935c0fc4ee0860b4e2200c960619e1fa6 192.168.1.171:7005 slave 18cbda8addae41f73c184cfd67e26414184a5498 0 1486371254934 2 connected 4 91a6d479718ca5a23bdb007f6f55088e0d979707 192.168.1.171:7004 slave 201503eb5f050814a56ae6a5c8623a0bf774fd0f 0 1486371252931 4 connected 5 201503eb5f050814a56ae6a5c8623a0bf774fd0f 192.168.1.171:7001 myself,master - 0 0 1 connected 0-5460 6 1dfc3c8f170506216ecf3b53841d681e07f3c81e 192.168.1.171:7006 slave d4a9a5b02611b4d8e7e4150f485b966f7de1c26b 0 1486371253932 6 connected 7 18cbda8addae41f73c184cfd67e26414184a5498 192.168.1.171:7002 master - 0 1486371253932 2 connected 5461-10922 8 192.168.1.171:7001>
第八步:(補充)
友情提示:當出現集羣沒法啓動時,刪除臨時的數據文件,再次從新啓動每個redis服務,而後從新構造集權環境。
redis-trib.rb官方羣操做命令:http://redis.io/topics/cluster-tutorial
推薦博客:http://blog.51yip.com/nosql/1726.html/comment-page-1
redis-cli客戶端操做命令:
http://blog.csdn.net/qachenzude/article/details/32098777