Docker 部署单机 Redis + exporter

资源清单

主机 IP
redis 10.0.0.1
软件 版本
docker 20.10.12
docker-compose 1.23.1
redis 5.0.8

一、Docker 安装

1. 使用国内 yum

# yum install -y yum-utils device-mapper-persistent-data lvm2
# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

2. 卸载旧版本的 docker

## 如果主机上已经有docker存在且不是想要安装的版本,需要先进行卸载。
# yum remove -y docker \
              docker-client \
              docker-client-latest \
              docker-common \
              docker-latest \
              docker-latest-logrotate \
              docker-logrotate \
              docker-selinux \
              docker-engine-selinux \
              docker-engine \
              container*

3. 安装 Docker20.10 版本

# yum -y install docker-ce-20.10.12-3.el7 docker-ce-cli-20.10.12-3.el7

4. 设置镜像加速

# mkdir /etc/docker
# vi /etc/docker/daemon.json

{
  "registry-mirrors": ["https://xxxxxxxxx.mirror.aliyuncs.com"]
}

5. 启动 docker

# systemctl start docker
# systemctl enable docker
# systemctl status docker

二、Docker-compose 安装

1. Docker-compose 安装

## github.com 可能访问超时,可以使用下面的获取下载下来后上传服务器即可
# curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

# curl -k "https://dl.cactifans.com/zabbix_docker/docker-compose" -o /usr/bin/docker-compose

# chmod a+x /usr/bin/docker-compose

2. 查看 docker-compose 版本

# docker-compose version

三、部署服务

1. docker-compose.yaml 资源清单

单主机模式部署 3主0从 必须使用 host模式
使用非主机模式的情况下代码层面连接到 redis-cluster 集群,
获取到的集群地址信息为 docker 容器内部 IP
若业务代码没有部署在 redis 主机则无法访问 redis-cluster 集群

version: "3"

services:
  master1:
    image: redis:5.0.8
    container_name: redis_master_1
    network_mode: host
    volumes:
      - /etc/localtime:/etc/localtime
      - /data/redis/master1/redis.conf:/etc/redis.conf
      - /data/redis/master1/data:/data
      - /data/redis/master1/log:/log
    command: ["redis-server", "/etc/redis.conf"]
    restart: always

  master2:
    image: redis:5.0.8
    container_name: redis_master_2
    network_mode: host
    volumes:
      - /etc/localtime:/etc/localtime
      - /data/redis/master2/redis.conf:/etc/redis.conf
      - /data/redis/master2/data:/data
      - /data/redis/master2/log:/log
    command: ["redis-server", "/etc/redis.conf"]
    restart: always

  master3:
    image: redis:5.0.8
    container_name: redis_master_3
    network_mode: host
    volumes:
      - /etc/localtime:/etc/localtime
      - /data/redis/master3/redis.conf:/etc/redis.conf
      - /data/redis/master3/data:/data
      - /data/redis/master3/log:/log
    command: ["redis-server", "/etc/redis.conf"]
    restart: always

  redis_exporter:
    image: oliver006/redis_exporter:v1.45.0
    container_name: redis_exporter
    depends_on:
      - master1
      - master2
      - master3
    ports:
      - 9121:9121
    volumes:
      - /etc/localtime:/etc/localtime
    command: ["-redis.addr", "redis://10.0.0.1:6379", "redis://10.0.0.1:6380", "redis://10.0.0.1:6381"]
    restart: always

2. 创建目录结构

先启动服务后,自动生成目录结构,在上传配置文件

# mkdir /data/redis/master{1,2,3}/{data,log} -pv

3. /etc/redis.conf 配置文件信息

# cat /data/redis/master1/redis.conf  # [ master2 | master3 ]

port 6379   # [ master2: 6380 | master3: 6381 ]
protected-mode no
bind 0.0.0.0
repl-diskless-sync no
repl-disable-tcp-nodelay no
cluster-enabled yes
appendonly yes
logfile "/log/redis.log"

4. 部署服务

# docker-compose up -d

# docker-compose ps -a
posted @ 2023-04-04 17:02  evescn  阅读(516)  评论(0编辑  收藏  举报