Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to build Redis Cluster Cluster Environment in Docker

2025-04-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to build a Redis Cluster cluster environment in Docker. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Using Docker to build Redis Cluster, the most important link is the problem of container communication. In the previous article, we have solved "detailed explanation of Docker network mode and network communication between containers". This article mainly exercises to use multiple containers to build a Redis Cluster cluster environment, which paves the way for learning Docker Compose. As the saying goes, there is no harm if there is no comparison. Only by comparison can we feel the benefits of Docker Compose.

For more information about Redis Cluster clusters, please read "the most easy-to-understand details of Redis Architecture patterns".

Follow the tips on Redis's official website: https://redis.io/topics/cluster-tutorial, in order to make Docker compatible with Redis Cluster, you need to use Docker's host network mode.

The host network mode needs to be specified by parameters-- net host or-- network host when creating the container. The host network mode allows the container to share the host network stack. The container will not virtualize its own network card or configure its own IP, but will use the host's IP and port.

Environment

In order to make the environment more realistic, this paper uses a multi-machine environment:

192.168.10.10

192.168.10.11

The infrastructure environment used by each machine is as follows:

CentOS 7.8.2003

Docker version 19.03.12

Set up

The overall construction steps are mainly divided into the following steps:

Download Redis image (in fact, this step can be omitted, because when creating a container, if the local image does not exist, it will be pulled remotely)

Write Redis configuration file

Create a Redis container

Create a Redis Cluster cluster.

Write Redis configuration files to create directories and files

Do the following on 192.168.10.10 and 192.168.10.11, respectively.

# create directory mkdir-p / usr/local/docker-redis/redis-cluster# change to specified directory cd / usr/local/docker-redis/redis-cluster/# write redis-cluster.tmpl file vi redis-cluster.tmpl write configuration file

The redis-cluster.tmpl file for the 192.168.10.10 machine is as follows:

Port ${PORT} requirepass 1234masterauth 1234protected-mode nodaemonize noappendonly yescluster-enabled yescluster-config-file nodes.confcluster-node-timeout 15000cluster-announce-ip 192.168.10.10cluster-announce-port ${PORT} cluster-announce-bus-port 1 ${PORT}

The redis-cluster.tmpl file for the 192.168.10.11 machine is as follows:

Port ${PORT} requirepass 1234masterauth 1234protected-mode nodaemonize noappendonly yescluster-enabled yescluster-config-file nodes.confcluster-node-timeout 15000cluster-announce-ip 192.168.10.11cluster-announce-port ${PORT} cluster-announce-bus-port 1 ${PORT}

Port: node port

Requirepass: add access authentication

Masterauth: if the master node has access authentication enabled, the slave node needs authentication to access the master node.

Protected-mode: protection mode. Default is yes, that is, enabled. After turning on the protected mode, you need to configure bind ip or set the access password; turn off the protected mode, and the external network can access it directly.

Daemonize: whether to start as a daemon thread (background startup). Default no

Appendonly: whether to enable AOF persistence mode. Default is no.

Cluster-enabled: whether to enable cluster mode. Default is no.

Cluster-config-file: cluster node information file

Cluster-node-timeout: cluster node connection timeout

Cluster-announce-ip: cluster node IP, enter the IP of the host

Cluster-announce-port: cluster node mapping port

Cluster-announce-bus-port: cluster node bus port.

Each Redis cluster node needs to open two TCP connections. A normal Redis TCP port used to provide services to clients, for example, 6379. There is also a port based on port 6379 plus 10000, such as 16379.

The second port is for the cluster bus, which is a node-to-node communication channel using the binary protocol. Nodes use cluster bus for fault detection, configuration updates, failover authorization, and so on. The client should never try to communicate with the cluster bus port, just communicate with the normal Redis command port, but make sure that both ports in the firewall are open, otherwise the Redis cluster node will not be able to communicate.

Execute the following command in the redis-cluster directory of the 192.168.10.10 machine:

For port in `seq 6371 6373`; do\ mkdir-p ${port} / conf\ & & PORT=$ {port} envsubst

< redis-cluster.tmpl >

${port} / conf/redis.conf\ & & mkdir-p ${port} / data;\ done

Execute the following command in the redis-cluster directory of the 192.168.10.11 machine:

For port in `seq 6374 6376`; do\ mkdir-p ${port} / conf\ & & PORT=$ {port} envsubst

< redis-cluster.tmpl >

${port} / conf/redis.conf\ & & mkdir-p ${port} / data;\ done

The above two shell for statements mean to cycle through the creation of 6371-6376 related directories and files.

The results of the view command executed on the 192.168.10.10 machine are as follows. Install yum install-y tree first if there is no tree command.

