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、

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