redis cluster configuration of docker in M1 mac

Keywords: Docker Redis

redis cluster configuration of docker in M1 mac

preface

In order to prevent readers from misunderstanding, this article is a personal practical note on setting up a redis cluster on a mac. The author is a mac system. Although many operations are similar to Linux, there are differences and many holes. This tutorial can also be used as a reference for setting up a redis cluster on a Linux docker. Finally, if you have any questions, welcome to discuss.

Tip: this tutorial is applicable to linux and mac systems, but it should be noted that the / usr/local directory in the mac system is actually closed by the mac and cannot be used for configuration. Although configuration files can be forcibly built through sudo, inexplicable situations will occur. This article will also list personal stepping points, hoping to help students who also use the mac system avoid the pit.

1, Prepare docker

A clever woman can't make bricks without rice, so she has to install a docker on the mac first.

1. Install docker

I currently use the mac as the main computer, so all demonstrations are completed on the mac. Of course, the download only provides the download address of the mac. First, I need to go to this website to download, https://docs.docker.com/deskt....

  1. Just drag the mac installation directly. I don't know why the docker container can't be started after the installation is completed, but it suddenly works after clicking uninstall. At present, after version iteration, docker can be used normally in m1.
  2. After installation, start the software. You can first run the ddocker info command of docker or select the image recommended by the software. The command is as follows: docker run - D - P 80:80 docker / getting started
  3. After running, you can directly access localhost. At this time, you will enter a quick start page of docker.

2. Update the image

Since the foreign docker is really slow, you need to switch to the domestic image warehouse first, and finally use the image address of Netease: http://hub-mirror.c.163.com . In the taskbar, click the Docker for mac application icon - > preferences... - > daemon - > registry mirrors. Fill in the accelerator address in the list. After modification, click apply & restart, and Docker will restart and apply the configured image address.

The new version of docker only needs to change the relevant json configuration of docker engine. For example, the following is the result after replacement, which is also a stepping point. Most of the tutorials on the Internet are old versions of docker. I haven't found it for a long time = - =:

Finally, in order to verify whether the configuration is effective, we can use docker info to view the corresponding configuration address at the bottom of the print information, indicating that the configuration is effective:

 Insecure Registries:
  127.0.0.0/8
 Registry Mirrors:
  https://docker.mirrors.ustc.edu.cn/
  https://hub-mirror.c.163.com/
 Live Restore Enabled: false

2, redis stand-alone deployment of docker

Single machine deployment is very simple. You only need the following commands:

#Pull the latest redis image by default
docker pull redis
#Start a redis service on the default 6379 port
docker run --name test-redis -p 6379:6379 -d redis
#Enter the inside of the container
docker exec -it test-redis /bin/bash
# Connect to redis
redis-cli
#After entering, you can ping the installation routine
ping

The stand-alone operation is still very fast. However, it should be noted that the configuration used for stand-alone operation is the default configuration, and there is no configuration file in the docker startup redis image. If you want to use the custom configuration file for startup like installing redis, you need to make the following changes.

Customize redis configuration startup

  1. If it is a custom configuration, first create a directory for mapping in any directory location and https://github.com/redis/redi... Just pull a template configuration file.
  2. Finally, configure through the following command contents
#-d container id returned by background operation
#-p port mapping
#-v data volume mapping
#Execute the command at the end of [COMMAMD]
docker run -d -p 6380:6380 -v /usr/docker/redis/myRedis/redis.conf:/etc/redis/redis.conf -v /usr/docker/redis/myRedis/data:/data --name myRedis redis redis-server /etc/redis/redis.conf

/usr/docker/redis/myRedis/redis.conf: it needs to be changed to the address of the configuration file you need to map. Similarly, the data file needs to be modified accordingly.

3, redis cluster deployment in docker (key)

First, you need to ensure that the above contents have been processed. Then you can install redis. The installation process is as follows:

1. Retrieve the redis image

docker search redis. After the display, I found a common official redis image. Just take it directly.