The result of the view command executed on the 192.168.10.11 machine is as follows.

The following is the profile details for each node.

= 192.168.10.10 = = [root@localhost redis-cluster] # cat / usr/local/docker-redis/redis-cluster/637 {1.. 3} / conf/redis.confport 6371requirepass 1234masterauth 1234protected-mode nodaemonize noappendonly yescluster-enabled yescluster-config-file nodes.confcluster-node-timeout 15000cluster-announce-ip 192.168.10.10cluster-announce-port 6371cluster-announce-bus-port 16371port 6372requirepass 1234masterauth 1234protected-mode nodaemonize noappendonly yescluster-enabled yescluster-config-file nodes.confcluster-node-timeout 15000cluster-announce-ip 192. 168.10.10cluster-announce-port 6372cluster-announce-bus-port 16372port 6373requirepass 1234masterauth 1234protected-mode nodaemonize noappendonly yescluster-enabled yescluster-config-file nodes.confcluster-node-timeout 15000cluster-announce-ip 192.168.10.10cluster-announce-port 6373cluster-announce-bus-port 16373 cat = 192.168.10.10 = = 192.168.10.11 = = [root@localhost redis-cluster] # cat / usr/local/docker-redis/redis-cluster/637 {4.. 6} / conf/redis.confport 6374requirepass 1234masterauth 1234protected-mode Nodaemonize noappendonly yescluster-enabled yescluster-config-file nodes.confcluster-node-timeout 15000cluster-announce-ip 192.168.10.11cluster-announce-port 6374cluster-announce-bus-port 16374port 6375requirepass 1234masterauth 1234protected-mode nodaemonize noappendonly yescluster-enabled yescluster-config-file nodes.confcluster-node-timeout 15000cluster-announce-ip 192.168.10.11cluster-announce-port 6375cluster-announce-bus-port 16375port 6376requirepass 1234masterauth 1234protected-mode nodaemonize noappendonly yescluster-enabled yescluster-config-file nodes.confcluster-node-timeout 15000cluster-announce-ip 192.168.10. 11cluster-announce-port 6376cluster-announce-bus-port 16376 container = 192.168.10.11 = = create Redis container create container

Map the ports between 6371 and 6376 of the host to 6 Redis containers, and map the host directory to the directory in the container (directory mount). Remember to specify the network mode and use the host network mode.

Execute the following command on the 192.168.10.10 machine:

For port in $(seq 6371 6373); do\ docker run-di-- restart always-- name redis-$ {port}-net host\-v / usr/local/docker-redis/redis-cluster/$ {port} / conf/redis.conf:/usr/local/etc/redis/redis.conf\-v / usr/local/docker-redis/redis-cluster/$ {port} / data:/data\ redis redis-server / usr/local/etc/redis/redis.conf;\ done

Execute the following command on the 192.168.10.11 machine:

For port in $(seq 6374 6376); do\ docker run-di-- restart always-- name redis-$ {port}-net host\-v / usr/local/docker-redis/redis-cluster/$ {port} / conf/redis.conf:/usr/local/etc/redis/redis.conf\-v / usr/local/docker-redis/redis-cluster/$ {port} / data:/data\ redis redis-server / usr/local/etc/redis/redis.conf;\ done

Run docker ps-n 3 on the 192.168.10.10 machine to see if the container is created successfully.

Run docker ps-n 3 on the 192.168.10.11 machine to see if the container is created successfully.

Create a Redis Cluster cluster

Enter any container node and enter the / usr/local/bin/ directory:

# enter the container docker exec-it redis-6371 bash# and change to the specified directory cd / usr/local/bin/

Next, we can create the Redis Cluster cluster with the following command.

Redis-cli-a 1234-- cluster create 192.168.10.10 cluster-replicas 6371 192.168.10.10 cluster-replicas 6372 192.168.10.10 cluster-replicas 6372 192.168.10.10

At the selection prompt, enter yes, and the result is as follows:

The cluster was successfully created as follows:

The following is the details returned when the cluster was created, that is, everything in the previous two images.

