行僧

参与开源,努力提升。 我的GitHub地址:https://github.com/playingjoker

docker实现Redis集群 有更新!

基于docker搭建Redis集群

默认大家会使用docker-compose和docker相关知识

集群方式为1个master,2个slave,3个sentinel
本地搭建为了图方便,在1台机器上做了主从,另一个机器上做了sentinel
实际应用过程中,防止灾难发生,尽量不要在同一台机器上部署

启动命令

  1. docker更新镜像
        docker-compose -f redis-node.yml pull
        docker-compose -f redis-sentinel.yml pull
    
  2. 启动node节点
        docker-compose -f redis-node.yml up -d
    
  3. 启动sentinel节点
        docker-compose -f redis-sentinel.yml up -d
    

配置文件修改

请自行修改yml配置文件以及conf目录下的Redis配置文件,替换为想要使用的ip地址和端口即可
在Linux机器上可能需要修改conf文件夹权限,也可以调整docker程序的用户分组和文件属性分组实现。
因为在做故障转移的时候,Redis会自行修改配置文件,比如master节点宕机之后,选举出新的master节点之后,会修改其他slave节点的slaveOf属性,调整为新的节点。
同时,原有master节点启动会,会变成新的slave节点,并增加slaveOf配置跟随新的master节点,配置文件的实时变动可保证容器随时重启不会发生主从配置错乱。

——————-我是华丽分割线————————–

下面附上docker-compose配置文件

redis的docker-compose配置文件

redis-node.yml

version: '3.1'
services:
    redis-master:
        image: redis
        restart: always
        container_name: redis-server1
        ports:
            - "6380:6380"
        volumes:
            - "./conf/node6380.conf:/usr/local/etc/redis/redis.conf"
            - "${HOME}/.redis/6380:/data"
        command: redis-server /usr/local/etc/redis/redis.conf

    redis-slave1:
        image: redis
        restart: always
        container_name: redis-server2
        ports:
            - "6381:6380"
        volumes:
            - "./conf/node6381.conf:/usr/local/etc/redis/redis.conf"
            - "${HOME}/.redis/6381:/data"
        command: redis-server /usr/local/etc/redis/redis.conf

    redis-slave2:
        image: redis
        restart: always
        container_name: redis-server3
        ports:
            - "6382:6380"
        volumes:
            - "./conf/node6382.conf:/usr/local/etc/redis/redis.conf"
            - "${HOME}/.redis/6382:/data"
        command: redis-server /usr/local/etc/redis/redis.conf

redis-sentinel.yml

version: '3.1'
services:
    redis-sentinel1:
        image: redis
        restart: always
        container_name: redis-sentinel1
        ports:
            - "26380:26380"
        volumes:
            - "./conf/sentinel26380.conf:/usr/local/etc/redis/redis.conf"
            - "${HOME}/.redis/26380:/data"
        command: redis-sentinel /usr/local/etc/redis/redis.conf

    redis-sentinel2:
        image: redis
        restart: always
        container_name: redis-sentinel2
        ports:
            - "26381:26380"
        volumes:
            - "./conf/sentinel26381.conf:/usr/local/etc/redis/redis.conf"
            - "${HOME}/.redis/26381:/data"
        command: redis-sentinel /usr/local/etc/redis/redis.conf

    redis-sentinel3:
        image: redis
        restart: always
        container_name: redis-sentinel3
        ports:
            - "26382:26380"
        volumes:
            - "./conf/sentinel26382.conf:/usr/local/etc/redis/redis.conf"
            - "${HOME}/.redis/26382:/data"
        command: redis-sentinel /usr/local/etc/redis/redis.conf

启动命令

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

评论
liuzhaoning 回复

:yum: :yum: :yum:

validate