Configure redisCluster cluster

Posted by shana on Sat, 14 Dec 2019 19:18:14 +0100

1. Install Redis image

docker pull yyyyttttwwww/redis

2. Create net2 network segment

docker network create --subnet=172.19.0.0/16 net2

3 create a node Redis container

#Rename the redis image:

docker tag docker.io/yyyyttttwwww/redis redis

#Delete old redis

docker rmi docker.io/yyyyttttwwww/redis

#Create 6 redis containers

docker run -it -d --name r1 -p 5001:6379 --net=net2 --ip 172.19.0.2 redis bash
docker run -it -d --name r2 -p 5002:6379 --net=net2 --ip 172.19.0.3 redis bash
docker run -it -d --name r3 -p 5003:6379 --net=net2 --ip 172.19.0.4 redis bash
docker run -it -d --name r4 -p 5004:6379 --net=net2 --ip 172.19.0.5 redis bash
docker run -it -d --name r5 -p 5005:6379 --net=net2 --ip 172.19.0.6 redis bash

4. Modify the configuration file of each redis container to enable the cluster function. The following is how to modify the r1 container. Other containers are modified in the same way. Although this method is not very fast, and there is a lot of repetitive labor. If the number of redis is hundreds, it will be very inconvenient. Here, let's first focus on meeting the goal and ignore the efficiency problem

docker exec -it r1 sh

vi /usr/redis/redis.conf

Note that you also need to modify bind. Bind corresponds to the specified ip when you docker run. The ip of each redis cluster is different. For example, if one of my redis ip is 172.19.0.2, then I need to modify bind to 172.19.0.2, as follows:

cd /usr/redis/src

./redis-server ../redis.conf
#Enter r1 node
docker exec -it r1 bash
cp /home/redis/redis.conf /usr/redis/redis.conf
cd /usr/redis/src
./redis-server ../redis.conf
#Enter r2 node
docker exec -it r2 bash
cp /home/redis/redis.conf /usr/redis/redis.conf
cd /usr/redis/src
./redis-server ../redis.conf
#Enter r3 node
docker exec -it r3 bash
cp /home/redis/redis.conf /usr/redis/redis.conf
cd /usr/redis/src
./redis-server ../redis.conf
#Enter r4 node
docker exec -it r4 bash
cp /home/redis/redis.conf /usr/redis/redis.conf
cd /usr/redis/src
./redis-server ../redis.conf
#Enter r5 node
docker exec -it r5 bash
cp /home/redis/redis.conf /usr/redis/redis.conf
cd /usr/redis/src
./redis-server ../redis.conf
#Enter r6 node
docker exec -it r6 bash
cp /home/redis/redis.conf /usr/redis/redis.conf
cd /usr/redis/src
./redis-server ../redis.conf

5. Use redis-trib.rb

docker exec -it r1 sh

cd /usr/redis

mkdir cluster

cp /usr/redis/src/redis-trib.rb  /usr/redis/cluster

cd /usr/redis/cluster

#Create ruby environment

gem #Test whether there is ruby Environment, my redis By default, the image has ruby Environment, if any, direct access#Create Cluster cluster

apk add ruby

apk add rubygems 

gem install redis

#Inspection test
ps -ef| grep redis 
root         65      0  0 06:54 ?        00:00:00 ./redis-server 172.19.0.2:6379 [cluster]
#Display 172.19.0.2:6379 instead of 127.0.0.1:6379 for success

#Create Cluster cluster
cd /usr/redis/cluster

./redis-trib.rb create --replicas 1 172.19.0.2:6379 172.19.0.3:6379 172.19.0.4:6379 172.19.0.5:6379 172.19.0.6:6379 172.19.0.7:6379

 

Topics: Big Data Redis Docker Ruby network