(我是利用已经安装过redis的主机上进行集群搭建的)
1、要让集群正常工作至少需要3个主节点,在这里我们要创建6个redis节点,其中三个为主节点,三个为从节点
2、创建相关目录,主文件夹是/data,在此文件夹下建立6个子文件夹,名称分别是:8000,8001,8002,8003,8004,8005:
[root@host-10-101-16-201 data]# pwd
/data
[root@host-10-101-16-201 data]# mkdir 8000 8001 8002 8003 8004 8005
[root@host-10-101-16-201 data]# ls
8000 8001 8002 8003 8004 8005 redis-4.0.1 redis-4.0.1.tar.gz tomcat
3、把Redis源文件里面包含的配置文件redis.conf拷贝一份,存放在8000目录下
[root@host-10-101-16-201 data]# cp redis-4.0.1/redis.conf 8000/
[root@host-10-101-16-201 data]# cd 8000
[root@host-10-101-16-201 8000]# ls
redis.conf
4、使用UE连到这台服务器上去,对redis.conf文件进行修改,修改后的redis.conf文件内容如下(注释在下面一行):
bind 10.101.16.201
# 绑定服务器IP地址
port 8000
# 绑定端口号,必须修改,以此来区分Redis实例
daemonize yes
# 后台运行
pidfile /var/run/redis-8000.pid
# 修改pid进程文件名,以端口号命名
logfile /data/8000/redis.log
# 修改日志文件名称,以端口号为目录来区分
dir /data/8000/
# 修改数据文件存放地址,以端口号为目录名来区分
cluster-enabled yes
# 启用集群
cluster-config-file nodes-8000.conf
# 配置每个节点的配置文件,同样以端口号为名称
cluster-node-timeout 15000
# 配置集群节点的超时时间,可改可不改
appendonly yes
# 启动AOF增量持久化策略
appendfsync always
# 发生改变就记录日志
5、把8000目录下面的redis.conf复制到其他五个目录下面,并且把文件里面的8000分别替换成各自的目录名
[root@host-10-101-16-201 8000]# cp redis.conf ../8001
[root@host-10-101-16-201 8000]# cp redis.conf ../8002
[root@host-10-101-16-201 8000]# cp redis.conf ../8003
[root@host-10-101-16-201 8000]# cp redis.conf ../8004
[root@host-10-101-16-201 8000]# cp redis.conf ../8005
6、根据配置文件启动6个Redis实例:
[root@host-10-101-16-201 8005]# cd ../redis-4.0.1/src/
[root@host-10-101-16-201 src]# pwd
/data/redis-4.0.1/src
[root@host-10-101-16-201 src]# ./redis-server ../../8000/redis.conf
[root@host-10-101-16-201 src]# ./redis-server ../../8001/redis.conf
[root@host-10-101-16-201 src]# ./redis-server ../../8002/redis.conf
[root@host-10-101-16-201 src]# ./redis-server ../../8003/redis.conf
[root@host-10-101-16-201 src]# ./redis-server ../../8004/redis.conf
[root@host-10-101-16-201 src]# ./redis-server ../../8005/redis.conf
[root@host-10-101-16-201 src]# ps -ef |grep redis
root 25515 1 0 10:40 ? 00:00:00 ./redis-server 10.101.16.201:8000 [cluster]
root 25641 1 0 10:40 ? 00:00:00 ./redis-server 10.101.16.201:8001 [cluster]
root 25724 1 0 10:41 ? 00:00:00 ./redis-server 10.101.16.201:8002 [cluster]
root 25747 1 0 10:41 ? 00:00:00 ./redis-server 10.101.16.201:8003 [cluster]
root 25774 1 0 10:41 ? 00:00:00 ./redis-server 10.101.16.201:8004 [cluster]
root 25800 1 0 10:41 ? 00:00:00 ./redis-server 10.101.16.201:8005 [cluster]
7、创建集群,执行redis-trib.rb脚本:
[root@host-10-101-16-201 src]# ruby redis-trib.rb create --replicas 1 10.101.16.201:8000 10.101.16.201:8001 10.101.16.201:8002 10.101.16.201:8003 10.101.16.201:8004 10.101.16.201:8005
-bash: ruby: command not found
(如果本机没有按住ruby则会报错)
8、安装Ruby,下载地址:http://www.ruby-lang.org/en/downloads/,我们安装最新版本:ruby-2.5.3,下载ruby-2.5.3.tar.gz,上传到服务器上
10、解压:tar -zxvf ruby-2.5.3.tar.gz
[root@host-10-101-16-201 data]# ls
8000 8001 8002 8003 8004 8005 redis-4.0.1 redis-4.0.1.tar.gz ruby-2.5.3 ruby-2.5.3.tar.gz
11、安装ruby:
[root@host-10-101-16-201 ruby-2.5.3]# ./configure
checking for ruby... false
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking target system type... x86_64-pc-linux-gnu
checking for gcc... no
checking for cc... no
checking for cl.exe... no
configure: error: in `/data/ruby-2.5.3':
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details
(没有安装编译器)
12、安装gcc:
[root@host-10-101-16-201 ruby-2.5.3]# yum install gcc
13、再次安装ruby
[root@host-10-101-16-201 ruby-2.5.3]# ./configure
[root@host-10-101-16-201 ruby-2.5.3]# make && make install
[root@host-10-101-16-201 etc]# ruby -v
ruby 2.5.3p105 (2018-10-18 revision 65156) [x86_64-linux]
14、默认ruby会安装在/usr/local/bin路径下面,配置一下环境变量
# cd /etc
# 打开 profile 修改path的最后添加: export PATH=/usr/local/bin/ruby:$PATH
# source profile
# echo $PATH 看下PATH变量中是否成功加入
15、切到redis目录,继续执行redis-trib.rb脚本:
[root@host-10-101-16-201 src]# pwd
/data/redis-4.0.1/src
[root@host-10-101-16-201 src]# ruby redis-trib.rb create --replicas 1 10.101.16.201:8000 10.101.16.201:8001 10.101.16.201:8002 10.101.16.201:8003 10.101.16.201:8004 10.101.16.201:8005
Traceback (most recent call last):
2: from redis-trib.rb:25:in `<main>'
1: from /usr/local/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require'
/usr/local/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require': cannot load such file -- redis (LoadError)
16、安装rubygems,最新版本会自动安装:
[root@host-10-101-16-201 src]# yum install rubygems
17、继续执行redis-trib.rb脚本:
[root@host-10-101-16-201 src]# ruby redis-trib.rb create --replicas 1 10.101.16.201:8000 10.101.16.201:8001 10.101.16.201:8002 10.101.16.201:8003 10.101.16.201:8004 10.101.16.201:8005
Traceback (most recent call last):
2: from redis-trib.rb:25:in `<main>'
1: from /usr/local/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require'
/usr/local/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require': cannot load such file -- redis (LoadError)
(貌似问题没有解决)
18、需要安装redis库:gem install redis
[root@host-10-101-16-201 src]# gem install redis
ERROR: Loading command: install (LoadError)
cannot load such file -- zlib
ERROR: While executing gem ... (NoMethodError)
undefined method `invoke_with_build_args' for nil:NilClass
19、安装zlib-1.2.11:
网址:http://www.zlib.net/
下载文件:zlib-1.2.11.tar.gz
通过FTP上传zlib-1.2.11.tar.gz到 /usr/local目录下,执行如下命令安装:
[root@host-10-101-16-201 data]# ls
8000 8001 8002 8003 8004 8005 redis-4.0.1 redis-4.0.1.tar.gz ruby-2.5.3 ruby-2.5.3.tar.gz zlib-1.2.11.tar.gz
[root@host-10-101-16-201 data]# tar -zxvf zlib-1.2.11.tar.gz
[root@host-10-101-16-201 data]# cd zlib-1.2.11
[root@host-10-101-16-201 zlib-1.2.11]# ./configure
[root@host-10-101-16-201 zlib-1.2.11]# make && make install
20、继续安装redis库:gem install redis:
[root@host-10-101-16-201 zlib-1.2.11]# gem install redis
ERROR: Loading command: install (LoadError)
cannot load such file -- zlib
ERROR: While executing gem ... (NoMethodError)
undefined method `invoke_with_build_args' for nil:NilClass
(貌似问题没有解决)
21、在ruby源文件中安装zlib,切换到ruby安装包后解压的目录
[root@host-10-101-16-201 data]# cd ruby-2.5.3/ext/zlib/
[root@host-10-101-16-201 zlib]# pwd
/data/ruby-2.5.3/ext/zlib
[root@host-10-101-16-201 zlib]# ruby extconf.rb --with-zlib-include=/usr/local/include/ --with-zlib-lib=/usr/local/lib/
(/usr/local是zlib默认的安装目录)
[root@host-10-101-16-201 zlib]# make && make install
make: *** No rule to make target `/include/ruby.h', needed by `zlib.o'. Stop.
22、解决上述报错:
打开ext/zlib/Makefile文件,搜索top_srcdir,找到下面这一行:
zlib.o: $(top_srcdir)/include/ruby.h
修改为:
zlib.o: ../../include/ruby.h
再进行编译安装:
[root@host-10-101-16-201 zlib]# make && make install
23、继续执行命令:gem install redis
[root@host-10-101-16-201 zlib]# gem install redis
ERROR: While executing gem ... (Gem::Exception)
Unable to require openssl, install OpenSSL and rebuild Ruby (preferred) or use non-HTTPS sources
24、安装openssl
下载地址:https://www.openssl.org/source/
[root@host-10-101-16-201 data]# ls
8000 8001 8002 8003 8004 8005 openssl-1.1.1.tar.gz redis-4.0.1 redis-4.0.1.tar.gz
[root@host-10-101-16-201 data]# tar -xzvf openssl-1.1.1.tar.gz
[root@host-10-101-16-201 data]# cd openssl-1.1.1
[root@host-10-101-16-201 openssl-1.1.1]# ./config -fPIC --prefix=/usr/local/openssl enable-shared
[root@host-10-101-16-201 openssl-1.1.1]# ./config -t5
[root@host-10-101-16-201 openssl-1.1.1]# make && make install
25、继续执行命令:gem install redis
[root@host-10-101-16-201 openssl-1.1.1]# gem install redis
ERROR: While executing gem ... (Gem::Exception)
Unable to require openssl, install OpenSSL and rebuild Ruby (preferred) or use non-HTTPS sources
(貌似问题没有解决)
26、在ruby源文件中安装openssl
[root@host-10-101-16-201 openssl-1.1.1]# cd /data/ruby-2.5.3/ext/openssl/
[root@host-10-101-16-201 openssl]# ruby extconf.rb --with-openssl-include=/usr/local/openssl/include/ --with-openssl-lib=/usr/local/openssl/lib
备注:/usr/local/openssl是我的openssl安装目录
[root@host-10-101-16-201 openssl]# make && make install
compiling openssl_missing.c
make: *** No rule to make target `/include/ruby.h', needed by `ossl.o'. Stop.
27、解决上述报错:
打开Makefile文件,将$(top_srcdir)全部替换成:../..
再进行编译安装:
[root@host-10-101-16-201 openssl]# make && make install
28、继续执行命令:gem install redis
[root@host-10-101-16-201 openssl]# gem install redis
Fetching: redis-4.0.3.gem (100%)
Successfully installed redis-4.0.3
Parsing documentation for redis-4.0.3
Installing ri documentation for redis-4.0.3
Done installing documentation for redis after 1 seconds
1 gem installed
29、启动集群:
[root@host-10-101-16-201 src]# pwd
/data/redis-4.0.1/src
[root@host-10-101-16-201 src]# ruby redis-trib.rb create --replicas 1 10.101.16.201:8000 10.101.16.201:8001 10.101.16.201:8002 10.101.16.201:8003 10.101.16.201:8004 10.101.16.201:8005
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
10.101.16.201:8000
10.101.16.201:8001
10.101.16.201:8002
Adding replica 10.101.16.201:8003 to 10.101.16.201:8000
Adding replica 10.101.16.201:8004 to 10.101.16.201:8001
Adding replica 10.101.16.201:8005 to 10.101.16.201:8002
M: 5825f39dc55c7dee5fdb0c726bdef0904c5368e3 10.101.16.201:8000
slots:0-5460 (5461 slots) master
M: 17a9ad055e50467a86445d94624a26f577f8747b 10.101.16.201:8001
slots:5461-10922 (5462 slots) master
M: cb647a50969ab20e0612dcd9f19c4ca3ac2e1a5d 10.101.16.201:8002
slots:10923-16383 (5461 slots) master
S: 90027cbadcb3fb47fd239fd1653fe65b39f594e4 10.101.16.201:8003
replicates 5825f39dc55c7dee5fdb0c726bdef0904c5368e3
S: 8f0b9ecc407d7c9301c4bdab78570f2adb8812ac 10.101.16.201:8004
replicates 17a9ad055e50467a86445d94624a26f577f8747b
S: bad062eb1e0240d77688a3e6277b0b52ea2c38a7 10.101.16.201:8005
replicates cb647a50969ab20e0612dcd9f19c4ca3ac2e1a5d
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 10.101.16.201:8000)
M: 5825f39dc55c7dee5fdb0c726bdef0904c5368e3 10.101.16.201:8000
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: 17a9ad055e50467a86445d94624a26f577f8747b 10.101.16.201:8001
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: 90027cbadcb3fb47fd239fd1653fe65b39f594e4 10.101.16.201:8003
slots: (0 slots) slave
replicates 5825f39dc55c7dee5fdb0c726bdef0904c5368e3
S: bad062eb1e0240d77688a3e6277b0b52ea2c38a7 10.101.16.201:8005
slots: (0 slots) slave
replicates cb647a50969ab20e0612dcd9f19c4ca3ac2e1a5d
M: cb647a50969ab20e0612dcd9f19c4ca3ac2e1a5d 10.101.16.201:8002
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: 8f0b9ecc407d7c9301c4bdab78570f2adb8812ac 10.101.16.201:8004
slots: (0 slots) slave
replicates 17a9ad055e50467a86445d94624a26f577f8747b
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@host-10-101-16-201 src]#
30、到此为止,集群终于搭建起来了,可以通过客户端进行访问了。
(欢迎打赏,一分也是爱)