如今項目上用redis的話,不多說不用集羣的狀況,畢竟若是生產上只有一臺redis會有極大的風險,好比機器掛掉,或者內存爆掉,就好比咱們生產環境node
曾今也遭遇到這種狀況,致使redis內存不夠掛掉的狀況,固然這些都是咱們及其不能容忍的,第一個必需要作到高可靠,其次纔是高性能,好了,下面我來redis
逐一搭建一下。centos
一:Redis集羣搭建ruby
1. 下載bash
首先去官網下載較新的3.2.0版本,下載方式仍是很是簡單的,好比官網介紹的這樣。app
$ 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配置socket
因爲咱們要作集羣,並且還要redis自帶的redis-trib.rb 能正常運行,咱們須要在集羣中開啓三臺master,三臺slave,因此這裏我須要創建6個文件性能
夾,並且文件夾的名稱就使用端口地址的名字,好比:6389. 6380....6384。ui
3. config配置。this
如今directory的分佈狀況大體如上圖,接下來要作的事情就是配置redis.conf了,在這裏須要配置四個選項。。。
<1> port 端口地址,好比6380文件夾下面的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
這個顧名思義,首先須要開啓redis的cluster模式,而後配置一個cluster-config-file文件,這個文件用於存放redis的實時信息,redis會動態追加和修
改這個conf下面的內容信息,不過要記住,這個nodes-6379.conf 能夠根據 端口文件夾依次配置,好比6380文件夾能夠改爲nodes-6380.conf這樣。。。
# 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
<3> directory
爲了方便管理,我這裏配置的root目錄取決於在哪一個文件夾,好比6380下面個人dir就是: dir ./6380/
# Note that you must specify a directory here, not a file name. dir ./6379/
<4> protected-mode
這個是redis 3.2 才追加的一個功能,從功能註釋中,咱們就能夠發現,這個默認就是不讓外界能夠訪問redis,因此這裏咱們就改成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就修改完畢了,其餘端口的文件夾也能夠依次配置之~
二:開啓redis
到如今爲止,各個端口文件夾都配置成功了,接下來準備開啓了,真的好麼麼噠~~~,窗口太多,有點萌萌的。
接下來咱們能夠看一下,在6379下面是否是有生成node-6379.conf文件,好比下面:
三:配置redis-trib.rb
由於redis-trib.rb是ruby寫的,而咱們的電腦確定是沒有ruby和一些配置依賴項,不過不要緊,有強大的yum安裝,一切都不是問題。
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 【必定要是管理員權限哦】
[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]#
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]#
結果苦逼了,仍是報錯,看提示貌似是少了一個rubygems,此次咱們仍是經過yum安裝。
[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]#
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]#
臥槽,仍是有問題,此次貌似是一個gem_original_require沒有安裝,固然這個通常是說ruby版本太老了,因此如今親要麼升級ruby版本,要麼直接安裝
ruby的redis驅動。
[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命令
終於貌似全部的依賴咱們都解決了,接下來就能夠真的執行了,真是一把辛酸淚啊。。。操。。。
[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]#
從上面能夠看到,trib程序告知咱們是否用 6379,6380,6381 做爲主, 6382,6383,6384做爲從。。而後我就恩准了。。就這樣咱們的集羣
就建立好了,好了,這篇就講到這裏,也不是特別複雜吧~~~