Moment For Technology

Docker builds Redis Cluster each step with operation diagram, command!!

Posted on Dec. 3, 2022, 9:47 a.m. by Patricia Jones
Category: The back-end Tag: The back-end

This is the fifth day of my participation in the August More text Challenge. For details, see: August More Text Challenge

In the past, whenever I learned anything, I always felt that as long as I could write a small Demo, I would be done. But as I learn more and more about clusters, JVMS, data structures, algorithms, underlying systems, and Liunx systems, I still have a long way to go.

Share a favorite saying: "eight hours for life, eight hours for development".

If you like it, please join us ??

A, environmental

  • Alibaba Cloud server CentOS 8
  • Docker version is 20.10.7
  • Redis image (pull to default latest image)

General steps: ?

  1. Download the Redis image
  2. Write a Redis configuration file
  3. Start the Redis container
  4. Create a Redis Cluster.
  5. Conduct actual tests

Two, early preparation

2.1. Search and pull the Redis image

docker search redis
docker pull redis
Copy the code

2.2 Docker container network

  1. Create a virtual nic ?

    Create a virtual network adapter for the redis-cluster to communicate with the outside world. The bridge mode is commonly used.

    docker network create myredis 
    Copy the code
  2. View the Docker nic information

    docker network ls
    Copy the code

3, view dockerr network details

docker network inspect myredis
Copy the code

4. Supplement (delete nic information and help commands)

docker network rm myredis Delete nic commands are separated by multiple Spaces
docker network --help Display can take parameters, etc
Copy the code

2.3. Write configuration files

A few commands from shLLE programming are used here to make things easier. ?

for port in $(seq 6379 6384); 
mkdir -p /home/redis/node-${port}/conf
touch /home/redis/node-${port}/conf/redis.conf
cat   EOF  /home/redis/node-${port}/conf/redis.conf port ${port} requirepass 1234 bind protected-mode no daemonize no  appendonly yes cluster-enabled yes cluster-config-file nodes.conf cluster-node-timeout 5000 cluster-announce-ip cluster-announce-port ${port} cluster-announce-bus-port 1${port} EOF
Copy the code

Command description: ?

  • port: Node port;
  • requirepass: Sets the password for authentication
  • protected-mode: Protection mode. The default value is yes, indicating that the protection mode is enabled. This parameter is required after the protected mode is enabledbind ipOr set an access password; When the protected mode is disabled, the external network can be directly accessed.
  • daemonize: whether to start the daemon thread (background), default no;
  • appendonly: whether to enable the AOF persistence mode. The default is no.
  • cluster-enabled: Whether to enable the cluster mode. The default value is no.
  • cluster-config-file: Cluster node information file;
  • cluster-node-timeout: Indicates the timeout period for cluster node connection.
  • cluster-announce-ip: IP address of a cluster node
    • Note:If you want your Redis cluster to be accessible to the Internet, simply enter the server IP address here
    • If you are only accessing inside the server for security, you need to make some changes here.
  • cluster-announce-port: Cluster node mapping port;
  • cluster-announce-bus-port: Cluster node bus port.

The redis website explains why two ports need to be mapped: redis website

Command execution is complete:

# yum install -y tree # yum install -y tree # yum install -y tree

The next step is to start the container pull

Start the container

Start the Redis container

Because to start six containers, one by one, would be a problem. Again, the power of shell programming.

for port in $(seq 6379 6384); \
do \
   docker run -it -d -p ${port}:${port} -p 1${port}: 1.${port} \
  --privileged=true -v /home/redis/node-${port}/conf/redis.conf:/usr/local/etc/redis/redis.conf \
  --privileged=true -v /home/redis/node-${port}/data:/data \
  --restart always --name redis-${port} --net myredis \
  --sysctl net.core.somaxconn=1024 redis redis-server /usr/local/etc/redis/redis.conf
Copy the code

Explanation: ?

  • - it: interaction

  • -D: runs in the background and prints the container after the container is started

  • --privileged: Whether to allow the Docker application container to get the host root permission.

  • -p: indicates port mapping

  • -v: mounts files

  • --sysctl parameter to set system parameters that can be used to adjust system performance

  • --restart always: Always restart the container when the container exits

  • --name: Name the container

  • --net Myredis: Use the virtual network card we created (check out Docker networking for more details)

Execution completed:

You can also use Docker ps-a to view containers in action.

You can see that everything has been started successfully.

? ?

3.2 Create a Redis Cluster

You can select a node to create a Redis cluster.

1. Enter the Redis-6379 container

docker exec -it redis-6379 /bin/bash
Copy the code

2. Create a cluster

IP address in the cluster create configuration file :6379 IP address :6380 IP address :6381 IP address :6382 IP address :6383 IP address :6384 --cluster-replicas 1Copy the code

This screen indicates that the pull has been successful.

3. View node information

After entering the container, run the redis-cli -c -a 1234 command to enter redis. ?

Enter cluster info to view cluster information

You can also enter cluster nodes to view information about all nodes

?? The next step is to enter the test phase pull. ?

Four, test,

4.1. Local test

We set it in 6381 and can take it out in 6379, which means we have successfully pulled ?

4.2 External Network test

Go to the Windows directory where you downloaded redis and go to CMD.

Tests indicate that we are ready to connect pull.

So let's open two CMD Windows, using different ports to enter.

At this point, we can prove that our cluster is already set up successfully pull.

4.3 Commands that may be used when an error occurs

Batch stop containers

 for port in $(seq 6379 6384); 
 docker stop redis-${port}
Copy the code

Deleting Containers in Batches

 for port in $(seq 6379 6384); 
 docker rm redis-${port}
Copy the code

4.4 possible mistakes

Waiting for the cluster to join......

Note: If the server is aliyun or Tencent Cloud, remember to open the security group rule, 6379-6384 and 16379-16384 to open.

If it is virtual machine, may involve the firewall, this should pay attention to.

Five, the blogger himself

? ?

If there is a mistake, welcome everyone not to add to grant instruction!!

If there is any doubt or execution error, please comment or private message, will be the first time to reply.

Together we ☺, or ah, we together ? ☺.

About (Moment For Technology) is a global community with thousands techies from across the global hang out!Passionate technologists, be it gadget freaks, tech enthusiasts, coders, technopreneurs, or CIOs, you would find them all here.