叢集中應該至少有三個節點,每個節點都有一個備份節點。需要6台伺服器。
如果條件有限,可以建構偽分佈式,以下步驟是在一台 linux 伺服器上建構一個有6個節點的 redis叢集。
mkdir /usr/local /redis-cluster
wget http://download.redis.io/releases/redis-5.0.0.tar.gz tar xzf redis-5.0.0.tar.gz cd redis-5.0.0 make make install prefix=/usr/local/redis
/root/software/redis/redis-cluster-conf/7001/redis.conf一些操作指令僅供參考:/ root/software/redis/redis-cluster-conf/7002/redis.conf
/root/software/redis/redis-cluster-conf/7003/redis.conf
#/root/software/redis/redis- cluster-conf/7004/redis.conf
/root/software/redis/redis-cluster-conf/7005/redis.conf
/root/software/redis/redis-cluster-conf/7006/redis. conf
cp redis.conf /usr/local/redis/bin cd /usr/local/redis/ cp -r bin ../redis-cluster/redis01 cd /usr/local/redis-cluster/redis01 rm dump.rdb #删除快照 vim redis.conf
port 7001 #端口 cluster-enabled yes #启用集群模式 cluster-config-file nodes.conf cluster-node-timeout 5000 #超时时间 appendonly yes daemonize yes #后台运行 protected-mode no #非保护模式 pidfile /var/run/redis_7001.pid bind 172.20.10.7 #127.0.0.1改为本机ip地址,可用ifconfig查看ip
[root@master redis-cluster]# cp -r redis01/ redis02 [root@master redis-cluster]# cp -r redis01/ redis03 [root@master redis-cluster]# cp -r redis01/ redis04 [root@master redis-cluster]# cp -r redis01/ redis05 [root@master redis-cluster]# cp -r redis01/ redis06
建立一個批次文件,同時啟動六個redis
vim startall.sh新增以下內容:
cd redis01 ./redis-server redis.conf cd .. cd redis02 ./redis-server redis.conf cd .. cd redis03 ./redis-server redis.conf cd .. cd redis04 ./redis-server redis.conf cd .. cd redis05 ./redis-server redis.conf cd .. cd redis06 ./redis-server redis.conf cd ..
然後執行
chmod u x start-all.sh將start -all.sh
變成可執行檔在目前目錄下啟動:
檢視:
<img src="https://img.php.cn/upload/article/000/887/227/168559062896660.jpg" alt="Centos7下redis5集群搭建與使用的方法" />
啟動後,可看到成功訊息,如下:#rrrr #輸入yes回車
>>> performing hash slots allocation on 6 nodes... master[0] -> slots 0 - 5460 master[1] -> slots 5461 - 10922 master[2] -> slots 10923 - 16383 adding replica 172.20.10.7:7004 to 172.20.10.7:7001 adding replica 172.20.10.7:7005 to 172.20.10.7:7002 adding replica 172.20.10.7:7006 to 172.20.10.7:7003 >>> trying to optimize slaves allocation for anti-affinity [warning] some slaves are in the same host as their master m: a4128b5e581c3722acd9b093c5f29f5056f680b0 172.20.10.7:7001 slots:[0-5460] (5461 slots) master m: d6fed6f21269b8469a3076ac5fb168bd20f70c26 172.20.10.7:7002 slots:[5461-10922] (5462 slots) master m: 51a0f62dacead745ce5351cdbe0bdbae553ce413 172.20.10.7:7003 slots:[10923-16383] (5461 slots) master s: 45cc35740ac67f7988bb75325871ba12d08a76e4 172.20.10.7:7004 replicates a4128b5e581c3722acd9b093c5f29f5056f680b0 s: 668054fe16cdf8741152cae863f5c636ed18b803 172.20.10.7:7005 replicates d6fed6f21269b8469a3076ac5fb168bd20f70c26 s: ae39b7db285703f8c08412d6b04998c60a634295 172.20.10.7:7006 replicates 51a0f62dacead745ce5351cdbe0bdbae553ce413 can i set the above configuration? (type 'yes' to accept):yes
至此,reids5 叢集建置完成。
6、叢集的操作 開啟此檔案修改連接埠為我們自己的,如下所示:
往下看再修改路徑與新增ip位址,如果不加會預設本機127.0.0.1
修改後,執行下列指令關閉叢集: /root/redis-5.0.0/utils/create-cluster/create-cluster stop方法二:#create-cluster目錄下編寫腳本檔:vim shutdown.sh
>>> 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.20.10.7:7001) m: a4128b5e581c3722acd9b093c5f29f5056f680b0 172.20.10.7:7001 slots:[0-5460] (5461 slots) master 1 additional replica(s) m: d6fed6f21269b8469a3076ac5fb168bd20f70c26 172.20.10.7:7002 slots:[5461-10922] (5462 slots) master 1 additional replica(s) s: 45cc35740ac67f7988bb75325871ba12d08a76e4 172.20.10.7:7004 slots: (0 slots) slave replicates a4128b5e581c3722acd9b093c5f29f5056f680b0 m: 51a0f62dacead745ce5351cdbe0bdbae553ce413 172.20.10.7:7003 slots:[10923-16383] (5461 slots) master 1 additional replica(s) s: 668054fe16cdf8741152cae863f5c636ed18b803 172.20.10.7:7005 slots: (0 slots) slave replicates d6fed6f21269b8469a3076ac5fb168bd20f70c26 s: ae39b7db285703f8c08412d6b04998c60a634295 172.20.10.7:7006 slots: (0 slots) slave replicates 51a0f62dacead745ce5351cdbe0bdbae553ce413 [ok] all nodes agree about slots configuration. >>> check for open slots... >>> check slots coverage... [ok] all 16384 slots covered.
chmod u x shutdown.sh
將shutdown.sh變成可執行檔
#在目前目錄下啟動: ./shutdown.sh
查看:ps aux|grep redis
官方:/usr/local/redis-cluster/redis-cli -a xxx -c - h 192.168.5.100 -p 8001
提示:-a存取服務端密碼,-c表示叢集模式,-h指定ip位址,-p指定連接埠號碼
6.2、重新啟動叢集/usr/local/redis-cluster/redis-cli -c -h 172.20.10.7 -p 7001 shutdown /usr/local/redis-cluster/redis-cli -c -h 172.20.10.7 -p 7002 shutdown /usr/local/redis-cluster/redis-cli -c -h 172.20.10.7 -p 7003 shutdown /usr/local/redis-cluster/redis-cli -c -h 172.20.10.7 -p 7004 shutdown /usr/local/redis-cluster/redis-cli -c -h 172.20.10.7 -p 7005 shutdown /usr/local/redis-cluster/redis-cli -c -h 172.20.10.7 -p 7006 shutdown
啟動:./startall.sh
# 7.測試群集 #其中-c表示以叢集方式連接redis,-h指定ip位址,-p指定連接埠號碼
cluster nodes 查詢叢集結點資訊
cluster info 查詢叢集狀態資訊
#以上是Centos7下redis5集群搭建與使用的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!