redis cluster mac installation

1. Install redis

The mac environment is installed with brew install

brew install redis

After installation, the default configuration starts the single point service

redis-server

Note: The brew default program is installed in the /usr/local/Cellar directory

/usr/local/Cellar/redis

The default configuration file is in

/usr/local/etc/redis.conf

2. Create a configuration file

Prepare to create 6 nodes, for convenience, create 6 working directories, and create directories according to the prepared port numbers

cd ~
mkdir rediscluster
cd rediscluster
mkdir 8001 8002 8003 8004 8005 8006

First copy the default configuration file to 8001, modify the configuration file

cd 8001
cp /usr/local/etc/redis.conf redis.conf
vi redis.conf

Find the following keywords in the configuration file and modify

# 端口号,每个目录都不同
port 800X
# 开启集群模式
cluster-enabled yes
#节点超时实际,单位毫秒
cluster-node-timeout 5000
# 每个Redis集群节点/实例需要一个单独的配置文件,同一宿主机系统中不同实例的配置文件名称不能冲突
cluster-config-file nodes-800X.conf
# 启动 AOF
appendonly yes

作者:等到的等待
链接:https://juejin.cn/post/6998537296899211278
来源:稀土掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

Copy the modified configuration file to several other folders, and modify the corresponding port number

Then enter each directory one by one and execute the command:

redis-server redis.conf

3. Associate all nodes

All nodes have been started, but at this point they are each independent of a single cluster node. In order to realize the cluster, they must be associated, just enter the redis-cli of a node

Execute the following command:

redis-cli -p 8001
127.0.0.1:8001> cluster meet 127.0.0.1 8002
OK
127.0.0.1:8001> cluster meet 127.0.0.1 8003
OK
127.0.0.1:8001> cluster meet 127.0.0.1 8004
OK
127.0.0.1:8001> cluster meet 127.0.0.1 8005
OK
127.0.0.1:8001> cluster meet 127.0.0.1 8006
OK

At this point all nodes are connected

4. Assign slots

Redis Cluster is composed of 16384 slots, so we need to distribute these slots to 3 nodes (3 masters and 3 slaves)

Excuting an order:

redis-cli -p 8001 cluster addslots {0..5461}
redis-cli -p 8003 cluster addslots {5462..10922}
redis-cli -p 8005 cluster addslots {10923..16383}

At this point the node has been allocated. Verify by command

redis-cli -p 8001 cluster nodes

2e2765b574da0ed4532b4568ba3d9536496955c4 127.0.0.1:8006@18006 master - 0 1672488398329 5 connected
b3d3bbfe7f6ec8c5c949bf6e60c82147f6cd7f7e 127.0.0.1:8003@18003 master - 0 1672488396315 3 connected 5462-10922
fcb16440bc6156034fe22fc268e5dc21d58af468 127.0.0.1:8004@18004 master - 0 1672488397825 0 connected
e6e8d6884e004d9cc58c3b5599eab456fda43fb0 127.0.0.1:8005@18005 master - 0 1672488398000 4 connected 10923-16383
4856476e14fe48e15418718f98a2aabc89a4bca4 127.0.0.1:8001@18001 myself,master - 0 1672488396000 1 connected 0-5461
74ff13d26dc78a166e24f2ac7de084e842fcdadb 127.0.0.1:8002@18002 master - 0 1672488397321 2 connected

As can be seen from the figure, nodes 8001, 8003, and 8005 already have slots

5. Master-slave replication

The master node already has a slot, and the next step is to associate the master node with the slave node to form a master-slave replication relationship.

Note: The cli command window of the slave node needs to be associated with the master node

redis-cli -p 8002 cluster replicate 4856476e14fe48e15418718f98a2aabc89a4bca4
OK
redis-cli -p 8004 cluster replicate b3d3bbfe7f6ec8c5c949bf6e60c82147f6cd7f7e
OK
redis-cli -p 8006 cluster replicate e6e8d6884e004d9cc58c3b5599eab456fda43fb0
OK

The following string is the id of the corresponding master node, and the command can be executed again

redis-cli -p 8001 cluster nodes

 You can see that the master and slave have been allocated

6. Tools to build clusters

The above commands are relatively cumbersome, and can be built through the small utility redis-trib.rb provided by redis, which is portable with ruby, and these commands are also used internally. In order to understand the principle of redis cluster, this film does not use this method. The specific construction method can be googled by yourself.

7. Verify the cluster

redis-cli -p 8001
127.0.0.1:8001> set a b
(error) MOVED 15495 127.0.0.1:8005

Seeing that the above command reports an error, it is because the cluster mode is not started when connecting with redis-cli, and the parameter -c needs to be added

redis-cli -c -p 8001
127.0.0.1:8001> set a b
-> Redirected to slot [15495] located at 127.0.0.1:8005
OK
127.0.0.1:8005> get a
"b"
127.0.0.1:8005> keys *
1) "a"
127.0.0.1:8005> set a c
OK
127.0.0.1:8005> get a
"c"
127.0.0.1:8005> set b d
-> Redirected to slot [3300] located at 127.0.0.1:8001
OK
127.0.0.1:8001> get b
"d"

As can be seen from the figure above, after connecting to node 8001 and adding a key value, the set operation is executed from directional storage to 8005. The following series of operations are practice commands

Guess you like

Origin blog.csdn.net/chen_peng7/article/details/128507611