2. Pull the image

docker pull redis also pulls the redis image locally. After pulling, execute docker images. The execution results are as follows:

zxd@zxddeMacBook-Pro ~ % docker images
REPOSITORY               TAG       IMAGE ID       CREATED        SIZE
redis                    latest    c3d77c9fc5b8   2 weeks ago    107MB
docker/getting-started   latest    613921574f76   4 months ago   26.7MB

3. Build redis customization

Execution: docker network create redis net. After executing the command, the following return results appear, which creates the communication end of the redis cluster

After creation, the following content appears:

a42040f20cb54027b75a68f3d000a7bb02f417e2f202297658bfc1a2c88041d7

4. Build cluster configuration file template file

Build the configuration file of the basic cluster. If you are a linux system, you can execute: CD / usr / local / SRC & & MKDIR redis cluster & & CD. / redis cluster & & touch redis cluster.conf. However, if you are a mac system, it is not recommended to do so. It is more recommended to build your own configuration under / User / username / redis/xxx. The following is the template of redis-cluster.conf file.

# ${PORT} does not need to be replaced with a placeholder. Note:
port ${PORT}
cluster-enabled yes
protected-mode no
cluster-config-file nodes.conf
cluster-node-timeout 5000
#External IP, the IP here should be changed to your server IP. [Note: 127.0.0.1 cannot be used]
cluster-announce-ip 192.168.0.12
cluster-announce-port ${PORT}
cluster-announce-bus-port 1${PORT}
appendonly yes

5. Generate a specific cluster configuration file

If it is a linux system, you can use the command CD / usr / local / SRC / redis cluster to enter the corresponding directory of build configuration. Of course, here is the directory corresponding to mac. Then we generate conf and data directories and generate configuration information:

seq 6000 6005: you can specify the traversal port yourself. Here are just six redis instances, that is, the default three master and three slave mode.

for port in `seq 6000 6005`; do 
  mkdir -p ./${port}/conf && PORT=${port} envsubst < ./redis-cluster.conf > ./${port}/conf/redis.conf && mkdir -p ./${port}/data;
done

The effects after generation are as follows. You can see that configuration files and data directories from 6000-6005 are generated, which is the basic configuration for building clusters:

If you open one of the configuration files, its contents are as follows. Here you can compare the previous template configurations to understand what has been changed:

port 6000
cluster-enabled yes
protected-mode no
cluster-config-file nodes.conf
cluster-node-timeout 5000
#External ip
cluster-announce-ip 192.168.0.12
cluster-announce-port 6000
cluster-announce-bus-port 16000
appendonly yes

6. Write and run the cluster running script

Then, in the current directory, we can create a script file start.sh and execute vim start.sh. After the script is written, use the shell command to execute as follows: sh start.sh run the script.

Let's focus on the following. As mentioned earlier, the / usr/local/src directory of the mac system is not accessible. Although sudo can be used to forcibly create or modify files, it is not a matter of permissions. The mac closes this folder and does not recommend operations in this folder, so the following commands are only applicable to the Linux system.

for port in `seq 6000 6005`; do 
  docker run -d -ti -p ${port}:${port} -p 1${port}:1${port} -v /usr/local/src/redis-cluster/${port}/conf/redis.conf:/usr/local/etc/redis/redis.conf -v /usr/local/src/redis-cluster/${port}/data:/data  --restart always --name redis-${port} --net redis-net --sysctl net.core.somaxconn=1024 redis redis-server /usr/local/etc/redis/redis.conf; 
done

To solve this problem, I made the following adjustments in the mac system (the folder contains some personal information and has been processed):

for port in `seq 6000 6005`; do 
  docker run -d -ti -p ${port}:${port} -p 1${port}:1${port} -v /Users/zxd/com/docker-redis/${port}/conf/redis.conf:/usr/local/etc/redis/redis.conf -v /Users/zxd/com/docker-redis/${port}/data:/data  --restart always --name redis-${port} --net redis-net --sysctl net.core.somaxconn=1024 redis redis-server /usr/local/etc/redis/redis.conf; 
