Redis Cluster5.0.6搭建

Redis Cluster

redis cluster 爲了保證數據的高可用性,加入了主從模式,一個主節點對應一個或多個從節點,主節點提供數據存取,從節點則是從主節點拉取數據備份,當這個主節點掛掉後,就會有這個從節點選取一個來充當主節點,從而保證集羣不會掛掉
集羣有ABC三個主節點, 如果這3個節點都沒有加入從節點,如果B掛掉了,我們就無法訪問整個集羣了。A和C的slot也無法訪問。
所以我們在集羣建立的時候,一定要爲每個主節點都添加了從節點, 比如像這樣, 集羣包含主節點A、B、C, 以及從節點A1、B1、C1, 那麼即使B掛掉系統也可以繼續正確工作。
B1節點替代了B節點,所以Redis集羣將會選擇B1節點作爲新的主節點,集羣將會繼續正確地提供服務。 當B重新開啓後,它就會變成B1的從節點。
不過需要注意,如果節點B和B1同時掛了,Redis集羣就無法繼續正確地提供服務了。

Redis集羣的搭建:

集羣中至少應該有奇數個節點,所以至少有三個節點,每個節點至少有一個備份節點,所以下面使用6節點(主節點、備份節點由redis-cluster集羣確定)

redis安裝

首先,檢查防火牆狀態,關閉防火牆,或打開防火牆對應端口,並添加iptables白名單

[root@redis_master01 ~]# firewall-cmd --zone=public --add-port=6379/tcp –permanent
[root@redis_master01 ~]# firewall-cmd --zone=public --add-port=16379/tcp --permanent
[root@redis_master01 ~]# iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 16379 -j ACCEPT
[root@redis_master01 ~]# iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 6379 -j ACCEPT

檢查是否安裝了gcc

[root@localhost ~]# gcc -v

如果沒有安裝,則需要在線安裝,make編譯需要依賴gcc的環境

[root@localhost ~]# yum install –y gcc-c++

下載安裝

下載redis安裝包

[root@localhost ~]# wget http://download.redis.io/releases/redis-5.0.6.tar.gz

解壓並進入redis目錄,查看是否存在Makefile文件,存在則直接make編譯redis源碼
在這裏插入圖片描述
如下則編譯成功
在這裏插入圖片描述
編譯成功後安裝到指定路徑,一般存放於/usr/local下的redis目錄,指令如下:

make install PREFIX=/usr/local/redis

在這裏插入圖片描述
查看/usr/local目錄,發現redis已經成功安裝至此目錄下
在這裏插入圖片描述
進入redis目錄,查看其中的文件
在這裏插入圖片描述
此時安裝已經完成,可以啓動redis。不過由於redis是在前端啓動,如果客戶端關閉,則redis服務也會停止,所以需要後臺啓動redis,後臺啓動方法如下:
第一步:複製redis解壓文件中的redis.conf文件到當前目錄/usr/local/redis/bin下
在這裏插入圖片描述
第二步:修改redis.conf文件中的內容,將 daemonize no -> daemonize yes,這樣就可以修改爲後臺啓動了
在這裏插入圖片描述
後臺啓動redis,可以通過ps命令查看redis服務是否已經啓動
在這裏插入圖片描述
打開redis連接,測試,如果返回PONG表示連接成功
在這裏插入圖片描述

redis集羣的配置

當六臺服務器的redis全部可以啓動時,我們就可以開始搭建集羣
搭建集羣的話,需要使用一個工具(腳本文件),這個工具在redis解壓文件的源代碼裏。因爲這個工具是一個ruby腳本文件,所以這個工具的運行需要ruby的運行環境,就相當於java語言的運行需要在jvm上。所以需要安裝ruby,指令如下:
[root@localhost local]# yum install ruby –y
全部安裝完成後,爲了方便我們分辨機器和管理,我們將機器名改爲如下格式

主機名 IP及端口 主機名 IP及端口
redis_master01 192.168.6.131:6379 redis_slave01 192.168.6.141:6379
redis_master02 192.168.6.132:6379 redis_slave02 192.168.6.142:6379
redis_master03 192.168.6.133:6379 redis_slave03 192.168.6.143:6379

在master01上進行如下配置:

