掘金 后端 ( ) • 2024-04-05 11:18

theme: z-blue

引言

redis自然是不必多说是一款高性能的非关系型数据库,本文的宗旨是借助docker来实现快速的搭建redis集群,以便于更好的学习redis。

官网下载redis.conf配置文件

下载配置文件的意义是借助docker的挂载,可以帮助我们在宿主机更好的修改配置。

配置文件下载链接

image.png 选择合适版本的配置文件即可!

单机版redis搭建

编写docker-compose文件

version: '3.2.0'
services:
  redis01:
    image: redis:6.2.10  
    container_name: redis01
    ports:
      - "6379:6379"
    volumes:
      - "/C/wh-workspace/work_demo/src/main/resources/redis01/conf/redis.conf:/usr/local/etc/redis.conf"
    command: redis-server /usr/local/etc/redis.conf  #安装配置文件启动redis
    restart: no
    networks:
      net-redis:
        ipv4_address: 172.19.0.2

networks:
  net-redis:
    name: net-redis
    driver: bridge
    ipam:
      config:
        - subnet: 172.19.0.0/16

客户端验证是否能正常访问

这里推荐一款官方推荐使用的客户端连接工具 RedisInsight

image.png

主从redis搭建

创建docker-compose文件

# 主从版 Redis 容器化部署
version: '3.2.0'
services:
  redis01:
    image: redis:6.2.10
    container_name: redis01
    ports:
      - "6379:6379"
    volumes:
      - "/C/wh-workspace/work_demo/src/main/resources/redis/redis01/conf/redis.conf:/usr/local/etc/redis.conf"
    command: redis-server /usr/local/etc/redis.conf
    restart: no
    networks:
      net-redis:
        ipv4_address: 172.19.0.2
  redis02:
    image: redis:6.2.10
    container_name: redis02
    ports:
      - "6380:6379"
    volumes:
      - "/C/wh-workspace/work_demo/src/main/resources/redis/redis02/conf/redis.conf:/usr/local/etc/redis.conf"
    command: redis-server /usr/local/etc/redis.conf
    restart: no
    networks:
      net-redis:
        ipv4_address: 172.19.0.3
  redis03:
    image: redis:6.2.10
    container_name: redis03
    ports:
      - "6381:6379"
    volumes:
      - "/C/wh-workspace/work_demo/src/main/resources/redis/redis03/conf/redis.conf:/usr/local/etc/redis.conf"
    command: redis-server /usr/local/etc/redis.conf
    restart: no
    networks:
      net-redis:
        ipv4_address: 172.19.0.4

networks:
  net-redis:
    name: net-redis
    driver: bridge
    ipam:
      config:
        - subnet: 172.19.0.0/16

修改配置文件

主服务器配置

  1. 绑定地址修改

在主服务器的redis.conf中将bind参数放开,修改成bind 0.0.0.0以便从服务器可以连接到主服务器。如果你希望主服务器只能被本机访问,可以设置为bind 127.0.0.1

bind 0.0.0.0
  1. 设置主服务器密码
requirepass redis01
  1. 配置持久化
save 3600 1
save 300 100
save 60 10000

从服务器配置

  1. 绑定地址修改

在主服务器的redis.conf中将bind参数放开,修改成bind 0.0.0.0以便从服务器可以连接到主服务器。如果你希望主服务器只能被本机访问,可以设置为bind 127.0.0.1

bind 0.0.0.0
  1. 指定主服务器地址
replicaof 172.19.0.2 6379 #有些版本这里是slaveof
  1. 设置主服务器的密码
masterauth redis01
  1. 配置持久化
save 3600 1
save 300 100
save 60 10000

运行docker创建redis容器

docker-compose -f docker-redis-compose.yml up -d

客户端验证

image.png

出从模式的缺点

主从模式,主节点负责写操作,从节点负责读操作,在一定程度上提高了redis的性能;但是相对的如果主节点挂了,就只能通过重启去进行数据的恢复,同时还需要重新的配置主从关系,这样相对就比较麻烦。

哨兵模式redis搭建

哨兵模式架构图

image.png

哨兵本质上也是由单个的redis组成的,为保证可用性最好哨兵也配置成多个

编写docker-compose文件

