docker compose 下 Redis 主备配置

1、准备两台虚拟机或者物理机

       node1 IP:192.168.123.78

       node2 IP:192.168.123.82

2、安装docker和docker compose

3、安装node1,配置docker-compose.yml

version: '3'
 services:
     redis-rs1:
          container_name: 'redis_node1'
          image: 'redis:5.0.3'
          restart: always
          volumes:
            - ./redis/data:/data
            - ./redis/conf/redis.conf:/etc/redis/redis.conf
          ports:
            - 6379:6379
          sysctls:
            - net.core.somaxconn=1024
          command: redis-server /etc/redis/redis.conf --port 6379 --requirepass 123456 --appendonly yes
     sentinel:
          container_name: 'sentinel'
          image: 'redis:5.0.3'
          restart: always
          volumes:
            - ./sentinel/conf/sentinel.conf:/etc/redis/sentinel.conf
          ports:
            - 26379:26379
          command: redis-sentinel /etc/redis/sentinel.conf

 获取配置文件 

    wget https://raw.githubusercontent.com/redis/redis/5.0/redis.conf

 修改node1下的redis.conf

    注释掉 bind
    添加或修改 masterauth 123456

4、安装node2,配置docker-compose.yml

version: '3'
 services:
     redis-rs2:
          container_name: 'redis_node2'
          image: 'redis:5.0.3'
          restart: always
          volumes:
            - ./redis/data:/data
            - ./redis/conf/redis.conf:/etc/redis/redis.conf
          ports:
            - 6379:6379
          sysctls:
            - net.core.somaxconn=1024
          command: redis-server /etc/redis/redis.conf --port 6379 --requirepass 123456 --appendonly yes --slaveof 192.168.3.53 6379 --masterauth 123456
     sentinel:
          container_name: 'sentinel'
          image: 'redis:5.0.3'
          restart: always
          volumes:
            - ./sentinel/conf/sentinel.conf:/etc/redis/sentinel.conf
          ports:
            - 26379:26379
          command: redis-sentinel /etc/redis/sentinel.conf

 获取配置文件 

    wget https://raw.githubusercontent.com/redis/redis/5.0/redis.conf

 修改node1下的redis.conf

    注释掉 bind
    添加或修改 masterauth 123456

5、配置node1下的sentinel.conf

port 26379
#daemonize yes
#pidfile /var/run/redis-sentinel.pid
logfile "sentinel.log"
sentinel myid 02ba5344dfb96dc699bcefc2e1d105190c0e85e3
sentinel deny-scripts-reconfig yes
sentinel monitor master001 192.168.123.82 6379 1
sentinel down-after-milliseconds master001 10000
sentinel auth-pass master001 123456

sentinel config-epoch master001 13
sentinel leader-epoch master001 13

sentinel known-replica master001 192.168.123.78 6379
# Generated by CONFIG REWRITE
dir "/data"
sentinel known-sentinel master001 192.168.123.78 26379 02ba5344dfb96dc699bcefc2e1d105190c0e85e3
sentinel known-sentinel master001 192.168.123.82 26379 f5e14a1e2d376544dcc2ce88a37a710d5c8a65a4
sentinel current-epoch 13
sentinel announce-ip "192.168.123.78"
sentinel announce-port 26379
masterauth "123456"

6、配置node2下的sentinel.conf 

port 26379
#daemonize yes
#pidfile /var/run/redis-sentinel.pid
logfile "sentinel.log"
sentinel myid f5e14a1e2d376544dcc2ce88a37a710d5c8a65a4
sentinel deny-scripts-reconfig yes
sentinel monitor master001 192.168.123.82 6379 1
sentinel down-after-milliseconds master001 10000
sentinel auth-pass master001 123456

sentinel config-epoch master001 13
sentinel leader-epoch master001 13

sentinel known-replica master001 192.168.123.78 6379
# Generated by CONFIG REWRITE
dir "/data"
sentinel known-sentinel master001 192.168.123.78 26379 02ba5344dfb96dc699bcefc2e1d105190c0e85e3
sentinel current-epoch 13
sentinel announce-ip "192.168.123.82"
sentinel announce-port 26379