[root@redis_master01 ~]# cd redis-5.0.6/
[root@redis_master01 redis-5.0.6]# mkdir -p /usr/local/redis/bin/
[root@redis_master01 redis-5.0.6]# vim redis.conf 
bind 192.168.6.131 #本機ip
port 6379 #設定的端口
daemonize yes #後臺啓動
cluster-enabled yes #啓動集羣模式
cluster-config-file nodes-6379.conf #集羣內部配置文件,改掉端口號
cluster-node-timeout 15000 #節點超時時間(ms)

啓動所有機器的節點
在這裏插入圖片描述
安裝ruby及它的依賴環境

[root@redis_master01 ~]# yum -y install ruby ruby-devel rubygems rpm-build

使用gem命令安裝redis接口

[root@redis_master01 ~]# gem install redis
Fetching: redis-4.1.3.gem (100%)
ERROR:  Error installing redis:
        redis requires Ruby version >= 2.3.0.
# 此處報錯表示Ruby版本太低需要升級

升級Ruby版本

[root@redis_master01 ~]# curl -L get.rvm.io | bash -s stable
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   194  100   194    0     0    272      0 --:--:-- --:--:-- --:--:--   272
100 24535  100 24535    0     0   7930      0  0:00:03  0:00:03 --:--:-- 18187
Downloading https://github.com/rvm/rvm/archive/1.29.9.tar.gz
Downloading https://github.com/rvm/rvm/releases/download/1.29.9/1.29.9.tar.gz.asc
gpg: Signature made Wed 10 Jul 2019 04:31:02 PM CST using RSA key ID 39499BDB
gpg: Can't check signature: No public key
GPG signature verification failed for '/usr/local/rvm/archives/rvm-1.29.9.tgz' - 'https://github.com/rvm/rvm/releases/download/1.29.9/1.29.9.tar.gz.asc'! Try to install GPG v2 and then fetch the public key:

    gpg2 --keyserver hkp://pool.sks-keyservers.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB

or if it fails:

    command curl -sSL https://rvm.io/mpapis.asc | gpg2 --import -
    command curl -sSL https://rvm.io/pkuczynski.asc | gpg2 --import -

In case of further problems with validation please refer to https://rvm.io/rvm/security

此時得到一個密鑰,然後用該密鑰下載Ruby

