(Redis系列二)Centos7 搭redis3.2.12叢集-3主3從的6個節點服
1、虛擬機器環境
使用的Linux環境已經版本:
Centos 7 64位系統
主機ip:
172.16.0.9
172.16.0.10
172.16.0.11
每臺伺服器是1主1從,實驗3臺伺服器課成為3主3從。,3個主的不掛,3個從的掛了沒事,但是如果一個主的掛了他會切換至該伺服器的其它從的,如果該從的也掛了那麼叢集就不能獲取資料了
Redis安裝的專案目錄、日誌、配置檔案等都存放在/data/redis/目錄下。
2、下載相關的安裝包以及解壓
首先在172.16.0.9 機器操作:
1 2 3 |
//download.redis.io/releases/redis-3.2.12.tar.gz
|
3、安裝
在/data/soft/redis-3.2.12/目錄下執行:
1 |
|
4、配置資訊
建立叢集配置、log日誌、資料所在的資料夾:
1 2 |
|
建立叢集埠資料夾:
1 2 3 |
|
複製配置檔案到/data/soft/redis-3.2.12/cluster-conf/6379目錄下:
1 |
|
修改6379目錄下redis.conf配置檔案(以下標紅的在原配置中註釋,新增以下內網至頭部):
#註釋掉ip或者設定0.0.0.0都是區域網訪問
bind 0.0.0.0
##埠號
port 6379
#指定了記錄日誌的檔案
logfile "/data/soft/redis-3.2.12/cluster-conf/6379/redis.log"
#該目錄要事先建立好,資料目錄,資料庫的寫入會在這個目錄。rdb、aof檔案也會寫在這個目錄
dir /data/soft/redis-3.2.12/cluster-conf/6379/
#是否開啟叢集
cluster-enabled yes
#叢集配置檔案的名稱,每個節點都有一個叢集相關的配置檔案,持久化儲存叢集的資訊,
#這個檔案並不需要手動配置,這個配置檔案有Redis生成並更新,
#每個Redis叢集節點需要一個單獨的配置檔案,請確保與例項執行的系統中配置檔名稱不衝突。
cluster-config-file nodes-6379.conf
#節點互連超時的閥值。叢集節點超時毫秒數
cluster-node-timeout 5000
#預設redis使用的是rdb方式持久化,這種方式在許多應用中已經足夠用了,
#但是redis如果中途宕機,會導致可能有幾分鐘的資料丟失,
#根據save來策略進行持久化,Append Only File是另一種持久化方式,可以提供更好的持久化特性,
#Redis會把每次寫入的資料在接收後都寫入 appendonly.aof 檔案,
#每次啟動時Redis都會先把這個檔案的資料讀入記憶體裡,先忽略RDB檔案。
appendonly yes
複製6379目錄下redis.conf至6380並修改埠及儲存路徑:
[[email protected] 6379]# cp /data/soft/redis-3.2.12/cluster-conf/6379/redis.conf /data/soft/redis-3.2.12/cluster-conf/6380/
[[email protected] 6380]# vim /data/soft/redis-3.2.12/cluster-conf/6380/redis.conf
5、使用scp複製安裝和配置好的redis複製到其他伺服器上
已經在172.16.0.9機器配置好,複製到 172.16.0.10 和 172.16.0.11 機器
在兩臺新機器上建立資料夾
mkdir /data/soft
複製:
1 2 |
|
1 2 |
|
6、啟動3臺機器的redis
3臺機器執行啟動方式:
[[email protected] 6380]# ps -ef|grep redis
root 12500 6029 0 15:43 pts/0 00:00:00 grep --color=auto redis
[[email protected] /]# /data/soft/redis-3.2.12/bin/redis-server /data/soft/redis-3.2.12/cluster-conf/6379/redis.conf &
[1] 12666
[[email protected] /]# /data/soft/redis-3.2.12/bin/redis-server /data/soft/redis-3.2.12/cluster-conf/6380/redis.conf &
[2] 12678
[[email protected] /]# ps -ef|grep redis
root 12666 6029 0 15:45 pts/0 00:00:00 /data/soft/redis-3.2.12/bin/redis-server 0.0.0.0:6379 [cluster]
root 12678 6029 0 15:45 pts/0 00:00:00 /data/soft/redis-3.2.12/bin/redis-server 0.0.0.0:6380 [cluster]
root 12682 6029 0 15:45 pts/0 00:00:00 grep --color=auto redis
[[email protected] 6379]# netstat -tunlp|grep redis
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 12666/redis-server
tcp 0 0 0.0.0.0:6380 0.0.0.0:* LISTEN 12678/redis-server
tcp 0 0 0.0.0.0:16379 0.0.0.0:* LISTEN 12666/redis-server
tcp 0 0 0.0.0.0:16380 0.0.0.0:* LISTEN 12678/redis-server
[[email protected] 6379]#
7、建立叢集
部署環境
yum install ruby -y
yum install rubygems -y
gem install redis
報錯,提示ruby版本需要大於2.2.2版本。CentOS7 yum庫中ruby的版本支援到 2.0.0,可gem 安裝redis需要最低是2.2.2
需要安裝個高版本的ruby
1 2 3 4 5 6 |
|
檢視rvm庫中已知的ruby版本
[[email protected] 6379]# rvm list known
[ruby-]1.8.6[-p420]
[ruby-]1.8.7[-head] # security released on head
[ruby-]1.9.1[-p431]
[ruby-]1.9.2[-p330]
[ruby-]1.9.3[-p551]
[ruby-]2.0.0[-p648]
[ruby-]2.1[.10]
[ruby-]2.2[.10]
[ruby-]2.3[.7]
[ruby-]2.4[.4]
[ruby-]2.5[.1]
[ruby-]2.6[.0-preview2]
檢視現在的版本
[[email protected] 6379]# ruby --version
ruby 2.0.0p648 (2015-12-16) [x86_64-linux]
使用一個ruby新版本
[[email protected] 6379]# rvm install 2.4.4
使用ruby新版本
rvm use 2.4.4
解除安裝一箇舊版本
rvm remove 2.0.0
檢視現在版本
在重新安裝ruby相關的redis就可以了
[[email protected] 6379]# gem install redis
Fetching: redis-4.0.2.gem (100%)
Successfully installed redis-4.0.2
Parsing documentation for redis-4.0.2
Installing ri documentation for redis-4.0.2
Done installing documentation for redis after 1 seconds
1 gem installed
開啟叢集
[[email protected] 6379]# /data/soft/redis-3.2.12/src/redis-trib.rb create --replicas 1 172.16.0.9:6379 172.16.0.9:6380 172.16.0.10:6379 172.16.0.10:6380 172.16.0.11:6379 172.16.0.11:6380
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
172.16.0.9:6379
172.16.0.10:6379
172.16.0.11:6379
Adding replica 172.16.0.10:6380 to 172.16.0.9:6379
Adding replica 172.16.0.9:6380 to 172.16.0.10:6379
Adding replica 172.16.0.11:6380 to 172.16.0.11:6379
M: b4f27f6441fc4e5dbf049268236bab7c0a62d4ef 172.16.0.9:6379
slots:0-5460 (5461 slots) master
S: 81e21ecba318e62a13c196c3ce64b75ac02b08ad 172.16.0.9:6380
replicates de36ff808165790059cbc67b82fdc8b10c05b67d
M: de36ff808165790059cbc67b82fdc8b10c05b67d 172.16.0.10:6379
slots:5461-10922 (5462 slots) master
S: 5e03f8867ae368f3a79fd4dd6c49eb46750fa8e7 172.16.0.10:6380
replicates b4f27f6441fc4e5dbf049268236bab7c0a62d4ef
M: 6b9dba0f0e9939b6020a1e20a8918a0f6a80fb68 172.16.0.11:6379
slots:10923-16383 (5461 slots) master
S: d34f949cd71e296d8b65c5c9420dfcdddae2b09e 172.16.0.11:6380
replicates 6b9dba0f0e9939b6020a1e20a8918a0f6a80fb68
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 172.16.0.9:6379)
M: b4f27f6441fc4e5dbf049268236bab7c0a62d4ef 172.16.0.9:6379
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: 5e03f8867ae368f3a79fd4dd6c49eb46750fa8e7 172.16.0.10:6380
slots: (0 slots) slave
replicates b4f27f6441fc4e5dbf049268236bab7c0a62d4ef
M: de36ff808165790059cbc67b82fdc8b10c05b67d 172.16.0.10:6379
slots:5461-10922 (5462 slots) master
1 additional replica(s)
M: 6b9dba0f0e9939b6020a1e20a8918a0f6a80fb68 172.16.0.11:6379
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: d34f949cd71e296d8b65c5c9420dfcdddae2b09e 172.16.0.11:6380
slots: (0 slots) slave
replicates 6b9dba0f0e9939b6020a1e20a8918a0f6a80fb68
S: 81e21ecba318e62a13c196c3ce
slots: (0 slots) slave
replicates de36ff808165790059cbc67b82fdc8b10c05b67d
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
完成安裝
進伺服器測試
[[email protected] bin]# /data/soft/redis-3.2.12/bin/redis-cli -c -h 172.16.0.9 -p 6379
172.16.0.9:6379> set aaa bbb
172.16.0.9:6379> keys *
1) "bbb"
172.16.0.9:6379> get aaa
-> Redirected to slot [10439] located at 172.16.0.10:6379
"bbb"
172.16.0.10:6379> exit
如果需要關閉redis叢集
[[email protected] bin]# netstat -tunlp|grep redis
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 14546/redis-server
tcp 0 0 0.0.0.0:6380 0.0.0.0:* LISTEN 9626/redis-server 0
tcp 0 0 0.0.0.0:16379 0.0.0.0:* LISTEN 14546/redis-server
tcp 0 0 0.0.0.0:16380 0.0.0.0:* LISTEN 9626/redis-server 0
[[email protected] 6379]# kill 9626
[[email protected] 6379]# kill 14546
其它兩臺也是
如果設定密碼
則修改配置檔案redis.conf
vim /data/soft/redis-3.2.12/cluster-conf/6379/redis.conf
vim /data/soft/redis-3.2.12/cluster-conf/6380/redis.conf
增加一列
requirepass “111111”
或者線上修改,上面的沒試通,下面的ok,登入這6個節點,全部執行一次,
masterauth為從節點訪問主節點的密碼requirepass為客戶端程式訪問redis時候的連線密碼
[[email protected] bin]# redis-cli -c -p 6379
config set masterauth wuwei111
config set requirepass wuwei111
再登入6個節點,執行config rewrite,就會把配置寫入啟動檔案redis.conf中
[[email protected] bin]# redis-cli -c -p 6379 -a wuwei111
config rewrite
重啟動服務啟動操作:
1、啟動了6個節點的redis
/data/soft/redis-3.2.12/bin/redis-server /data/soft/redis-3.2.12/cluster-conf/6379/redis.conf &
/data/soft/redis-3.2.12/bin/redis-server /data/soft/redis-3.2.12/cluster-conf/6380/redis.conf &
2、啟動叢集
/data/soft/redis-3.2.12/src/redis-trib.rb create --replicas 1 172.16.0.9:6379 172.16.0.9:6380 172.16.0.10:6379 172.16.0.10:6380 172.16.0.11:6379 172.16.0.11:6380