Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to build Redis Cluster quickly by Docker

Shulou Source: shulou.com Published: 2022-06-02 10:17:01 10月02日 Update

What the editor wants to share with you this time is how to quickly build a Redis cluster in Docker. The article is rich in content. Interested friends can learn about it. I hope you can get something after reading this article.

What is a Redis cluster

Redis cluster is a distributed database scheme provided by Redis. The cluster shares data through sharding and provides replication and failover functions.

Node

A Redis cluster is usually composed of multiple nodes (node). At the beginning, each node is independent of each other, and they are all in a cluster that contains only their own. In order to build a truly working cluster, we must connect the independent nodes to form a cluster containing multiple nodes.

Cluster configuration

Configuration file

Adjust CLUSTER node configuration

# Open cluster cluster cluster-enabled yes# cluster configuration file cluster-config-file nodes-6379.conf# cluster node timeout cluster-node-timeout 15000

Docker quickly set up Redis Cluster

Install Redis

Reference article: https://www.jb51.net/article/150054.htm

Preparatory work

├── conf │ ├── redis.conf │ └── sentinel.conf ├── redis │ ├── data_6379 │ ├── data_6380 │ ├── data_6381 │ ├── data_6382 │ ├── data_6383 │ └── data_6384 └── scripts cluster.sh run.sh sentinel.sh

Run.sh script file

#! / usr/bin/env bashset-e# script current directory cPath=$ (cd $(dirname "$0") | | exit; pwd) # root directory dirPath=$ (dirname "$cPath") # get port port= "$1" if [[! "$port"]] Then port=6379fi# create data directory mkdir-p "$dirPath" / redis/data_ "$port" # delete the started service containerId=$ (docker ps-a | grep "redis_$port" | awk-F''{print $1}') if [["$containerId"]] Then docker rm-f ${containerId} > / dev/nullfi# launch service containerName=redis_ "$port" docker run-itd-- privileged=true-p "$port": 6379-- name ${containerName}\-v = "$dirPath" / conf/redis.conf:/etc/redis/redis.conf\-v = "$dirPath" / redis/data_ "$port": / data\ redis\ redis-server / etc/redis/redis.conf > / dev/null# get the container IP address dockerIp=$ (docker inspect-f) {{.NetworkSettings.IPAddress}} "$containerName") # get the container startup status isRunning=$ (docker inspect-f "{{.State.running}}"$containerName") if [["$isRunning" = "true"]] Then echo "Container: $containerName-IP:$dockerIp-launched successfully" fi

Cluster.sh script file

#! / usr/bin/env bashset-e # script current directory cPath=$ (cd $(dirname "$0") | | exit; pwd) # number of startup clusters num= "$1" if [[! "$num"]]; then num=6fisPort=6378for ((iTunes redis_63846fecf70465b8 redis "docker-entrypoint.s …" 27 seconds ago Up 26 seconds 0.0.0.0 6379/tcp redis_63831af15e90b7a0 redis 6383-> "docker-entrypoint.s …" 28 seconds ago Up 27 seconds 0.0.0.0 6379/tcp redis_63826c495f31a5df redis 6382-> 6379/tcp redis_63826c495f31a5df redis "docker-entrypoint.s …" 28 seconds ago Up 28 seconds 0.0.0.0 6379/tcp redis_6381e54fd9fd0550 redis 6381-> "docker-entrypoint.s …" 29 seconds ago Up 28 seconds 0.0.0.0 docker-entrypoint.s 6380-> 6379/tcp redis_6380be92ad2f7046 redis "docker-entrypoint.s …" 29 seconds ago Up 29 seconds 0.0.0.014 6379-> 6379/tcp redis_6379

So far, 6 independent cluster nodes have been created and are not working properly.

Create a cluster

You can skip it here. I'm trying to save trouble.

Get the IP addresses of all containers that start with redis_

Docker inspect-f "{{.NetworkSettings.IPAddress}}: 6379" ``NetworkSettings.IPAddress}}: 6379 "`NetworkSettings.IPAddress

Create a cluster for the first time

. / redis-cli-- cluster create 172.17.0.2 cluster-replicas 6379, 172.17.0.3 cluster-replicas 6379, 172.17.0.4 purl 6379, 172.17.0.5 purl 6379, 172.17.0.6 cluster-replicas 6379--

Output result

Licas 1 > > Performing hash slots allocation on 6 nodes...Master [0]-> Slots 0-5460Master [1]-> Slots 5461-10922Master [2]-> Slots 10923-16383Adding replica 172.17.0.6 to 172.17.0.3:6379Adding replica 172.17.0.7 to 172.17.0.3:6379Adding replica 172.17.0.5 to 172.17.0.4 to 6379M: e8da1fef656984de3ec2a677edc8d9c48d01cd95 172.17.0.2 5460Master 6379 slots: [0-5460 ] (5461 slots) masterM: 68b925ab0fbbc1a632c1754587fb6dad3fa14c91 172.17.0.3 slots 6379 slots: [5461-10922] (5462 slots) masterM: 0a46ab2f6d176738b55fe699c2df1c34f8200d06 172.17.0.4 slots 6379 slots: [10923-16383] (5461 slots) masterS: bd3064ad5297dfc258e9236943455c589be8b2a3 172.17.0.56379 replicates 0a46ab2f6d176738b55fe699c2df1c34f8200d06S: f1d8c897882d29e6538b1158525493b3b782289a 172.17.0.6 slots 6379 replicates e8da1fef656984de3ec2a677edc8d9c48d01cd95S: 619e1cb52f39e07b321719b77fc3631fa6293cef 172.17.0.7 replicates 68b925ab0fbbc1a632c1754587fb6dad3fa14c91Can I set the above configuration? (type 'yes' to accept):

