Summary record / Zhu Jiqian
Recently, I bought a 4-core 16 Tencent cloud lightweight application server, which cost me nearly 400 oceans. I plan to build a pile of docker component clusters. First, I plan to build a redis cluster through docker. I plan to use three ports, 700170027003.
Tencent ECS has firewall restrictions, so these six ports need to be opened in the firewall of the control panel——
Note: why do you need to open their corresponding bus ports 170011700217003 after opening 700170027003? Because this is the port of their cluster heartbeat connection. If it is not opened, the cluster allocation slot will fail when creating a cluster for each startup container node.
After completing this step, you can start pulling the redis image through docker. Here, you can pull redis: version 5.0.5——
docker pull redis:5.0.5
Create redis template Conf template, which will automatically generate the corresponding redis.conf for each node Conf file——
mkdir /app/redis && cd /app/redis/src && mkdir redis-cluster && cd ./redis-cluster && touch redis-template.conf
Next, modify redis template For the contents of the conf template file, directly use vi redis template Conf instruction——
Enter the following——
#Node port port ${PORT} #Set as cluster node cluster-enabled yes #The Internet connection must be set to no protected-mode no #Cluster node file cluster-config-file nodes.conf cluster-node-timeout 5000 #Change it to your Internet ip. Suppose that my Tencent ECS Internet ip is 14.253.73 xx cluster-announce-ip 14.253.73.xx #Internet port cluster-announce-port ${PORT} #Bus port cluster-announce-bus-port 1${PORT} appendonly yes
A redis net network dedicated to the redis cluster is created in docker for use by the cluster
docker network create redis-net
Enter redis template Conf template file directory——
cd /app/redis/redis-cluster/
Copy the following instructions directly from the command line, and then press enter to execute——
for port in `seq 7001 7003`; do mkdir -p ./${port}/conf && PORT=${port} envsubst < ./redis-template.conf > ./${port}/conf/redis.conf && mkdir -p ./${port}/data; done
After execution, check the command ll and find that 7001 ~ 7003 directories are automatically generated——
Next, copy the following instructions directly——
for port in `seq 7001 7003`; do docker run -d -ti -p ${port}:${port} -p 1${port}:1${port} -v /app/redis/redis-cluster/${port}/conf/redis.conf:/usr/local/etc/redis/redis.conf -v /app/redis/redis-cluster/${port}/data:/data --restart always --name redis-${port} --net redis-network --sysctl net.core.somaxconn=1024 redis redis-server /usr/local/etc/redis/redis.conf; done
Press enter to start the docker container instance automatically——
If the normal startup is successful, view it through the docker ps -a command, and the following information is displayed——
At this time, we only need to enter the redis of one of the docker containers and form a cluster of nodes. Here, we enter the redis-7001 container——
docker exec -it redis-7001 /bin/bash
Then execute the following instructions——
redis-cli --cluster create Extranet ip:7001 Extranet ip:7002 Extranet ip:7003 --cluster-replicas 0
Note: if it is changed to three master and three slave, the cluster instruction here is——
redis-cli --cluster create Extranet ip:7001 Extranet ip:7002 Extranet ip:7003 Extranet ip:7004 Extranet ip:7005 Extranet ip:7006 --cluster-replicas 1
When the following requests appear, you can directly fill in yes——
Can I set the above configuration? (type 'yes' to accept): yes
After the last successful startup, the following message will appear——
At this time, we can test that we can connect to the cluster node through the instruction redis cli - C - H extranet IP - P 7001. After connecting normally, we can check the cluster status through the instruction cluster info. The display of ok indicates that the cluster is normal. At this time, we can also view the specific slot allocation information of each node through cluster nodes——
At this step, you can connect to the redis cluster deployed by docker on Tencent ECS through springboot+redis. I test and verify that it is feasible——
Reference article: https://blog.csdn.net/weixin_44015043/article/details/105868513