当前位置:   article > 正文

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

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

redis集群(cluster)笔记

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

redis 三主六从高可用docker(不固定ip)

redis 三主六从高可用dockerswarm高级版(不固定ip)

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

跨主机部署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
  • 1
  • 2
  • 3
  • 4

在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
  • 1
  • 2
  • 3
  • 4
  • 5

在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
  • 2
  • 3
  • 4
  • 5

1. 在3台主机上配置swarm

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

  • 在node1上执行:

docker swarm init --advertise-addr 192.168.56.100
docker swarm join-token manager
  • 1
  • 2

返回类似以下内容:

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

2. 创建swarm网络

在node1上执行以下命令:

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

3.文件目录如下

├─node1
│  ├─conf
│  ├─redis1
│  │  └─data
│  └─redis2
│      └─data
├─node2
│  ├─conf
│  ├─redis1
│  │  └─data
│  └─redis2
│      └─data
└─node3
    ├─conf
    ├─redis1
    │  └─data
    └─redis2
        └─data
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

4.文件介绍

4.1 所有conf文件夹里面有一个redis.conf
port 6379
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
  • 1
  • 2
  • 3
  • 4
  • 5
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
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
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
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
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
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
4.3 其余的是目录

5.node1执行

rm -rf /home/redis/node2
rm -rf /home/redis/node3
cd /home/redis/node1 && docker-compose up -d
  • 1
  • 2
  • 3

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
  • 1
  • 2
  • 3
  • 4

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
  • 1
  • 2
  • 3
  • 4

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
  • 1
  • 2

在这里插入图片描述

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

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

7.常用命令

# 查看节点
cluster nodes
  • 1
  • 2
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/124061
推荐阅读
相关标签
  

闽ICP备14008679号