环境相关
docker 安装参考
参考: Docker 安装
docker-lnmp 集成环境
参考:Docker-lnmp
Redis 主从配置
指定自定义网络
1 | [root@VM_0_6_centos redis]# pwd |
创建 Dockerfile
1 | FROM centos:latest |
在 Dockerfile 目录 执行下面的代码,注意后面上下文点号
docker build -t redis .
使用此docker 镜像 创建容器
代码如下:1
2
3docker 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 | [root@VM_0_6_centos redis]# docker network inspect mynetwork |
进入容器
1 | [root@VM_0_6_centos redis]# docker exec -it ff01a57b5de5 /bin/bash |
主从复制
主从复制说明
为了解决单一节点 机器故障、容量瓶颈等问题,会把数据复制多个副本部署到其它节点上进行冗余备份。实现Redis的高可用。
主从复制->哨兵->集群
配置
redis服务器启动后,直接通过客户端执行命令:1
2
3slaveof <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 | [root@VM_0_6_centos redis]# docker exec -it redis-master /bin/bash |
多个从节点的时候,避免主节点同步压力的增加,可以将其它从节点依赖于从节点同步