鎼徐楂樺彲鐢ㄧ殑redis闆嗙兢锛岄伩鍏峴tandalone妯″紡甯︾粰浣犵殑鑻﹂毦

鎼缓楂樺彲鐢ㄧ殑redis闆嗙兢锛岄伩鍏峴tandalone妯″紡甯︾粰浣犵殑鑻﹂毦
鐜板湪椤圭洰涓婄敤redis鐨勮瘽锛屽緢灏戣涓嶇敤闆嗙兢鐨勬儏鍐碉紝姣曠珶濡傛灉鐢熶骇涓婂彧鏈変竴鍙皉edis浼氭湁鏋佸ぇ鐨勯闄╋紝姣斿鏈哄櫒鎸傛帀锛屾垨鑰呭唴瀛樼垎鎺夛紝灏辨瘮濡傛垜浠敓浜х幆澧?

鏇句粖涔熼伃閬囧埌杩欑鎯呭喌锛屽鑷磖edis鍐呭瓨涓嶅鎸傛帀鐨勬儏鍐碉紝褰撶劧杩欎簺閮芥槸鎴戜滑鍙婂叾涓嶈兘瀹瑰繊鐨勶紝绗竴涓繀椤昏鍋氬埌楂樺彲闈狅紝鍏舵鎵嶆槸楂樻€ц兘锛屽ソ浜嗭紝涓嬮潰鎴戞潵

閫愪竴鎼缓涓€涓嬨€?



涓€锛歊edis闆嗙兢鎼缓

1. 涓嬭浇

   棣栧厛鍘诲畼缃戜笅杞借緝鏂扮殑3.2.0鐗堟湰锛屼笅杞芥柟寮忚繕鏄潪甯哥畝鍗曠殑锛屾瘮濡傚畼缃戜粙缁嶇殑杩欐牱銆?

$ wget http://download.redis.io/releases/redis-3.2.0.tar.gz
$ tar xzf redis-3.2.0.tar.gz
$ cd redis-3.2.0
$ make

2. redis閰嶇疆

銆€鐢变簬鎴戜滑瑕佸仛闆嗙兢锛岃€屼笖杩樿redis鑷甫鐨剅edis-trib.rb 鑳芥甯歌繍琛岋紝鎴戜滑闇€瑕佸湪闆嗙兢涓紑鍚笁鍙癿aster锛屼笁鍙皊lave锛屾墍浠ヨ繖閲屾垜闇€瑕佸缓绔?涓枃浠跺す锛岃€屼笖鏂囦欢澶圭殑鍚嶇О灏变娇鐢ㄧ鍙e湴鍧€鐨勫悕瀛楋紝姣斿锛?389. 6380....6384銆?
鎼徐楂樺彲鐢ㄧ殑redis闆嗙兢锛岄伩鍏峴tandalone妯″紡甯︾粰浣犵殑鑻﹂毦

3. config閰嶇疆銆?

   鐜板湪directory鐨勫垎甯冩儏鍐靛ぇ鑷村涓婂浘锛屾帴涓嬫潵瑕佸仛鐨勪簨鎯呭氨鏄厤缃畆edis.conf浜嗭紝鍦ㄨ繖閲岄渶瑕侀厤缃洓涓€夐」銆傘€傘€?



<1> port  绔彛鍦板潃锛屾瘮濡?380鏂囦欢澶逛笅闈㈢殑port灏辨槸6380锛?

# Accept connections on the specified port, default is 6379 (IANA #815344).
# If port 0 is specified Redis will not listen on a TCP socket.
port 6379



<2> cluster-enabled 鍜?cluster-config-file

       杩欎釜椤惧悕鎬濅箟锛岄鍏堥渶瑕佸紑鍚痳edis鐨刢luster妯″紡锛岀劧鍚庨厤缃竴涓猚luster-config-file鏂囦欢锛岃繖涓枃浠剁敤浜庡瓨鏀緍edis鐨勫疄鏃朵俊鎭紝redis浼氬姩鎬佽拷鍔犲拰淇?

鏀硅繖涓猚onf涓嬮潰鐨勫唴瀹逛俊鎭紝涓嶈繃瑕佽浣忥紝杩欎釜nodes-6379.conf 鍙互鏍规嵁 绔彛鏂囦欢澶逛緷娆¢厤缃紝姣斿6380鏂囦欢澶瑰彲浠ユ敼鎴恘odes-6380.conf杩欐牱銆傘€傘€?
澶嶅埗浠g爜

