搭建集群需要使用到官方提供的ruby脚本。
需要安装ruby的环境。ruby安装2.7.1
ruby安装文档详见https://blog.csdn.net/baidu_38432732/article/details/106568431
redis-trib.rb脚本需要的ruby包:
需要上传到linux服务。
安装ruby的包:
下载地址https://rubygems.org/gems/rubygems-update-3.1.4.gem
安装rubygems包
[root@localhost ~]# ruby -v
ruby 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-linux]
[root@localhost ~]# gem install rubygems-update-3.1.4.gem
Successfully installed rubygems-update-3.1.4
Parsing documentation for rubygems-update-3.1.4
Installing ri documentation for rubygems-update-3.1.4
Done installing documentation for rubygems-update after 45 seconds
1 gem installed
1、redis包下载http://download.redis.io/releases/redis-5.0.9.tar.gz
# tar -xf redis-5.0.9.tar.gz
# cd redis-5.0.9
# make
# make install prifix=/usr/local/redis
2、 将redis源码下src目录下的以下文件拷贝到我们的redis下
# cd redis-5.0.9/src
# mkdir /data/redis01
# cp redis-cli redis.conf redis-sentinel redis-server redis-trib.rb /data/redis01
3、修改redis配置文件
注意以下五个参数:
# vim redis.conf
protected-mode no # 关闭保护模式
port 6379 # 不同节点不同端口号
daemonize yes # 开启后台启动
pidfile /var/run/redis_6379.pid # 服务id文件名称及地址更改防止冲突
# bind 127.0.0.1 # 注释掉绑定的127.0.0.1,服务对外开放
4、复制多个文件,分别拷贝到不同的主机上,并启动各个节点服务
[root@localhost redis01]# ./redis-server redis.conf
28245:C 05 Jun 2020 11:58:18.858 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
28245:C 05 Jun 2020 11:58:18.858 # Redis version=5.0.9, bits=64, commit=00000000, modified=0, pid=28245, just started
28245:C 05 Jun 2020 11:58:18.858 # Configuration loaded
[root@localhost redis01]# cd ../redis02/
[root@localhost redis02]# ./redis-server redis.conf
28250:C 05 Jun 2020 11:58:28.074 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
28250:C 05 Jun 2020 11:58:28.074 # Redis version=5.0.9, bits=64, commit=00000000, modified=0, pid=28250, just started
28250:C 05 Jun 2020 11:58:28.074 # Configuration loaded
5、创建一个集群
[root@localhost redis01]# ./redis-trib.rb create --replicas 1 192.168.0.242:6379 192.168.0.242:6380 192.168.0.243:6379 192.168.0.243:6380 192.168.0.244:6379 192.168.0.244:6380
WARNING: redis-trib.rb is not longer available!
You should use redis-cli instead.
All commands and features belonging to redis-trib.rb have been moved
to redis-cli.
In order to use them you should call redis-cli with the --cluster
option followed by the subcommand name, arguments and options.
Use the following syntax:
redis-cli --cluster SUBCOMMAND [ARGUMENTS] [OPTIONS]
Example:
redis-cli --cluster create 192.168.0.242:6379 192.168.0.242:6380 192.168.0.243:6379 192.168.0.243:6380 192.168.0.244:6379 192.168.0.244:6380 --cluster-replicas 1
To get help about all subcommands, type:
redis-cli --cluster help
因为我下载的是redis-5.0.9上面也说了现在redis--trib.rb全部移到了redis-cli,至此上面的ruby相关的资料都可以不安装
接下来创建集群,此时我们必须关闭redis的保护模式
[root@localhost redis01]# ./redis-cli --cluster create --cluster-replicas 1 192.168.0.242:6379 192.168.0.242:6380 192.168.0.243:6379 192.168.0.243:6380 192.168.0.244:6379 192.168.0.244:6380
[ERR] Node 192.168.0.242:6379 DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server. 3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. 4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.
[root@localhost redis01]# ./redis-cli --cluster create 192.168.0.242:6379 192.168.0.242:6380 192.168.0.243:6379 192.168.0.243:6380 192.168.0.244:6379 192.168.0.244:6380 --cluster-replicas 1
[ERR] Node 192.168.0.242:6379 DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server. 3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. 4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.
6、正式床架集群
[root@localhost redis02]# cd ../redis01
[root@localhost redis01]# ./redis-cli --cluster create 192.168.0.242:6379 192.168.0.242:6380 192.168.0.243:6379 192.168.0.243:6380 192.168.0.244:6379 192.168.0.244:6380 --cluster-replicas 1
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.0.243:6380 to 192.168.0.242:6379
Adding replica 192.168.0.244:6380 to 192.168.0.243:6379
Adding replica 192.168.0.242:6380 to 192.168.0.244:6379
M: 2db59cce8b73eac58f81147ce505f5a3ec8ee957 192.168.0.242:6379
slots:[0-5460] (5461 slots) master
S: af8c284dda526f2142b117627e8b62f8511babae 192.168.0.242:6380
replicates b52d403b848a83c1b4aa2a2a20c0c5a0bab233df
M: 1ee40f4166085844b0ed16b4aa56959f693dd76a 192.168.0.243:6379
slots:[5461-10922] (5462 slots) master
S: 2d89cad0ab2a979474b84a3fd9d011f14b8beacb 192.168.0.243:6380
replicates 2db59cce8b73eac58f81147ce505f5a3ec8ee957
M: b52d403b848a83c1b4aa2a2a20c0c5a0bab233df 192.168.0.244:6379
slots:[10923-16383] (5461 slots) master
S: 20682cf03c7aa01eb84ac764a9d64e9ea7d6f0bd 192.168.0.244:6380
replicates 1ee40f4166085844b0ed16b4aa56959f693dd76a
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.0.242:6379)
M: 2db59cce8b73eac58f81147ce505f5a3ec8ee957 192.168.0.242:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: b52d403b848a83c1b4aa2a2a20c0c5a0bab233df 192.168.0.244:6379
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 2d89cad0ab2a979474b84a3fd9d011f14b8beacb 192.168.0.243:6380
slots: (0 slots) slave
replicates 2db59cce8b73eac58f81147ce505f5a3ec8ee957
S: 20682cf03c7aa01eb84ac764a9d64e9ea7d6f0bd 192.168.0.244:6380
slots: (0 slots) slave
replicates 1ee40f4166085844b0ed16b4aa56959f693dd76a
S: af8c284dda526f2142b117627e8b62f8511babae 192.168.0.242:6380
slots: (0 slots) slave
replicates b52d403b848a83c1b4aa2a2a20c0c5a0bab233df
M: 1ee40f4166085844b0ed16b4aa56959f693dd76a 192.168.0.243:6379
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
登录集群1,并set一个值
[root@localhost redis01]# ./redis-cli -h 192.168.0.242 -p 6379 -c
192.168.0.242:6379> set a 1
-> Redirected to slot [15495] located at 192.168.0.244:6379
OK
192.168.0.244:6379> get a
"1"
登录之后的其他集群,并get值
[root@localhost redis01]# ./redis-cli -h 192.168.0.242 -p 6380 -c
192.168.0.242:6380> get a
-> Redirected to slot [15495] located at 192.168.0.244:6379
"1"
192.168.0.244:6379>
[root@localhost redis01]# ./redis-cli -h 192.168.0.243 -p 6380 -c
192.168.0.243:6380> get a
-> Redirected to slot [15495] located at 192.168.0.244:6379
"1"
192.168.0.244:6379>
[root@localhost redis01]# ./redis-cli -h 192.168.0.243 -p 6379 -c
192.168.0.243:6379> get a
-> Redirected to slot [15495] located at 192.168.0.244:6379
"1"
192.168.0.244:6379>
[root@localhost redis01]# ./redis-cli -h 192.168.0.244 -p 6379 -c
192.168.0.244:6379> get a
"1"
192.168.0.244:6379>
[root@localhost redis01]# ./redis-cli -h 192.168.0.244 -p 6380 -c
192.168.0.244:6380> get a
-> Redirected to slot [15495] located at 192.168.0.244:6379
"1"
至此所有的集群节点都是通的,redis集群部署成功