Docker 部署 redis 高可用(哨兵模式)

目录

一、简介

二、环境准备

三、部署步骤

四、redis可视化界面安装

五、测试


一、简介

哨兵模式是一种特殊的模式,首先Redis提供了哨兵的命令,哨兵是一个独立的进程,作为进程,它会独立运行。其原理是哨兵通过发送命令,等待Redis服务器响应,从而监控运行的多个Redis实例。

本文介绍基于docker和redis-sentinel的高可用redis集群搭建,大多数情况下,redis-sentinel也需要做高可用,这里先对redis搭建一主二从环境,另外需要3个redis-sentinel监控redis master。

官方推荐:一个健康的集群部署,至少需要3个Sentinel实例。

二、环境准备

节点IP 节点名称 系统 安装Docker及版本 端口开放
192.168.51.187 node187 CentOS 7 Docker Engine - Community 19.03.14 7001, 27001
192.168.51.226 node226 CentOS 7 Docker Engine - Community 19.03.14 7002, 27002
192.168.51.236 node236 CentOS 7 Docker Engine - Community 19.03.14 7003, 27003

说明:在四个节点上分别安装Docker, 具体安装步骤可参考: https://blog.csdn.net/DZP_dream/article/details/104670943

三、部署步骤

1.拉取镜像,创建文件夹(分别在三个节点执行)

拉取镜像
docker pull redis

创建文件夹
mkdir redis

2.配置文件

节点一:

redis7001.conf 资源地址: https://download.csdn.net/download/DZP_dream/13764030?spm=1001.2014.3001.5503

sentinel27001.conf  资源地址: https://download.csdn.net/download/DZP_dream/13764042?spm=1001.2014.3001.5503

节点二:

redis7002.conf  资源地址: https://download.csdn.net/download/DZP_dream/13764033?spm=1001.2014.3001.5503

sentinel27002.conf  https://download.csdn.net/download/DZP_dream/13764049?spm=1001.2014.3001.5503

节点三:

redis7003.conf  资源地址: https://download.csdn.net/download/DZP_dream/13764039?spm=1001.2014.3001.5503

sentinel27003.conf 资源地址:  https://download.csdn.net/download/DZP_dream/13764054?spm=1001.2014.3001.5503

配置文件修改以下内容

Docker 部署 redis 高可用(哨兵模式)_第1张图片

3.redis安装(在三个节点分别执行)

#redis安装
#节点一执行
[root@node187 redis]# sudo docker run -d --restart=always \
-p 7001:7001 \
-v /home/redis/redis1/redis7001.conf:/usr/local/etc/redis/redis.conf \
-v /home/redis/redis1/data:/data \
--name=redis1 redis:latest redis-server /usr/local/etc/redis/redis.conf 

#节点二执行
[root@node226 redis]# sudo docker run -d --restart=always \
-p 7002:7002 \
-v /home/redis/redis2/redis7002.conf:/usr/local/etc/redis/redis.conf \
-v /home/redis/redis2/data:/data \
--name=redis2 redis:latest redis-server /usr/local/etc/redis/redis.conf 

#节点三执行
[root@node236 redis]# sudo docker run -d --restart=always -p 7003:7003 \
-v /home/redis/redis3/redis7003.conf:/usr/local/etc/redis/redis.conf \
-v /home/redis/redis3/data:/data \
--name=redis3 redis:latest redis-server /usr/local/etc/redis/redis.conf 

4.哨兵安装(在三个节点分别执行)

#哨兵
#在节点一执行 
[root@node187 redis]# sudo docker run -d --restart=always \
-p 27001:27001 \
-v /home/redis/redis27001/sentinel27001.conf:/usr/local/etc/redis/sentinel.conf \
-v /home/redis/redis27001/data:/data \
--name=redis27001 redis:latest redis-sentinel /usr/local/etc/redis/sentinel.conf 

#在节点二执行 
[root@node226 redis]# sudo docker run -d --restart=always \
-p 27002:27002 \
-v /home/redis/redis27002/sentinel27002.conf:/usr/local/etc/redis/sentinel.conf \
-v /home/redis/redis27002/data:/data \
--name=redis27002 redis:latest redis-sentinel /usr/local/etc/redis/sentinel.conf 