# Normal Redis instances can't be part of a Redis Cluster; only nodes that are
# started as cluster nodes can. In order to start a Redis instance as a
# cluster node enable the cluster support uncommenting the following:
#
cluster-enabled yes

# Every cluster node has a cluster configuration file. This file is not
# intended to be edited by hand. It is created and updated by Redis nodes.
# Every Redis Cluster node requires a different cluster configuration file.
# Make sure that instances running in the same system do not have
# overlapping cluster configuration file names.
#
cluster-config-file nodes-6379.conf

澶嶅埗浠g爜



<3> directory

      涓轰簡鏂逛究绠$悊锛屾垜杩欓噷閰嶇疆鐨剅oot鐩綍鍙栧喅浜庡湪鍝釜鏂囦欢澶癸紝姣斿6380涓嬮潰鎴戠殑dir灏辨槸: dir ./6380/

# Note that you must specify a directory here, not a file name.
dir ./6379/


<4> protected-mode

      杩欎釜鏄痳edis 3.2 鎵嶈拷鍔犵殑涓€涓姛鑳斤紝浠庡姛鑳芥敞閲婁腑锛屾垜浠氨鍙互鍙戠幇锛岃繖涓粯璁ゅ氨鏄笉璁╁鐣屽彲浠ヨ闂畆edis锛屾墍浠ヨ繖閲屾垜浠氨鏀逛负no锛屽彲浠ヨ繙绋嬭闂€?

# By default protected mode is enabled. You should disable it only if
# you are sure you want clients from other hosts to connect to Redis
# even if no authentication is configured, nor a specific set of interfaces
# are explicitly listed using the "bind" directive.
protected-mode no

ok锛屽埌鐜板湪涓烘锛屾垜浠殑config灏变慨鏀瑰畬姣曚簡锛屽叾浠栫鍙g殑鏂囦欢澶逛篃鍙互渚濇閰嶇疆涔媬

浜岋細寮€鍚痳edis

    鍒扮幇鍦ㄤ负姝紝鍚勪釜绔彛鏂囦欢澶归兘閰嶇疆鎴愬姛浜嗭紝鎺ヤ笅鏉ュ噯澶囧紑鍚簡锛岀湡鐨勫ソ涔堜箞鍝拁~~锛岀獥鍙eお澶氾紝鏈夌偣钀岃悓鐨勩€?
鎼徐楂樺彲鐢ㄧ殑redis闆嗙兢锛岄伩鍏峴tandalone妯″紡甯︾粰浣犵殑鑻﹂毦
鎺ヤ笅鏉ユ垜浠彲浠ョ湅涓€涓嬶紝鍦?379涓嬮潰鏄笉鏄湁鐢熸垚node-6379.conf鏂囦欢锛屾瘮濡備笅闈細
鎼徐楂樺彲鐢ㄧ殑redis闆嗙兢锛岄伩鍏峴tandalone妯″紡甯︾粰浣犵殑鑻﹂毦

涓夛細閰嶇疆redis-trib.rb

   鍥犱负redis-trib.rb鏄痳uby鍐欑殑锛岃€屾垜浠殑鐢佃剳鑲畾鏄病鏈塺uby鍜屼竴浜涢厤缃緷璧栭」锛屼笉杩囨病鍏崇郴锛屾湁寮哄ぇ鐨剏um瀹夎锛屼竴鍒囬兘涓嶆槸闂銆?



1. 鎵цreplicas鍛戒护

[jack@localhost ~]$ cluster/redis-trib.rb  create --replicas 1 192.168.161.133:6379 192.168.161.133:6380 192.168.161.133:6381 192.168.161.133:6382 192.168.161.133:6383 192.168.161.133:6384
/usr/bin/env: ruby: No such file or directory
[jack@localhost ~]$

   鍙互鐪嬪埌ruby鏄病鏈夊畨瑁呯殑锛屾墍浠ヤ笅涓€姝ユ垜浠瀹夎ruby浜嗐€傘€傘€?



2. 瀹夎ruby 銆愪竴瀹氳鏄鐞嗗憳鏉冮檺鍝︺€?
澶嶅埗浠g爜