done

The following is an explanation of some parameters:

#-d container id returned by background operation
#-p port mapping. Note that there are two - PS in the above. The first is the startup port and the latter is the communication port of the bus
#-v data volume mapping
#--Net redis net custom network (redis net)
#--sysctl net.core.somaxconn=1024 is a kernel parameter in Linux, indicating the upper limit of the backlog of socket listen. Its main function is to limit the size of the listening queue for receiving new TCP connections. The default 128 is too small for a high load web services environment that often handles new connections. In most environments, this value is recommended to be increased to 1024 or more
#Redis redis server / usr / local / etc / redis / redis.conf use the configuration file to start a redis service command. Note that redis.conf here will actually be mapped to conf in different folders

7. Build redis cluster

The cluster is built as follows to verify whether it can be used. Of course, / usr/local/bin here uses the corresponding storage location of configuration files.

Note that you need to change the ip to the ip configured in the previous template, otherwise it cannot be started

cd /usr/local/bin && redis-cli --cluster create 192.168.0.12:6000 192.168.0.12:6001 192.168.0.12:6002 192.168.0.12:6003 192.168.0.12:6004 ip:6005 --cluster-replicas 1

Then we run docker ps and the following results appear. In addition, there may be an error message because the port is not open, but because I built it locally, this problem does not occur:

docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED        STATUS        PORTS                                                        NAMES
deb9f97f6f0a   redis     "docker-entrypoint.s..."   33 hours ago   Up 30 hours   0.0.0.0:6005->6005/tcp, 0.0.0.0:16005->16005/tcp, 6379/tcp   redis-6005
b226de3d1fa1   redis     "docker-entrypoint.s..."   33 hours ago   Up 30 hours   0.0.0.0:6004->6004/tcp, 0.0.0.0:16004->16004/tcp, 6379/tcp   redis-6004
c805e9704a1a   redis     "docker-entrypoint.s..."   33 hours ago   Up 30 hours   0.0.0.0:6003->6003/tcp, 0.0.0.0:16003->16003/tcp, 6379/tcp   redis-6003
fbae17f03f77   redis     "docker-entrypoint.s..."   33 hours ago   Up 30 hours   0.0.0.0:6002->6002/tcp, 0.0.0.0:16002->16002/tcp, 6379/tcp   redis-6002
93e668ff8f2b   redis     "docker-entrypoint.s..."   33 hours ago   Up 30 hours   0.0.0.0:6001->6001/tcp, 0.0.0.0:16001->16001/tcp, 6379/tcp   redis-6001
9bdb2de54709   redis     "docker-entrypoint.s..."   33 hours ago   Up 30 hours   0.0.0.0:6000->6000/tcp, 0.0.0.0:16000->16000/tcp, 6379/tcp   redis-6000

8. Verify that the cluster is available

Finally, we verify whether the cluster is successfully available by connecting to a node:

First, enter any Redis container (CONTAINER ID is CONTAINER ID)

docker exec -it deb9f97f6f0a(Change to your container ID) /bin/bash

Then enter redis cli (ip is your server ip)

redis-cli -h 192.168.0.12 -p 6000

View node messages

cluster nodes

View cluster information

cluster info

9. Verify cluster distribution

Finally, actually input a few simple commands to verify whether it can be segmented and distributed to multiple machines for processing

Cluster mode connection: the following example shows that the operation is normal (first exit the current redis cli, and then execute the following code. The ip is your server ip).

redis-cli -c -h ip -p 6000 set test 1
redis-cli -c -h ip -p 6000 get test

Other supplements

Turn off the system firewall

systemctl restart firewalld

Restart docker

systemctl restart docker

Write at the end

There are many contents, and the probability of success at one time is not very high. If you have any questions, please comment.

Posted by amandas on Thu, 04 Nov 2021 10:07:26 -0700