CentOS7離線Redis集羣搭建

一、環境準備

  1. Centos7(離線環境下)
  2. redis安裝包redis-5.0.4.tar.gz      下載地址:https://redis.io/download
  3. ruby安裝包ruby-2.5.5.tar.gz      下載地址:http://www.ruby-lang.org/en/downloads/
  4. zlib安裝包zlib-1.2.11.tar.gz      下載地址:http://www.zlib.net
  5. rubygems安裝包rubygems-3.0.3.tgz      下載地址: https://rubygems.org/pages/download
  6. oppenssl安裝包openssl-1.0.2n.tar.gz      下載地址:https://oomake.com/download/openssl
  7. redis依賴庫redis-4.1.0.gem      下載地址:https://rubygems.org/gems/redis/

將以上安裝包都放到CentOS7系統中(基於xshell  xftp上傳),可以自定義路徑和文件夾,此處我將安裝包都放在/root/public下面

二、環境安裝

1、redis安裝需要依託gcc環境,先檢查gcc是否安裝,通過版本號查看,如果已經安裝gcc會出現如下提示。

[root@localhost ~]# gcc -v
使用內建 specs。
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/lto-wrapper
目標:x86_64-redhat-linux
配置爲:../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin --enable-initfini-array --disable-libgcj --with-isl=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/isl-install --with-cloog=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/cloog-install --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
線程模型:posix
gcc 版本 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) 

2、安裝ruby。進入public文件夾,解壓ruby安裝文件。

[root@localhost public]# tar -zxvf ruby-2.5.5.tar.gz 

3、進入解壓後的ruby文件夾,將ruby安裝到指定目錄/usr/local/ruby,編譯安裝

[root@localhost ruby-2.5.5]# ./configure --prefix=/usr/local/ruby  ##是將ruby安裝到指定目錄,也可以自定義
[root@localhost ruby-2.5.5]# make && make install  ##編譯安裝

4、ruby安裝完成後,配置ruby環境變量。編輯vim /etc/profile文件,在文件當中PATH當中加入安裝Ruby的bin目錄路徑。然後保存執行命令:source /etc/profile讓配置立即生效,然後echo $PATH可以查看變量已被添加。ruby -v 命令查看是否安裝成功。

[root@localhost rubygems-3.0.3]# source /etc/profile  ##配置生效
[root@localhost ruby-2.5.5]# echo $PATH
/usr/local/ruby/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/git/bin:/root/bin:/usr/local/git/bin

[root@localhost ruby-2.5.5]# ruby -v   ##查看ruby安裝成功
ruby 2.5.5p157 (2019-03-15 revision 67260) [x86_64-linux]

5、zlib安裝。進入public文件夾,解壓zlib安裝文件,並安裝。

[root@localhost public]# tar -zxvf zlib-1.2.11.tar.gz  ##解壓
[root@localhost public]# cd zlib-1.2.11    ##進入解壓後的安裝包
[root@localhost zlib-1.2.11]# ./configure --prefix=/usr/local/zlib  ##設置安裝路徑
[root@localhost zlib-1.2.11]# make && make install   ##編譯安裝

6、安裝rubygems-3.0.3。 進入public文件夾,解壓rubygems安裝文件,然後執行命令:ruby setup.rb

[root@localhost public]# tar -zxvf rubygems-3.0.3.tgz  ##解壓
[root@localhost public]# cd rubygems-3.0.3  ##進入解壓後文件
[root@localhost rubygems-3.0.3]# ruby setup.rb  ##執行命令

   安裝成功出現如下信息。

7、rubygems安裝完成後,配置rubygems環境變量。編輯vim /etc/profile文件,在文件當中PATH當中加入安裝rubygems的bin目錄路徑,路徑爲安裝目錄bin文件夾/root/public/rubygems-3.0.3/bin/ 【這個路徑就是你解壓出rubygems的路徑】 然後保存,執行命令:source /etc/profile讓配置立即生效,然後echo $PATH可以查看變量已被添加。

8、redis安裝。進入public文件夾,解壓redis並編譯安裝。

[root@localhost public]# tar -zxvf redis-5.0.4.tar.gz  ##解壓
[root@localhost public]# cd redis-5.0.4   ##進入解壓後文件夾
[root@localhost redis-5.0.4]# make && make install  ##編譯安裝

9、進入redis安裝文件解壓後的文件夾下運行redis-server redis.conf命令啓動redis,如果啓動成功,會出現如下界面。

[root@localhost redis-5.0.4]# redis-server redis.conf   ##啓動redis
48367:C 24 Apr 2019 18:51:47.462 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
48367:C 24 Apr 2019 18:51:47.463 # Redis version=5.0.4, bits=64, commit=00000000, modified=0, pid=48367, just started
48367:C 24 Apr 2019 18:51:47.463 # Configuration loaded
48367:M 24 Apr 2019 18:51:47.463 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 5.0.4 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 48367
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

48367:M 24 Apr 2019 18:51:47.466 # Server initialized
48367:M 24 Apr 2019 18:51:47.466 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
48367:M 24 Apr 2019 18:51:47.466 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
48367:M 24 Apr 2019 18:51:47.466 * Ready to accept connections

