天天看點

redis4.0.8 三主三從叢集建構與踩坑(3個伺服器)

前提

        CentOS 7

        預設已經完成redis單機安裝

安裝ruby2.3.0

        wget https://cache.ruby-lang.org/pub/ruby/2.3/ruby-2.3.1.tar.gz

        進入目錄執行 

                指令一:./configure --prefix=

                指令二:make && make install

        使用ruby -v 檢視版本,如圖則安裝成功

redis4.0.8 三主三從叢集建構與踩坑(3個伺服器)

安裝redis

        gem install redis

修改配置檔案redis.conf

        在redis安裝目錄建立檔案cluster-conf/6379,cluster-conf/6380.

        拷貝redis.conf  至cluster-conf/6379,cluster-conf/6380裡面并修改配置

        以6379端口為例(叢集配置不分主從,其他端口/節點配置是一樣的) 

                port 6379

                bind 0.0.0.0(不限制IP,也可以直接注釋這行配置)

                protected-mode yes(保護模式 外網通路需配置bind ip或者設定通路密碼)

                daemonize yes(允許背景)

                pidfile /var/run/redis_6379.pid

                logfile "/usr/local/redis/redis-4.0.8/log/redis-6379.log"

                dbfilename dump_6379.rdb

                requirepass jt123(密碼)

                appendfilename "appendonly_6379.aof"

                (以獨立日志的方式記錄每次寫指令,重新開機時再重新執行AOF檔案中的指令達到恢複資料的目的。AOF的主要作用是解決了資料持久化的實時性,目前已經是Redis持久化的主流方式。)

                cluster-enabled yes(開啟叢集模式)

                cluster-config-file nodes-6379.conf(確定在系統中執行個體沒有重複叢集配置檔案名稱)

                 cluster-node-timeout 15000(逾時時間)

啟動redis

        3個伺服器都要執行下面指令

                redis-server ./cluster-conf/6379/redis.conf 

                redis-server ./cluster-conf/6380/redis.conf      

        确認效果,如下圖則啟動成功

redis4.0.8 三主三從叢集建構與踩坑(3個伺服器)

建構叢集

        任何伺服器節點執行下面指令(伺服器ip)

                redis-trib.rb create --replicas 1 1.14.69.85:6379 1.14.61.12:6380 139.155.1.12:6379 139.165.1.63:6380 139.155.39.298:6379 139.135.39.212:6380

        确認效果,如下圖則叢集建構成功

redis4.0.8 三主三從叢集建構與踩坑(3個伺服器)

連接配接測試

        連接配接指令:redis-cli -c -h 139.135.1.23 -p 6379 -a jt123

坑點

        1.建立redis叢集報錯:ERR Slot 0 is already busy (Redis::CommandError)

                錯誤提示是說:slot插槽被占用了、這是因為 搭建叢集前時,以前redis的舊資料和配置資訊沒有清理幹淨。

                解決方案:用redis-cli 登入到每個節點執行  flushall  和 cluster reset  就可以了。

        2.建立redis叢集,Sorry, can't connect to node

                原因:因為redis設定了密碼

                解決方案:

                        需要修改

                                /usr/local/rvm/gems/ruby-2.4.1/gems/redis-4.0.1/lib/redis/client.rb

                                可能大家路徑不一樣,find / -name client.rb找一下,修改password

redis4.0.8 三主三從叢集建構與踩坑(3個伺服器)

         3.搭建Redis叢集遇到的問題:Waiting for the cluster to join...

                原因:叢集端口沒有開放,redis端口+10000

                解決方案:如redis端口6379,叢集時伺服器必須開放16379端口

總結

        redis5.0以下搭建叢集需要安裝ruby環境,這個有點麻煩。其他的有問題百度都能找到解決方案。