[root@redis_master01 ~]# gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
gpg: keyring `/root/.gnupg/secring.gpg' created
gpg: requesting key D39DC0E3 from hkp server keys.gnupg.net
gpg: requesting key 39499BDB from hkp server keys.gnupg.net
gpg: /root/.gnupg/trustdb.gpg: trustdb created
gpg: key D39DC0E3: public key "Michal Papis (RVM signing) <[email protected]>" imported
gpg: key 39499BDB: public key "Piotr Kuczynski <[email protected]>" imported
gpg: no ultimately trusted keys found
gpg: Total number processed: 2
gpg:               imported: 2  (RSA: 2)

下載完成後安裝

[root@redis_master01 ~]# curl -sSL https://get.rvm.io | bash -s stable
Downloading https://github.com/rvm/rvm/archive/1.29.9.tar.gz
Downloading https://github.com/rvm/rvm/releases/download/1.29.9/1.29.9.tar.gz.asc
gpg: Signature made Wed 10 Jul 2019 04:31:02 PM CST using RSA key ID 39499BDB
gpg: Good signature from "Piotr Kuczynski <[email protected]>"
gpg: WARNING: This key is not certified with a trusted signature!
gpg:          There is no indication that the signature belongs to the owner.
Primary key fingerprint: 7D2B AF1C F37B 13E2 069D  6956 105B D0E7 3949 9BDB
GPG verified '/usr/local/rvm/archives/rvm-1.29.9.tgz'
Creating group 'rvm'
Installing RVM to /usr/local/rvm/
Installation of RVM in /usr/local/rvm/ is almost complete:

  * First you need to add all users that will be using rvm to 'rvm' group,
    and logout - login again, anyone using rvm will be operating with `umask u=rwx,g=rwx,o=rx`.

  * To start using RVM you need to run `source /etc/profile.d/rvm.sh`
    in all your open shell windows, in rare cases you need to reopen all shell windows.
  * Please do NOT forget to add your users to the rvm group.
     The installer no longer auto-adds root or users to the rvm group. Admins must do this.
     Also, please note that group memberships are ONLY evaluated at login time.
     This means that users must log out then back in before group membership takes effect!
Thanks for installing RVM [0m
Please consider donating to our open collective to help us maintain RVM.

  Donate: https://opencollective.com/rvm/donate

安裝完成,加載環境,讓rvm可用

[root@redis_master01 ~]# source /usr/local/rvm/scripts/rvm

查看Ruby可用版本
在這裏插入圖片描述
安裝

[root@redis_master01 ~]# rvm install 2.3.0
Searching for binary rubies, this might take some time.
No binary rubies available for: centos/7/x86_64/ruby-2.3.0.
Continuing with compilation. Please read 'rvm help mount' to get more information on binary rubies.
Checking requirements for centos.
Installing requirements for centos.
Installing required packages: autoconf, automake, bison, libffi-devel, libtool, readline-devel, sqlite-devel, zlib-devel, openssl-devel..................
Requirements installation successful.
Installing Ruby from source to: /usr/local/rvm/rubies/ruby-2.3.0, this may take a while depending on your cpu(s)...
ruby-2.3.0 - #downloading ruby-2.3.0, this may take a while depending on your connection...
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 13.5M  100 13.5M    0     0  25197      0  0:09:22  0:09:22 --:--:-- 27717
ruby-2.3.0 - #extracting ruby-2.3.0 to /usr/local/rvm/src/ruby-2.3.0.....
ruby-2.3.0 - #applying patch /usr/local/rvm/patches/ruby/ruby_2_3_gcc7.patch.
ruby-2.3.0 - #applying patch /usr/local/rvm/patches/ruby/changeset_r53419.diff.
ruby-2.3.0 - #applying patch /usr/local/rvm/patches/ruby/2.3.0/random_c_using_NR_prefix.patch.
ruby-2.3.0 - #configuring..........................................................
ruby-2.3.0 - #post-configuration..
ruby-2.3.0 - #compiling.................................................................................
ruby-2.3.0 - #installing............................
ruby-2.3.0 - #making binaries executable..
ruby-2.3.0 - #downloading rubygems-3.0.6
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  866k  100  866k    0     0  27757      0  0:00:31  0:00:31 --:--:-- 23609
No checksum for downloaded archive, recording checksum in user configuration.
ruby-2.3.0 - #extracting rubygems-3.0.6.....
ruby-2.3.0 - #removing old rubygems........
ruby-2.3.0 - #installing rubygems-3.0.6...............................................
ruby-2.3.0 - #gemset created /usr/local/rvm/gems/ruby-2.3.0@global
ruby-2.3.0 - #importing gemset /usr/local/rvm/gemsets/global.gems.............................................-
ruby-2.3.0 - #generating global wrappers.......
ruby-2.3.0 - #gemset created /usr/local/rvm/gems/ruby-2.3.0
ruby-2.3.0 - #importing gemsetfile /usr/local/rvm/gemsets/default.gems evaluated to empty gem list
ruby-2.3.0 - #generating default wrappers.......
ruby-2.3.0 - #adjusting #shebangs for (gem irb erb ri rdoc testrb rake).
Install of ruby-2.3.0 - #complete 
Please be aware that you just installed a ruby that requires 3 patches just to be compiled on an up to date linux system.
This may have known and unaccounted for security vulnerabilities.
Please consider upgrading to ruby-2.6.3 which will have all of the latest security patches.
Ruby was built without documentation, to build it run: rvm docs generate-ri

# 安裝gem redis接口
[root@redis_master01 ~]# rvm -v
rvm 1.29.9 (latest) by Michal Papis, Piotr Kuczynski, Wayne E. Seguin [https://rvm.io]
[root@redis_master01 ~]# ruby --version
ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-linux]
[root@redis_master01 ~]# gem install redis
Fetching redis-4.1.3.gem
Successfully installed redis-4.1.3
Parsing documentation for redis-4.1.3
Installing ri documentation for redis-4.1.3
Done installing documentation for redis after 2 seconds
1 gem installed

# 安裝rubygems
[root@redis_master01 ~]# yum install -y rubygems

此時,所有的ruby和redis-trib.rb需要的環境已經安裝完成

Redis集羣驗證

啓動集羣:

[root@redis_master01 ~]# redis-cli --cluster create 192.168.6.131:6379 192.168.6.132:6379 192.168.6.133:6379 192.168.6.141:6379 192.168.6.142:6379 192.168.6.143:6379 --cluster-replicas 1
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.6.142:6379 to 192.168.6.131:6379
Adding replica 192.168.6.143:6379 to 192.168.6.132:6379
Adding replica 192.168.6.141:6379 to 192.168.6.133:6379
M: 9fbc435ee076f30405e764c511f3d26d822e5f84 192.168.6.131:6379
   slots:[0-5460] (5461 slots) master
M: 0412f62f70b71f537094d2337214adaf5548be61 192.168.6.132:6379
   slots:[5461-10922] (5462 slots) master
M: 436b36279143b5874a1b2cd0a7396d62e5643f4b 192.168.6.133:6379
   slots:[10923-16383] (5461 slots) master
S: e43a902fe87c542c363848d64abbeaa6668eade5 192.168.6.141:6379
   replicates 436b36279143b5874a1b2cd0a7396d62e5643f4b
S: d04a0d358eef0a1af18193b8682bf3d0c9575600 192.168.6.142:6379
   replicates 9fbc435ee076f30405e764c511f3d26d822e5f84
S: ae4054660819b9b9e2458fa118b9e2d76eb50f03 192.168.6.143:6379
   replicates 0412f62f70b71f537094d2337214adaf5548be61
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.6.131:6379)
M: 9fbc435ee076f30405e764c511f3d26d822e5f84 192.168.6.131:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: ae4054660819b9b9e2458fa118b9e2d76eb50f03 192.168.6.143:6379
   slots: (0 slots) slave
   replicates 0412f62f70b71f537094d2337214adaf5548be61
M: 0412f62f70b71f537094d2337214adaf5548be61 192.168.6.132:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: e43a902fe87c542c363848d64abbeaa6668eade5 192.168.6.141:6379
   slots: (0 slots) slave
   replicates 436b36279143b5874a1b2cd0a7396d62e5643f4b
S: d04a0d358eef0a1af18193b8682bf3d0c9575600 192.168.6.142:6379
   slots: (0 slots) slave
   replicates 9fbc435ee076f30405e764c511f3d26d822e5f84
M: 436b36279143b5874a1b2cd0a7396d62e5643f4b 192.168.6.133:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

在master01上登陸,設置數據,然後連接master02進行測試
在這裏插入圖片描述
此時手動關掉一個主節點master02模擬故障宕機,集羣在短暫延遲後自動將從節點推舉爲主節點:
在這裏插入圖片描述
在這裏插入圖片描述
當掛掉的主機重新上線時,又會變爲從節點
在這裏插入圖片描述

添加新的主節點

新建節點,啓動redis服務:
在這裏插入圖片描述
在master1上管理集羣,添加新節點:
添加主節點:

[root@redis_master01 ~]# redis-cli --cluster add-node 192.168.6.134:6379 192.168.6.133:6379
# 添加節點時,要保證此節點爲空,且需要刪除nodes-6379.conf和dump.rdb文件

添加從節點:

[root@redis_master01 bin]# redis-cli --cluster add-node 192.168.6.144:6379 192.168.6.134:6379 --cluster-slave --cluster-master-id ef0b03f125da0fe85a7e06dd3122aeae22f992d1

add-node: 後面的分別跟着新加入的slave和slave對應的master
cluster-slave:表示加入的是slave節點
–cluster-master-id:表示slave對應的master的node ID

在這裏插入圖片描述
在這裏插入圖片描述
檢查集羣,添加成功

分配slot
–cluster-from:表示slot目前所在的節點的node ID,多個ID用逗號分隔
–cluster-to:表示需要新分配節點的node ID(貌似每次只能分配一個)
–cluster-slots:分配的slot數量

在這裏插入圖片描述
在線遷移數據槽
在這裏插入圖片描述
5461表示需要分配的節點槽數量
9fbc435ee076f30405e764c511f3d26d822e5f84新節點的node id
all表示全部節點重新分配
在這裏插入圖片描述
輸入yes確認重新分配
在這裏插入圖片描述
平衡各節點槽數量

[root@redis_master01 ~]# redis-cli --cluster rebalance --cluster-threshold 1 192.168.6.131:6379

在這裏插入圖片描述
平衡完成
在這裏插入圖片描述

解決報錯

如果在添加集羣時出現一直等待的問題,則是防火牆和iptables對應端口沒有打開:
在這裏插入圖片描述
執行

[root@redis_master01 ~]# firewall-cmd --zone=public --add-port=6379/tcp –permanent
[root@redis_master01 ~]# firewall-cmd --zone=public --add-port=16379/tcp --permanent
[root@redis_master01 ~]# iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 16379 -j ACCEPT
[root@redis_master01 ~]# iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 6379 -j ACCEPT
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章