Redis 集群的搭建

集群搭建参考官网:https://redis.io/topics/cluster-tutorial

redis 集群至少需要三个 master 节点,我们这里搭建三个 master 节点,并且给每一个 master 在搭建一个 slave 节点,总共 6 个 redis 节点,这里用一台机器部署这 6 个节点。三主三从模式,Redis 5.0 以上。

1、创建 Redis 节点安装路径

[root@localhost ~]# mkdir /usr/local/redis_cluster 

2、创建 7000 -7005 文件夹,分配redis.conf

[root@localhost ~]# cd /usr/local/redis_cluster/
[root@localhost redis_cluster]# mkdir 7000 7001 7002 7003 7004 7005
[root@localhost redis_cluster]# wget http://download.redis.io/redis-stable/redis.conf
[root@localhost redis_cluster]# ls
7000  7001  7002  7003  7004  7005  redis.conf
[root@localhost redis_cluster]# cp redis.conf ./7000
[root@localhost redis_cluster]# cp redis.conf ./7001
[root@localhost redis_cluster]# cp redis.conf ./7002
[root@localhost redis_cluster]# cp redis.conf ./7003
[root@localhost redis_cluster]# cp redis.conf ./7004
[root@localhost redis_cluster]# cp redis.conf ./7005

3、配置每一个的 redis.conf

# 关闭保护模式 用于公网访问
protected-mode no
# 端口
port 7000

# 开启集群模式
cluster-enabled yes
cluster-config-file nodes-7000.conf
cluster-node-timeout 5000

# 后台启动
daemonize yes
pidfile /var/run/redis_7000.pid
logfile "7000.log"

# dir /redis/data
# bind 127.0.0.1

# 连接从节点密码
masterauth 123456
# 设置密码
requirepass 123456

4、redis 启动

[root@localhost redis_cluster]# ./redis-5.0.8/src/redis-server ./7000/redis.conf
[root@localhost redis_cluster]# ps -ef|grep -i redis
[root@localhost redis_cluster]# ./redis-5.0.8/src/redis-server ./7001/redis.conf
[root@localhost redis_cluster]# ./redis-5.0.8/src/redis-server ./7002/redis.conf
[root@localhost redis_cluster]# ./redis-5.0.8/src/redis-server ./7003/redis.conf
[root@localhost redis_cluster]# ./redis-5.0.8/src/redis-server ./7004/redis.conf
[root@localhost redis_cluster]# ./redis-5.0.8/src/redis-server ./7005/redis.conf

5、创建 redis 集群

官网告知:
创建集群
现在我们有许多实例正在运行,我们需要通过将一些有意义的配置写入节点来创建集群。
如果您使用的是Redis 5,这很容易完成,这是因为嵌入到中的Redis Cluster命令行实用程序为我们提供了帮助,该实用程序redis-cli可用于创建新集群,检查或重新分片现有集群等。

命令: ./redis-5.0.8/src/redis-cli --cluster create -a 123456 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 --cluster-replicas 1

[root@localhost redis_cluster]# ./redis-5.0.8/src/redis-cli --cluster create -a 123456 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 --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 127.0.0.1:7004 to 127.0.0.1:7000
Adding replica 127.0.0.1:7005 to 127.0.0.1:7001
Adding replica 127.0.0.1:7003 to 127.0.0.1:7002
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: d48cfd2e99a71377e42629495529fcbcfd0d9543 127.0.0.1:7000
   slots:[0-5460] (5461 slots) master
M: 370cf09b9d18445cb9ce10dea9466f95cc0394d3 127.0.0.1:7001
   slots:[5461-10922] (5462 slots) master
M: 309eb1db4b3265f8898d144b0cfccd3f5e468574 127.0.0.1:7002
   slots:[10923-16383] (5461 slots) master
S: 052e1e68b84e54901df10159b04fbd9afd05aedd 127.0.0.1:7003
   replicates d48cfd2e99a71377e42629495529fcbcfd0d9543
S: ee4dba83f69c7bcde1b0266b4d995ece7a1bc79c 127.0.0.1:7004
   replicates 370cf09b9d18445cb9ce10dea9466f95cc0394d3
S: 488163d590240d9ff325b8fbf94c877aef588117 127.0.0.1:7005
   replicates 309eb1db4b3265f8898d144b0cfccd3f5e468574
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 127.0.0.1:7000)
M: d48cfd2e99a71377e42629495529fcbcfd0d9543 127.0.0.1:7000
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: 309eb1db4b3265f8898d144b0cfccd3f5e468574 127.0.0.1:7002
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: ee4dba83f69c7bcde1b0266b4d995ece7a1bc79c 127.0.0.1:7004
   slots: (0 slots) slave
   replicates 370cf09b9d18445cb9ce10dea9466f95cc0394d3
S: 488163d590240d9ff325b8fbf94c877aef588117 127.0.0.1:7005
   slots: (0 slots) slave
   replicates 309eb1db4b3265f8898d144b0cfccd3f5e468574
S: 052e1e68b84e54901df10159b04fbd9afd05aedd 127.0.0.1:7003
   slots: (0 slots) slave
   replicates d48cfd2e99a71377e42629495529fcbcfd0d9543
M: 370cf09b9d18445cb9ce10dea9466f95cc0394d3 127.0.0.1:7001
   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.

集群验证

在某台机器上 连接任意一个redis 客户端 就可以拿到所有的数据。

[root@localhost redis_cluster]# ./redis-5.0.8/src/redis-cli -h 127.0.0.1 -a 123456 -c -p 7000
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:7000> keys *
(empty list or set)
127.0.0.1:7000> info replication
127.0.0.1:7000> cluster nodes
309eb1db4b3265f8898d144b0cfccd3f5e468574 127.0.0.1:7002@17002 master - 0 1586487109560 3 connected 10923-16383
ee4dba83f69c7bcde1b0266b4d995ece7a1bc79c 127.0.0.1:7004@17004 slave 370cf09b9d18445cb9ce10dea9466f95cc0394d3 0 1586487109360 5 connected
488163d590240d9ff325b8fbf94c877aef588117 127.0.0.1:7005@17005 slave 309eb1db4b3265f8898d144b0cfccd3f5e468574 0 1586487109000 6 connected
052e1e68b84e54901df10159b04fbd9afd05aedd 127.0.0.1:7003@17003 slave d48cfd2e99a71377e42629495529fcbcfd0d9543 0 1586487108353 4 connected
d48cfd2e99a71377e42629495529fcbcfd0d9543 127.0.0.1:7000@17000 myself,master - 0 1586487108000 1 connected 0-5460
370cf09b9d18445cb9ce10dea9466f95cc0394d3 127.0.0.1:7001@17001 master - 0 1586487108857 2 connected 5461-10922

注意: 主节点写入的时候,不会立马同步到 slave 分支,异步的去获取 key-value 值。

你可能感兴趣的:(Redis 集群的搭建)