您好,登錄后才能下訂單哦!
這篇文章主要介紹“集群的搭建步驟是什么”,在日常操作中,相信很多人在集群的搭建步驟是什么問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”集群的搭建步驟是什么”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
下載redis壓縮包,然后解壓壓縮文件,我們統一將Redis下載在/opt目錄下
進入到解壓縮后的redis文件目錄(此時可以看到Makefile文件),編譯redis源文件;
執行命令如下
$ cd /opt $ wget http://download.redis.io/releases/redis-4.0.9.tar.gz $ tar zxvf redis-4.0.9.tar.gz $ cd redis-4.0.9 $ make $ make install
把編譯好的redis源文件安裝到/usr/local/redis目錄下,如果/local目錄下沒有redis目錄,會自動新建redis目錄;
然后修改該$ ./src/redis-server redis.conf文件->daemonize:no 改為daemonize:yes;
進入/usr/local/redis/bin目錄,直接./redis-server啟動redis(此時為前端啟動redis);
注意,由于Redis的保護模式,只綁定了本機的127.0.0.1,從其他機器是不能訪問的。所以我們需要添加本機的ip
192.168.xxx.xxx。
在/bin目錄下通過./redis-server redis.conf啟動redis(此時為后臺啟動)。
$ cd /opt/redis-4.0.9 $ ./src/redis-server .redis.conf
根據官方推薦,集群部署至少要 3 臺以上的master節點,最好使用 3 主 3 從六個節點的模式。
咱們準備 6 個配置文件 ,端口 7001,7002,7003,7004,7005,7006
并在redis-cluster目錄下創建6個節點的配置文件。分別為:
redis-7000.conf redis-7001.conf redis-7002.conf redis-7003.conf redis-7004.conf redis-7005.conf
后面的7000,7001等是redis啟動的端口號。接下來編輯文件的內容:
#該集群階段的端口 port 7000 #為每一個集群節點指定一個pid_file pidfile /var/run/redis_7000.pid #在bind指令后添加本機的ip bind 127.0.0.1 149.28.37.147 #找到Cluster配置的代碼段,使得Redis支持集群 cluster-enabled yes #每一個集群節點都有一個配置文件,這個文件是不能手動編輯的。確保每一個集群節點的配置文件不通 cluster-config-file nodes-7000.conf #集群節點的超時時間,單位:ms,超時后集群會認為該節點失敗 cluster-node-timeout 5000 #最后將appendonly改成yes appendonly yes
看以下啟動情況
首先我們創建Redis的配置文件目錄,如下:
$ cd /opt $ mkdir redis-cluster
這樣一個節點的配置就完成,其他的5個節點也做同樣的配置。并將6個節點的Redis實例啟動:
$ nohup /opt/redis-4.0.9/src/redis-server /opt/redis-cluster/redis-7000.conf & $ nohup /opt/redis-4.0.9/src/redis-server /opt/redis-cluster/redis-7001.conf & $ nohup /opt/redis-4.0.9/src/redis-server /opt/redis-cluster/redis-7002.conf & $ nohup /opt/redis-4.0.9/src/redis-server /opt/redis-cluster/redis-7003.conf & $ nohup /opt/redis-4.0.9/src/redis-server /opt/redis-cluster/redis-7004.conf & $ nohup /opt/redis-4.0.9/src/redis-server /opt/redis-cluster/redis-7005.conf &
使用這6個節點創建集群:
$ /opt/redis-4.0.9/src/redis-trib.rb create --replicas 1 149.28.37.147:7000 149.28.37.147:7001 149.28.37.147:7002 149.28.37.147:7003 149.28.37.147:7004 149.28.37.147:7005
或
$ redis-cli --cluster create 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006 --cluste r-replicas 1
--replicas 1 表示我們希望為集群中的每個主節點創建一個從節點。
執行命令后會顯示:
>>> Creating cluster >>> Performing hash slots allocation on 6 nodes... >>> Using 3 masters: >>> 149.28.37.147:7000 >>> 149.28.37.147:7001 >>> 149.28.37.147:7002 --------------------------------------------- # 執行成功結果如下 # 我們可以看到 7001,7002,7003 成為了 master 節點, # 分別占用了 slot [0-5460],[5461-10922],[10923-16383] >>> Performing hash slots allocation on 6 nodes... Master[0] -> Slots 0 - 5460 Master[1] -> Slots 5461 - 10922 Master[2] -> Slots 10923 - 16383 -------------------------------------------- >>> Adding replica 149.28.37.147:7004 to 149.28.37.147:7000 >>> Adding replica 149.28.37.147:7005 to 149.28.37.147:7001 >>> Adding replica 149.28.37.147:7003 to 149.28.37.147:7002 >>> Trying to optimize slaves allocation for anti-affinity >>> [WARNING] Some slaves are in the same host as their master >>> M: 65625091304b0fa2dd75a00f62b6aceac1701094 149.28.37.147:7000 >>> slots:0-5460 (5461 slots) master >>> M: 4da569bf8402e4f75ab6e0fe7076919c22e3f900 149.28.37.147:7001 >>> slots:5461-10922 (5462 slots) master >>> M: b977680e24f23f8fec96876d9014803ca752e2e2 149.28.37.147:7002 >>> slots:10923-16383 (5461 slots) master >>> S: 7183e47a64bca23157140229352455d1a1407dc2 149.28.37.147:7003 >>> replicates b977680e24f23f8fec96876d9014803ca752e2e2 >>> S: b2f916a643fefef1d43dbd1ef5d22f72c0ee43d6 149.28.37.147:7004 >>> replicates 65625091304b0fa2dd75a00f62b6aceac1701094 >>> S: e362d9aae5fe3e9c343d266a5ab952272e0e37b0 149.28.37.147:7005 >>> replicates 4da569bf8402e4f75ab6e0fe7076919c22e3f900 >>> 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 149.28.37.147:7000) >>> M: 65625091304b0fa2dd75a00f62b6aceac1701094 149.28.37.147:7000 >>> slots:0-5460 (5461 slots) master >>> 1 additional replica(s) >>> M: b977680e24f23f8fec96876d9014803ca752e2e2 149.28.37.147:7002 >>> slots:10923-16383 (5461 slots) master >>> 1 additional replica(s) >>> S: e362d9aae5fe3e9c343d266a5ab952272e0e37b0 149.28.37.147:7005 >>> slots: (0 slots) slave >>> replicates 4da569bf8402e4f75ab6e0fe7076919c22e3f900 >>> S: b2f916a643fefef1d43dbd1ef5d22f72c0ee43d6 149.28.37.147:7004 >>> slots: (0 slots) slave >>> replicates 65625091304b0fa2dd75a00f62b6aceac1701094 >>> M: 4da569bf8402e4f75ab6e0fe7076919c22e3f900 149.28.37.147:7001 >>> slots:5461-10922 (5462 slots) master >>> 1 additional replica(s) >>> S: 7183e47a64bca23157140229352455d1a1407dc2 149.28.37.147:7003 >>> slots: (0 slots) slave >>> replicates b977680e24f23f8fec96876d9014803ca752e2e2 >>> [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... >>> [OK] All 16384 slots covered. >>> 集群搭建完畢。我們可以使用Spring-Boot非常方便的去訪問Redis集群了。
注意 集群模式下要帶參數 -c,表示集群,否則不能正常存取數據!!!
[root@localhost redis-5.0.5]# redis-cli -p 7100 -c
127.0.0.1:7001> set k1 v1 -> Redirected to slot [12706] located at 127.0.0.1:7003 OK
127.0.0.1:7003> get k1 "v1"
[root@localhost redis-5.0.5]# redis-cli -p 7001 -c 127.0.0.1:7001> get k1 -> Redirected to slot [12706] located at 127.0.0.1:7003 "v1"
127.0.0.1:7003>
到此,關于“集群的搭建步驟是什么”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。