Root@localhost:/usr/local/bin# redis-cli-a 1234-- cluster create 192.168.10.10 root@localhost:/usr/local/bin# redis-cli 6371 192.168.10.10 cluster-replicas 1Warning: 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-5460Master [1]-> Slots 5461-10922Master [2]-> Slots 10923-16383Adding replica 192.168.10.11 slots 6376 to 192.168.10.10:6371Adding replica 192.168.10.11 slots 6373 to 192.168.10.11:6374Adding replica 192.168.10.11 slots: [0-5460] (5461 slots) masterM: ac805b90b6e20e26dc4268454bb2855beea6cc19 192.168.10 .10 masterS 6372 slots: [10923-16383] (5461 slots) masterS: db35494fcc5db0c88d27da7885c817e6cdcc9373 192.168.10.10 masterS 6373 replicates 7013270480d37eeab79b9cd0272e934d4548136aM: 7013270480d37eeab79b9cd0272e934d4548136a 192.168.10.11 replicates 7013270480d37eeab79b9cd0272e934d4548136aM 6374 slots: [5461-10922] (5462 slots) masterS: 8435e1b0d51f2690c5f94f9a5682a4ac34e94326 192.168.10.11 replicates ac805b90b6e20e26dc4268454bb2855beea6cc19S 6375 replicates ac805b90b6e20e26dc4268454bb2855beea6cc19S: 7b13c16fa6fe8e13cdc0b4846b87edffed55c62e 192.168.10.11Rule 6376 replicates 299cf79ddafc83dced27f628f1f82dac483fbc4eCan 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 clusterWaiting for the cluster to join. > > Performing Cluster Check (using node 192.168.10.10 master 6371) M: 299cf79ddafc83dced27f628f1f82dac483fbc4e 192.168.10.10 master 6371 slots: [0-5460] (5461 slots) master 1 additional replica (s) S: 8435e1b0d51f2690c5f94f9a5682a4ac34e94326 192.168.10.11 master 6375 slots: (0 slots) slave replicates ac805b90b6e20e26dc4268454bb2855beea6cc19S: db35494fcc5db0c88d27da7885c817e6cdcc9373 192.168.10.106373 Slots: (0 slots) slave replicates 7013270480d37eeab79b9cd0272e934d4548136aS: 7b13c16fa6fe8e13cdc0b4846b87edffed55c62e 192.168.10.11 slots 6376 slots: (0 slots) slave replicates 299cf79ddafc83dced27f628f1f82dac483fbc4eM: 7013270480d37eeab79b9cd0272e934d4548136a 192.168.10.11 master 6374 slots: [5461-10922] (5462 slots) master 1 additional replica (s) M: ac805b90b6e20e26dc4268454bb2855beea6cc19 192.168.10.106372 slots: [10923-168383] (5461 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.

At this point, a highly available Redis Cluster cluster has been built. As shown in the following figure, the cluster contains 6 Redis nodes, 3 masters and 3 slaves. The three master nodes allocate slots to handle command requests from the client, while the slave node can be used to replace the master node after a failure of the master node.

View cluster status

Let's first go to the container and then check the status of the cluster through some commands commonly used in the cluster.

# enter the container docker exec-it redis-6371 bash# and change to the specified directory cd / usr/local/bin/ to check the cluster status redis-cli-a 1234-- cluster check 192.168.10.11 cd 6375

View cluster information and node information # connect to a node in the cluster redis-cli-c-a 1234-h 192.168.10.11-p 637nodes view cluster information cluster info# view cluster node information cluster nodes

SET/GET

Perform writes and reads in the 6371 node with the following commands:

# enter the container and connect to a node in the cluster docker exec-it redis-6371 / usr/local/bin/redis-cli-c-a 1234-h 192.168.10.10-p 637 write data set name mrhelloworldset aaa 111set bbb 22 read data get nameget aaaget bbb

Don't worry, let me explain the operation in the figure above:

First enter the container and connect to a node in the cluster

Then execute the first set command, the set name mrhelloworld,name key, and get a value of [5798] based on the hash function. The current slot allocation in the cluster environment is [0-5460] 6371 nodes, [5461-10922] 6374 nodes, and [10923-16383] 6372 nodes, so the storage of this key is allocated to 6374 nodes.

Let's take a look at the second set command set aaa. Here you may have some questions. Why can't you see the value of the aaa key calculated according to the hash function? Because just redirected to the 6374 node to insert the data, if there is still data insertion, it happens that the value obtained after the hash function operation is also within the range of the node, then you can insert the data directly.

Then comes the third set command, the set bbb,bbb key, which is calculated according to the hash function to get a value of [5287], so the storage of the key is allocated to the 6371 node.

Then there is the read operation. The fourth command, get name,name, gets a value of [5798] according to the hash function, which is redirected to the 6374 node to read.

The fifth command get aaa,aaa key according to the hash function after the operation of the value is also in the 6374 node, directly read

The sixth command, get bbb,bbb, gets a value of [5287] based on the hash function and is redirected to 6371 nodes to read.

From the above, we know that the storage of the name key is allocated to 6374 nodes. What if we directly connect to 6374 nodes and get this value? Yes, there is no need to redirect the node, because the data is on that node, so read and return directly.

Client connection

Finally, there is a wave of client connection operations, any node, to see if you can access the Redis Cluster cluster externally.

This is the end of the article on "how to build a Redis Cluster cluster environment for Docker". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report