之前寫過一篇基於redis-3.2.4版本的安裝日記,這篇是基於redis-6.2.6改動不少,故再次記錄一下
兩台電腦10.2.5.147,10.2.5.148,都是centos7.5
本次搭建4主4從集群
1.單機安裝
1.1.下載安裝包
直接從redis官網下載安裝包,官網地址:https://redis.io/download
直接使用命令下載:wget http://download.redis.io/releases/redis-6.2.6.tar.gz
1.2.安裝編譯
將安裝包直接上傳到了/opt目錄
解壓 tar -zxvf redis-6.2.6.tar.gz 之后,得到一個redis-6.2.6的文件夾
個人比較喜歡將文件信息放在/usr/local/redis目錄下,所以cp /opt/redis-6.2.6/* /usr/local/redis
接下來就是編譯了
進入到/usr/local/redis目錄,執行make命令
make編譯報錯
make[3]: 進入目錄“/usr/local/redis/deps/hiredis” cc -std=c99 -pedantic -c -O3 -fPIC -Wall -W -Wstrict-prototypes -Wwrite-strings -Wno-missing-field-initializers -g -ggdb alloc.c make[3]: cc:命令未找到 make[3]: *** [alloc.o] 錯誤 127 make[3]: 離開目錄“/usr/local/redis/deps/hiredis” make[2]: *** [hiredis] 錯誤 2 make[2]: 離開目錄“/usr/local/redis/deps” make[1]: [persist-settings] 錯誤 2 (忽略) CC adlist.o /bin/sh: cc: 未找到命令 make[1]: *** [adlist.o] 錯誤 127 make[1]: 離開目錄“/usr/local/redis/src”
安裝更新gcc
yum isntall gcc-c++
make編譯報錯
致命錯誤:jemalloc/jemalloc.h:沒有那個文件或目錄
分配器allocator, 如果有MALLOC 這個 環境變量, 會有用這個環境變量的 去建立Redis。
而且libc 並不是默認的 分配器, 默認的是 jemalloc, 因為 jemalloc 被證明 有更少的 fragmentation problems 比libc。
但是如果你又沒有jemalloc 而只有 libc 當然 make 出錯。 所以加這么一個參數,運行如下命令:
make MALLOC=libc
make編譯報錯
cc: error: ../deps/hiredis/libhiredis.a: No such file or directory cc: error: ../deps/lua/src/liblua.a: No such file or directory cc: error: ../deps/jemalloc/lib/libjemalloc.a: No such file or directory make: *** [redis-server] Error 1
進入redis下的deps下的運行如下命令
make lua hiredis linenoise
沒有報錯之后,在接下來安裝redis
執行make install命令,最后安裝結果如下
[root@localhost redis]# make install cd src && make install make[1]: 進入目錄“/usr/local/redis/src” Hint: It's a good idea to run 'make test' ;) INSTALL redis-server INSTALL redis-benchmark INSTALL redis-cli make[1]: 離開目錄“/usr/local/redis/src”
安裝成功
1.3 試運行
安裝完成,需要修改如下配置,找到/usr/local/redis目錄下的redis.conf文件
打開文件,修改如下參數
允許遠程連接,將下一行注釋
#bind 127.0.0.1 -::1
redis默認是前台線程,改為后台啟動,修改參數
daemonize yes
設置redis登錄密碼,找到參數設置
requirepass 你的密碼
啟動redis:redis-server /usr/local/redis/redis.conf
啟動之后查看對應redis線程已啟動,redis已可以使用
2.搭建集群
4主4從集群分布:
10.2.5.147 服務器,7010 7011 7012 7013四個節點
10.2.5.148 服務器,7015 7016 7017 7018四個節點
2.1.節點文件拷貝
在/usr/local/redis目錄下新建集群節點目錄redis_cluster文件夾
mkdir -p /usr/local/redis/redis_cluster
在目錄下創建4個節點文件夾
mkdir -p /usr/local/redis/redis_cluster/node01_7010 mkdir -p /usr/local/redis/redis_cluster/node02_7011 mkdir -p /usr/local/redis/redis_cluster/node03_7012 mkdir -p /usr/local/redis/redis_cluster/node04_7013
將redis文件夾中的redis.conf文件分別拷貝至上述4個文件夾中,並且依次修改每個文件,內容如下
#以node01_7010節點為例 port 7010 #每個節點配置自己的端口號 pidfile /var/run/redis_7010.pid #pid文件 cluster-enabled yes #啟用集群 cluster-config-file nodes_7010.conf #集群配置文件首次啟動自動生成 cluster-node-timeout 15000 #超時時間 appendonly yes #aof日志
其他節點端口號依次為7011 7012 7013對應修改配置文件即可
修改完成后,啟用4個節點
#啟動7010節點 redis-server /usr/local/redis/redis_cluster/node01_7010/redis.conf #啟動7011節點 redis-server /usr/local/redis/redis_cluster/node02_7011/redis.conf #啟動7012節點 redis-server /usr/local/redis/redis_cluster/node03_7012/redis.conf #啟動7013節點 redis-server /usr/local/redis/redis_cluster/node04_7013/redis.conf
在10.2.5.148服務器中,同樣進行上述操作即可,占用端口分別為7015 7016 7017 7018
2.2.安裝ruby
安裝2.4版本的ruby
2.3.組件集群
在任意一台服務器中,進行集群組建
redis-cli --cluster create 10.2.5.147:7010 10.2.5.147:7011 10.2.5.147:7012 10.2.5.147:7013 10.2.5.148:7015 10.2.5.148:7016 10.2.5.148:7017 10.2.5.148:7018 --cluster-replicas 1
系統會隨機選擇主從關系
>>> Performing hash slots allocation on 8 nodes... Master[0] -> Slots 0 - 4095 Master[1] -> Slots 4096 - 8191 Master[2] -> Slots 8192 - 12287 Master[3] -> Slots 12288 - 16383 Adding replica 10.2.5.148:7017 to 10.2.5.147:7010 Adding replica 10.2.5.147:7013 to 10.2.5.148:7015 Adding replica 10.2.5.148:7018 to 10.2.5.147:7011 Adding replica 10.2.5.147:7012 to 10.2.5.148:7016 M: 988000d58f5b4c999c20172ef4ee1572ab24f22e 10.2.5.147:7010 slots:[0-4095] (4096 slots) master M: b0d35f7d14f5c9ef1c261a6ef4cff3a09f39bd75 10.2.5.147:7011 slots:[8192-12287] (4096 slots) master S: 738825958b81184e899b6f5a473a846392e6a9b6 10.2.5.147:7012 replicates 757e7f5f9d973acc182c9dadcd1d1f905571d43d S: 723a484f7570dad408b7b50e525af5125b56e05a 10.2.5.147:7013 replicates 0a8d20f140315deeb649bf100793018243771394 M: 0a8d20f140315deeb649bf100793018243771394 10.2.5.148:7015 slots:[4096-8191] (4096 slots) master M: 757e7f5f9d973acc182c9dadcd1d1f905571d43d 10.2.5.148:7016 slots:[12288-16383] (4096 slots) master S: 59b175c9eba3c9931f14ac5fd65ad3ca1a19c140 10.2.5.148:7017 replicates 988000d58f5b4c999c20172ef4ee1572ab24f22e S: c2927e8d368d4698b0accbc9e76216a3b5c67d12 10.2.5.148:7018 replicates b0d35f7d14f5c9ef1c261a6ef4cff3a09f39bd75 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 10.2.5.147:7010) M: 988000d58f5b4c999c20172ef4ee1572ab24f22e 10.2.5.147:7010 slots:[0-4095] (4096 slots) master 1 additional replica(s) S: c2927e8d368d4698b0accbc9e76216a3b5c67d12 10.2.5.148:7018 slots: (0 slots) slave replicates b0d35f7d14f5c9ef1c261a6ef4cff3a09f39bd75 M: 0a8d20f140315deeb649bf100793018243771394 10.2.5.148:7015 slots:[4096-8191] (4096 slots) master 1 additional replica(s) S: 59b175c9eba3c9931f14ac5fd65ad3ca1a19c140 10.2.5.148:7017 slots: (0 slots) slave replicates 988000d58f5b4c999c20172ef4ee1572ab24f22e S: 723a484f7570dad408b7b50e525af5125b56e05a 10.2.5.147:7013 slots: (0 slots) slave replicates 0a8d20f140315deeb649bf100793018243771394 M: b0d35f7d14f5c9ef1c261a6ef4cff3a09f39bd75 10.2.5.147:7011 slots:[8192-12287] (4096 slots) master 1 additional replica(s) S: 738825958b81184e899b6f5a473a846392e6a9b6 10.2.5.147:7012 slots: (0 slots) slave replicates 757e7f5f9d973acc182c9dadcd1d1f905571d43d M: 757e7f5f9d973acc182c9dadcd1d1f905571d43d 10.2.5.148:7016 slots:[12288-16383] (4096 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.
至此集群已經組建起來了,可以試試是否可用
在10.2.5.147服務器中推送消息、存儲數據至10.2.5.148的7017節點
[root@localhost redis]# redis-cli -h 10.2.5.148 -c -p 7017 10.2.5.148:7017> PUBLISH testchannel "hello!" (integer) 0 10.2.5.148:7017> PUBLISH testchannel "hello!" (integer) 0 10.2.5.148:7017> SET accessToken 111111111111111111111111111 -> Redirected to slot [2275] located at 10.2.5.147:7010 OK
在10.2.5.148服務器中登錄10.2.5.147的監聽隊列,獲取數據
[root@localhost redis]# redis-cli -h 10.2.5.147 -c -p 7010 10.2.5.147:7010> SUBSCRIBE testchannel Reading messages... (press Ctrl-C to quit) 1) "subscribe" 2) "testchannel" 3) (integer) 1 1) "message" 2) "testchannel" 3) "hello!" [root@localhost redis]# redis-cli -h 10.2.5.147 -c -p 7010 10.2.5.147:7010> get accessToken "111111111111111111111111111"
完成