#在节点三执行 
[root@node236 redis]# sudo docker run -d --restart=always \
-p 27003:27003 \
-v /home/redis/redis27003/sentinel27003.conf:/usr/local/etc/redis/sentinel.conf \
-v /home/redis/redis27003/data:/data \
--name=redis27003 redis:latest redis-sentinel /usr/local/etc/redis/sentinel.conf 

5.查看安装情况

#节点一
[root@node187 redis]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                NAMES
2a70a484a8f7        redis:latest        "docker-entrypoint.s…"   29 seconds ago      Up 23 seconds       6379/tcp, 0.0.0.0:27001->27001/tcp   redis27001
abfb008b2c61        redis:latest        "docker-entrypoint.s…"   5 minutes ago       Up 5 minutes        6379/tcp, 0.0.0.0:7001->7001/tcp     redis1

#节点二
[root@node226 redis]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                NAMES
07201fab4202        redis:latest        "docker-entrypoint.s…"   21 seconds ago      Up 20 seconds       6379/tcp, 0.0.0.0:27002->27002/tcp   redis27002
ad247183f7ce        redis:latest        "docker-entrypoint.s…"   3 minutes ago       Up 3 minutes        6379/tcp, 0.0.0.0:7002->7002/tcp     redis2

#节点三
[root@node236 redis]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                NAMES
23aaca699769        redis:latest        "docker-entrypoint.s…"   15 seconds ago      Up 13 seconds       6379/tcp, 0.0.0.0:27003->27003/tcp   redis27003
0f863fc72756        redis:latest        "docker-entrypoint.s…"   3 minutes ago       Up 3 minutes        6379/tcp, 0.0.0.0:7003->7003/tcp     redis3

6.查看redis状态

[root@node187 redis1]# docker exec -it redis1 bash   
#主容器查询集群状态
root@e0fcaafbb45a:/data# redis-cli -a aAzhjy@123 -p 7001
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:7001> info Replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.51.226,port=7002,state=online,offset=670505,lag=1
slave1:ip=192.168.51.236,port=7003,state=online,offset=670791,lag=1
master_replid:705a47c71c1c661af4e08b1861b800652ec932e8
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:670791
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:670791

7.查看哨兵监控情况

[root@node187 redis1]# docker exec -it redis27001 bash
root@30d656aaceab:/data# redis-cli -p 27001

#查看集群主库情况
127.0.0.1:27001> sentinel master mymaster
 1) "name"
 2) "mymaster"
 3) "ip"
 4) "192.168.51.187"
 5) "port"
 6) "7001"
31) "num-slaves"
32) "2"
33) "num-other-sentinels"
34) "2"

#查看集群从库情况
127.0.0.1:27001> sentinel slaves mymaster
1)  1) "name"
    2) "192.168.51.226:7002"
    3) "ip"
    4) "192.168.51.226"
    5) "port"
    6) "7002"
2)  1) "name"
    2) "192.168.51.236:7003"
    3) "ip"
    4) "192.168.51.236"
    5) "port"
    6) "7003"

8.项目中配置访问

Docker 部署 redis 高可用(哨兵模式)_第2张图片

9.通过客户端访问查看

 

Docker 部署 redis 高可用(哨兵模式)_第3张图片

四、redis可视化界面安装

1.创建数据库redis-manager

在节点二中的mysql数据库中创建名为redis-manager的数据库,用来存储redis集群信息

2.redis-manager安装

sudo docker run -d  --name redis-manager  \
-p 8048:8182 \
-e DATASOURCE_URL='jdbc:mysql://192.168.51.226:3306/redis-manager?characterEncoding=UTF-8&useUnicode=true&serverTimezone=Asia/Shanghai' \
-e DATASOURCE_USERNAME='root' \
-e DATASOURCE_PASSWORD='aAzhjy@123' \
reasonduan/redis-manager

3.查看集群情况

地址: http://192.168.51.226:8048

账号密码: admin/admin

Docker 部署 redis 高可用(哨兵模式)_第4张图片

五、测试

1.停止一台redis服务

 

 

 

 

你可能感兴趣的:(redis,redis,高可用)