redis 三主三从高可用集群docker swarm

2023-12-13 13:28:37

由于数据量过大,单个Master复制集难以承担,因此需要对多个复制集进行集群,形成水平扩展每个复制集只负责存储整个数据集的一部分,这就是Redis的集群,其作用是提供在多个Redis节点间共享数据的程序集。
官网介绍地址
redis集群(cluster)笔记

跨主机部署redis

安装文件下载链接(或者按照下面的步骤自己创建)

链接:https://pan.baidu.com/s/1tLgLYWkawTvU17yvU1M_5A?pwd=xp9p

主机IP
node1192.168.56.100
node2192.168.56.101
node3192.168.56.102

在这里插入图片描述
在node1 开放防火墙:

firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.56.101" accept"
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.56.102" accept"
firewall-cmd --reload
firewall-cmd --list-all

在node2 开放防火墙:

firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.56.100" accept"
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.56.102" accept"

firewall-cmd --reload
firewall-cmd --list-all

在node3 开放防火墙:

firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.56.101" accept"
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.56.100" accept"

firewall-cmd --reload
firewall-cmd --list-all

1. 在3台主机上配置swarm

  • 创建 /home/redis
mkdir -p /home/redis && cd /home/redis
  • 3台机器分别将redis.zip拷贝到 /home/redis

  • 在node1上执行:

docker swarm init --advertise-addr 192.168.56.100
docker swarm join-token manager

返回类似以下内容:

docker swarm join --token SWMTKN-1-614xi9dvksycykobgifxb4pgopc1wwgczwqct5wqkq8zao6tmx-0ds4jj3ozclrr2wukcaoakxso 192.168.56.100:2377
  • 在node2、node3上执行上面的返回结果:
docker swarm join --token SWMTKN-1-2c2xopn2rld8oltcof24sue370681ijhbo3bwcqarjlhq9lkea-2g53o5qn2anre4j9puv4hecrn 192.168.0.101:2377

2. 创建swarm网络

在node1上执行以下命令:

docker network create --subnet=10.0.1.0/24 -d overlay --attachable pxc_swarm

3.文件目录如下

├─node1
│  ├─conf
│  ├─redis1
│  │  └─data
│  └─redis2
│      └─data
├─node2
│  ├─conf
│  ├─redis1
│  │  └─data
│  └─redis2
│      └─data
└─node3
    ├─conf
    ├─redis1
    │  └─data
    └─redis2
        └─data

4.文件介绍

4.1 所有conf文件夹里面有一个redis.conf
port 6379
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
4.2 所有node下面的目录有一个docker-compose.yml
node1 docker-compose.yml
version: '3'

networks:
  pxc_swarm:
    external: true 

services:
  redis1:
    image: redis:6.0-alpine
    hostname: redis1
    container_name: redis1
    volumes:
      - "/home/redis/redis1/data:/data"
      - "/home/redis/conf/redis.conf:/etc/redis.conf"
    command: redis-server /etc/redis.conf 
    restart: always
    privileged: true
    networks:
      pxc_swarm:
        ipv4_address: 10.0.1.10
  redis2:
    image: redis:6.0-alpine
    hostname: redis2
    container_name: redis2
    volumes:
      - "/home/redis/redis2/data:/data"
      - "/home/redis/conf/redis.conf:/etc/redis.conf"
    command: redis-server /etc/redis.conf 
    restart: always
    privileged: true
    networks:
      pxc_swarm:
        ipv4_address: 10.0.1.11
  
node2 docker-compose.yml
version: '3'

networks:
  pxc_swarm:
    external: true 

services:
  redis1:
    image: redis:6.0-alpine
    hostname: redis1
    container_name: redis1
    volumes:
      - "/home/redis/redis1/data:/data"
      - "/home/redis/conf/redis.conf:/etc/redis.conf"
    command: redis-server /etc/redis.conf 
    restart: always
    privileged: true
    networks:
      pxc_swarm:
        ipv4_address: 10.0.1.20
  redis2:
    image: redis:6.0-alpine
    hostname: redis2
    container_name: redis2
    volumes:
      - "/home/redis/redis2/data:/data"
      - "/home/redis/conf/redis.conf:/etc/redis.conf"
    command: redis-server /etc/redis.conf 
    command: redis-server /etc/redis.conf 
    restart: always
    privileged: true
    networks:
      pxc_swarm:
        ipv4_address: 10.0.1.21
  
node3 docker-compose.yml
version: '3'

networks:
  pxc_swarm:
    external: true 

services:
  redis1:
    image: redis:6.0-alpine
    hostname: redis1
    container_name: redis1
    volumes:
      - "/home/redis/redis1/data:/data"
      - "/home/redis/conf/redis.conf:/etc/redis.conf"
    command: redis-server /etc/redis.conf 
    restart: always
    privileged: true
    networks:
      pxc_swarm:
        ipv4_address: 10.0.1.30
  redis2:
    image: redis:6.0-alpine
    hostname: redis2
    container_name: redis2
    volumes:
      - "/home/redis/redis2/data:/data"
      - "/home/redis/conf/redis.conf:/etc/redis.conf"
    command: redis-server /etc/redis.conf 
    command: redis-server /etc/redis.conf 
    restart: always
    privileged: true
    networks:
      pxc_swarm:
        ipv4_address: 10.0.1.31
  
4.3 其余的是目录

5.node1执行

rm -rf /home/redis/node2
rm -rf /home/redis/node3
cd /home/redis/node1 && docker-compose up -d

6.node2执行

rm -rf /home/redis/node1
rm -rf /home/redis/node3
docker run --rm redis:6.0-alpine redis-cli -v
cd /home/redis/node2 && docker-compose up -d

7.node3执行

rm -rf /home/redis/node1
rm -rf /home/redis/node2
docker run --rm redis:6.0-alpine redis-cli -v
cd /home/redis/node3 && docker-compose up -d

8.加入集群

docker exec -it redis1 /bin/sh
redis-cli --cluster create 10.0.1.10:6379 10.0.1.20:6379 10.0.1.11:6379 10.0.1.30:6379 10.0.1.21:6379 10.0.1.31:6379 --cluster-replicas 1

在这里插入图片描述

M: 1ff0c227fff72b893e8c7514a7d8f49183f0a397 10.0.1.10:6379
   slots:[0-5460] (5461 slots) master
M: 5242fb87c0c7f0179d32727b6043cc546ec1a731 10.0.1.20:6379
   slots:[5461-10922] (5462 slots) master
M: 7daa8da3450adc53ac88eda3a3c1a8f2c1976c02 10.0.1.11:6379
   slots:[10923-16383] (5461 slots) master
S: 962e2b4516cb7186f407d6ffc3a91b62603e0a09 10.0.1.30:6379
   replicates 7daa8da3450adc53ac88eda3a3c1a8f2c1976c02
S: 04fff0c1488896bef11aa508edbbf5ac6c29815b 10.0.1.21:6379
   replicates 1ff0c227fff72b893e8c7514a7d8f49183f0a397
S: 2e43a4d7b46b20e60220b4f495d8370b07c717bd 10.0.1.31:6379
   replicates 5242fb87c0c7f0179d32727b6043cc546ec1a731

10  21
20  31
11  30

所有主从不允许在同一台机器上

7.常用命令

# 查看节点
cluster nodes

文章来源:https://blog.csdn.net/qq_36120342/article/details/134892732
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。