這是個人第一篇博文,整理了我在部署Redis集羣踩過的坑,以及詳細的部署過程,同時歸結了部署過程當中遇到的問題的處理方法。
一、部署的環境:SUSE Linux Enterprise 11 sp4
二、涉及到的安裝包以下:
(1) 安裝包:redis-4.0.2.tar.gz
下載地址:http://download.redis.io/releases/ redis官網http://www.redis.io
(2) 接口包:redis-4.0.0.gem
(3) 腳本語言包:ruby-2.4.2.tar.gz rubygems-2.7.3.tgz
(4) 依賴庫:
zlib-1.2.11.tar.gz ;
openssl-1.0.2j.tar.gz ;
libopenssl-devel-0.9.8h-30.11.x86_64.rpm ;
gcc
部署操做步驟:
1、環境準備
一、本次部署共使用3臺服務器,每臺服務器2個節點,總共6個節點;
安裝3臺SUSE Linux操做系統,鏡像爲SLES-11-SP4-DVD-x86_64-GM-DVD1.iso
二、因爲生產環境服務器不能上外網,因此須要提早準備好相關軟件包node
2、redis部署
2.1安裝redis軟件包
一、優先安裝好gcc、 gcc-c++、make、unzip等基礎環境軟件包
這些rpm軟件包下載比較簡單, 這裏不具體介紹。
二、上傳redis-4.0.2.tar.gz安裝包到/opt/software目錄下
三、解壓安裝c++
suse01:~# mkdir /opt/software suse01:~# cd /opt/software suse01:~# tar -zxvf redis-4.0.2.tar.gz suse01:~# cd redis-4.0.2 suse01:~# make suse01:~# cd src
坑一:不是直接makeredis
suse01:~# make MALLOC=libc suse01:~# sudo make install
四、redis部署集羣環境準備算法
suse01:~# mkdir -p /usr/local/redis/bin suse01:~# mkdir -p /usr/local/redis/etc/ suse01:~# cp /opt/software/redis-4.0.2/redis.conf /usr/local/redis/etc suse01:~# cd /opt/software/redis-4.0.2/src suse01:~# cp mkreleasehdr.sh redis-benchmark redis-server /usr/local/redis/bin
2.2 redis集羣建立準備工做ruby
2.2.1 安裝ruby
一、上傳安裝包ruby-2.4.2.tar.gz到/opt/software目錄
二、解壓並完成ruby安裝服務器
suse01:~# cd /opt/software suse01:~# tar -zxvf ruby-2.4.2.tar.gz suse01:~# cd ruby-2.4.2 suse01:~# ./configure suse01:~# make suse01:~# sudo make install
三、修改profile配置文件session
suse01:~# vi /etc/profile export PATH=$HOME/bin:$JAVA_HOME/bin:$PATH:/usr/bin/ruby suse01:~# source /etc/profile
四、查詢安裝的版本架構
suse01:~# ruby -v
2.2.2 安裝zlib
一、上傳安裝包zlib-1.2.11.tar.gz到/opt/software目錄
二、解壓並完成zlib安裝ssh
suse01:~# cd /opt/software suse01:~# tar -zxvf zlib-1.2.11.tar.gz suse01:~# cd zlib-1.2.11 suse01:~# ./configure suse01:~# make suse01:~# sudo make install
三、修改Makefile文件ide
suse01:~# cd /opt/software/ruby-2.4.2/ext/zlib suse01:~# ruby extconf.rb
坑二:修改Makefile文件頭
修改的參數具體以下(不然make操做會報錯) suse01:~# vi Makefile zlib.o:$(top_srcdir)/include/ruby.h ****#將$(topsrcdir)修改成../..**** 修改後的具體代碼結果以下 zlib.o: ../../include/ruby.h suse01:~# make suse01:~# sudo make install
2.2.3 安裝Rubygems
一、上傳安裝包rubygems-2.7.3.tgz到/opt/software目錄
二、解壓並完成rubygems安裝
suse01:~# tar -zxvf rubygems-2.7.3.tgz suse01:~# cd rubygems-2.7.3 suse01:~# ruby setup.rb
2.2.4 安裝openssl
一、上傳openssl-1.0.2j.tar.gz到/opt/software/目錄下;
二、解壓openssl-1.0.2j.tar.gz
suse01:~# cd /opt/software/ suse01:~# tar -zxvf openssl-1.0.2j.tar.gz
三、安裝openssl
suse01:~# cd openssl-1.0.2j suse01:~# ./config suse01:~# make suse01:~# sudo make install
四、備份舊版openssl
suse01:~# mkdir -p /backup/openssl_old suse01:~# mv /usr/bin/openssl /backup/openssl_old/
五、替換新版openssl
suse01:~# ln -s /usr/local/ssl/bin/openssl /usr/bin/openssl
6)檢查更新後的OpenSSL版本
suse01:~# openssl version
2.2.5 安裝libopenssl-devel
suse01:~# cd .. suse01:~# rpm -ivh libopenssl-devel-0.9.8h-30.11.x86_64.rpm --nodeps
2.2.6 安裝redis-4.0.0.gem
一、上傳redis-4.0.0.gem文件到/opt/software目錄
二、修改Makefile文件
suse01:~# cd /opt/software/ruby-2.4.2/ext/openssl suse01:~# ruby extconf.rb
坑三:修改Makefile文件頭
suse01:~# vi Makefile
重要備註:將Makefile文件中全部的$(top_srcdir)/include/ruby.h修改成../../include/ruby.h 根據環境不一樣,$(top_srcdir)/include/ruby.h出現的次數有些是33次,有些是38次,無論出現的次數是多少,記得修改,不然make沒法繼續執行
suse01:~# make suse01:~# sudo make install
以上的三個坑若是沒有注意,如下是具體運行相關命令時提示的錯誤記錄:
錯誤一:當運行ruby extconf.rb時界面報錯,提示以下錯誤信息:
*** extconf.rb failed *** Could not create Makefile due to some reason, probably lack of necessary libraries and/or headers. Check the mkmf.log file for more details. You may need configuration options.
處理方法:
一、添加參數從新編譯生成Makefile;二、安裝接口包
suse01:~# cd /opt/software/ruby-2.4.2/ext/openssl suse01:~# ruby extconf.rb --with-openssl-include=/usr/local/ssl/include --with-openssl-lib=/usr/local/ssl/lib suse01:~# vi Makefile
具體修改的參數信息是將Makefile文件中全部的$(top_srcdir)/include/ruby.h(計數統計是33個或38個)修改成../../include/ruby.h
suse01:~# make suse01:~# sudo make install
錯誤二:當運行make時界面報錯,提示以下錯誤信息:
suse01:~# make gcc -shared -o openssl.so ossl_x509.o ossl_x509cert.o ossl_x509crl.o ossl_pkcs7.o ossl_cipher.o ossl_pkey_dh.o ossl_ssl_session.o ossl_x509attr.o ossl_x509ext.o ossl_ocsp.o ossl_asn1.o ossl_x509name.o ossl_x509revoked.o ossl_rand.o ossl_pkcs12.o ossl_x509store.o ossl_ssl.o ossl.o ossl_digest.o ossl_config.o ossl_pkcs5.o ossl_bio.o ossl_pkey_dsa.o ossl_pkey.o ossl_x509req.o ossl_pkey_ec.o ossl_hmac.o openssl_missing.o ossl_ns_spki.o ossl_pkey_rsa.o ossl_engine.o ossl_bn.o -L. -L/usr/local/ruby/lib -Wl,-R/usr/local/ruby/lib -L/usr/local/ssl/lib -Wl,-R/usr/local/ssl/lib -L. -rdynamic -Wl,-export-dynamic -lssl -lcrypto -lrt -ldl -lcrypt -lm -lc /usr/bin/ld: /usr/local/ssl/lib/libssl.a(s2_meth.o): relocation R_X86_64_32 against `a local symbol' can not be used when making a shared object; recompile with -fPIC /usr/local/ssl/lib/libssl.a: could not read symbols: Bad value collect2: ld returned 1 exit status make: *** [openssl.so] Error 1
處理方法
一、從新編譯openssl;二、重建ruby;三、安裝接口包
從新編譯openssl時,須要在./config後面添加–fPIC參數,爲防止報缺失-fPIC能夠在從新編譯openssl時就添加–fPIC參數,具體操做步驟以下:
1、從新編譯openssl
suse01:~# cd /opt/software/openssl-1.0.2j suse01:~# make clean suse01:~# ./config -fPIC suse01:~# ./config -t suse01:~# make suse01:~# sudo make install
2、重建ruby
suse01:~# cd /opt/software/ruby-2.3.2/ext/openssl suse01:~# ruby extconf.rb --with-openssl-include=/usr/local/ssl/include --with-openssl-lib=/usr/ssl/lib suse01:~# vi Makefile
備註:將Makefile文件中全部的$(top_srcdir)/include/ruby.h修改成../../include/ruby.h
suse01:~# make suse01:~# sudo make install
2.2.7 執行安裝命令
suse01:~# cd /opt/software suse01:~# gem install redis-4.0.0.gem
3、建立redis集羣
3.1建立redis集羣
3.1.1 集羣基本介紹
Redis集羣中內置了 16384(2的14次方)個哈希槽,當須要在Redis集羣中放置一個 key-value時,redis先對 key 使用 crc16 算法算出一個結果,而後把結果對 16384 求餘數,這樣每一個 key 都會對應一個編號在 0-16383 之間的哈希槽,redis會根據節點數量大體均等的將哈希槽映射到不一樣的節點。
Redis集羣,高可用架構採用6個redis節點部署,建立三主三從,主從分佈在不一樣服務器,保證一臺服務器宕機,從節點頂爲主節點,保證任什麼時候刻都有三個主節點可用。
3.1.2 修改配置文件
一、修改守護進程配置及配置集羣鏈接超時時間
修改redis.conf文件第137行,將daemonize屬性no改成yes
suse01:~# vi /usr/local/redis/etc/redis.conf daemonize yes yes後臺運行守護進程
去掉如下2條配置的#號註釋標識
#cluster-enabled yes #第814行,開啓集羣 #cluster-node-timeout 15000 #第828行,請求超時默認15秒,可自行設置**
二、三臺redis主機拷貝配置文件
server1: 172.107.14.52
server2: 172.107.14.53
server3: 172.107.14.54
suse01:~# cd /usr/local/redis/etc
server1:172.107.14.52
suse01:~# cp redis.conf redis-7001.conf suse01:~# cp redis.conf redis-7002.conf
server2:172.107.14.53
suse01:~# cp redis.conf redis-7001.conf suse01:~# cp redis.conf redis-7002.conf
server3:172.107.14.54
suse01:~# cp redis.conf redis-7001.conf suse01:~# cp redis.conf redis-7002.conf suse01:~# vi redis-7001.conf suse01:~# vi redis-7002.conf
三、三個主機,修改配置文件:
bind 127.0.0.1 #第69行,修改成當前建立集羣服務器的ip 如:bind 172.107.14.52 port 7001 #第93行,端口號700一、 7002 pidfile /var/run/redis_7001.pid #第158行,pidfile文件對應700一、7002 logfile "/usr/local/redis/log/redis_7001.log" #第171行,logfile文件對應700一、7002 dir ./ #第264行,./修改成/usr/local/redis/data/
刪除如下1條配置的#號註釋標識
cluster-config-file nodes-7001.conf #第822行,集羣配置對應700一、7002
給755權限(關於權限的問題,也能夠另外新建redis用戶及組,把相關目錄權限受權給redis用戶及組,並設置redis用戶爲nologin)
suse01:~# chmod 755 /usr/local/redis/etc/redis-7001.conf suse01:~# chmod 755 /usr/local/redis/etc/redis-7002.conf
四、建立相應的日誌文件
suse01:~# mkdir /usr/local/redis/backup suse01:~# mkdir /usr/local/redis/data/ suse01:~# mkdir -p /usr/local/redis/log/ suse01:~#cd /usr/local/redis/log/ suse01:~# touch redis_7001.log suse01:~# touch redis_7002.log
五、啓動redis:
(1)、三臺主機分別啓動兩個redis節點
suse01:~# cd /usr/local/redis/bin suse01:~# ./redis-server /usr/local/redis/etc/redis-7001.conf suse01:~# ./redis-server /usr/local/redis/etc/redis-7002.conf
(2)、驗證集羣是否建立成功(在53服務器上執行該操做)
suse01:~# cd /opt/software/redis-4.0.2/src suse01:~# ./redis-trib.rb create --replicas 1 172.107.14.52:7001 172.107.14.52:7002 172.107.14.53:7001 172.107.14.53:7002 172.107.14.54:7001 172.107.14.54:7002
(3)查看集羣信息
suse01:~# redis-cli -c -p 7001 -h 172.107.14.52 cluster nodes
4、Redis集羣設置SSH免密登錄
一、服務器5二、5三、54三臺相互之間須要免密登錄。
二、在三臺服務器上分別運行:
例如:52上實現與5三、54之間免密登錄可進行以下操做
(1)在52服務器上運行ssh-keygen -t rsa
suse01:~# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key(/root/.ssh/id_rsa): #回車
Enter passphrase (empty for no passphrase): #回車表明無需密碼登錄
Enter same passphrase again: #回車
Your identification has been saved in /root/.ssh/id_rsa. 私鑰的存儲路徑
Your public key has been saved in /root/.ssh/id_rsa.pub. 公鑰的存儲路徑
The key fingerprint is:
92:94:ad:a9:89:72:3b:65:3c:38:a2:81:93 [MD5] root@suse02 MD5加密key值
(2)使用ll命令可查看生成的私鑰與公鑰
suse01:~# cd /root/.ssh 使用ll命令查看到生成的私鑰與公鑰以下圖:
(3)在52上運行命令把公鑰複製到53服務器上,實現免密登錄
suse01:~# ssh-copy-id -i /root/.ssh/id_rsa.pub root@172.107.14.53
The authenticity of host ’172.107.14.53 (172.107.14.53)’ can’t be established.
ECDSA key fingerprint is51:07:ad:f7:aa:16:8d:51:52:b5:7g:p2:02[MD5].
Are you sure you want to continus connecting (yes/no)? #輸入yes
/usr/bin/ssh-copy-id:INFO:attempting to log in with the new key(s),to filter out any that are already installed
/usr/bin/ssh-copy-id:INFO:1 key(s) remain to be installed – if you are prompted now it is to install the new keys
Password: #輸入53服務器的用戶密碼
Number of key(s) added:1
Now try logging into the machine,with: 「ssh root@’172.107.14.53’」
And check to make sure that only the key(s) you wanted were added.
(4)在52服務器上使用ssh root@172.107.14.53命令提示以下信息表示成功實現免密登錄
suse01:~# ssh root@172.107.14.53
Last login:The May 29 15:57:03 2018 from 76.10.127.200
suse02:~#使用exit命令退出登錄
備註:一、這只是實現了52與53之間的免密登錄,接下來還須要配置52與54之間的免密登陸,只須要在對應在節點上作以上相同的操做便可,最終實現三臺相互之間免密登錄。
二、若是免密登陸測試拒絕,須要給文件受權,查看日誌
suse01:~# cat /var/log/secure suse01:~# chmod 700 /root/.ssh suse01:~# chmod 600 /root/.ssh/authorized_keys
至此完成整個Redis集羣的部署!!