1.下载redis的稳定版本
wget https://download.redis.io/redis-stable.tar.gz
2..安装redis软件
- tar -xzvf redis-stable.tar.gz
- cd redis-stable
- make
- make install
编译过程中提示报错,需要安装gcc
3.编译配置文件
7001服务
- port 7001 #修改端口
- bind 10.3.10.20 #修改为对应主机IP地址
- daemonize yes
- pidfile /var/run/redis_7001.pid #修改PID文件名
- dir /opt/redis-cluster/7001/data #定义存储文件夹
- cluster-enabled yes
- cluster-config-file nodes-7001.conf #修改集群文件名
- cluster-node-timeout 5000
- appendonly yes
7002服务
- port 7002 #修改端口
- bind 10.3.10.20 #修改为对应主机IP地址
- daemonize yes
- pidfile /var/run/redis_7002.pid #修改PID文件名
- dir /opt/redis-cluster/7002/data #定义存储文件夹
- cluster-enabled yes
- cluster-config-file nodes-7002.conf #修改集群文件名
- cluster-node-timeout 5000
- appendonly yes
7003服务
- port 7003 #修改端口
- bind 10.3.10.20 #修改为对应主机IP地址
- daemonize yes
- pidfile /var/run/redis_7003.pid #修改PID文件名
- dir /opt/redis-cluster/7003/data #定义存储文件夹
- cluster-enabled yes
- cluster-config-file nodes-7003.conf #修改集群文件名
- cluster-node-timeout 5000
- appendonly yes
7004服务
- port 7004 #修改端口
- bind 10.3.10.20 #修改为对应主机IP地址
- daemonize yes
- pidfile /var/run/redis_7004.pid #修改PID文件名
- dir /opt/redis-cluster/7004/data #定义存储文件夹
- cluster-enabled yes
- cluster-config-file nodes-7004.conf #修改集群文件名
- cluster-node-timeout 5000
- appendonly yes
7005服务
- port 7005 #修改端口
- bind 10.3.10.20 #修改为对应主机IP地址
- daemonize yes
- pidfile /var/run/redis_7005.pid #修改PID文件名
- dir /opt/redis-cluster/7005/data #定义存储文件夹
- cluster-enabled yes
- cluster-config-file nodes-7005.conf #修改集群文件名
- cluster-node-timeout 5000
- appendonly yes
7006服务
- port 7006 #修改端口
- bind 10.3.10.20 #修改为对应主机IP地址
- daemonize yes
- pidfile /var/run/redis_7006.pid #修改PID文件名
- dir /opt/redis-cluster/7006/data #定义存储文件夹
- cluster-enabled yes
- cluster-config-file nodes-7006.conf #修改集群文件名
- cluster-node-timeout 5000
- appendonly yes
4.启动服务
- 1.拷贝redis-server到服务目录
- cd /opt/redis-cluster
-
- cp /opt/redis-cluster/redis-stable/src/redis-server 7001
-
- cp /opt/redis-cluster/redis-stable/src/redis-server 7002
-
- cp /opt/redis-cluster/redis-stable/src/redis-server 7003
-
- cp /opt/redis-cluster/redis-stable/src/redis-server 7004
-
- cp /opt/redis-cluster/redis-stable/src/redis-server 7005
-
- cp /opt/redis-cluster/redis-stable/src/redis-server 7006
-
- 2.启动服务
-
- ./redis-server redis-7001.conf
- ./redis-server redis-7002.conf
- ./redis-server redis-7003.conf
- ./redis-server redis-7004.conf
- ./redis-server redis-7005.conf
- ./redis-server redis-7006.conf
5.安装集群所需的软件
- yum install ruby rubygems -y
-
- gem install redis
-
- 升级rubygems gem
-
- curl -sSL https://rvm.io/mpapis.asc | gpg2 --import -
- curl -sSL https://get.rvm.io | bash -s stable
- source /etc/profile.d/rvm.sh
- rvm requirements
- source /usr/local/rvm/scripts/rvm
-
- rvm install 2.4.0
- rvm use 2.4.0
-
- gem install redis
-
-
-
-
6.组建集群
- cd /opt/redis-cluster/redis-stable/src
- ./redis-cli --cluster create 10.3.10.20:7001 10.3.10.20:7002 10.3.10.20:7003 10.3.10.20:7004 10.3.10.20:7005 10.3.10.20:7006 --cluster-replicas 1
-
-
-
集群输出如下
- Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
- >>> Performing hash slots allocation on 6 nodes...
- Master[0] -> Slots 0 - 5460
- Master[1] -> Slots 5461 - 10922
- Master[2] -> Slots 10923 - 16383
- Adding replica 10.3.10.20:7005 to 10.3.10.20:7001
- Adding replica 10.3.10.20:7006 to 10.3.10.20:7002
- Adding replica 10.3.10.20:7004 to 10.3.10.20:7003
- >>> Trying to optimize slaves allocation for anti-affinity
- [WARNING] Some slaves are in the same host as their master
- M: cf656ddf619b566a48d15b7f076e6d773d646895 10.3.10.20:7001
- slots:[0-5460] (5461 slots) master
- M: acb4146d7e15c2799cc1c9bb0d34d02e568bde13 10.3.10.20:7002
- slots:[5461-10922] (5462 slots) master
- M: 419bfdab0a4cfcfa3ee78ef8bfaad59f968cbee3 10.3.10.20:7003
- slots:[10923-16383] (5461 slots) master
- S: 8345ec77e8795dcce9dde5c61bf10939150c290a 10.3.10.20:7004
- replicates acb4146d7e15c2799cc1c9bb0d34d02e568bde13
- S: 66c43b50c6e8fc8101502d7466379899305df17f 10.3.10.20:7005
- replicates 419bfdab0a4cfcfa3ee78ef8bfaad59f968cbee3
- S: ab8f6e8c1ffcf48d6903b15fbe58e6ce4e2ad117 10.3.10.20:7006
- replicates cf656ddf619b566a48d15b7f076e6d773d646895
- 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.3.10.20:7001)
- M: cf656ddf619b566a48d15b7f076e6d773d646895 10.3.10.20:7001
- slots:[0-5460] (5461 slots) master
- 1 additional replica(s)
- S: 8345ec77e8795dcce9dde5c61bf10939150c290a 10.3.10.20:7004
- slots: (0 slots) slave
- replicates acb4146d7e15c2799cc1c9bb0d34d02e568bde13
- M: acb4146d7e15c2799cc1c9bb0d34d02e568bde13 10.3.10.20:7002
- slots:[5461-10922] (5462 slots) master
- 1 additional replica(s)
- M: 419bfdab0a4cfcfa3ee78ef8bfaad59f968cbee3 10.3.10.20:7003
- slots:[10923-16383] (5461 slots) master
- 1 additional replica(s)
- S: ab8f6e8c1ffcf48d6903b15fbe58e6ce4e2ad117 10.3.10.20:7006
- slots: (0 slots) slave
- replicates cf656ddf619b566a48d15b7f076e6d773d646895
- S: 66c43b50c6e8fc8101502d7466379899305df17f 10.3.10.20:7005
- slots: (0 slots) slave
- replicates 419bfdab0a4cfcfa3ee78ef8bfaad59f968cbee3
- [OK] All nodes agree about slots configuration.
- >>> Check for open slots...
- >>> Check slots coverage...
- [OK] All 16384 slots covered.
7.组建集群报错
[ERR] Node 10.3.10.20:7001 DENIED Redis is running in protected mode because protected mode is enabled and no password is set for the default user. In this mode connections are only accepted from the loopback interface. If you want to connect from exters 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 SUREt 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 thg the server. 3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. 4) Setup a an authentication password for the default user. NOTE: You only need to do one of the above things in order for the servaccepting connections from the outside.
解决方式
在配置文件增加
masterauth "redis"
requirepass "redis"
重启redis进程
之后使用如下命令创建集群
./redis-cli --cluster create 10.3.10.20:7001 10.3.10.20:7002 10.3.10.20:7003 10.3.10.20:7004 10.3.10.20:7005 10.3.10.20:7006 --cluster-replicas 1 -a htjs
8.集群验证
./redis-cli -c -h 10.3.10.20 -p 7001 -a htjs
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
10.3.10.20:7001>
10.3.10.20:7001>
10.3.10.20:7001>
10.3.10.20:7001>
10.3.10.20:7001> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:480
cluster_stats_messages_pong_sent:471
cluster_stats_messages_sent:951
cluster_stats_messages_ping_received:466
cluster_stats_messages_pong_received:480
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:951
total_cluster_links_buffer_limit_exceeded:0
10.3.10.20:7001> cluster nodes
8345ec77e8795dcce9dde5c61bf10939150c290a 10.3.10.20:7004@17004 slave acb4146d7e15c2799cc1c9bb0d34d02e568bde13 0 1658303013066 2 connected
acb4146d7e15c2799cc1c9bb0d34d02e568bde13 10.3.10.20:7002@17002 master - 0 1658303013568 2 connected 5461-10922
419bfdab0a4cfcfa3ee78ef8bfaad59f968cbee3 10.3.10.20:7003@17003 master - 0 1658303014572 3 connected 10923-16383
ab8f6e8c1ffcf48d6903b15fbe58e6ce4e2ad117 10.3.10.20:7006@17006 slave cf656ddf619b566a48d15b7f076e6d773d646895 0 1658303012564 1 connected
66c43b50c6e8fc8101502d7466379899305df17f 10.3.10.20:7005@17005 slave 419bfdab0a4cfcfa3ee78ef8bfaad59f968cbee3 0 1658303014069 3 connected
cf656ddf619b566a48d15b7f076e6d773d646895 10.3.10.20:7001@17001 myself,master - 0 1658303014000 1 connected 0-5460
10.3.10.20:7001>