Enter: yes, the slots will be distributed evenly

> > 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 172.17.0.2 using node 6379) M: e8da1fef656984de3ec2a677edc8d9c48d01cd95 172.17.0.2 using node 6379 slots: [0-5460] (5461 slots) master 1 additional replica (s) S: f1d8c897882d29e6538b1158525493b3b782289a 172.17.0.6 Performing Cluster Check 6379 slots: (0 slots) slave replicates e8da1fef656984de3ec2a677edc8d9c48d01cd95S: bd3064ad5297dfc258e9236943455c589be8b2a3 172.17.0.56379 slots: (0 slots) slave replicates 0a46ab2f6d176738b55fe699c2df1c34f8200d06M: 0a46ab2f6d176738b55fe699c2df1c34f8200d06 172.17.0.4 master 6379 slots: [10923-16383] (5461 slots) master 1 additional replica (s) S: 619e1cb52f39e07b321719b77fc3631fa6293cef 172.17.0.7 master 6379 slots: (0 slots) slave replicates 68b925ab0fbbc1a632c1754587fb6dad3fa14c91M: 68b925ab0fbbc1a632c1754587fb6dad3fa14c91 172.17.0.3 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 16384slots covered.

Connect the cluster

Connect through the client

Redis-cli-c

Execute command: cluster info

127.0.0.1 purl 6379 > cluster infocluster_state:okcluster_slots_assigned:16384cluster_slots_ok:16384cluster_slots_pfail:0cluster_slots_fail:0cluster_known_nodes:6cluster_size:3cluster_current_epoch:6cluster_my_epoch:1cluster_stats_messages_ping_sent:104cluster_stats_messages_pong_sent:120cluster_stats_messages_sent:224cluster_stats_messages_ping_received:115cluster_stats_messages_pong_received:104cluster_stats_messages_meet_received:5cluster_stats_messages_received:224

See: cluster_state:ok indicates that the cluster can work properly.

Client console: cluster help

127.0.0.1 cluster help 6379 > 1) CLUSTER arg arg... Arg. Subcommands are: 2) ADDSLOTS [slot.]-- Assign slots to current node. 3) BUMPEPOCH-Advance the cluster config epoch. 4) COUNT-failure-reports-Return number of failure reports for. 5) COUNTKEYSINSLOT-Return the number of keys in. 6) DELSLOTS [slot...]-- Delete slots information from current node. 7) FAILOVER [force | takeover]-- Promote current replica node to being a master. 8) FORGET-Remove a node from the cluster. 9) GETKEYSINSLOT-Return key names stored by current node in a slot.10) FLUSHSLOTS-Delete current node own slots information.11) INFO-Return information about the cluster.12) KEYSLOT-- Return the hash slot for. 13) MEET [bus-port]-- Connect nodes into a working cluster.14) MYID-- Return the node id.15) NODES-- Return cluster configuration seen by node. Output format:16)... 17) REPLICATE-- Configure current node as replica to. 18) RESET [hard | soft]-- Reset current node (default: soft) .19) SET-config-epoch-Set config epoch of current node.20) SETSLOT (importing | migrating | stable | node)-- Set slot state.21) REPLICAS-- Return replicas.22) SAVECONFIG-Force saving cluster configuration on disk.23) SLOTS-- Return information about slots range mappings. Each range is made of:24) start, end, master and replicas IP addresses, ports and ids

Check the cluster-related commands provided by the client: redis-cli-- cluster help

Cluster Manager Commands: create host1:port1... HostN:portN-cluster-replicas check host:port-cluster-search-multiple-owners info host:port fix host:port-cluster-search-multiple-owners reshard host:port-cluster-from-cluster-to-cluster-slots-cluster-yes cluster-timeout cluster-pipeline -cluster-replace rebalance host:port-- cluster-weight-- cluster-use-empty-masters-- cluster-timeout-- cluster-simulate-- cluster-pipeline-- cluster-threshold-- cluster-replace add-node new_host:new_port existing_host:existing_port-- cluster-slave-- cluster- Master-id del-node host:port node_id call host:port command arg arg.. Arg set-timeout host:port milliseconds import host:port-- cluster-from-- cluster-copy-- cluster-replace finished reading this article about how Docker can quickly build a Redis cluster. If you think the article is well written, you can share it with more people.

Tags: Cluster container node success script file article work configuration customer client data directory result service stand-alone content command address multiple Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL MariaDB NVidia Apple Microsoft