Detailed explanation of the master-slave configuration tutorial of redis under Docker

Detailed explanation of the master-slave configuration tutorial of redis under Docker

1. Pull the redis image

docker pull redis

2. Start three redis container services, using ports 6379, 6380, and 6381 respectively

docker run --name redis-6379 -p 6379:6379 -d redis
docker run --name redis-6380 -p 6380:6379 -d redis
docker run --name redis-6381 -p 6381:6379 -dredis

3. View the container

[tcy@tcy1 ~]$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a9fa77adc598 daocloud.io/library/redis "docker-entrypoint.s 2 hours ago Up 2 hours 0.0.0.0:6381->6379/tcp redis-6381   
6ee2f2f007e6 daocloud.io/library/redis "docker-entrypoint.s 2 hours ago Up 2 hours 0.0.0.0:6380->6379/tcp redis-6380   
ab54741166e1 daocloud.io/library/redis "docker-entrypoint.s 3 hours ago Up 3 hours 0.0.0.0:6379->6379/tcp redis-6379

4. Test container, success

docker exec -it ab54741166e1 redis-cli: Enter the container [root@tcy1 tcy]# docker exec -it ab54741166e1 redis-cli
127.0.0.1:6379> set b tcy
OK
127.0.0.1:6379> get b
"tcy"
127.0.0.1:6379> quit[root@tcy1 tcy]#

5. Start redis cluster configuration

5.1. Check the IP address of the container intranet

[root@tcy1 tcy]# docker inspect a9fa77adc598

insert image description here

The intranet IP addresses of the three redis are:

redis-6379: 172.17.0.1:6379
redis-6380: 172.17.0.2:6379
redis-6381:172.17.0.3:6379

5.2. Enter the docker container and check the current redis role (master or slave)

[root@tcy1 tcy]# docker exec -it ab54741166e1 /bin/bash
root@ab54741166e1:/data# redis-cli
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:0
master_replid:d43d1ae8cde6cb084220e18b926aba79e0bb2504
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
127.0.0.1:6379> quit
root@ab54741166e1:/data# exit 
exit

Currently all three are in master state

5.3. Use the redis-cli command to change the host of redis-6380 and redis-6381 to 172.17.0.1:6379

[root@tcy1 tcy]# docker exec -it a9fa77adc598 /bin/bash //redis-6380
root@a9fa77adc598:/data# redis-cli
127.0.0.1:6379> SLAVEOF 172.17.0.1 6379
OK
127.0.0.1:6379> quit
root@a9fa77adc598:/data# exit
exit
[root@tcy1 tcy]# docker exec -it 6ee2f2f007e6 /bin/bash //redis-6381
root@6ee2f2f007e6:/data# redis-cli
127.0.0.1:6379> SLAVEOF 172.17.0.1 6379
OK
127.0.0.1:6379> quit

5.4. Check whether redis-6379 already has 2 slaves, connected_slaves: 2, yes

[root@tcy1 tcy]# docker exec -it ab54741166e1 /bin/bash
root@ab54741166e1:/data# redis-cli
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=172.17.0.3,port=6379,state=online,offset=378,lag=1
slave1:ip=172.17.0.2,port=6379,state=online,offset=378,lag=0
master_replid:ce193b15cfd57f7dc3ccfbf2a4aef6156b131e6d
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:378
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:378
127.0.0.1:6379> quit
root@ab54741166e1:/data# exit
exit

5.5. Configure Sentinel

Enter the three redis containers for configuration and create the sentinel.conf file in the container root directory

The file content is: sentinel monitor mymaster 172.17.0.1 6379 1

[root@tcy1 tcy]# docker exec -it a9fa77adc598 /bin/bash
root@a9fa77adc598:/data# cd / && touch sentinel.conf 
root@a9fa77adc598:/# vim /sentinel.conf

If: bash: vim: command not found

Solution: 1. apt-get update 2. apt-get install vim

Finally, start the Redis Sentinel:

