溫馨提示×

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

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

NoSQL之Redis——Redis群集

發(fā)布時(shí)間:2020-06-21 20:28:12 來(lái)源:網(wǎng)絡(luò) 閱讀:194 作者:一拳超人007 欄目:系統(tǒng)運(yùn)維

實(shí)驗(yàn)環(huán)境

用兩臺(tái)服務(wù)器模擬6臺(tái)服務(wù)器(添加網(wǎng)卡)

主服務(wù)器M1  192.168.13.128
主服務(wù)器M2  192.168.13.135
主服務(wù)器M3  192.168.13.136
從服務(wù)器S1   192.168.13.129
從服務(wù)器S2   192.168.13.137
從服務(wù)器S3   192.168.13.138

1,在兩臺(tái)服務(wù)器上都安裝Redis

[root@localhost ~]# yum install gcc gcc-c++ make -y  ##安裝環(huán)境組件
[root@localhost ~]# mount.cifs //192.168.100.3/LNMP-C7 /mnt/  ##掛載
Password for root@//192.168.100.3/LNMP-C7:  
[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  ##安裝
[root@localhost redis-5.0.7]# cd utils/
[root@localhost utils]# ./install_server.sh    ##執(zhí)行腳本進(jìn)行配置
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]   ##默認(rèn)端口
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]   ##數(shù)據(jù)文件
Selected default - /var/lib/redis/6379
Please select the redis executable path [] /usr/local/redis/bin/redis-server
##可執(zhí)行文件路徑
[root@localhost utils]# ln -s /usr/local/redis/bin/* /usr/local/bin/   ##便于系統(tǒng)識(shí)別
[root@localhost utils]# netstat -ntap | grep 6379
tcp    0    0 127.0.0.1:6379      0.0.0.0:*      LISTEN   44510/redis-server 

2,在兩臺(tái)服務(wù)器上修改配置文件

[root@localhost utils]# vim /etc/redis/6379.conf 
#bind 127.0.0.1          ##注釋第70行的監(jiān)聽(tīng)127地址,已監(jiān)聽(tīng)所有地址
protected-mode no     ##去掉第89行注釋關(guān)閉安全保護(hù)
port 6379                   ##去掉第93行注釋,開(kāi)啟端口6379
daemonize yes          ##去掉第137行注釋,以獨(dú)立進(jìn)程啟動(dòng)
cluster-enabled yes   ##去掉第833行注釋,開(kāi)啟群集功能
cluster-config-file nodes-6379.conf  ##去掉第841行注釋,群集名稱(chēng)文件設(shè)置
cluster-node-timeout 15000             ##去掉第847行注釋,群集超時(shí)時(shí)間設(shè)置
appendonly yes                                ##去掉第700行注釋,開(kāi)啟aof持久化
[root@localhost utils]# /etc/init.d/redis_6379 restart   ##重啟服務(wù)
Stopping ...
Redis stopped
Starting Redis server...
[root@localhost utils]# cd /var/lib/redis/6379/
[root@localhost 6379]# ls
appendonly.aof  dump.rdb  nodes-6379.conf   ##生成aof,rdb和節(jié)點(diǎn)文件

3,在主服務(wù)器上安裝rvm,Ruby控制群集軟件

[root@master 6379]# gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
##導(dǎo)入key文件
[root@master 6379]# curl -sSL https://get.rvm.io | bash -s stable ##安裝rvm
[root@localhost utils]# source /etc/profile.d/rvm.sh  ##執(zhí)行環(huán)境變量
[root@localhost utils]# rvm list known   ##列出ruby可以安裝的版本
[root@localhost utils]# rvm install 2.4.1  ##安裝2.4.1 版本
[root@localhost utils]# rvm use 2.4.1  ##使用rubyruby2.4.1 版本
Using /usr/local/rvm/gems/ruby-2.4.1
[root@localhost utils]# ruby -v   ##查看當(dāng)前ruby2.4.1 版本
ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-linux]
[root@localhost utils]# gem install redis  #再次安裝Redis