# 哨兵模式 Redis 容器化部署
version: '3.2.0'
services:
  redis01:
    image: redis:6.2.10
    container_name: redis01
    ports:
      - "6379:6379"
    volumes:
      - "/C/wh-workspace/work_demo/src/main/resources/redis/redis01/conf/redis.conf:/usr/local/etc/redis.conf"
    command: redis-server /usr/local/etc/redis.conf
    restart: no
    networks:
      net-redis:
        ipv4_address: 172.19.0.2
  redis02:
    image: redis:6.2.10
    container_name: redis02
    ports:
      - "6380:6379"
    volumes:
      - "/C/wh-workspace/work_demo/src/main/resources/redis/redis02/conf/redis.conf:/usr/local/etc/redis.conf"
    command: redis-server /usr/local/etc/redis.conf
    restart: no
    networks:
      net-redis:
        ipv4_address: 172.19.0.3
  redis03:
    image: redis:6.2.10
    container_name: redis03
    ports:
      - "6381:6379"
    volumes:
      - "/C/wh-workspace/work_demo/src/main/resources/redis/redis03/conf/redis.conf:/usr/local/etc/redis.conf"
    command: redis-server /usr/local/etc/redis.conf
    restart: no
    networks:
      net-redis:
        ipv4_address: 172.19.0.4

  sentinel01:
    image: redis:6.2.10
    container_name: sentinel01
    ports:
      - "16379:16379"
    volumes:
      - "/C/wh-workspace/work_demo/src/main/resources/redis/sentinel01/conf:/usr/local/etc/sentinel01/conf"
    command: ["sh","-c","mkdir -p /usr/local/etc/sentinel01/conf && chmod -R 777 /usr/local/etc/sentinel01/conf && redis-sentinel /usr/local/etc/sentinel01/conf/sentinel.conf --sentinel"]
    restart: no
    depends_on:
      - redis01
      - redis02
      - redis03
    networks:
      net-redis:
        ipv4_address: 172.19.0.5
  sentinel02:
    image: redis:6.2.10
    container_name: sentinel02
    ports:
      - "16380:16380"
    volumes:
      - "/C/wh-workspace/work_demo/src/main/resources/redis/sentinel02/conf:/usr/local/etc/sentinel02/conf"
    command: [ "sh","-c","mkdir -p /usr/local/etc/sentinel02/conf && chmod -R 777 /usr/local/etc/sentinel02/conf && redis-sentinel /usr/local/etc/sentinel02/conf/sentinel.conf --sentinel" ]
    restart: no
    depends_on:
      - redis01
      - redis02
      - redis03
    networks:
      net-redis:
        ipv4_address: 172.19.0.6

networks:
  net-redis:
    name: net-redis
    driver: bridge
    ipam:
      config:
        - subnet: 172.19.0.0/16

配置sentinel.conf文件

# 哨兵节点默认开启的端口
port 16379
# 哨兵节点保证和其他服务通信
bind 0.0.0.0
# 设置监听的服务器名 IP 端口 2:表示两个哨兵节点都任务主服务下线才是真的下线
sentinel monitor redis01 172.19.0.2 6379 1
# 主节点密码
sentinel auth-pass redis01 redis01
# 哨兵保存日志
logfile "/var/log/sentinel01.log"
# 多少毫秒内没有应答,则判定为主节点下线
sentinel down-after-milliseconds redis01 5000

运行docker创建redis容器

docker-compose -f docker-redis-compose.yml up -d

查看哨兵状态

进入容器,进入redis-cli后查看哨兵信息

image.png

模拟先收到停调redis主节点,看看哨兵是否选举新的节点成为主节点.

image.png

主节已经发生了改变

哨兵模式的缺点

哨兵模式虽然实现了故障转移,从新选举出主节点保证系统的高可用,本质是还是基于中心化的集群配置,无法对海量数据进行分片存储实现数据的分布式管理,如果要实现数据的分片储存需要用到cluster集群来实现(后续要写一篇关于cluster集群的搭建),对于业务量不是很大的系统哨兵和主从模式的集群已经够用了。如果你的系统redis存储的数据量很大,建议使用cluster集群进行数据分片存储,同时每个集群新增哨兵模式保证部分节点宕机,能实现故障转移。