溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎樣進行Redis群集搭建

發(fā)布時間:2021-11-09 16:07:10 來源:億速云 閱讀:135 作者:柒染 欄目:建站服務器

怎樣進行Redis群集搭建,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。

Redis群集搭建

實驗環(huán)境

兩臺Centos 7虛擬機,均添加三塊網(wǎng)卡用以模擬六臺服務器實景服務器角色IP地址
centos7-1主節(jié)點M1192.168.142.130
centos7-2主節(jié)點M2192.168.142.145
centos7-3主節(jié)點M3192.168.142.146
centos7-4從節(jié)點S1192.168.142.143
centos7-5從節(jié)點S2192.168.142.147
centos7-min從節(jié)點S3192.168.142.148

實驗步驟

第一步:安裝Redis

#安裝編譯環(huán)境
[root@localhost ~]# yum install gcc gcc-c++ make -y

#遠程掛載源碼包
[root@localhost ~]# mount.cifs //192.168.142.1/redis /mnt
Password for root@//192.168.142.1/redis:  

#解壓源碼包
[root@localhost ~]# cd /mnt
[root@localhost mnt]# tar zxvf redis-5.0.7.tar.gz -C /opt

#編譯與安裝
[root@localhost mnt]# cd /opt/redis-5.0.7/
[root@localhost redis-5.0.7]# make
[root@localhost redis-5.0.7]# make PREFIX=/usr/local/redis/ install

#建立服務命令軟鏈接到系統(tǒng)
[root@localhost redis-5.0.7]# ln -s /usr/redis/bin/* /usr/local/bin/

#切入utils目錄
[root@localhost redis-5.0.7]# cd /opt/redis-5.0.7/utils/

#執(zhí)行啟動腳本
[root@localhost utils]# ./install_server.sh
#以下內(nèi)容,默認回車即可
Welcome to the redis service installer
This script will help you easily set up a running redis server

Please select the redis port for this instance: [6379] 
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf] 
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log] 
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379] 
Selected default - /var/lib/redis/6379
Please select the redis executable path [] /usr/local/redis/bin/redis-server
#此處需手動指定擴展目錄路徑/usr/local/redis/bin/redis-server

#使用進程控制啟動服務
[root@localhost utils]# /etc/init.d/redis_6379 start
Starting Redis server...

#配置redis的6379.conf文件,追加監(jiān)聽地址
[root@localhost utils]# vim /etc/redis/6379.conf 

#注釋第70行的監(jiān)聽127地址,已監(jiān)聽所有地址
#bind 127.0.0.1 

#去掉第89行注釋關(guān)閉安全保護
protected-mode no

#去掉第93行注釋,開啟端口6379
port 6379

#去掉第137行注釋,以獨立進程啟動
daemonize yes

#去掉第833行注釋,開啟群集功能
cluster-enabled yes

#去掉第841行注釋,群集名稱文件設置
cluster-config-file nodes-6379.conf

#去掉第847行注釋,群集超時時間設置
cluster-node-timeout 15000

#去掉第700行注釋,開啟aof持久化
appendonly yes

#重啟服務
[root@localhost utils]# /etc/init.d/redis_6379 restart
Stopping ...
Redis stopped
Starting Redis server...

第二步:主服務器安裝rvm組件

#導入key文件
[root@localhost utils]# gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3

#安裝rvm
[root@localhost utils]# curl -sSL https://get.rvm.io | bash -s stable

#執(zhí)行環(huán)境變量
[root@localhost utils]# source /etc/profile.d/rvm.sh

#列出ruby可以安裝的版本
[root@localhost utils]# rvm list known

#安裝2.4.1 版本
[root@localhost utils]# rvm install 2.4.1

#使用rubyruby2.4.1 版本
[root@localhost utils]# rvm use 2.4.1
Using /usr/local/rvm/gems/ruby-2.4.1

#查看當前ruby2.4.1 版本
[root@localhost utils]# ruby -v
ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-linux]

#再次安裝Redis
[root@localhost utils]# gem install redis

第三步:創(chuàng)建Redis集群

#加入所有節(jié)點地址,并加6379端口號
[root@localhost utils]# redis-cli --cluster create 192.168.142.130:6379 192.168.142.145:6379 192.168.142.146:6379 192.168.142.143:6379 192.168.142.147:6379 192.168.142.148: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.142.147:6379 to 192.168.142.130:6379
Adding replica 192.168.142.148:6379 to 192.168.142.145:6379
Adding replica 192.168.142.143:6379 to 192.168.142.146:6379
M: a27b43ec695099b36a5c79beae70cb0364f27338 192.168.142.130:6379
   slots:[0-5460] (5461 slots) master
M: a27b43ec695099b36a5c79beae70cb0364f27338 192.168.142.145:6379
   slots:[5461-10922] (5462 slots) master
M: a27b43ec695099b36a5c79beae70cb0364f27338 192.168.142.146:6379
   slots:[10923-16383] (5461 slots) master
S: b6d317c5f192bf84e8f464ffbf6481529cd0708a 192.168.142.143:6379
   replicates a27b43ec695099b36a5c79beae70cb0364f27338
S: b6d317c5f192bf84e8f464ffbf6481529cd0708a 192.168.142.147:6379
   replicates a27b43ec695099b36a5c79beae70cb0364f27338
S: b6d317c5f192bf84e8f464ffbf6481529cd0708a 192.168.142.148:6379
   replicates a27b43ec695099b36a5c79beae70cb0364f27338
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.142.130:6379)
M: a27b43ec695099b36a5c79beae70cb0364f27338 192.168.142.130:6379
   slots:[0-16383] (16384 slots) master
   1 additional replica(s)
S: b6d317c5f192bf84e8f464ffbf6481529cd0708a 192.168.142.148:6379
   slots: (0 slots) slave
   replicates a27b43ec695099b36a5c79beae70cb0364f27338
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

第四步:驗證群集讀寫

#從主節(jié)點計入數(shù)據(jù)庫寫數(shù)據(jù)
[root@localhost ~]# redis-cli -h 192.168.142.130 -p 6379
192.168.142.130:6379> set name yangjia
OK
192.168.142.130:6379> get name
"yangjia"

#從另一節(jié)點查看數(shù)據(jù)信息
[root@localhost ~]# redis-cli -h 192.168.142.146 -p 6379
192.168.142.146:6379> keys *
1) "name"
192.168.142.146:6379> get name
"yangjia"

#在從節(jié)點無法查看數(shù)據(jù),提示切換主節(jié)點服務器地址
[root@localhost ~]# redis-cli -h 192.168.142.143 -p 6379
192.168.142.143:6379> keys *
1) "name"
192.168.142.143:6379> get name
(error) MOVED 5798 192.168.142.146:6379

看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進一步的了解或閱讀更多相關(guān)文章,請關(guān)注億速云行業(yè)資訊頻道,感謝您對億速云的支持。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI