redis搭建cluster

redis搭建cluster

成员 主机IP
1 6379.conf、6389.conf 192.168.184.200
2 6380.conf、6390.conf 192.168.184.201
3 6381.conf、6391.conf 192.168.184.202

所有机器操作

修改主机名为redis1:hostnamectl set-hostname redis1

一、下载安装包

wget https://download.redis.io/releases/redis-6.2.6.tar.gz

二、安装

首先解压安装包,如下

tar -xf redis-6.2.6.tar.gz -C /usr/local/

cd /usr/local/redis-6.2.6/ && make && cd src && make install
mkdir myredis && cd myredis

注:如出现/bin/sh: cc: command not found错误,是因为没有安装 gcc 环境,使用命令yum install gcc安装 gcc 环境即可。

6379.conf、6389.conf主机操作

三、修改配置文件

cat > 6379.conf <<EOF
#后台启动
daemonize yes
#端口
port 6379
#集群配置
cluster-enabled yes
cluster-config-file nodes-6379.conf
cluster-node-timeout 15000
appendonly yes
protected-mode no
EOF
#将6379替换为相应的数字
例如:sed  's/6379/6380/g' 6379.conf > 6380.conf

启动集群

redis-cli --cluster create 192.168.184.200:6379 192.168.184.200:6389 192.168.184.201:6380 192.168.184.201:6390 192.168.184.202:6381 192.168.184.202:6391 --cluster-replicas 1

出现以下输出

[root@redis1 myredis]# redis-cli --cluster create 192.168.184.200:6379 192.168.184.200:6389 192.168.184.201:6380 192.168.184.201:6390 192.168.184.202:6381 192.168.184.202:6391 --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.184.201:6390 to 192.168.184.200:6379
Adding replica 192.168.184.202:6391 to 192.168.184.201:6380
Adding replica 192.168.184.200:6389 to 192.168.184.202:6381
M: 8d10bea851c9a395a5468ef7a532d6ef1467b8c3 192.168.184.200:6379
slots:[0-5460] (5461 slots) master
S: 89a3c8a41b1fc2acfda6daf818af81ca40270504 192.168.184.200:6389
replicates a98851a90a238ec225c8927a1762af1e1817802f
M: 42d223e013d8ac5ee151b3de95c90f9bb18b4406 192.168.184.201:6380
slots:[5461-10922] (5462 slots) master
S: f6cf2b976dda41188c3da43db3af659d5f5ea3df 192.168.184.201:6390
replicates 8d10bea851c9a395a5468ef7a532d6ef1467b8c3
M: a98851a90a238ec225c8927a1762af1e1817802f 192.168.184.202:6381
slots:[10923-16383] (5461 slots) master
S: 53a327979a054722046e178039c2651b8dc37634 192.168.184.202:6391
replicates 42d223e013d8ac5ee151b3de95c90f9bb18b4406
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.184.200:6379)
M: 8d10bea851c9a395a5468ef7a532d6ef1467b8c3 192.168.184.200:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 89a3c8a41b1fc2acfda6daf818af81ca40270504 192.168.184.200:6389
slots: (0 slots) slave
replicates a98851a90a238ec225c8927a1762af1e1817802f
M: a98851a90a238ec225c8927a1762af1e1817802f 192.168.184.202:6381
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 53a327979a054722046e178039c2651b8dc37634 192.168.184.202:6391
slots: (0 slots) slave
replicates 42d223e013d8ac5ee151b3de95c90f9bb18b4406
S: f6cf2b976dda41188c3da43db3af659d5f5ea3df 192.168.184.201:6390
slots: (0 slots) slave
replicates 8d10bea851c9a395a5468ef7a532d6ef1467b8c3
M: 42d223e013d8ac5ee151b3de95c90f9bb18b4406 192.168.184.201:6380
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.
检查cluster nodes
127.0.0.1:6379> cluster nodes
89a3c8a41b1fc2acfda6daf818af81ca40270504 192.168.184.200:6389@16389 slave a98851a90a238ec225c8927a1762af1e1817802f 0 1641456563000 5 connected
a98851a90a238ec225c8927a1762af1e1817802f 192.168.184.202:6381@16381 master - 0 1641456563000 5 connected 10923-16383
53a327979a054722046e178039c2651b8dc37634 192.168.184.202:6391@16391 slave 42d223e013d8ac5ee151b3de95c90f9bb18b4406 0 1641456564363 3 connected
f6cf2b976dda41188c3da43db3af659d5f5ea3df 192.168.184.201:6390@16390 slave 8d10bea851c9a395a5468ef7a532d6ef1467b8c3 0 1641456565379 1 connected
42d223e013d8ac5ee151b3de95c90f9bb18b4406 192.168.184.201:6380@16380 master - 0 1641456563000 3 connected 5461-10922
8d10bea851c9a395a5468ef7a532d6ef1467b8c3 192.168.184.200:6379@16379 myself,master - 0 1641456562000 1 connected 0-5460
posted @ 2022-02-18 14:10  i小杰  阅读(69)  评论(0)    收藏  举报