4,在主服務(wù)器上添加兩塊網(wǎng)卡

NoSQL之Redis——Redis群集

[root@master ruby-2.4.1]# service network restart   ##重啟網(wǎng)卡
[root@master ruby-2.4.1]# systemctl stop firewalld.service   ##關(guān)閉防火墻
[root@master ruby-2.4.1]# setenforce 0

5,在從服務(wù)器上也添加兩塊網(wǎng)卡

NoSQL之Redis——Redis群集

[root@slave utils]# service network restart   ##重啟網(wǎng)卡
[root@slave utils]# systemctl stop firewalld.service   ##關(guān)閉防火墻
[root@slave utils]# setenforce 0

6,在master服務(wù)器上創(chuàng)建集群

##6個(gè)實(shí)例分為三組,每組一主一從
[root@master ruby-2.4.1]# redis-cli --cluster create 192.168.13.128:6379 192.168.13.129:6379 192.168.13.135:6379 192.168.13.136:6379 192.168.13.137:6379 192.168.13.138:6379 --cluster-replicas 1
##創(chuàng)建群集,每組一主一從
>>> 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.13.137:6379 to 192.168.13.128:6379
Adding replica 192.168.13.138:6379 to 192.168.13.129:6379
Adding replica 192.168.13.136:6379 to 192.168.13.135:6379
M: 6e0414dbd3045b2793b07be884d7335fd008a72d 192.168.13.128:6379
     slots:[0-5460] (5461 slots) master    ##128,129,135為主
M: cdea81d9007892c55c637e3416993d30fe297ba3 192.168.13.129:6379
     slots:[5461-10922] (5462 slots) master
M: 6e0414dbd3045b2793b07be884d7335fd008a72d 192.168.13.135:6379
     slots:[10923-16383] (5461 slots) master
S: 6e0414dbd3045b2793b07be884d7335fd008a72d 192.168.13.136:6379
     replicates 6e0414dbd3045b2793b07be884d7335fd008a72d  ##136,137,138為副本
S: cdea81d9007892c55c637e3416993d30fe297ba3 192.168.13.137:6379
     replicates 6e0414dbd3045b2793b07be884d7335fd008a72d
S: cdea81d9007892c55c637e3416993d30fe297ba3 192.168.13.138:6379
     replicates cdea81d9007892c55c637e3416993d30fe297ba3
...
Can I set the above configuration? (type 'yes' to accept): yes  ##選擇yes

7,驗(yàn)證群集讀寫(xiě)原理

[root@master opt]# redis-cli -h 192.168.13.129 -p 6379   ##主服務(wù)器
192.168.13.129:6379> set name zhangsan   ##創(chuàng)建鍵值對(duì)
OK 
192.168.13.129:6379> keys *
1) "name"
192.168.13.129:6379> get name
"zhangsan"
192.168.13.129:6379> exit
[root@master opt]# redis-cli -h 192.168.13.137 -p 6379   ##從服務(wù)器
192.168.13.137:6379> keys *    ##查看從上也有
1) "name"
192.168.13.137:6379> get name
"zhangsan"
[root@master opt]# redis-cli -h 192.168.13.128 -p 6379 
192.168.13.128:6379> hset person age 20   ##用hash方式建立鍵值對(duì)
(integer) 1
192.168.13.128:6379> hset person name lisi
(integer) 1
192.168.13.128:6379> keys *
1) "person"
192.168.13.128:6379> hget person age   ##獲取鍵的值
"20"
192.168.13.128:6379> expire person 5     ##設(shè)置鍵的刪除時(shí)間5s
(integer) 1
192.168.13.128:6379> keys *
1) "person"
192.168.13.128:6379> keys *
(empty list or set)

謝謝閱讀!

向AI問(wèn)一下細(xì)節(jié)

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

AI