當前環境:centos7.4 一臺
系統:CentOS Linux release 7.4.1708 (Core)
工具:SecureCRT(Xshell)以及SecureFX(Xftp)
編譯環境:gcc version 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC)
安裝包:javascript
redis-4.0.9.tar.gz下載(3.0開始支持集羣功能)
ruby-2.5.1.tar.gz 下載(通常咱們選擇2.4或者2.5就行,儘可能不要用yum安裝,由於yum安裝的ruby是2.0.0的,gem install redis ruby的版本必須是2.2.2以上)
redis-4.0.0.gem下載
zlib-1.2.11.tar.gz下載css
啓動端口:7001 7002 7003 7004 7005 7006
配置文件:/app/redis/redis-cluster/
本文當中,咱們在一臺Linux上搭建6個節點的Redis集羣(實際生產環境,須要3臺Linux服務器分佈存放3個Master)
使用root用戶安裝,由於普通用戶沒有建立目錄及編譯等權限,須要受權
如下只講述離線安裝步驟html
第一步:安裝gcc環境
查看gcc版本,若是沒有安裝則須要進行安裝java
gcc -v gcc version 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC)
使用yum命令在線安裝yum -y install gcc gcc-c++
node
第二步:解壓redis並編譯安裝linux
/app/redis/
tar -xvf redis-4.0.9.tar.gz
cd redis-4.0.9/ make /app/redis/redis-4.0.9/src make install
Hint: It’s a good idea to run ‘make test’ ;)
INSTALL install
INSTALL install
INSTALL install
INSTALL install
INSTALL installc++
redis安裝完以後會在/usr/local/bin目錄下多幾個文件redis
/usr/local/bin
-rwxr-xr-x 1 root root 5600190 Jun 19 14:33 redis-benchmark -rwxr-xr-x 1 root root 8331349 Jun 19 14:33 redis-check-aof -rwxr-xr-x 1 root root 8331349 Jun 19 14:33 redis-check-rdb -rwxr-xr-x 1 root root 5739794 Jun 19 14:33 redis-cli lrwxrwxrwx 1 root root 12 Jun 19 14:33 redis-sentinel -> redis-server -rwxr-xr-x 1 root root 8331349 Jun 19 14:33 redis-server
第三步:解壓ruby並編譯安裝sql
/app/redis
tar -xvf ruby-2.5.1.tar.gz
cd ruby-2.5.1/ ./configure --prefix=/usr/local/ruby --prefix是將ruby安裝到指定目錄,也能夠自定義 make && make install
/usr/local/ruby 能夠看到在ruby目錄下多了四個目錄 drwxr-xr-x 2 root root 4096 Jun 19 14:48 bin drwxr-xr-x 3 root root 4096 Jun 19 14:48 include drwxr-xr-x 4 root root 4096 Jun 19 14:48 lib drwxr-xr-x 5 root root 4096 Jun 19 14:48 share
配置ruby環境變量shell
vi /etc/profile
export PATH=$PATH:/usr/local/ruby/bin :wq source /etc/profile
echo $PATH
/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/openssh-7.5p1/bin:/root/bin:/usr/local/ruby/bin
查看ruby版本號
ruby -v ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux]
第四步:建立redis-cluster集羣目錄並拷貝redis的gem包以及在src下將redis-trib.rb 集羣管理工具拷貝到集羣目錄
/app/redis
mkdir redis-cluster
cp redis-4.0.0.gem redis-cluster/ /app/redis/redis-4.0.9/src cp redis-trib.rb /app/redis/redis-cluster/
第五步:使用gem安裝redis的gem包
/app/redis/redis-cluster
gem install redis-4.0.0.gem
若是安裝沒有任何問題會出現如下提示
Successfully installed redis-4.0.0
Parsing documentation for redis-4.0.0
Installing ri documentation for redis-4.0.0
Done installing documentation for redis after 1 seconds
1 gem installed
如在第五步報錯須要zlib或者openssl,可參照報錯合集2
第六步:找到原先ruby安裝目錄,並作以下修改
/app/redis/ruby-2.5.1/ext/zlib
ruby extconf.rb --成功會出現creating Makefile
creating Makefile --若是沒有出現creating Makefile,執行下面的命令
ruby extconf.rb --with-zlib-dir=/usr/local/zlib/
vi Makefile
將 zlib.o: $(top_srcdir)/include/ruby.h
修改成 zlib.o: ../../include/ruby.h
make
linking shared-object zlib.so
make install /usr/bin/install -c -m 0755 zlib.so /usr/local/ruby/lib/ruby/site_ruby/2.5.0/x86_64-linux
若是上面再執行make以前不修改Makefile,將會報下面的錯誤
make: * No rule to make target/include/ruby.h', needed by
zlib.o’. Stop.
第七步:找到原先的ruby安裝目錄並作以下修改
/app/redis/ruby-2.5.1/ext/openssl
ruby extconf.rb --成功會出現creating Makefile
如發現沒有出現creating Makefile,可參照報錯合集2
vi Makefile
將全部的$(top_srcdir)修改成 ../..($(top_srcdir)不止一個)
make
linking shared-object openssl.so
make install /usr/bin/install -c -m 0755 openssl.so /usr/local/ruby/lib/ruby/site_ruby/2.5.0/x86_64-linux installing default openssl libraries
若是上面再執行make以前不修改Makefile,將會報下面的錯誤
make: * No rule to make target/include/ruby.h', needed by
ossl.o’. Stop.
以前講到是咱們須要6個節點的Redis做爲集羣,因此咱們須要建立6個文件夾,分別存放6個節點的配置信息,6個節點須要對應6個端口號,好比7001~7006,這個端口號咱們能夠自行定義。
/app/redis/redis-cluster/ mkdir 700{1,2,3,4,5,6} --批量建立六個文件夾
將原先redis安裝目錄下的配置文件redis.conf拷貝到新建立的六個文件夾下面
/app/redis/redis-4.0.9 cp redis.conf /app/redis/redis-cluster/7001/ cp redis.conf /app/redis/redis-cluster/7002/ cp redis.conf /app/redis/redis-cluster/7003/ cp redis.conf /app/redis/redis-cluster/7004/ cp redis.conf /app/redis/redis-cluster/7005/ cp redis.conf /app/redis/redis-cluster/7006/
將redis安裝以後生成的服務端與客戶端拷貝到新建立的六個文件夾下面
cd /usr/local/bin/ cp redis-server redis-cli /app/redis/redis-cluster/7001/ cp redis-server redis-cli /app/redis/redis-cluster/7002/ cp redis-server redis-cli /app/redis/redis-cluster/7003/ cp redis-server redis-cli /app/redis/redis-cluster/7004/ cp redis-server redis-cli /app/redis/redis-cluster/7005/ cp redis-server redis-cli /app/redis/redis-cluster/7006/
修改新建立的六個文件夾下面的配置文件redis.conf的部分參數
/app/redis/redis-cluster/ 修改7001-7006中的redis.conf bind 192.168.4.212 連入主機的ip地址,不修改外部沒法連入你的redis緩存服務器中 port 700X x爲文件夾名稱,你在700幾就填幾 daemonize yes 開啓守護進程模式。在該模式下,redis會在後臺運行,並將進程pid號寫入至redis.conf選項pidfile設置的文件中,此時redis將一直運行,除非手動kill該進程。 pidfile /app/redis/redis-cluster/700x/redis_700x.pid x爲文件夾名稱,你在700幾就填幾 cluster-enabled yes 開啓集羣模式
/app/redis/redis-cluster
寫個批處理 vim start-all.sh,若是vim不支持就用vi
vim start-all.sh
cd 7001 ./redis-server redis.conf cd .. cd 7002 ./redis-server redis.conf cd .. cd 7003 ./redis-server redis.conf cd .. cd 7004 ./redis-server redis.conf cd .. cd 7005 ./redis-server redis.conf cd .. cd 7006 ./redis-server redis.conf cd ..
並執行 chmod +x start-all.sh
命令進行受權
啓動實例以前在根路徑下配置多種環境yum -y install gcc gcc-c++
啓動集羣實例,
./start-all.sh ps -ef|grep redis root 22911 1 0 17:20 ? 00:00:00 ./redis-server 192.168.4.212:7001 [cluster] root 22913 1 0 17:20 ? 00:00:00 ./redis-server 192.168.4.212:7002 [cluster] root 22915 1 0 17:20 ? 00:00:00 ./redis-server 192.168.4.212:7003 [cluster] root 22917 1 0 17:20 ? 00:00:00 ./redis-server 192.168.4.212:7004 [cluster] root 22919 1 0 17:20 ? 00:00:00 ./redis-server 192.168.4.212:7005 [cluster] root 22927 1 0 17:20 ? 00:00:00 ./redis-server 192.168.4.212:7006 [cluster] root 22943 24007 0 17:20 pts/9 00:00:00 grep redis
配置集羣
/app/redis/redis-cluster
建立了三個主節點,三個從節點。其中—replicas1 表示每一個主節點下面有1個從節點,從節點能夠是任意多個。
./redis-trib.rb create --replicas 1 192.168.4.212:7001 192.168.4.212:7002 192.168.4.212:7003 192.168.4.212:7004 192.168.4.212:7005 192.168.4.212:7006
>>> Creating cluster >>> Performing hash slots allocation on 6 nodes... Using 3 masters: 192.168.4.212:7001 --主節點 192.168.4.212:7002 --主節點 192.168.4.212:7003 --主節點 Adding replica 192.168.4.212:7005 to 192.168.4.212:7001 --主節點對應的從節點 Adding replica 192.168.4.212:7006 to 192.168.4.212:7002 --主節點對應的從節點 Adding replica 192.168.4.212:7004 to 192.168.4.212:7003 --主節點對應的從節點 >>> Trying to optimize slaves allocation for anti-affinity [WARNING] Some slaves are in the same host as their master M: 878f46c817107cd7a59269b0335f9a8be7529080 192.168.4.212:7001 slots:0-5460 (5461 slots) master --主節點分配的hash槽 M: cfda6a1cf00977311fa46f2920a4e1198e8b9f36 192.168.4.212:7002 slots:5461-10922 (5462 slots) master --主節點分配的hash槽 M: efcf4009a56f0a488b72b6c50d0deefd7b416df3 192.168.4.212:7003 slots:10923-16383 (5461 slots) master --主節點分配的hash槽 S: 1a0bbbb75c8fa0f377767fe0ba194c8d63814b23 192.168.4.212:7004 replicates cfda6a1cf00977311fa46f2920a4e1198e8b9f36 --從節點沒有hash槽 S: c8d88f8e36b5741c7e013a6b84f24a64976f2901 192.168.4.212:7005 replicates efcf4009a56f0a488b72b6c50d0deefd7b416df3 --從節點沒有hash槽 S: 10f48bfea3db737b55d8fee14ad795252d396e2f 192.168.4.212:7006 replicates 878f46c817107cd7a59269b0335f9a8be7529080 --從節點沒有hash槽 Can I set the above configuration? (type 'yes' to accept): yes --選擇yes, 意思是服從這種主從分配方式,咱們也能夠經過配置文件本身指定slave >>> 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.4.212:7001) --如下是詳細的主從節點分佈 M: 878f46c817107cd7a59269b0335f9a8be7529080 192.168.4.212:7001 slots:0-5460 (5461 slots) master 1 additional replica(s) M: efcf4009a56f0a488b72b6c50d0deefd7b416df3 192.168.4.212:7003 slots:10923-16383 (5461 slots) master 1 additional replica(s) M: cfda6a1cf00977311fa46f2920a4e1198e8b9f36 192.168.4.212:7002 slots:5461-10922 (5462 slots) master 1 additional replica(s) S: 1a0bbbb75c8fa0f377767fe0ba194c8d63814b23 192.168.4.212:7004 slots: (0 slots) slave replicates cfda6a1cf00977311fa46f2920a4e1198e8b9f36 S: 10f48bfea3db737b55d8fee14ad795252d396e2f 192.168.4.212:7006 slots: (0 slots) slave replicates 878f46c817107cd7a59269b0335f9a8be7529080 S: c8d88f8e36b5741c7e013a6b84f24a64976f2901 192.168.4.212:7005 slots: (0 slots) slave replicates efcf4009a56f0a488b72b6c50d0deefd7b416df3 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage...
/app/redis/redis-cluster/7001 ./redis-cli -c -h 192.168.4.212 -p 7001 ./redis-cli -c -h 192.168.4.212 -p 7001 shutdown //關閉7001節點,若是沒有-h參數,默認鏈接127.0.0.1,若是沒有-p參數,默認鏈接6379端口(全部若是用默認的,就沒有-h -p) 說明:-h+host -p+端口號 -c是要鏈接集羣,注意坑,不加會報錯的
[root@4-212/app/redis/redis-cluster/7001]#./redis-cli -c -h 192.168.4.212 -p 7001 192.168.4.212:7001> set name xiaowang -> Redirected to slot [5798] located at 192.168.4.212:7002 OK 192.168.4.212:7002> get name "xiaowang" 192.168.4.212:7002> quit
telnet IP 端口infocluster info 打印集羣的信息cluster nodes 列出集羣當前已知的全部節點(node),以及這些節點的相關信息