(一)redis docker 主从配置

环境相关

docker 安装参考

参考: Docker 安装

docker-lnmp 集成环境

参考:Docker-lnmp

Redis 主从配置

指定自定义网络

1
2
3
4
5
[root@VM_0_6_centos redis]# pwd
/data/web/redis

[root@VM_0_6_centos redis]# docker network create --subnet=172.10.0.0/16 mynetwork
1e74de7cbd62306d0f1785e83ef5426cafb021cccd6e1bdfaa316cd7017911d9

创建 Dockerfile

1
2
3
4
FROM centos:latest
RUN groupadd -r redis && useradd -r -g redis redis
RUN yum -y update && yum -y install epel-release && yum -y install redis && yum -y install net-tools
EXPOSE 6379

在 Dockerfile 目录 执行下面的代码,注意后面上下文点号

docker build -t redis .

使用此docker 镜像 创建容器

代码如下:

1
2
3
docker run -itd --name redis-master --net mynetwork -p 6380:6379 --ip 172.10.0.2 redis
docker run -itd --name redis-slave --net mynetwork -p 6381:6379 --ip 172.10.0.3 redis
docker run -itd --name redis-slave2 --net mynetwork -p 6382:6379 --ip 172.10.0.4 redis

参数说明:
参考: 菜鸟教程文档

查看网络

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
[root@VM_0_6_centos redis]# docker network inspect mynetwork
[
{
"Name": "mynetwork",
"Id": "1e74de7cbd62306d0f1785e83ef5426cafb021cccd6e1bdfaa316cd7017911d9",
"Created": "2019-09-22T14:01:10.470838603+08:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "172.10.0.0/16"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"3391a5251e6324274a6f431b0346fabb9010cbcc0fe156c988ffb6bd20148a72": {
"Name": "redis-master",
"EndpointID": "6e6505d09e0b2a58d80a8b3e306f506dd2da8a7c77f345a051d05cce4618a6a4",
"MacAddress": "02:42:ac:0a:00:02",
"IPv4Address": "172.10.0.2/16",
"IPv6Address": ""
},
"ff01a57b5de5d54a387d5945fe8b7d5d12b2cf3476b6f93ed6dc4a0f82fa0aef": {
"Name": "redis-slave",
"EndpointID": "5a5d3ea726d9b7b9fc7555138e209ad3ec04fa758713c210c1cd8269806f2b05",
"MacAddress": "02:42:ac:0a:00:03",
"IPv4Address": "172.10.0.3/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
}
]

进入容器

1
2
[root@VM_0_6_centos redis]# docker exec -it ff01a57b5de5 /bin/bash
[root@ff01a57b5de5 /]#

主从复制

主从复制说明

为了解决单一节点 机器故障、容量瓶颈等问题,会把数据复制多个副本部署到其它节点上进行冗余备份。实现Redis的高可用。

主从复制->哨兵->集群

配置

redis服务器启动后,直接通过客户端执行命令:

1
2
3
slaveof <masterip> <masterport> 则该redis实例成为从节点

slaveof no one 断开连接

修改redis保护模式 配置从节点依赖的主节点IP

主节点调整

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
[root@VM_0_6_centos redis]# docker exec -it redis-master /bin/bash
[root@ff01a57b5de5 /]# vi /etc/redis.conf
...
# JUST COMMENT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#bind 127.0.0.1
bind 0.0.0.0
# Protected mode is a layer of security protection, in order to avoid that
# Redis instances left open on the internet are accessed and exploited.
#
# When protected mode is on and if:
#
# 1) The server is not binding explicitly to a set of addresses using the
# "bind" directive.
# 2) No password is configured.
#
# The server only accepts connections from clients connecting from the
# IPv4 and IPv6 loopback addresses 127.0.0.1 and ::1, and from Unix domain
# sockets.
#
# By default protected mode is enabled. You should disable it only if
# you are sure you want clients from other hosts to connect to Redis
# even if no authentication is configured, nor a specific set of interfaces
# are explicitly listed using the "bind" directive.
#protected-mode yes
protected-mode no

...

从节点调整

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
[root@VM_0_6_centos redis]# docker exec -it redis-slave /bin/bash
[root@ff01a57b5de5 /]# vi /etc/redis.conf
...
# JUST COMMENT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#bind 127.0.0.1
bind 0.0.0.0
# Protected mode is a layer of security protection, in order to avoid that
# Redis instances left open on the internet are accessed and exploited.
#
# When protected mode is on and if:
#
# 1) The server is not binding explicitly to a set of addresses using the
# "bind" directive.
# 2) No password is configured.
#
# The server only accepts connections from clients connecting from the
# IPv4 and IPv6 loopback addresses 127.0.0.1 and ::1, and from Unix domain
# sockets.
#
# By default protected mode is enabled. You should disable it only if
# you are sure you want clients from other hosts to connect to Redis
# even if no authentication is configured, nor a specific set of interfaces
# are explicitly listed using the "bind" directive.
#protected-mode yes
protected-mode no

...

# replicaof <masterip> <masterport>
replicaof 172.10.0.2 6379
# If the master is password protected (using the "requirepass" configuration
# directive below) it is possible to tell the replica to authenticate before
# starting the replication synchronization process, otherwise the master will
# refuse the replica request.
#
# masterauth <master-password>

...

数据测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
[root@VM_0_6_centos redis]# docker exec -it redis-master /bin/bash
[root@3391a5251e63 /]# vi /etc/redis.conf
[root@3391a5251e63 /]# redis-server /etc/redis.conf &
[1] 28
[root@3391a5251e63 /]# redis-cli
127.0.0.1:6379> set name uiste
OK
127.0.0.1:6379> get name
"uiste"
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=172.10.0.3,port=6379,state=online,offset=5545,lag=0
master_replid:73aa73a6ca2674e214b2081989f8f5cd642b1774
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:5545
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:5545


[root@VM_0_6_centos ~]# docker exec -it redis-slave /bin/bash
[root@ff01a57b5de5 /]# redis-server /etc/redis.conf &
[1] 73
[root@ff01a57b5de5 /]# redis-cli
127.0.0.1:6379> get name
"uiste"
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:172.10.0.2
master_port:6379
master_link_status:up
master_last_io_seconds_ago:3
master_sync_in_progress:0
slave_repl_offset:5503
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:73aa73a6ca2674e214b2081989f8f5cd642b1774
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:5503
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:5503

多个从节点的时候,避免主节点同步压力的增加,可以将其它从节点依赖于从节点同步