Environment preparation: CentOS7
Three virtual machines: because the redis cluster is highly available, six nodes are configured here. Two nodes are deployed for each. ip and port are respectively:
192.168.1.103:6379
192.168.1.103:6380
192.168.1.106:6379
192.168.1.106:6380
192.168.1.1076379
192.168.1.107:6380
1. docker installation
Let's take a look at my previous blog summary: https://blog.csdn.net/weixin_41645232/article/details/104578306
2. Pull the redis image
# No write version. The latest version is pulled by default docer pull redis
3. Prepare the redis environment (the three operations are the same in this step. Take 192.168.1.107 as an example.)
3.1 create redis configuration directory and mapping data circle directory to backup synchronization and start docker container
# Create directory mkdir -p /opt/redis/redis-6379/conf mkdir -p /opt/redis/redis-6379/data
3.2 redis configuration file. In actual production, you can download redis.conf from the redis source code. Because this is just a test, you can directly configure and write parameters that are simple and can run
# To configuration directory cd /opt/redis/redis-6379/conf vim ./redis.conf # Write the following port 6379 #port cluster-enabled yes #Turn on cluster mode cluster-config-file nodes.conf #Cluster node information file cluster-node-timeout 5000 #Timeout time cluster-announce-ip 192.168.1.107 #Deploy the corresponding ip on the redis host cluster-announce-port 6379 #Cluster mapping port cluster-announce-bus-port 16379 #Cluster bus port appendonly yes #Instruction append persistence mode
3.3 docker redis container startup script
# cd to redis-6379 directory cd /opt/redis/redis-6379 # Edit container startup script vim ./redis-6379-start.sh # Write the following #!/bin/bash docker run -d -ti \ --privileged=true -v /opt/redis/redis-6379/conf/redis.conf:/usr/local/etc/redis/redis.conf \ --privileged=true -v /opt/redis/redis-6379/data:/data \ --restart always --name redis-6379 --net host \ --sysctl net.core.somaxconn=1024 redis redis-server /usr/local/etc/redis/redis.conf # Modify script execution permission chmod a+u ./redis-6379-start.sh
3.4 execute to start redis-6379 container
./redis-6379-start.sh
Here, the redis-6379 of 192.168.1.107 has been started. We just need to modify the configuration to start the second local redis-6380
3.5 copy, modify and start redis-6380
# cd to / opt/redis directory, and copy redis-6379 directly cd /opt/redis cp -r ./redis-6379 ./redis-6380 # Modify the redis.conf file configuration item directly port 6380 #port cluster-announce-ip 192.168.1.107 #Need to be replaced when other host configuration cluster-announce-port 6380#Cluster mapping port cluster-announce-bus-port 16380 #Cluster bus port # Modify startup script //Directly replace 6379 with 6380, and other items do not need to be changed
3.6 start redis-6380
# Modify the script name mv ./redis-6379-start.sh ./redis-6380-start.sh # start-up ./redis-6380-start.sh
4. Follow the steps of 192.168.1.107, and operate on 192.168.1.103 and 192.168.1.106.
5. All the above steps have started the redis service, but the cluster mode has not really started yet. Next, enter a redis container at random. Here, take 192.168.1.107 as an example to start the cluster mode.
# Enter the redis-6379 container and assign terminals docker exec -it redis-6379 bash # Execute command to start cluster mode redis-cli --cluster create 192.168.1.107:6379 192.168.1.107:6380 192.168.1.103:6379 192.168.1.103:6380 192.168.1.106:6379 192.168.1.106:6380 --cluster-replicas 1
Command display process (mainly a process of assigning master-slave nodes and hashing slots, just enter yes in the middle):
>>> 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.1.103:6380 to 192.168.1.107:6379 Adding replica 192.168.1.106:6380 to 192.168.1.103:6379 Adding replica 192.168.1.107:6380 to 192.168.1.106:6379 M: e8aef9ed8e05b38f8bc65879bd30a825b306c7b6 192.168.1.107:6379 slots:[0-5460] (5461 slots) master S: 66559e06fefa1e6aa4c73f6141addc750df16406 192.168.1.107:6380 replicates 3baa5bc431925f1099561bc82355c992e19c42f8 M: d7d899e9ad644e86641f82bb97adf2f7f7ff8214 192.168.1.103:6379 slots:[5461-10922] (5462 slots) master S: 7ec346484effbe562ea0bf3e40ed9e9538b973b7 192.168.1.103:6380 replicates e8aef9ed8e05b38f8bc65879bd30a825b306c7b6 M: 3baa5bc431925f1099561bc82355c992e19c42f8 192.168.1.106:6379 slots:[10923-16383] (5461 slots) master S: fe233c5c139ea7bb7c2b91c94b8efd6d6f592096 192.168.1.106:6380 replicates d7d899e9ad644e86641f82bb97adf2f7f7ff8214 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.1.107:6379) M: e8aef9ed8e05b38f8bc65879bd30a825b306c7b6 192.168.1.107:6379 slots:[0-5460] (5461 slots) master 1 additional replica(s) M: d7d899e9ad644e86641f82bb97adf2f7f7ff8214 192.168.1.103:6379 slots:[5461-10922] (5462 slots) master 1 additional replica(s) S: 7ec346484effbe562ea0bf3e40ed9e9538b973b7 192.168.1.103:6380 slots: (0 slots) slave replicates e8aef9ed8e05b38f8bc65879bd30a825b306c7b6 S: 66559e06fefa1e6aa4c73f6141addc750df16406 192.168.1.107:6380 slots: (0 slots) slave replicates 3baa5bc431925f1099561bc82355c992e19c42f8 M: 3baa5bc431925f1099561bc82355c992e19c42f8 192.168.1.106:6379 slots:[10923-16383] (5461 slots) master 1 additional replica(s) S: fe233c5c139ea7bb7c2b91c94b8efd6d6f592096 192.168.1.106:6380 slots: (0 slots) slave replicates d7d899e9ad644e86641f82bb97adf2f7f7ff8214 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.
6. test
If you see that a set key can be redirected to other nodes and it is proved to be successful, pay attention to add - c to its redis client to enter the cluster mode. Otherwise, when a key needs to be stored to other nodes, an error will be reported and it cannot be moved to other nodes
[root@localhost redis-6380]# docker exec -it redis-6379 redis-cli -c 127.0.0.1:6379> keys * (empty list or set) 127.0.0.1:6379> set name whf -> Redirected to slot [5798] located at 192.168.1.103:6379 OK 192.168.1.103:6379> set age 19 -> Redirected to slot [741] located at 192.168.1.107:6379 OK