[jack@localhost ~]$ sudo
usage: sudo [-D level] -h | -K | -k | -V
usage: sudo -v [-AknS] [-D level] [-g groupname|#gid] [-p prompt] [-u user
            name|#uid]
usage: sudo -l[l] [-AknS] [-D level] [-g groupname|#gid] [-p prompt] [-U user
            name] [-u user name|#uid] [-g groupname|#gid] [command]
usage: sudo [-AbEHknPS] [-r role] [-t type] [-C fd] [-D level] [-g
            groupname|#gid] [-p prompt] [-u user name|#uid] [-g groupname|#gid]
            [VAR=value] [-i|-s] [<command>]
usage: sudo -e [-AknS] [-r role] [-t type] [-C fd] [-D level] [-g
            groupname|#gid] [-p prompt] [-u user name|#uid] file ...
[jack@localhost ~]$ su
Password:
jacsu: incorrect password
[jack@localhost ~]$ yum install ruby
Loaded plugins: fastestmirror, refresh-packagekit, security
You need to be root to perform this command.
[jack@localhost ~]$ jack
bash: jack: command not found
[jack@localhost ~]$ su
Password:
[root@localhost jack]# yum install ruby
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
* base: mirror.bit.edu.cn
* extras: mirror.bit.edu.cn
* updates: mirror.bit.edu.cn
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package ruby.x86_64 0:1.8.7.374-4.el6_6 will be installed
--> Processing Dependency: ruby-libs = 1.8.7.374-4.el6_6 for package: ruby-1.8.7.374-4.el6_6.x86_64
--> Processing Dependency: libruby.so.1.8()(64bit) for package: ruby-1.8.7.374-4.el6_6.x86_64
--> Running transaction check
---> Package ruby-libs.x86_64 0:1.8.7.374-4.el6_6 will be installed
--> Processing Dependency: libreadline.so.5()(64bit) for package: ruby-libs-1.8.7.374-4.el6_6.x86_64
--> Running transaction check
---> Package compat-readline5.x86_64 0:5.2-17.1.el6 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
Package                Arch         Version                   Repository  Size
================================================================================
Installing:
ruby                   x86_64       1.8.7.374-4.el6_6         base       538 k
Installing for dependencies:
compat-readline5       x86_64       5.2-17.1.el6              base       130 k
ruby-libs              x86_64       1.8.7.374-4.el6_6         base       1.7 M

Transaction Summary
================================================================================
Install       3 Package(s)

Total download size: 2.3 M
Installed size: 7.8 M
Is this ok [y/N]: y
Downloading Packages:
(1/3): compat-readline5-5.2-17.1.el6.x86_64.rpm          | 130 kB     00:00    
(2/3): ruby-1.8.7.374-4.el6_6.x86_64.rpm                 | 538 kB     00:00    
(3/3): ruby-libs-1.8.7.374-4.el6_6.x86_64.rpm            | 1.7 MB     00:02    
--------------------------------------------------------------------------------
Total                                           747 kB/s | 2.3 MB     00:03    
warning: rpmts_HdrFromFdno: Header V3 RSA/SHA256 Signature, key ID c105b9de: NOKEY
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
Importing GPG key 0xC105B9DE:
Userid : CentOS-6 Key (CentOS 6 Official Signing Key) <centos-6-key@centos.org>
Package: centos-release-6-5.el6.centos.11.1.x86_64 (@anaconda-CentOS-201311272149.x86_64/6.5)
From   : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
Is this ok [y/N]: y
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : compat-readline5-5.2-17.1.el6.x86_64                         1/3
  Installing : ruby-libs-1.8.7.374-4.el6_6.x86_64                           2/3
  Installing : ruby-1.8.7.374-4.el6_6.x86_64                                3/3
  Verifying  : compat-readline5-5.2-17.1.el6.x86_64                         1/3
  Verifying  : ruby-libs-1.8.7.374-4.el6_6.x86_64                           2/3
  Verifying  : ruby-1.8.7.374-4.el6_6.x86_64                                3/3

Installed:
  ruby.x86_64 0:1.8.7.374-4.el6_6                                              

Dependency Installed:
  compat-readline5.x86_64 0:5.2-17.1.el6  ruby-libs.x86_64 0:1.8.7.374-4.el6_6

Complete!
[root@localhost jack]#

澶嶅埗浠g爜



3. 瀹夎rubygems

     鎺ョ潃鎴戜滑鍐嶆杩愯 replicas鍛戒护锛岀湅鐪嬩細鎬庝箞鏍枫€傘€傘€?

[root@localhost jack]# cluster/redis-trib.rb  create --replicas 1 192.168.161.133:6379 192.168.161.133:6380 192.168.161.133:6381 192.168.161.133:6382 192.168.161.133:6383 192.168.161.133:6384
cluster/redis-trib.rb:24:in `require': no such file to load -- rubygems (LoadError)
    from cluster/redis-trib.rb:24
[root@localhost jack]#

     缁撴灉鑻﹂€间簡锛岃繕鏄姤閿欙紝鐪嬫彁绀鸿矊浼兼槸灏戜簡涓€涓猺ubygems锛岃繖娆℃垜浠繕鏄€氳繃yum瀹夎銆?
澶嶅埗浠g爜

[root@localhost jack]# yum install -y rubygems
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
* base: mirror.bit.edu.cn
* extras: mirror.bit.edu.cn
* updates: mirror.bit.edu.cn
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package rubygems.noarch 0:1.3.7-5.el6 will be installed
--> Processing Dependency: ruby-rdoc for package: rubygems-1.3.7-5.el6.noarch
--> Running transaction check
---> Package ruby-rdoc.x86_64 0:1.8.7.374-4.el6_6 will be installed
--> Processing Dependency: ruby-irb = 1.8.7.374-4.el6_6 for package: ruby-rdoc-1.8.7.374-4.el6_6.x86_64
--> Running transaction check
---> Package ruby-irb.x86_64 0:1.8.7.374-4.el6_6 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
Package           Arch           Version                    Repository    Size
================================================================================
Installing:
rubygems          noarch         1.3.7-5.el6                base         207 k
Installing for dependencies:
ruby-irb          x86_64         1.8.7.374-4.el6_6          base         317 k
ruby-rdoc         x86_64         1.8.7.374-4.el6_6          base         381 k

Transaction Summary
================================================================================
Install       3 Package(s)

Total download size: 905 k
Installed size: 3.0 M
Downloading Packages:
(1/3): ruby-irb-1.8.7.374-4.el6_6.x86_64.rpm             | 317 kB     00:00    
(2/3): ruby-rdoc-1.8.7.374-4.el6_6.x86_64.rpm            | 381 kB     00:00    
(3/3): rubygems-1.3.7-5.el6.noarch.rpm                   | 207 kB     00:00    
--------------------------------------------------------------------------------
Total                                           625 kB/s | 905 kB     00:01    
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : ruby-irb-1.8.7.374-4.el6_6.x86_64                            1/3
  Installing : ruby-rdoc-1.8.7.374-4.el6_6.x86_64                           2/3
  Installing : rubygems-1.3.7-5.el6.noarch                                  3/3
  Verifying  : ruby-rdoc-1.8.7.374-4.el6_6.x86_64                           1/3
  Verifying  : ruby-irb-1.8.7.374-4.el6_6.x86_64                            2/3
  Verifying  : rubygems-1.3.7-5.el6.noarch                                  3/3

Installed:
  rubygems.noarch 0:1.3.7-5.el6                                                

Dependency Installed:
  ruby-irb.x86_64 0:1.8.7.374-4.el6_6    ruby-rdoc.x86_64 0:1.8.7.374-4.el6_6  

Complete!
[root@localhost jack]#

澶嶅埗浠g爜



4. 瀹夎redis椹卞姩



   杩樻槸缁х画涓嶆蹇冿紝鎴戜滑缁х画杩愯replicas鍛戒护鍛戒护銆?

[root@localhost jack]# cluster/redis-trib.rb  create --replicas 1 192.168.161.133:6379 192.168.161.133:6380 192.168.161.133:6381 192.168.161.133:6382 192.168.161.133:6383 192.168.161.133:6384
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require': no such file to load -- redis (LoadError)
    from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
    from cluster/redis-trib.rb:25
[root@localhost jack]#



鍗фЫ锛岃繕鏄湁闂锛岃繖娆¤矊浼兼槸涓€涓猤em_original_require娌℃湁瀹夎锛屽綋鐒惰繖涓竴鑸槸璇磖uby鐗堟湰澶€佷簡锛屾墍浠ョ幇鍦ㄤ翰瑕佷箞鍗囩骇ruby鐗堟湰锛岃涔堢洿鎺ュ畨瑁?

ruby鐨剅edis椹卞姩銆?

[root@localhost jack]# gem install redis
Successfully installed redis-3.3.0
1 gem installed
Installing ri documentation for redis-3.3.0...
Installing RDoc documentation for redis-3.3.0...



6. 杩愯replicas鍛戒护

   缁堜簬璨屼技鎵€鏈夌殑渚濊禆鎴戜滑閮借В鍐充簡锛屾帴涓嬫潵灏卞彲浠ョ湡鐨勬墽琛屼簡锛岀湡鏄竴鎶婅緵閰告唱鍟娿€傘€傘€傛搷銆傘€傘€?
澶嶅埗浠g爜

[root@localhost jack]# cluster/redis-trib.rb  create --replicas 1 192.168.161.133:6379 192.168.161.133:6380 192.168.161.133:6381 192.168.161.133:6382 192.168.161.133:6383 192.168.161.133:6384
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.161.133:6379
192.168.161.133:6380
192.168.161.133:6381
Adding replica 192.168.161.133:6382 to 192.168.161.133:6379
Adding replica 192.168.161.133:6383 to 192.168.161.133:6380
Adding replica 192.168.161.133:6384 to 192.168.161.133:6381
M: fdae457e803e2e04a7c549c69b44a2beefdae3bc 192.168.161.133:6379
   slots:0-5460 (5461 slots) master
M: 74285cee0fa65e64b443ecc630e447a8a65ee9f8 192.168.161.133:6380
   slots:5461-10922 (5462 slots) master
M: 14ed067a1b85044325d5800fa8479a6b4e41a10c 192.168.161.133:6381
   slots:10923-16383 (5461 slots) master
S: c50b1ff1fe3a00b16703f3b5705d2dafe046240b 192.168.161.133:6382
   replicates fdae457e803e2e04a7c549c69b44a2beefdae3bc
S: 8f7bd92f7ffb48e327820693a5820b5be7ea5556 192.168.161.133:6383
   replicates 74285cee0fa65e64b443ecc630e447a8a65ee9f8
S: 4a85ed078b4c99afad7f3a9a8df09082c681a649 192.168.161.133:6384
   replicates 14ed067a1b85044325d5800fa8479a6b4e41a10c
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.161.133:6379)
M: fdae457e803e2e04a7c549c69b44a2beefdae3bc 192.168.161.133:6379
   slots:0-5460 (5461 slots) master
M: 74285cee0fa65e64b443ecc630e447a8a65ee9f8 192.168.161.133:6380
   slots:5461-10922 (5462 slots) master
M: 14ed067a1b85044325d5800fa8479a6b4e41a10c 192.168.161.133:6381
   slots:10923-16383 (5461 slots) master
M: c50b1ff1fe3a00b16703f3b5705d2dafe046240b 192.168.161.133:6382
   slots: (0 slots) master
   replicates fdae457e803e2e04a7c549c69b44a2beefdae3bc
M: 8f7bd92f7ffb48e327820693a5820b5be7ea5556 192.168.161.133:6383
   slots: (0 slots) master
   replicates 74285cee0fa65e64b443ecc630e447a8a65ee9f8
M: 4a85ed078b4c99afad7f3a9a8df09082c681a649 192.168.161.133:6384
   slots: (0 slots) master
   replicates 14ed067a1b85044325d5800fa8479a6b4e41a10c
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@localhost jack]#

澶嶅埗浠g爜

浠庝笂闈㈠彲浠ョ湅鍒帮紝trib绋嬪簭鍛婄煡鎴戜滑鏄惁鐢?6379锛?380锛?381 浣滀负涓伙紝 6382锛?383锛?384浣滀负浠庛€傘€傜劧鍚庢垜灏辨仼鍑嗕簡銆傘€傚氨杩欐牱鎴戜滑鐨勯泦缇?

灏卞垱寤哄ソ浜嗭紝濂戒簡锛岃繖绡囧氨璁插埌杩欓噷锛屼篃涓嶆槸鐗瑰埆澶嶆潅鍚~~