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 with Docker Compose

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

Docker Compose how to build a Redis Cluster cluster environment, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

The following uses Docker Compose to take you to build again, its main purpose is to let you feel the convenience brought by Docker Compose.

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.

For more information about Docker network mode, please read "detailed explanation of Docker Network Mode and Network Communication between containers".

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

Write Docker Compose template file

Create and start all service containers

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 16376template = 192.168.10.11 = = write Docker Compose template file

Create the docker-compose.yml file and edit it in the / usr/local/docker-redis directory of the 192.168.10.10 machine.

# describe the version information of the Compose file version: "3.8" # define the service, you can have multiple services: redis-6371: # Service name image: redis # Image container_name: redis-6371 # Container name restart: always # the container always restarts network_mode: "host" # host Network Mode volumes: # data volume Directory mount-/ usr/local/docker-redis/redis-cluster/6371/conf/redis.conf:/usr/local/etc/redis/redis.conf-/ usr/local/docker-redis/redis-cluster/6371/data:/data command: redis-server / usr/local/etc/redis/redis.conf # overrides the command redis-6372: image: redis container_name: redis-6372 network that is executed by default after the container starts _ mode: "host" volumes:-/ usr/local/docker-redis/redis-cluster/6372/conf/redis.conf:/usr/local/etc/redis/redis.conf-/ usr/local/docker-redis/redis-cluster/6372/data:/data command: redis-server / usr/local/etc/redis/redis.conf redis-6373: image: redis container_name: redis-6373 network_mode: "host" volumes :-/ usr/local/docker-redis/redis-cluster/6373/conf/redis.conf:/usr/local/etc/redis/redis.conf-/ usr/local/docker-redis/redis-cluster/6373/data:/data command: redis-server / usr/local/etc/redis/redis.conf

Create the docker-compose.yml file and edit it in the / usr/local/docker-redis directory of the 192.168.10.11 machine.

# describe the version information of the Compose file version: "3.8" # define the service, you can have multiple services: redis-6374: # Service name image: redis # Image container_name: redis-6374 # Container name restart: always # the container always restarts network_mode: "host" # host Network Mode volumes: # data volume Directory mount-/ usr/local/docker-redis/redis-cluster/6374/conf/redis.conf:/usr/local/etc/redis/redis.conf-/ usr/local/docker-redis/redis-cluster/6374/data:/data command: redis-server / usr/local/etc/redis/redis.conf # overrides the command redis-6375: image: redis container_name: redis-6375 network that is executed by default after the container starts _ mode: "host" volumes:-/ usr/local/docker-redis/redis-cluster/6375/conf/redis.conf:/usr/local/etc/redis/redis.conf-/ usr/local/docker-redis/redis-cluster/6375/data:/data command: redis-server / usr/local/etc/redis/redis.conf redis-6376: image: redis container_name: redis-6376 network_mode: "host" volumes :-/ usr/local/docker-redis/redis-cluster/6376/conf/redis.conf:/usr/local/etc/redis/redis.conf-/ usr/local/docker-redis/redis-cluster/6376/data:/data command: redis-server / usr/local/etc/redis/redis.conf create and start all service containers

Execute the following command under the / usr/local/docker-redis directory of the 192.168.10.10 and 192.168.10.11 machines, respectively:

Docker-compose up-d

Create a Redis Cluster cluster

Please make sure that your two machines can communicate with each other, then 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: fb76ada83a70ea8d367f22f8a6436d427f12022c 192.168.10.10 masterM: fb76ada83a70ea8d367f22f8a6436d427f12022c 192.168.10.10 .10 masterS 6372 slots: [10923-16383] (5461 slots) masterS: 20f6913a4b08afe3b4092a4f6c70fddd9506f2fa 192.168.10.10 masterS 6373 replicates c130120ffbd7c4e442ac9fc9870e977dc801cb54M: c130120ffbd7c4e442ac9fc9870e977dc801cb54 192.168.10.11 replicates c130120ffbd7c4e442ac9fc9870e977dc801cb54M 6374 slots: [5461-10922] (5462 slots) masterS: d4d5f7fd94fcd2239e82e481f0529380b288ff51 192.168.10.11 replicates fb76ada83a70ea8d367f22f8a6436d427f12022cS 6375 replicates fb76ada83a70ea8d367f22f8a6436d427f12022cS: e08b8f629e2dfa474a836910634052d83c77d06a 192.168.10.11Rule 6376 replicates 04444afa3ca66de6af4a182984c79d4d140293d7Can 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: 04444afa3ca66de6af4a182984c79d4d140293d7 192.168.10.10 master 6371 slots: [0-5460] (5461 slots) master 1 additional replica (s) S: 20f6913a4b08afe3b4092a4f6c70fddd9506f2fa 192.168.10.10 VR 6373 slots: (0 slots) slave replicates c130120ffbd7c4e442ac9fc9870e977dc801cb54S: d4d5f7fd94fcd2239e82e481f0529380b288ff51 192.168.10.11 Slots: (0 slots) slave replicates fb76ada83a70ea8d367f22f8a6436d427f12022cM: fb76ada83a70ea8d367f22f8a6436d427f12022c 192.168.10.106372 slots: [10923-16383] (5461 slots) master 1 additional replica (s) M: c130120ffbd7c4e442ac9fc9870e977dc801cb54 192.168.10.11 master 6374 slots: [5461-10922] (5462 slots) master 1 additional replica (s) S: e08b8f629e2dfa474a836910634052d83c77d06a 192.168.10.11 master 6376 slots: (0 slots) slave replicates 04444afa3ca66de6af4a182984c79d4d140293d7 [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.

After reading the above, have you mastered how Docker Compose can build a Redis Cluster cluster environment? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

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

Internet Technology

Wechat

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

12
Report