Get the Redis configuration file


wget http://download.redis.io/redis-stable/redis.conf
Copy the code

Copy the primary and secondary configuration files


cp redis.conf redis-master.conf 
cp redis.conf redis-slave.conf
Copy the code

Modify the primary library configuration information

#Set any IP to be reachable, comment this line
# bind 127.0.0.1 
#Set totrueTo make redis service run in the background
daemonize yes 
#Set a password
requirepass lindj
Copy the code

Modify slave library configuration information


#Set any IP to be reachable, comment this line
# bind 127.0.0.1
#Set totrueTo make redis service run in the background
daemonize yes
#Set a password
requirepass lindj 
#< masterIP > indicates the IP address of the master library, and < masterPort > indicates the port on which the master library is started
slaveof <masterip> <masterport>
#If the master library has a password, it must be configured. 
      
        indicates the master library access password
      
masterauth lindj
Copy the code

Final file list


Start the Redis cluster


  1. Follow this command to run the primary and secondary redis libraries separately
Sh docker_restart.sh < name > < Port >Copy the code

Docker_restart. sh Contents are as follows

#! /bin/shapplication_name=$1 port=$2 echo "application=${application_name}" echo "port=${port}" echo "container is stoping and removing" containerId=$(docker ps -a | grep -E "${application_name}" | awk '{print $1}') if [ !  -z $containerId ] then docker stop $containerId && docker rm $containerId fi echo "container is running" docker run -it  --name ${application_name} \ -d -p ${port}:${port} redis redis-server --requirepass lindj --port ${port}Copy the code
  1. Enter the container started in the first step
Docker exec it <redis library name > bashCopy the code
  1. Load the configuration
redis-server /usr/local/etc/redis/redis.conf
Copy the code

Repeat steps 2 and 3 until all containers have been executed and the Redis cluster is started.

test


Enter the main library

docker exec -it redis-master bash
#Connect the redisRedis -cli -a lindj -p < port > set name 123Copy the code

Enter from the library

docker exec -it redis-slave1 bash
#Connect the redisRedis -cli -a lindj -p < port > get nameCopy the code

A link to the


Docker + Redis master-slave environment construction


** Three people must have my teacher, welcome to guide learning **