7、关闭防火墙

firewall-cmd --permanent --add-port=6379/tcp
firewall-cmd --permanent --add-port=26379/tcp
firewall-cmd --reload

8、测试验证

进入到node1或者node2的redis容器中

docker exec -it 5942a4c958ce /bin/bash

9、查看redis的模式

root@5942a4c958ce:/data# redis-cli -h 127.0.0.1 -p 6379 -a "123456"
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:192.168.123.82
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:36973
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:08a5a246af256bcf10d1d3618805527fb1fe7544
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:36973
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:31055
repl_backlog_histlen:5919

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用Docker Compose部署Redis集群的步骤: 1. 在服务器上安装DockerDocker Compose。 2. 创建一个目录来存储Redis配置文件和数据文件。 ```shell mkdir -p /usr/local/redis/data /usr/local/redis/logs /usr/local/redis/conf chmod -R 777 /usr/local/redis/data* chmod -R 777 /usr/local/redis/logs* ``` 3. 创建一个Redis配置文件。 ```shell cd /usr/local/redis/conf vim redis.conf ``` 在配置文件中添加以下内容: ``` bind 0.0.0.0 protected-mode no port 6379 cluster-enabled yes cluster-config-file nodes.conf cluster-node-timeout 5000 appendonly yes ``` 4. 创建一个Docker Compose文件。 ```shell cd /usr/local/redis vim docker-compose.yml ``` 在文件中添加以下内容: ``` version: '3' services: redis1: image: redis:6.2.5 container_name: redis1 command: redis-server /usr/local/etc/redis/redis.conf volumes: - ./conf/redis.conf:/usr/local/etc/redis/redis.conf - ./data/redis1:/data - ./logs/redis1:/logs ports: - "6379:6379" networks: redis-cluster: ipv4_address: 172.16.238.10 redis2: image: redis:6.2.5 container_name: redis2 command: redis-server /usr/local/etc/redis/redis.conf volumes: - ./conf/redis.conf:/usr/local/etc/redis/redis.conf - ./data/redis2:/data - ./logs/redis2:/logs ports: - "6380:6379" networks: redis-cluster: ipv4_address: 172.16.238.11 redis3: image: redis:6.2.5 container_name: redis3 command: redis-server /usr/local/etc/redis/redis.conf volumes: - ./conf/redis.conf:/usr/local/etc/redis/redis.conf - ./data/redis3:/data - ./logs/redis3:/logs ports: - "6381:6379" networks: redis-cluster: ipv4_address: 172.16.238.12 redis4: image: redis:6.2.5 container_name: redis4 command: redis-server /usr/local/etc/redis/redis.conf volumes: - ./conf/redis.conf:/usr/local/etc/redis/redis.conf - ./data/redis4:/data - ./logs/redis4:/logs ports: - "6382:6379" networks: redis-cluster: ipv4_address: 172.16.238.13 redis5: image: redis:6.2.5 container_name: redis5 command: redis-server /usr/local/etc/redis/redis.conf volumes: - ./conf/redis.conf:/usr/local/etc/redis/redis.conf - ./data/redis5:/data - ./logs/redis5:/logs ports: - "6383:6379" networks: redis-cluster: ipv4_address: 172.16.238.14 redis6: image: redis:6.2.5 container_name: redis6 command: redis-server /usr/local/etc/redis/redis.conf volumes: - ./conf/redis.conf:/usr/local/etc/redis/redis.conf - ./data/redis6:/data - ./logs/redis6:/logs ports: - "6384:6379" networks: redis-cluster: ipv4_address: 172.16.238.15 networks: redis-cluster: ipam: driver: default config: - subnet: 172.16.238.0/24 ``` 5. 启动Redis集群。 ```shell docker-compose up -d ``` 这将启动6个Redis容器,其中3个是主节点,3个是从节点,并且外部可以通过6379端口访问Redis集群。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值