天天看點

【搭建步驟】Redis 叢集模式cluster配置及搭建

1、redis從官網下載下傳。本文5.0.7

2、解壓 tar -zxvf

3、編譯 make

4、make install 在src出現redis-server

5、在redis同目錄建立 8001 8002 8003 8004 8005 8006檔案夾

6、将redis安裝目錄/usr/local/redis-4.0.6下的redis.conf檔案拷貝至8001檔案夾,并修改以下配置:

daemonize yes #開啟背景運作

port 8001 #工作端口

bind 172.16.0.15 #綁定機器的内網IP,一定要設定呀老鐵,不要用127.0.0.1

dir /usr/local/redis-cluster/8001/ #指定工作目錄,rdb,aof持久化檔案将會放在該目錄下,不同執行個體一定要配置不同的工作目錄

cluster-enabled yes #啟用叢集模式

cluster-config-file nodes-8001.conf #生成的叢集配置檔案名稱,叢集搭建成功後會自動生成,在工作目錄下

cluster-node-timeout 5000 #節點當機發現時間,可以了解為主節點當機後從節點更新為主節點時間

appendonly yes #開啟AOF模式

pidfile /var/run/redis_8001.pid #pid file所在目錄

7、把8001檔案夾下的redis.conf檔案拷貝到其他5個目錄,并重新修改port 、dir、cluster-config-file 三個屬性

8、由于建立叢集需要用到redis-trib這個指令,它依賴Ruby和RubyGems,是以我們要先安裝一下。可能出現ruby版本太舊需要更新

9、Ruby安裝完成之後,我們開始啟動6個節點

[root@VM_0_15_centos redis-4.0.6]# ./src/redis-server redis-cluster/8001/redis.conf [root@VM_0_15_centos redis-4.0.6]# ./src/redis-server redis-cluster/8002/redis.conf [root@VM_0_15_centos redis-4.0.6]# ./src/redis-server redis-cluster/8003/redis.conf [root@VM_0_15_centos redis-4.0.6]# ./src/redis-server redis-cluster/8004/redis.conf [root@VM_0_15_centos redis-4.0.6]# ./src/redis-server redis-cluster/8005/redis.conf [root@VM_0_15_centos redis-4.0.6]# ./src/redis-server redis-cluster/8006/redis.conf

10、.檢視一下服務是否正常起來了

[root@VM_0_15_centos redis-4.0.6]# ps -ef|grep redis

root 20290 1 0 18:33 ? 00:00:02 ./src/redis-server *:8001 [cluster]

root 20295 1 0 18:33 ? 00:00:02 ./src/redis-server *:8002 [cluster]

root 20300 1 0 18:33 ? 00:00:02 ./src/redis-server *:8003 [cluster]

root 20305 1 0 18:33 ? 00:00:02 ./src/redis-server *:8004 [cluster]

root 20310 1 0 18:33 ? 00:00:02 ./src/redis-server *:8005 [cluster]

root 20312 1 0 18:33 ? 00:00:02 ./src/redis-server *:8006 [cluster]

root 22913 15679 0 19:31 pts/2 00:00:00 grep --color=auto redis

11、開始建立叢集

[root@VM_0_15_centos redis-4.0.6]# ./src/redis-trib.rb create --replicas 1 172.16.0.15:8001 172.16.0.15:8002 172.16.0.15:8003 172.16.0.15:8004 172.16.0.15:8005 172.16.0.15:8006

redis指令5.0更改為

./redis-5.0.7/src/redis-cli --cluster create 172.17.13.145:8001 172.17.13.145:8002 172.17.13.145:8003 172.17.13.145:8004 172.17.13.145:8005 172.17.13.145:8006 --cluster-replicas 1

12由控制台可以看到,叢集已經建立完畢,那麼我們到8001這個節點看一下叢集節點狀态

Tips :

  1. 如果想重新建立叢集,需要登入到每個節點,執行flushdb,然後執行cluster reset,重新開機節點;
  2. 如果要批量殺掉Redis程序,可以使用pkill redis-server指令;
  3. 如果redis開啟了密碼認證,則需要在redis.conf中增加屬性 : masterauth yourpassword ,并且需要修改/usr/local/share/gems/gems/redis-3.3.3/lib/redis目錄下的client.rb檔案,将password屬性設定為redis.conf中的requirepass的值,不同的作業系統client.rb的位置可能不一樣,可以使用 find / -name "client.rb"全盤查找一下;
  4. Redis開啟密碼認證後,在叢集操作時問題會比較多,是以建議不要開啟密碼認證,搭配使用防火牆保證Redis的安全。

繼續閱讀