10、安裝redis依賴庫redis-4.1.0.gem。否則一會兒啓動會報錯。進入存放依賴庫的public文件夾下。

[root@localhost public]# gem install redis-4.1.0.gem 

11、安裝oppenssl,進入存放oppenssl的public文件夾下,解壓並安裝。

[root@localhost public]# tar -zxvf openssl-1.0.2n.tar.gz   ##解壓
[root@localhost public]# cd openssl-1.0.2n  ##進入解壓後的安裝文件
[root@localhost openssl-1.0.2n]# ./config -fPIC --prefix=/usr/local/openssl enable-shared ##配置安裝路徑
[root@localhost openssl-1.0.2n]# make && make install  ##編輯安裝

12、關閉防火牆:service friewalld stop

三、redis集羣搭建

1、在public文件夾下創建redis-custer文件目錄,並在redis-custer目錄下創建集羣文件7000 7001 7002 7003 7004 7005六個文件夾。

[root@localhost public]# mkdir redis-custer
[root@localhost public]# cd redis-custer/
[root@localhost redis-custer]#  mkdir 7000 7001 7002 7003 7004 7005

2、將/root/public/redis-5.0.4/src目錄下的redis-cli redis-server redis-trib.rb複製到這六個文件夾中。同時將/root/public/redis-5.0.4/目錄下的redis.conf 文件拷貝到這6個目錄中,並對這6個redis.conf文件做修改。

[root@localhost src]# cp redis-cli redis-server redis-trib.rb /root/public/redis-custer/7000
[root@localhost src]# cp redis-cli redis-server redis-trib.rb /root/public/redis-custer/7001
[root@localhost src]# cp redis-cli redis-server redis-trib.rb /root/public/redis-custer/7002
[root@localhost src]# cp redis-cli redis-server redis-trib.rb /root/public/redis-custer/7003
[root@localhost src]# cp redis-cli redis-server redis-trib.rb /root/public/redis-custer/7004
[root@localhost src]# cp redis-cli redis-server redis-trib.rb /root/public/redis-custer/7005   
##下面爲複製redis.conf文件
[root@localhost redis-5.0.4]# cp redis.conf /root/public/redis-custer/7000
[root@localhost redis-5.0.4]# cp redis.conf /root/public/redis-custer/7001
[root@localhost redis-5.0.4]# cp redis.conf /root/public/redis-custer/7002
[root@localhost redis-5.0.4]# cp redis.conf /root/public/redis-custer/7003
[root@localhost redis-5.0.4]# cp redis.conf /root/public/redis-custer/7004
[root@localhost redis-5.0.4]# cp redis.conf /root/public/redis-custer/7005

3、修改每個集羣文件夾下面的redis.conf文件

port  7000                              //端口7000,7001,7002,7003,7004,7005      

bind 本機ip    //默認ip爲127.0.0.1 需要改爲其他節點機器可訪問的ip 否則創建集羣時無法訪問對應的端口,無法創建集羣

daemonize    yes                      //redis後臺運行

pidfile  /var/run/redis_7000.pid          //pidfile文件對應7000…7005

cluster-enabled  yes                   //開啓集羣  把註釋#去掉

cluster-config-file  nodes_7000.conf   //集羣的配置  配置文件首次啓動自動生成 7000…7005

cluster-node-timeout  15000        //請求超時  默認15秒,可自行設置

appendonly  yes                 //aof日誌開啓  有需要就開啓,它會每次寫操作都記錄一條日誌

 

 

 

 

 

 

4、進入redis/root/public/redis-5.0.4/src目錄下,給根目錄下拷貝一個redis-trib.rb,用於創建集羣。

[root@localhost src]# cp redis-trib.rb ../

5、分別啓動redis的6個節點。

[root@localhost redis-custer]# redis-server ./7000/redis.conf 
[root@localhost redis-custer]# redis-server ./7001/redis.conf 
[root@localhost redis-custer]# redis-server ./7002/redis.conf 
[root@localhost redis-custer]# redis-server ./7003/redis.conf 
[root@localhost redis-custer]# redis-server ./7004/redis.conf 
[root@localhost redis-custer]# redis-server ./7005/redis.conf 

8、創建redis集羣,執行命令redis-cli --cluster create 192.168.160.129:7000 192.168.160.129:7001 192.168.160.129:7002 192.168.160.129:7003 192.168.160.129:7004 192.168.160.129:7005

注意:如果是多臺服務器一起作爲redis集羣,那麼創建集羣時的IP地址和端口號也寫成相應的IP地址和端口號,這裏寫的IP地址和端口號要和每個集羣文件下面的redis.conf文件中配置IP和端口號一致。

[root@localhost redis-5.0.4]# redis-cli --cluster create 192.168.160.129:7000 192.168.160.129:7001 192.168.160.129:7002 192.168.160.129:7003 192.168.160.129:7004 192.168.160.129:7005

9、測試redis

[root@localhost redis-5.0.4]# redis-cli -h 192.168.160.129 -c -p 7002
192.168.160.129:7002> set name fengfeng
OK
192.168.160.129:7002> get name
"fengfeng"
192.168.160.129:7002> 

10、

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章