Redis for the master-slave configuration in Docker

Billion, environment preparation

  1. Two bottles Docker computer (or two virtual machines)

    I have here is two Centos7 systems running in VMware

    We CentOS-64-0 as the host (Master), CentOS-64-1 as a slave (Slave)

    

 

 

  2. Install redis

    Redis installed in the docker (only the redis pull down, not further configuration)

    

 

 

 

First, using an external configuration file redis the docker

  1. Profiles redis.conf

    Redis first to write a good profile on both your host, namely redis.conf

    You can download the configuration file templates from the official website Centos7 redis system, use the following command to download to a local:

    wget http://download.redis.io/redis-stable/redis.conf -O <filename>

    

 

  2. Configuration Parameters

    Host (Master) modified downloaded redis.conf, mainly modify the parameters as follows:

#bind 127.0.0.1 # If the bind option is empty, then allow all available from the network interface connection
protected-mode no # protection mode, if yes, then only allow local client connections
After appendonly yes # open, Redis data will be written each time after receiving written appendonly.aof all files, data Redis will put that file is read into memory each time you start in

 

    Slave (Slave) modified downloaded redis.conff, mainly modify the parameters as follows:

#bind 127.0.0.1
protected-mode no
appendonly yes

# replicaof <master ip> <master port>
replicaof 192.168.22.130 6379 #Redis host (Master) IP port

 

Second, create redis container docker in, and to start an external file

  1. Start from the same master machine:

# docker redis 以配置文件运行:
#docker run -p <容器端口>:<主机端口> --name <容器名> -v <本地配置文件映射容器配置文件> -v <本地文件夹挂载到容器文件夹> -d(表示以守护进程方式启动容器) <启动redis服务并制定配置文件(容器中的路径)>
docker run -p 6379:6379 --name myredis -v /usr/local/docker/redis.conf:/usr/local/redis.conf -v /usr/local/docker/data:/usr/local/data -d redis redis-server /usr/local/redis.conf

 

  2.进入redis

  确保docker以及redis容器都启动之后,使用docker exec -it <redis容器名> redis-cli命令进入redis

  

 

   3.测试

    在主机存入一个key:

      

 

    在从机获取:

      

 

 

 

 

 

三、注意

  1.replicaof 和 slaveof

    在redis5.x的主从配置中,从机配置要配置 replicaof 参数。而早期版本,要配置的是slaveof参数。

 

  2.已有redis容器

    得先删除该容器,待配置好外部redis.conf后,重新创建。

 

  3.daemonize参数

    在docker中,redis.conf文件中的daemonize参数要设置为no(默认是no)。

    daemonize=yes的意思为后台运行redis,但这会导致容器跑不起来。

 

  4.protected-mode

    设置为yes时,只允许本地服务端连接,导致不同机子(或虚拟机)的主从机无法连接。

 

Guess you like

Origin www.cnblogs.com/Drajun/p/12339788.html
Recommended