一、redis节点准备
1、上传redis安装文件,并解压
[root@master redis-cluster]# ll
总用量 1708
drwxrwxr-x. 6 root root 4096 12月 12 2018 redis-4.0.12
-rw-r--r--. 1 root root 1740544 8月 29 22:12 redis-4.0.12.tar.gz
2、安装并编译redis
cd redis-4.0.12
make && make install
3、创建六个文件夹,7000、7001、7002、7003、7004、7005.
[root@master redis-cluster]# ls
7000 7001 7002 7003 7004 7005
4、将redis的src文件夹下的redis文件拷贝到六个文件夹并分别修改
[root@master redis-cluster]# cd redis-4.0.12
[root@master redis-4.0.12]# ls
00-RELEASENOTES CONTRIBUTING deps Makefile README.md runtest runtest-sentinel src utils
BUGS COPYING INSTALL MANIFESTO redis.conf runtest-cluster sentinel.conf tests
[root@master redis-4.0.12]# cp redis.conf ../7000
#配置
#redis后台运行
daemonize yes
#pidfile文件 7000~7005
pidfile /var/run/redis_7000.pid
#7000~7005
port 7000
#开启集群
cluster-enabled yes
#配置文件首次启动自动生成 7000~7005
cluster-config-file nodes_7000.conf
#请求超时
cluster-node-timeout 5000
#aof日志
appendonly no
#保护机制
protected-mode no
#绑定地址,需要别的机器能ping通的地址
bind 192.168.21.137
或者将bind 127.0.0.1 注掉
5、创建start-all.sh文件
cd 7000
redis-server redis.conf
cd ..
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 ..
6、添加可执行权限
[root@master redis-cluster]# chmod u+x start-all.sh
7、启动节点并检查是否成功
[root@master redis-cluster]# ./start-all.sh
4742:C 29 Aug 22:49:39.392 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
4742:C 29 Aug 22:49:39.392 # Redis version=4.0.12, bits=64, commit=00000000, modified=0, pid=4742, just started
4742:C 29 Aug 22:49:39.392 # Configuration loaded
4744:C 29 Aug 22:49:39.408 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
4744:C 29 Aug 22:49:39.408 # Redis version=4.0.12, bits=64, commit=00000000, modified=0, pid=4744, just started
4744:C 29 Aug 22:49:39.408 # Configuration loaded
4746:C 29 Aug 22:49:39.533 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
4746:C 29 Aug 22:49:39.534 # Redis version=4.0.12, bits=64, commit=00000000, modified=0, pid=4746, just started
4746:C 29 Aug 22:49:39.534 # Configuration loaded
4754:C 29 Aug 22:49:39.674 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
4754:C 29 Aug 22:49:39.675 # Redis version=4.0.12, bits=64, commit=00000000, modified=0, pid=4754, just started
4754:C 29 Aug 22:49:39.675 # Configuration loaded
4759:C 29 Aug 22:49:39.783 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
4759:C 29 Aug 22:49:39.783 # Redis version=4.0.12, bits=64, commit=00000000, modified=0, pid=4759, just started
4759:C 29 Aug 22:49:39.783 # Configuration loaded
4764:C 29 Aug 22:49:39.853 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
4764:C 29 Aug 22:49:39.853 # Redis version=4.0.12, bits=64, commit=00000000, modified=0, pid=4764, just started
4764:C 29 Aug 22:49:39.854 # Configuration loaded
[root@master redis-cluster]# ps -aux | grep redis
root 4743 0.3 0.9 147360 9612 ? Ssl 22:49 0:00 redis-server *:7000 [cluster]
root 4745 0.3 0.9 147360 9612 ? Ssl 22:49 0:00 redis-server *:7001 [cluster]
root 4753 0.4 0.9 147360 9612 ? Ssl 22:49 0:00 redis-server *:7003 [cluster]
root 4758 0.4 0.9 147360 9612 ? Ssl 22:49 0:00 redis-server *:7002 [cluster]
root 4760 0.4 0.9 147360 9608 ? Ssl 22:49 0:00 redis-server *:7004 [cluster]
root 4765 0.4 0.9 147360 9612 ? Ssl 22:49 0:00 redis-server *:7005 [cluster]
root 4773 0.0 0.0 112720 984 pts/0 S+ 22:49 0:00 grep --color=auto redis
[root@master redis-cluster]# netstat -tnlp | grep redis
tcp 0 0 0.0.0.0:7004 0.0.0.0:* LISTEN 4760/redis-server *
tcp 0 0 0.0.0.0:7005 0.0.0.0:* LISTEN 4765/redis-server *
tcp 0 0 0.0.0.0:17000 0.0.0.0:* LISTEN 4743/redis-server *
tcp 0 0 0.0.0.0:17001 0.0.0.0:* LISTEN 4745/redis-server *
tcp 0 0 0.0.0.0:17002 0.0.0.0:* LISTEN 4758/redis-server *
tcp 0 0 0.0.0.0:17003 0.0.0.0:* LISTEN 4753/redis-server *
tcp 0 0 0.0.0.0:17004 0.0.0.0:* LISTEN 4760/redis-server *
tcp 0 0 0.0.0.0:17005 0.0.0.0:* LISTEN 4765/redis-server *
tcp 0 0 0.0.0.0:7000 0.0.0.0:* LISTEN 4743/redis-server *
tcp 0 0 0.0.0.0:7001 0.0.0.0:* LISTEN 4745/redis-server *
tcp 0 0 0.0.0.0:7002 0.0.0.0:* LISTEN 4758/redis-server *
tcp 0 0 0.0.0.0:7003 0.0.0.0:* LISTEN 4753/redis-server *
二、创建集群
redis 官方提供了一个 redis-trib.rb
(src目录下) 工具用于搭建集群。很明显是 ruby 写的,所以需要 ruby 环境。
1、安装基础的环境
[root@localhost redis-cluster]# yum -y install zlib ruby ruby-devel rubygems rpm-build
2、用 gem 安装 redis 接口
Fetching: redis-4.1.2.gem (100%)
Successfully installed redis-4.1.2
Parsing documentation for redis-4.1.2
Installing ri documentation for redis-4.1.2
Done installing documentation for redis after 4 seconds
1 gem installed
3、确保开放端口
4、将redis安装目录src下的文件redis-trib.rb拷贝到redis-cluster目录下
[root@master src]# cp redis-trib.rb /usr/local/redis-cluster/
运行 redis-trib.rb
查看 集群创建帮助
5、创建集群(前提:节点已启动)
#其中 --replicas 1 意思为为每个 master 分配 1 各 slave
[root@master redis-cluster]# ./redis-trib.rb create --replicas 1 192.168.1.106:7000 192.168.1.106:7001 192.168.1.106:7002 192.168.1.106:7003 192.168.1.106:7004 192.168.1.106:7005
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.1.106:7000
192.168.1.106:7001
192.168.1.106:7002
Adding replica 192.168.1.106:7004 to 192.168.1.106:7000
Adding replica 192.168.1.106:7005 to 192.168.1.106:7001
Adding replica 192.168.1.106:7003 to 192.168.1.106:7002
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 29e9c1ac41c7dcf799a1d9c3ed8cd6cb8e4195fe 192.168.1.106:7000
slots:0-5460 (5461 slots) master
M: 03add56b8313d087da36e878c52d5a64fb1123a3 192.168.1.106:7001
slots:5461-10922 (5462 slots) master
M: e1f3cdc41c008d731f84171b5fa96186608725f6 192.168.1.106:7002
slots:10923-16383 (5461 slots) master
S: 4d07f84542282c0845d250e8f3b99d8b4b120c79 192.168.1.106:7003
replicates e1f3cdc41c008d731f84171b5fa96186608725f6
S: 99a6fbe963492188ff136213c59519313c41862d 192.168.1.106:7004
replicates 29e9c1ac41c7dcf799a1d9c3ed8cd6cb8e4195fe
S: 5b01b3995061ee96fcf023ac3cb43487f6230d2d 192.168.1.106:7005
replicates 03add56b8313d087da36e878c52d5a64fb1123a3
Can I set the above configuration? (type 'yes' to accept): yes
>>> 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.1.106:7000)
M: 29e9c1ac41c7dcf799a1d9c3ed8cd6cb8e4195fe 192.168.1.106:7000
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: 03add56b8313d087da36e878c52d5a64fb1123a3 192.168.1.106:7001
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: 99a6fbe963492188ff136213c59519313c41862d 192.168.1.106:7004
slots: (0 slots) slave
replicates 29e9c1ac41c7dcf799a1d9c3ed8cd6cb8e4195fe
S: 4d07f84542282c0845d250e8f3b99d8b4b120c79 192.168.1.106:7003
slots: (0 slots) slave
replicates e1f3cdc41c008d731f84171b5fa96186608725f6
M: e1f3cdc41c008d731f84171b5fa96186608725f6 192.168.1.106:7002
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: 5b01b3995061ee96fcf023ac3cb43487f6230d2d 192.168.1.106:7005
slots: (0 slots) slave
replicates 03add56b8313d087da36e878c52d5a64fb1123a3
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
创建集群成功
6、测试集群
[root@master redis-cluster]# redis-cli -h 192.168.1.106 -p 7000
192.168.1.106:7000>
三、安装过程中遇到的问题
问题1、用 gem 安装 redis 接口,无响应
解决办法:更换gem源代码镜像
1、查看当前gem的源地址
[root@master ~]# gem sources #列出默认源
*** CURRENT SOURCES ***
https://rubygems.org/
2、删除默认源代码镜像
[root@master ~]# gem sources --remove https://rubygems.org/
https://rubygems.org/ removed from sources
3、更换镜像,添加科大源
[root@master ~]# gem sources -a https://mirrors.ustc.edu.cn/rubygems/ #添加科大源
https://mirrors.ustc.edu.cn/rubygems/ added to sources
问题2、gem安装redis报错,redis requires Ruby version >= 2.3.0.
[root@master ~]# gem install redis
Fetching: redis-4.1.2.gem (100%)
ERROR: Error installing redis:
redis requires Ruby version >= 2.3.0.
解决:CentOS7 yum库中ruby的版本支持到 2.0.0,可gem 安装redis需要最低是2.2.2,采用rvm来更新ruby
1、查看当前ruby的版本
[root@master ~]# ruby --version
ruby 2.0.0p648 (2015-12-16) [x86_64-linux]
2、安装RVM需先安装crul,安装curl
yum install curl
3、安装RVM,按提示进行相关操作
[root@master ~]# curl -L get.rvm.io | bash -s stable
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 194 100 194 0 0 89 0 0:00:02 0:00:02 --:--:-- 89
100 24535 100 24535 0 0 4108 0 0:00:05 0:00:05 --:--:-- 8873
Downloading https://github.com/rvm/rvm/archive/1.29.9.tar.gz
Downloading https://github.com/rvm/rvm/releases/download/1.29.9/1.29.9.tar.gz.asc
gpg: 已创建目录‘/root/.gnupg’
gpg: 新的配置文件‘/root/.gnupg/gpg.conf’已建立
gpg: 警告:在‘/root/.gnupg/gpg.conf’里的选项于此次运行期间未被使用
gpg: 钥匙环‘/root/.gnupg/pubring.gpg’已建立
gpg: 于 2019年07月10日 星期三 16时31分02秒 CST 创建的签名,使用 RSA,钥匙号 39499BDB
gpg: 无法检查签名:没有公钥
GPG signature verification failed for '/usr/local/rvm/archives/rvm-1.29.9.tgz' - 'https://github.com/rvm/rvm/releases/download/1.29.9/1.29.9.tar.gz.asc'! Try to install GPG v2 and then fetch the public key:
gpg2 --keyserver hkp://pool.sks-keyservers.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
or if it fails:
command curl -sSL https://rvm.io/mpapis.asc | gpg2 --import -
command curl -sSL https://rvm.io/pkuczynski.asc | gpg2 --import -
In case of further problems with validation please refer to https://rvm.io/rvm/security
解决安装RVM过程中报错的两种方式
- 添加公钥
curl -sSL https://rvm.io/mpapis.asc | gpg2 --import -
curl -sSL https://rvm.io/pkuczynski.asc | gpg2 --import -
- 或者直接执行
gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
然后再次执行curl -L get.rvm.io | bash -s stable 安装RVM成功。
4、查看配置文件
[root@master ~]# find / -name rvm -print
/usr/local/rvm
/usr/local/rvm/src/rvm
/usr/local/rvm/src/rvm/bin/rvm
/usr/local/rvm/src/rvm/lib/rvm
/usr/local/rvm/src/rvm/scripts/rvm
/usr/local/rvm/bin/rvm
/usr/local/rvm/lib/rvm
/usr/local/rvm/scripts/rvm
5、激活RVM,使配置生效
[root@master ~]# source /usr/local/rvm/scripts/rvm
6、下载RVM依赖
[root@master ~]# rvm requirements
Checking requirements for centos.
Installing requirements for centos.
Installing required packages: autoconf, automake, bison, libffi-devel, libtool, readline-devel, sqlite-devel..........
Requirements installation successful.
7、查看rvm库中已知的ruby版本
rvm list known
8、使用RVM更换ruby的版本为2.3.8
root@master ~]# rvm use 2.3.8 # 使用一个ruby版本
Using /usr/local/rvm/gems/ruby-2.3.8
[root@master ~]# rvm use 2.3.8 --default # 设为默认
Using /usr/local/rvm/gems/ruby-2.3.8
[root@master ~]# rvm remove 2.0.0 # 删除一个已知版本
ruby-2.0.0-p648 - #already gone
Using /usr/local/rvm/gems/ruby-2.3.8
[root@master ~]# ruby -v # 查看当前版本
ruby 2.3.8p459 (2018-10-18 revision 65136) [x86_64-linux]