root@a9fa77adc598:/# redis-sentinel /sentinel.conf
342:X 24 Jun 11:37:58.934 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
342:X 24 Jun 11:37:58.957 # Redis version=4.0.10, bits=64, commit=00000000, modified=0, pid=342, just started
342:X 24 Jun 11:37:58.958 # Configuration loaded
342:X 24 Jun 11:37:58.959 # You requested maxclients of 10000 requiring at least 10032 max file descriptors.
342:X 24 Jun 11:37:58.959 # Server can't set maximum open files to 10032 because of OS error: Operation not permitted.
342:X 24 Jun 11:37:58.960 # Current maximum open files is 4096. maxclients has been reduced to 4064 to compensate for low ulimit. If you need higher maxclients increase 'ulimit -n'.
        _._                         
      _.-``__ ''-._                       
   _.-`` `. `_. ''-._ Redis 4.0.10 (00000000/0) 64 bit
 .-`` .-```. ```\/ _.,_ ''-._                  
 ( ' , .-` | `, ) Running in sentinel mode
 |`-._`-...-` __...-.``-._|'` _.-'| Port: 26379
 | `-._ `._ / _.-' | PID: 342
 `-._ `-._ `-./ _.-' _.-'                  
 |`-._`-._ `-.__.-' _.-'_.-'|                 
 | `-._`-._ _.-'_.-' | http://redis.io    
 `-._ `-._`-.__.-'_.-' _.-'                  
 |`-._`-._ `-.__.-' _.-'_.-'|                 
 | `-._`-._ _.-'_.-' |                 
 `-._ `-._`-.__.-'_.-' _.-'                  
   `-._ `-.__.-' _.-'                    
     `-._ _.-'                      
       `-.__.-'                        
 
342:X 24 Jun 11:37:59.068 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
342:X 24 Jun 11:37:59.089 # Sentinel ID is dfd5a5bfe1036b1df3395c4ba858329034fc5b7e
342:X 24 Jun 11:37:59.091 # +monitor master mymaster 172.17.0.1 6379 quorum 1
342:X 24 Jun 11:37:59.110 * +slave slave 172.17.0.3:6379 172.17.0.3 6379 @ mymaster 172.17.0.1 6379
342:X 24 Jun 11:37:59.115 * +slave slave 172.17.0.2:6379 172.17.0.2 6379 @ mymaster 172.17.0.1 6379
 
342:X 24 Jun 11:39:27.601 * +sentinel sentinel ba9b0d0539d8273edfcbd922fe138f50daa78bbb 172.17.0.2 26379 @ mymaster 172.17.0.1 6379
342:X 24 Jun 11:41:59.144 * +sentinel sentinel f0510f8582b72c056531f219397ed8826683e665 172.17.0.1 26379 @ mymaster 172.17.0.1 6379

Open multiple windows for easy observation.

insert image description here

Sentinel sentinel configuration completed

5.6 Testing

Close Master

[tcy@tcy1 ~]$ docker stop ab54741166e1
ab54741166e1

At this time, the remaining two slaves will automatically elect a new master, and here 172.17.0.2 is elected as the master.

insert image description here

Check 172.17.0.2, it has become the host.

[root@tcy1 /]# docker exec -it 6ee2f2f007e6 /bin/bash
root@6ee2f2f007e6:/data# redis-cli
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=172.17.0.3,port=6379,state=online,offset=66906,lag=1
master_replid:5a7489c8181ddf0d73d418d30d6a4c8e039198ba
master_replid2:ce193b15cfd57f7dc3ccfbf2a4aef6156b131e6d
master_repl_offset:67041
second_repl_offset:65534
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:85
repl_backlog_histlen:66957
127.0.0.1:6379>

Summarize

The above is the master-slave configuration of redis under Docker introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to you in time!

You may also be interested in:
  • Implementation of Redis master-slave cluster based on Docker
  • Detailed explanation of Redis master-slave replication practice using Docker
  • How to use docker to build redis master-slave
  • Use Docker to build a Redis master-slave replication cluster
  • CentOS 6 uses Docker to deploy redis master-slave database operation example
  • Example practice of building Redis master-slave + sentinel based on Docker

<<:  How to use explain to query SQL execution plan in MySql

>>:  How to use vue3 to build a material library

Recommend

Code to enable IE8 in IE7 compatibility mode

The most popular tag is IE8 Browser vendors are sc...

Example code for css flex layout with automatic line wrapping

To create a flex container, simply add a display:...

MySQL backup table operation based on Java

The core is mysqldump and Runtime The operation i...

How to prevent duplicate submission in jquery project

In new projects, axios can prevent duplicate subm...

Node.js+express message board function implementation example

Table of contents Message Board Required librarie...

JavaScript Timer Details

Table of contents 1. Brief Introduction 2. setInt...

Explanation of building graph database neo4j in Linux environment

Neo4j (one of the Nosql) is a high-performance gr...

A brief discussion on the understanding of TypeScript index signatures

Table of contents 1. What is an index signature? ...

MySQL slow query: Enable slow query

1. What is the use of slow query? It can record a...

...

Object-Oriented Programming with XHTML and CSS

<br />If only XHTML and CSS were object-orie...

25 Examples of News-Style Website Design

bmi Voyager Pitchfork Ulster Grocer Chow True/Sla...

Docker installation of MySQL (8 and 5.7)

This article will introduce how to use Docker to ...