Redis集群部署的過(guò)程詳解
Redis集群部署
1 Redis各節(jié)點(diǎn)部署
使用源碼安裝各節(jié)點(diǎn),不過(guò)與非cluster方式不同的是,配置文件中需啟動(dòng)cluster相關(guān)的配置。
因本次為偽分布式部署,生產(chǎn)環(huán)境部署時(shí)建議至少3臺(tái)機(jī)器部署(其中每臺(tái)機(jī)器1主1從)
ip | port |
192.168.56.101 | 7000 |
192.168.56.101 | 7001 |
192.168.56.101 | 7002 |
192.168.56.101 | 7003 |
192.168.56.101 | 7004 |
192.168.56.101 | 7005 |
1.1 啟動(dòng)cluster各節(jié)點(diǎn)
創(chuàng)建數(shù)據(jù)目錄
mkdir -p /data/redis/cluster/{7000,7001,7002,7003,7004,7005}
配置文件中主要修改如下內(nèi)容,其他的可按需調(diào)整,也可保持默認(rèn)值,各節(jié)點(diǎn)中注意修改對(duì)應(yīng)的端口號(hào)
bind 192.168.56.101 port 7000 daemonize yes pidfile /data/redis/cluster/7000/redis_7000.pid logfile "/data/redis/cluster/7000/redis_7000.log" appendonly yes appendfilename "appendonly.aof" appendfsync everysec cluster-enabled yes cluster-config-file nodes-7000.conf #注意此文件自動(dòng)生成,且初始化時(shí)不要有和此重名的文件 cluster-node-timeout 5000 cluster-slave-validity-factor 10 cluster-migration-barrier 1 cluster-require-full-coverage yes cluster-slave-no-failover no
啟動(dòng)各節(jié)點(diǎn),建議用redis用戶啟動(dòng)
useradd redis chown -R redis:redis /data/redis/ su - redis cd /data/redis/cluster/7001 cp /data/redis/cluster/7000/redis.conf . sed -i "s#7000#7001#g" redis.conf redis-server redis.conf
其他節(jié)點(diǎn)和7001類似啟動(dòng),啟動(dòng)后進(jìn)程中會(huì)標(biāo)記redis節(jié)點(diǎn)以cluster模式啟動(dòng)
2. 按照依賴
因redis5之前版本前cluster安裝依賴ruby,且版本要求比較苛刻,本次安裝的版本redis4.0.14,依賴的ruby版本為>=ruby2.4,因此大家安裝時(shí)可以安裝高版本的ruby,本次使用的是ruby2.7.5版本
2.1 編譯安裝ruby
下載ruby,建議從官網(wǎng)下載源碼進(jìn)行編譯安裝https://www.ruby-lang.org/en/downloads/
tar -zxvf ruby-2.7.5.tar.gz cd ruby-2.7.5 ./configure make make install
安裝完畢后,檢查ruby以及gem版本
2.2 安裝openssl-devel及zlib-devel
安裝完ruby后,使用gem安裝redis包,此時(shí)如果沒有安裝openssl 則回報(bào)如下錯(cuò)誤
gem install redis ERROR: Loading command: install (LoadError) cannot load such file -- openssl ERROR: While executing gem ... (NoMethodError) undefined method `invoke_with_build_args' for nil:NilClass
按照過(guò)程如下:
yum方式先安裝openssl
yum install openssl-devel -y
再進(jìn)入ruby源碼目錄中的ext目錄下,找到openssl目錄,進(jìn)入后進(jìn)行安裝
cd ruby-2.7.5/ext/openssl ruby extconf.rb make make install
zlib-devel包如報(bào)錯(cuò),也可同上方式處理。
在執(zhí)行make,若出現(xiàn)如下報(bào)錯(cuò):
make: *** 沒有規(guī)則可以創(chuàng)建“ossl_asn1.o”需要的目標(biāo)“/include/ruby.h” 停止。
可以在Makefile頂部中的增加 top_srcdir = …/…
再次執(zhí)行 make && make install
2.3 gem安裝redis
gem install redis
3. 初始化redis集群
相關(guān)依賴安裝完成后,即可初始化redis集群,命令及過(guò)程如下:
[redis@localhost redis-4.0.14]$ src/redis-trib.rb create --replicas 1 192.168.56.101:7000 192.168.56.101:7001 192.168.56.101:7002 192.168.56.101:7003 192.168.56.101:7004 192.168.56.101:7005 >>> Creating cluster >>> Performing hash slots allocation on 6 nodes... Using 3 masters: 192.168.56.101:7000 192.168.56.101:7001 192.168.56.101:7002 Adding replica 192.168.56.101:7004 to 192.168.56.101:7000 Adding replica 192.168.56.101:7005 to 192.168.56.101:7001 Adding replica 192.168.56.101:7003 to 192.168.56.101:7002 >>> Trying to optimize slaves allocation for anti-affinity [WARNING] Some slaves are in the same host as their master M: cd6663924f0c31e6b60b815dca9cb7ea863f5573 192.168.56.101:7000 slots:0-5460 (5461 slots) master M: abc1f43c9a4e8813e9da15433ac66cd185dc39fe 192.168.56.101:7001 slots:5461-10922 (5462 slots) master M: 43fa53cec1ae164f784e5d439aaf80ee2f7e35af 192.168.56.101:7002 slots:10923-16383 (5461 slots) master S: 6ffcd16f1d445b0091c6239bc0988fb8a77fac96 192.168.56.101:7003 replicates cd6663924f0c31e6b60b815dca9cb7ea863f5573 S: c523846d491f8df0bc97033b025b0d24375a13f8 192.168.56.101:7004 replicates abc1f43c9a4e8813e9da15433ac66cd185dc39fe S: 905dc9de7e074c282aab44b4ed5680a2020bcf4c 192.168.56.101:7005 replicates 43fa53cec1ae164f784e5d439aaf80ee2f7e35af 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.56.101:7000) M: cd6663924f0c31e6b60b815dca9cb7ea863f5573 192.168.56.101:7000 slots:0-5460 (5461 slots) master 1 additional replica(s) M: 43fa53cec1ae164f784e5d439aaf80ee2f7e35af 192.168.56.101:7002 slots:10923-16383 (5461 slots) master 1 additional replica(s) S: c523846d491f8df0bc97033b025b0d24375a13f8 192.168.56.101:7004 slots: (0 slots) slave replicates abc1f43c9a4e8813e9da15433ac66cd185dc39fe S: 6ffcd16f1d445b0091c6239bc0988fb8a77fac96 192.168.56.101:7003 slots: (0 slots) slave replicates cd6663924f0c31e6b60b815dca9cb7ea863f5573 M: abc1f43c9a4e8813e9da15433ac66cd185dc39fe 192.168.56.101:7001 slots:5461-10922 (5462 slots) master 1 additional replica(s) S: 905dc9de7e074c282aab44b4ed5680a2020bcf4c 192.168.56.101:7005 slots: (0 slots) slave replicates 43fa53cec1ae164f784e5d439aaf80ee2f7e35af [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.
至此,redis集群初始化完畢,各節(jié)點(diǎn)slot范圍及角色也打印出來(lái)了
Redis4 Cluster部署
1、安裝redis集群節(jié)點(diǎn)
因本次為偽分布式部署,生產(chǎn)環(huán)境部署時(shí)建議至少3臺(tái)機(jī)器部署(其中每臺(tái)機(jī)器1主1從),依舊和redis4.0.14的方式一樣部署
**ip ** | **port ** |
192.168.56.101 | 7000 |
192.168.56.101 | 7001 |
192.168.56.101 | 7002 |
192.168.56.101 | 7003 |
192.168.56.101 | 7004 |
192.168.56.101 | 7005 |
1.1 啟動(dòng)cluster各節(jié)點(diǎn)
創(chuàng)建數(shù)據(jù)目錄
mkdir -p /data/redis/cluster/{7000,7001,7002,7003,7004,7005}
配置文件中主要修改如下內(nèi)容,其他的可按需調(diào)整,也可保持默認(rèn)值,各節(jié)點(diǎn)中注意修改對(duì)應(yīng)的端口號(hào)
bind 192.168.56.103 port 7000 daemonize yes pidfile /data/redis/cluster/7000/redis_7000.pid logfile "/data/redis/cluster/7000/redis_7000.log" appendonly yes appendfilename "appendonly.aof" appendfsync everysec cluster-enabled yes cluster-config-file nodes-7000.conf #注意此文件自動(dòng)生成,且初始化時(shí)不要有和此重名的文件 cluster-node-timeout 5000 cluster-slave-validity-factor 10 cluster-migration-barrier 1 cluster-require-full-coverage yes cluster-slave-no-failover no
啟動(dòng)各節(jié)點(diǎn),建議用redis用戶啟動(dòng)
useradd redis chown -R redis:redis /data/redis/ su - redis cd /data/redis/cluster/7001 cp /data/redis/cluster/7000/redis.conf . sed -i "s#7000#7001#g" redis.conf redis-server redis.conf
其他節(jié)點(diǎn)和7001類似啟動(dòng),啟動(dòng)后進(jìn)程中會(huì)標(biāo)記redis節(jié)點(diǎn)以cluster模式啟動(dòng)
2. 初始化集群
redis5.x之后的版本初始化集群相當(dāng)便捷,命令及過(guò)程如下
redis-cli --cluster create --cluster-replicas 1 192.168.56.103:7000 192.168.56.103:7001 192.168.56.103:7002 192.168.56.103:7003 192.168.56.103:7004 192.168.56.103:7005 >>> 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.56.103:7004 to 192.168.56.103:7000 Adding replica 192.168.56.103:7005 to 192.168.56.103:7001 Adding replica 192.168.56.103:7003 to 192.168.56.103:7002 >>> Trying to optimize slaves allocation for anti-affinity [WARNING] Some slaves are in the same host as their master M: 84ea774c08450db01bf5a518e3b9e55fd26d4d34 192.168.56.103:7000 slots:[0-5460] (5461 slots) master M: eb98e53273fd348deb5eabcc6bfffc20484749b1 192.168.56.103:7001 slots:[5461-10922] (5462 slots) master M: e059d418c11401189558d0f33bd5658297c10939 192.168.56.103:7002 slots:[10923-16383] (5461 slots) master S: 23eed1c27ad11c685e5a226e2f82d5c0b6384c79 192.168.56.103:7003 replicates e059d418c11401189558d0f33bd5658297c10939 S: cfa2549ea7782bb4f0ed6d45e9e3704a7d38d540 192.168.56.103:7004 replicates 84ea774c08450db01bf5a518e3b9e55fd26d4d34 S: c3e14c4139bfa88af03ea97679715f051a122806 192.168.56.103:7005 replicates eb98e53273fd348deb5eabcc6bfffc20484749b1 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.56.103:7000) M: 84ea774c08450db01bf5a518e3b9e55fd26d4d34 192.168.56.103:7000 slots:[0-5460] (5461 slots) master 1 additional replica(s) S: 23eed1c27ad11c685e5a226e2f82d5c0b6384c79 192.168.56.103:7003 slots: (0 slots) slave replicates e059d418c11401189558d0f33bd5658297c10939 M: eb98e53273fd348deb5eabcc6bfffc20484749b1 192.168.56.103:7001 slots:[5461-10922] (5462 slots) master 1 additional replica(s) S: c3e14c4139bfa88af03ea97679715f051a122806 192.168.56.103:7005 slots: (0 slots) slave replicates eb98e53273fd348deb5eabcc6bfffc20484749b1 S: cfa2549ea7782bb4f0ed6d45e9e3704a7d38d540 192.168.56.103:7004 slots: (0 slots) slave replicates 84ea774c08450db01bf5a518e3b9e55fd26d4d34 M: e059d418c11401189558d0f33bd5658297c10939 192.168.56.103:7002 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.
看到如下結(jié)果,代表成功配置并分配slot完成
查看各節(jié)點(diǎn)信息也可以用如下命令
[redis@localhost 7005]$ redis-cli -h 192.168.56.103 -p 7000 cluster nodes 23eed1c27ad11c685e5a226e2f82d5c0b6384c79 192.168.56.103:7003@17003 slave e059d418c11401189558d0f33bd5658297c10939 0 1646118171000 4 connected eb98e53273fd348deb5eabcc6bfffc20484749b1 192.168.56.103:7001@17001 master - 0 1646118171604 2 connected 5461-10922 c3e14c4139bfa88af03ea97679715f051a122806 192.168.56.103:7005@17005 slave eb98e53273fd348deb5eabcc6bfffc20484749b1 0 1646118171000 6 connected cfa2549ea7782bb4f0ed6d45e9e3704a7d38d540 192.168.56.103:7004@17004 slave 84ea774c08450db01bf5a518e3b9e55fd26d4d34 0 1646118170000 5 connected e059d418c11401189558d0f33bd5658297c10939 192.168.56.103:7002@17002 master - 0 1646118169590 3 connected 10923-16383 84ea774c08450db01bf5a518e3b9e55fd26d4d34 192.168.56.103:7000@17000 myself,master - 0 1646118171000 1 connected 0-5460
以上就是Redis集群部署的過(guò)程詳解的詳細(xì)內(nèi)容,更多關(guān)于Redis集群部署的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
redis緩存數(shù)據(jù)庫(kù)中數(shù)據(jù)的方法
這篇文章主要為大家詳細(xì)介紹了redis緩存數(shù)據(jù)庫(kù)中數(shù)據(jù)的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07Deepin UOS編譯安裝Redis的實(shí)現(xiàn)步驟
本文主要介紹了Deepin UOS編譯安裝Redis的實(shí)現(xiàn)步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01