Docker builds primary/secondary redis replication
Install Redis
Docker search Redis 2. Pull image Docker pull RedisCopy the code
Master/slave replication
1. Run docker image, first use Docker to start three Redis container services, using ports 6379, 6380 and 6381 respectively
docker run --name redis-6379 -p 6379:6379 -d redis redis-server
docker run --name redis-6380 -p 6380:6379 -d redis redis-server
docker run --name redis-6381 -p 6381:6379 -d redis redis-server
Copy the code
2. Configure the Redis cluster
Docker inspect containerid (containerid), IPAddress is the IP address of the container Intranet. "SandboxKey": "/var/run/docker/netns/6db03f3155e4", "SecondaryIPAddresses": null, "SecondaryIPv6Addresses": null, "EndpointID": "a9d1954bd522aa5fe0432e62c0454ca49fc06a907c9ecb8d6ecd105a43d7d103", "Gateway": "172.17.0.1", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "IPAddress": "172.17.0.5", "IPPrefixLen": 16, "IPv6Gateway": "", "MacAddress": "02:42:ac:11:00:05", "Networks": { "bridge": { "IPAMConfig": null, "Links": null, "Aliases": null, "NetworkID": "42c4ad50844ace06fb7d67efa4c332911981ac1432ab7783acd5ad2745446d2a", "EndpointID": "A9d1954bd522aa5fe0432e62c0454ca49fc06a907c9ecb8d6ecd105a43d7d103", "Gateway" : "172.17.0.1", "IPAddress" : "172.17.0.5", "IPPrefixLen": 16, "IPv6Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "MacAddress": "02:42:ac:11:00:05", "DriverOpts": null } }Copy the code
3. I docker three redis Intranet IP
Redis-6381 172.17.0.5 redIS-6380 172.17.0.4 redIS-6379 172.17.0.3Copy the code
4. Inside the docker container (docker exec it <container_id> redis-cli), check whether the current redis role is master or slave (info replication).
# Replication role:master connected_slaves:0 master_replid:590672be7b831c0b5e7c353a7b6228fbd5e1ec92 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:0Copy the code
As you can see, the three consoles are all master characters; Run the redis-cli command to change the redis-6380 and redis-6381 hosts to Redis-6379
SLAVEOF 172.17.0.3 6379
Copy the code
Check the info of reids-6379 host again. There are already two slaves.
Replication role:master Connected_SLAVES :2 Slave0: IP =172.17.0.4,port=6379,state=online,offset=112,lag=1 = 172.17.0.5 slave1: IP and port = 6379, state = online, offset = 112, lag = 1 master_replid: 91695 f20fd0dadb32635b922da4315c79d626b7e master_replid2:0000000000000000000000000000000000000000 master_repl_offset:112 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:112Copy the code
5. Test the master-slave effect
The master node:
127.0.0.1:6379> set ph redick
OK
127.0.0.1:6379> get ph
"redick"
127.0.0.1:6379>
Copy the code
From the node 1:
127.0.0.1:6379 > get ph "redick"Copy the code
From the node 2:
127.0.0.1:6379 > get ph "redick"Copy the code
Redis-cli = redis-cli = redis-cli = redis-cli = redis-cli = redis-cli = redis-cli
From 1: 127.0.0.1:6379> set K KKK (error) read only You can't write against a read only replica. From 2:127.0.0.1:6379> set k kk (error) read only You can't write against a read only replica.Copy the code
So far, Redis has completed setting up master and slave through Docker
Redis Sentinel (Sentinel) configuration
Sentinel background
The previous introduction of using Docker to build the master/slave environment of Redis, but this is only to add the backup of the slave library (master/slave replication) to the data, when the master library down, the slave library will not automatically upgrade the master library, that is to say, the master/slave cluster of Redis is not highly available. Currently, there are two schemes for highly available (master/slave replication, master/slave switchover) Redis cluster. One is Redis-Sentinel, with only one master and consistent data of each instance. One is a Redis-cluster, also known as a distributed Redis cluster. There can be multiple masters on which data is sharded. This paper introduces the highly available REDis cluster construction based on Docker and Redis-Sentinel. In most cases, Redis-Sentinel also needs to do high availability. Here, one master and two slave environment is set up for Redis first. Three additional Redis-Sentinels are required to monitor the Redis master. Obviously, it is not reliable to monitor a RedIS cluster using a single Redis-Sentinel process, since redis-Sentinel itself has single-point-of-failure-problem, When problems occur, the entire Redis cluster system will not be able to switch master/slave as expected. Official recommendation: A healthy cluster deployment requires at least 3 Sentinel instances. In addition, redis-Sentinel only needs to be configured to monitor the Redis master, and clusters can communicate with each other through the master.Copy the code
redis-sentinel
As an independent service, RedIS-Sentinel is used to manage multiple Redis instances. The system mainly performs the following three tasks:Copy the code
- Monitor: Checks whether the master and slave instances of Redis are working properly
- Notification: Sends Notification alarms through the API when the monitored Redis service is faulty
- Automatic Failover: When detecting that the redis master database is not working properly, Redis-Sentinel will start automatic fault diagnosis and migration operations, such as removing the failed Redis master service, upgrading one of the slave servers to the new master server, and changing the other slave servers of the failed master server to replicate the new master server. When a client tries to connect to a failed primary server, the cluster also returns the address of the latest primary server to the client, allowing the cluster to use the new primary server to replace the failed server
Conf configuration file
Sentinel. conf is the core configuration file for starting redis-sentinel, which can be downloaded from the official website. The redis installation package is also available by default.Copy the code
Configuration sentinel. Conf
# myMaster: custom cluster name, if you need to monitor multiple Redis clusters, <master-name> <master-redis-ip>: master library IP <master-redis-port>: master library port <quorum>: minimum number of votes. Since there are three redis-Sentinel instances, 2 sentinel monitor mymaster <master-redis-ip> <master-redis-port> <quorum> Sentinel auth-pass mymaster redisPassword # add daemonize yesCopy the code
Make three copies of the sentinel.conf configuration file, sentinel1.conf, sentinel2.conf and sentinel3.conf, and edit the ports of the three configuration files to be 26379,26380,26381, respectively.
Three sentinels were activated separately
redis-sentinel sentinel_1.conf
redis-sentinel sentinel_2.conf
redis-sentinel sentinel_3.conf
Copy the code
Using the Redis client, connect to the Redis-Sentinel API to view the monitoring status
Redis - cli - p 26379 (26380 | 26381) sentinel master mymaster or sentinel slaves mymaster 1) "the name" 2) "mymaster" 3) "IP" 4) "192.168.0.104" 5) "port" 6) "6379" 7) "runid" 8) "3 f6078b50fb2b63a2b05d26f60370285ca5c21c7" 9) "flags" 1) "master" 2) "link-pending-commands" 3) "0" 4) "link-refcount" 5) "1" 6) "last-ping-sent" 7) "0" 8) "last-ok-ping-reply" 9) "178" 10) "last-ping-reply" 11) "178" 12) "down-after-milliseconds" 13) "5000" 14) "info-refresh" 15) "3936" 16) "role-reported" 17) "master" 18) "role-reported-time" 19) "144400" 20) "config-epoch" 21) "0" 22) "num-slaves" 23) "2" 24) "num-other-sentinels" 25) "2" 26) "quorum" 27) "2" 28) "failover-timeout" 29) "900000" 30) "parallel-syncs" 31) "2"Copy the code
test
Docker exec -it redis-master bash redis-cli -h <host> -p 6300 DEBUG sleep 60Copy the code
Go to the redis-slave or Redis-slave2 container and check Info Replication. You can see that the master has completed the switchover.
Redis-sentinel Outputs the switchover information of the primary database. After 60 seconds, the original redis primary database is restored. However, the current REDis service cannot recover the identity of the original primary database.
The Reids Sentinel configuration is complete
Configure the Redis Cluster based on the Docker installation
Node planning
SanZhu re-understandingCopy the code
Container name | Container IP | Port mapping | The operation mode |
---|---|---|---|
redis-master-1 | 172.50.0.2 | 6391 -> 6391 16391 -> 16391 | master |
redis-master-2 | 172.50.0.3 | 6392 -> 6392 16392 -> 16392 | master |
redis-master-3 | 172.50.0.4 | 6393 -> 6393 16393 -> 16393 | master |
redis-slave-1 | 172.30.0.2 | 6394 -> 6394 16391 -> 16394 | slave |
redis-slave-2 | 172.30.0.3 | 6395 -> 6395 16391 -> 16395 | slave |
redis-slave-3 | 172.30.0.4 | 6396 -> 6396 16391 -> 16396 | slave |
Write docker-comemage. yml and redis.conf
- docker-compose.yml
Version: "3.6" services: redis-master-1: image: redis:5.0 # Container_name: redis-master-1 # containing_dir: /config /nodes-6391.conf -port =6391 /config /nodes-6391.conf # Redis cluster monitoring port stdin_open: true # Standard input open networks: # docker network configuration redis-master: ipv4_address: 172.50.0.2 tty: true privileged: true # Docker network configuration redis-master: ipv4_address: 172.50.0.2 tty: true privileged ["/Users/penghuiliu/docker-file/docker-redis/docker-redis-cluster-master/config:/config"] Sh redis-master-2: image: redis:5.0 working_dir: /config container_name: Redis-master-2 environment: -port =6392 Networks: redis-master: ipv4_address: 172.50.0.3 ports: - "6392:6392" - "16392:16392" stdin_open: true tty: true privileged: true volumes: ["/Users/penghuiliu/docker-file/docker-redis/docker-redis-cluster-master/config:/config"] entrypoint: - /bin/bash-redis. sh redis-master-3: image: redis:5.0 container_name: redis-master-3 working_dir: /config environment: -port =6393 networks: redis-master: ipv4_address: 172.50.0.4 ports: - "6393:6393" - "16393:16393" stdin_open: true tty: true privileged: true volumes: ["/Users/penghuiliu/docker-file/docker-redis/docker-redis-cluster-master/config:/config"] entrypoint: - /bin/bash-redis. sh redis-slave-1: image: redis:5.0 container_name: redis-slave-1 working_dir: /config environment: -port =6394 networks: redis-slave: ipv4_address: 172.30.0.2 Ports: - "6394:6394" - "16394:16394" stdin_open: true tty: true privileged: true volumes: ["/Users/penghuiliu/docker-file/docker-redis/docker-redis-cluster-master/config:/config"] entrypoint: - /bin/bash-salve-2: image: redis:5.0 working_dir: /config container_name: redis-salve-2 environment: -port =6395 ports: - "6395:6395" - "16395:16395" stdin_open: true networks: redis-slave: ipv4_address: 172.30.0.3 tty: true privileged: true volumes: ["/Users/penghuiliu/docker-file/docker-redis/docker-redis-cluster-master/config:/config"] entrypoint: - /bin/bash-redis. sh redis-salve-3: image: redis:5.0 container_name: redis-slave-3 working_dir: /config environment: - PORT=6396 ports: - "6396:6396" - "16396:16396" stdin_open: true networks: redis-slave: ipv4_address: 172.30.0.4 tty: true privileged: true volumes: ["/Users/penghuiliu/docker-file/docker-redis/docker-redis-cluster-master/config:/config"] entrypoint: - /bin/bash - redis.sh networks: redis-master: driver: bridge ipam: driver: default config: - subnet: 172.50.0.0/16 redis-slave: driver: bridge ipam: driver: default config: -subnet: 172.30.0.0/16Copy the code
Start the Redis cluster
Docker-compose up -d docker-compose ps/docker-compose PSCopy the code
Initializing a Cluster
1.宿主机IP:ifconfig,我们己IP:192.168.3.78
2.查找redis-master-1的容器id:2d4596b188a8
liupenghui:~ penghuiliu$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
58e97782e2fe redis:5.0 "/bin/bash redis.sh" 2 minutes ago Up 2 minutes 0.0.0.0:6393->6393/tcp, 6379/tcp, 0.0.0.0:16393->16393/tcp redis-master-3
f9bd7e330f82 redis:5.0 "/bin/bash redis.sh" 2 minutes ago Up 2 minutes 0.0.0.0:6392->6392/tcp, 6379/tcp, 0.0.0.0:16392->16392/tcp redis-master-2
2d4596b188a8 redis:5.0 "/bin/bash redis.sh" 2 minutes ago Up 2 minutes 0.0.0.0:6391->6391/tcp, 6379/tcp, 0.0.0.0:16391->16391/tcp redis-master-1
7b42859712fd redis:5.0 "/bin/bash redis.sh" 2 minutes ago Up 2 minutes 0.0.0.0:6395->6395/tcp, 6379/tcp, 0.0.0.0:16395->16395/tcp redis-salve-2
3becb97b314c redis:5.0 "/bin/bash redis.sh" 2 minutes ago Up 2 minutes 0.0.0.0:6396->6396/tcp, 6379/tcp, 0.0.0.0:16396->16396/tcp redis-slave-3
ae2ee02885e4 redis:5.0 "/bin/bash redis.sh" 2 minutes ago Up 2 minutes 0.0.0.0:6394->6394/tcp, 6379/tcp, 0.0.0.0:16394->16394/tcp redis-slave-1
af951c611b57 ea93faa92337 "/docker-entrypoint.…" 3 weeks ago Up 7 days 2888/tcp, 3888/tcp, 0.0.0.0:2181->2181/tcp, 8080/tcp some-zookeeper
fc9441156e5b mysql:5.7 "docker-entrypoint.s…" 4 weeks ago Up 7 days 33060/tcp, 0.0.0.0:3316->3306/tcp mysql-master
c13e1e2cf151 mysql:5.7 "docker-entrypoint.s…" 4 weeks ago Up 7 days 33060/tcp, 0.0.0.0:3326->3306/tcp mysql-slave
3.进入master-1容器:
liupenghui:~ penghuiliu$ docker exec -it 2d4596b188a8 /bin/bash
4.创建三主三从的redis集群:
root@2d4596b188a8:/config# redis-cli --cluster create 192.168.3.78:6391 192.168.3.78:6392 192.168.3.78:6393 192.168.3.78:6394 192.168.3.78:6395 192.168.3.78:6396 --cluster-replicas 1
中途输入 “yes”确认初始化
root@2d4596b188a8:/config# redis-cli --cluster create 192.168.3.78:6391 192.168.3.78:6392 192.168.3.78:6393 192.168.3.78:6394 192.168.3.78:6395 192.168.3.78:6396 --cluster-replicas 1
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.3.78:6395 to 192.168.3.78:6391
Adding replica 192.168.3.78:6396 to 192.168.3.78:6392
Adding replica 192.168.3.78:6394 to 192.168.3.78:6393
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: c66af09ca2a87bed9523dc449e3cc09a5071f10f 192.168.3.78:6391
slots:[0-5460] (5461 slots) master
M: 893b57a4360df40ce3bf40dbb16f53d1ad85b900 192.168.3.78:6392
slots:[5461-10922] (5462 slots) master
M: d0e40d450becd47a3a412973b3acedfd51740796 192.168.3.78:6393
slots:[10923-16383] (5461 slots) master
S: e7758429d2668d38f11ba22da75e88fa2f52ddb3 192.168.3.78:6394
replicates 893b57a4360df40ce3bf40dbb16f53d1ad85b900
S: b13e68cf772bf5591cb1a9ada8036f27010a89f2 192.168.3.78:6395
replicates d0e40d450becd47a3a412973b3acedfd51740796
S: b823fdd7f14baae537ec04ea6245da3024292497 192.168.3.78:6396
replicates c66af09ca2a87bed9523dc449e3cc09a5071f10f
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
.
>>> Performing Cluster Check (using node 192.168.3.78:6391)
M: c66af09ca2a87bed9523dc449e3cc09a5071f10f 192.168.3.78:6391
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: e7758429d2668d38f11ba22da75e88fa2f52ddb3 172.50.0.1:6394
slots: (0 slots) slave
replicates 893b57a4360df40ce3bf40dbb16f53d1ad85b900
M: 893b57a4360df40ce3bf40dbb16f53d1ad85b900 172.50.0.1:6392
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: b13e68cf772bf5591cb1a9ada8036f27010a89f2 172.50.0.1:6395
slots: (0 slots) slave
replicates d0e40d450becd47a3a412973b3acedfd51740796
S: b823fdd7f14baae537ec04ea6245da3024292497 172.50.0.1:6396
slots: (0 slots) slave
replicates c66af09ca2a87bed9523dc449e3cc09a5071f10f
M: d0e40d450becd47a3a412973b3acedfd51740796 172.50.0.1:6393
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
5.查看redis初始化结果:
root@2d4596b188a8:/config# redis-cli -c -h 192.168.3.78 -p 6391
192.168.3.78:6391> cluster nodes
e7758429d2668d38f11ba22da75e88fa2f52ddb3 172.50.0.1:6394@16394 slave 893b57a4360df40ce3bf40dbb16f53d1ad85b900 0 1610008020447 4 connected
c66af09ca2a87bed9523dc449e3cc09a5071f10f 172.50.0.2:6391@16391 myself,master - 0 1610008019000 1 connected 0-5460
893b57a4360df40ce3bf40dbb16f53d1ad85b900 172.50.0.1:6392@16392 master - 0 1610008019000 2 connected 5461-10922
b13e68cf772bf5591cb1a9ada8036f27010a89f2 172.50.0.1:6395@16395 slave d0e40d450becd47a3a412973b3acedfd51740796 0 1610008021459 5 connected
b823fdd7f14baae537ec04ea6245da3024292497 172.50.0.1:6396@16396 slave c66af09ca2a87bed9523dc449e3cc09a5071f10f 0 1610008018414 6 connected
d0e40d450becd47a3a412973b3acedfd51740796 172.50.0.1:6393@16393 master - 0 1610008019430 3 connected 10923-16383
6.测试集群
192.168.3.78:6391> set test testvalue
-> Redirected to slot [6918] located at 172.50.0.1:6392
OK
test哈希槽计算后,被分配到了6392 服务上,所以会转到6392服务器上,在6392获取test的值
172.50.0.1:6393> get test
-> Redirected to slot [6918] located at 172.50.0.1:6392
"testvalue"
Copy the code