Redis集群部署过程记录(3台服务器共6个节点)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Redis集群部署过程记录(3台服务器共6个节点)相关的知识,希望对你有一定的参考价值。
这是我的第一篇博文,整理了我在部署Redis集群踩过的坑,以及详细的部署过程,同时归结了部署过程中遇到的问题的处理方法。
1、部署的环境:SUSE Linux Enterprise 11 sp4
2、涉及到的安装包如下:
(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
2、由于生产环境服务器不能上外网,所以需要提前准备好相关软件包
二、redis部署
2.1安装redis软件包
1、优先安装好gcc、 gcc-c++、make、unzip等基础环境软件包
这些rpm软件包下载比较简单, 这里不具体介绍。
2、上传redis-4.0.2.tar.gz安装包到/opt/software目录下
3、解压安装
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
坑一:不是直接make
suse01:~# make MALLOC=libc
suse01:~# sudo make install
4、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集群创建准备工作
2.2.1 安装ruby
1、上传安装包ruby-2.4.2.tar.gz到/opt/software目录
2、解压并完成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
3、修改profile配置文件
suse01:~# vi /etc/profile
export PATH=$HOME/bin:$JAVA_HOME/bin:$PATH:/usr/bin/ruby
suse01:~# source /etc/profile
4、查询安装的版本
suse01:~# ruby -v
2.2.2 安装zlib
1、上传安装包zlib-1.2.11.tar.gz到/opt/software目录
2、解压并完成zlib安装
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
3、修改Makefile文件
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
1、上传安装包rubygems-2.7.3.tgz到/opt/software目录
2、解压并完成rubygems安装
suse01:~# tar -zxvf rubygems-2.7.3.tgz
suse01:~# cd rubygems-2.7.3
suse01:~# ruby setup.rb
2.2.4 安装openssl
1、上传openssl-1.0.2j.tar.gz到/opt/software/目录下;
2、解压openssl-1.0.2j.tar.gz
suse01:~# cd /opt/software/
suse01:~# tar -zxvf openssl-1.0.2j.tar.gz
3、安装openssl
suse01:~# cd openssl-1.0.2j
suse01:~# ./config
suse01:~# make
suse01:~# sudo make install
4、备份旧版openssl
suse01:~# mkdir -p /backup/openssl_old
suse01:~# mv /usr/bin/openssl /backup/openssl_old/
5、替换新版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
1、上传redis-4.0.0.gem文件到/opt/software目录
2、修改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.
处理方法:
1、添加参数重新编译生成Makefile;2、安装接口包
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
处理方法
1、重新编译openssl;2、重建ruby;3、安装接口包
重新编译openssl时,需要在./config后面添加–fPIC参数,为防止报缺失-fPIC可以在重新编译openssl时就添加–fPIC参数,具体操作步骤如下:
一、重新编译openssl
suse01:~# cd /opt/software/openssl-1.0.2j
suse01:~# make clean
suse01:~# ./config -fPIC
suse01:~# ./config -t
suse01:~# make
suse01:~# sudo make install
二、重建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
三、创建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 修改配置文件
1、修改守护进程配置及配置集群连接超时时间
修改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秒,可自行设置**
2、三台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
3、三个主机,修改配置文件:
bind 127.0.0.1 #第69行,修改为当前创建集群服务器的ip 如:bind 172.107.14.52
port 7001 #第93行,端口号7001、 7002
pidfile /var/run/redis_7001.pid #第158行,pidfile文件对应7001、7002
logfile "/usr/local/redis/log/redis_7001.log" #第171行,logfile文件对应7001、7002
dir ./ #第264行,./修改为/usr/local/redis/data/
删除以下1条配置的#号注释标识
cluster-config-file nodes-7001.conf #第822行,集群配置对应7001、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
4、创建相应的日志文件
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
5、启动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
四、Redis集群设置SSH免密登陆
1、服务器52、53、54三台相互之间需要免密登陆。
2、在三台服务器上分别运行:
例如:52上实现与53、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] [email protected] MD5加密key值
(2)使用ll命令可查看生成的私钥与公钥
suse01:~# cd /root/.ssh
使用ll命令查看到生成的私钥与公钥如下图:
(3)在52上运行命令把公钥复制到53服务器上,实现免密登陆
suse01:~# ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]
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 [email protected]’172.107.14.53’”
And check to make sure that only the key(s) you wanted were added.
(4)在52服务器上使用ssh [email protected]命令提示如下信息表示成功实现免密登陆
suse01:~# ssh [email protected]
Last login:The May 29 15:57:03 2018 from 76.10.127.200
suse02:~#使用exit命令退出登陆
备注:1、这只是实现了52与53之间的免密登陆,接下来还需要配置52与54之间的免密登录,只需要在对应在节点上做以上相同的操作即可,最终实现三台相互之间免密登陆。
2、如果免密登录测试拒绝,需要给文件授权,查看日志
suse01:~# cat /var/log/secure
suse01:~# chmod 700 /root/.ssh
suse01:~# chmod 600 /root/.ssh/authorized_keys
至此完成整个Redis集群的部署!!
以上是关于Redis集群部署过程记录(3台服务器共6个节点)的主要内容,如果未能解决你的问题,请参考以下文章