Docker-swarm 快速搭建redis集群

Docker-swarm 快速搭建redis集群,第1张

Docker-swarm 搭建redis集群 环境配置

1.三台虚拟机,VM16

*** 作系统IP备注
centos7192.168.2.131
centos7192.168.2.132
centos7192.168.2.133

2.redis配置

hostnameIP端口备注
manager192.168.2.131700117001
manager192.168.2.131700217002
worker01192.168.2.132700317003
worker01192.168.2.132700417004
worker02192.168.2.133700517005
worker02192.168.2.133700617006
修改hostname

192.168.2.131

sudo hostnamectl set-hostname manager

重启后永久生效,或者执行 exec bash 使立即生效

192.168.2.132

sudo hostnamectl set-hostname worker01

重启后永久生效,或者执行 exec bash 使立即生效

192.168.2.133

sudo hostnamectl set-hostname worker02

重启后永久生效,或者执行 exec bash 使立即生效

创建目录

分别在集群的各服务器(131/132/133)上创建目录:

sudo rm -rf /home/data/redis/ && sudo mkdir -p /home/data/redis/{7001,7002,7003,7004,7005,7006}/{data,conf} && chmod 777 -R /home/data/
创建网络
docker network create --driver overlay mynetwork

这里创建网络类型为overlay,网络类型主要使用比较多的是bridge、overlay ,由于这里使用的是swarm集群部署,要让所有服务在同一个网络中,则需要使用overlay 。

[root@worker2 ~]# docker network ls
NETWORK ID     NAME              DRIVER    SCOPE
147f2ede7454   bridge            bridge    local
e29b8451a401   docker_gwbridge   bridge    local
4b767db33495   host              host      local
splhg7ef8xyc   ingress           overlay   swarm
b037f0b632fa   mynet             bridge    local
6zxw8ah74hor   mynetwork         overlay   swarm
b6f757b6cadd   none              null      local
063d396e139c   somenetwork       bridge    local
编写compose.yml模版文件
version: "3.8"
services:
  redis7001:
    image: redis:alpine
    container_name: redis7001
    #设置主机名
    hostname: redis7001
    restart: always
    #privileged: true
    #挂载目录,相当于 docker run -v 主机目录:容器目录
    volumes:
      - /home/data/redis/7001/data:/data
      - /home/data/redis/7001/conf:/conf
    #启动容器执行命令,相当于docker run [镜像:tag]  [命令], 登录redis: redis-cli -h 192.168.0.80 -p 6379 -a Dszn@2020
    command: redis-server --appendonly yes --cluster-enabled yes --cluster-config-file /conf/nodes.conf --cluster-announce-ip 192.168.2.131 --cluster-announce-port 7001 --cluster-announce-bus-port 17001
    ports:
      - "7001:6379"
      - "17001:16379"
    #指定环境变量,相当于docker run -e 参数, 登录mysql: mysql -h192.168.3.80 -P3306 -uroot -pDs20Pwd@
    environment:
      - TZ=Asia/Shanghai
    networks:
      - mynetwork
    deploy:
      placement:
        constraints:
          - node.hostname == manager
          - node.role == manager
  redis7002:
    image: redis:alpine
    container_name: redis7002
    #设置主机名
    hostname: redis7002
    restart: always
    #privileged: true
    #挂载目录,相当于 docker run -v 主机目录:容器目录
    volumes:
      - /home/data/redis/7002/data:/data
      - /home/data/redis/7002/conf:/conf
    #启动容器执行命令,相当于docker run [镜像:tag]  [命令], 登录redis: redis-cli -h 192.168.0.80 -p 6379 -a Dszn@2020
    command: redis-server --appendonly yes --cluster-enabled yes --cluster-config-file /conf/nodes.conf --cluster-announce-ip 192.168.2.131 --cluster-announce-port 7002 --cluster-announce-bus-port 17002
    ports:
      - "7002:6379"
      - "17002:16379"
    #指定环境变量,相当于docker run -e 参数, 登录mysql: mysql -h192.168.3.80 -P3306 -uroot -pDs20Pwd@
    environment:
      - TZ=Asia/Shanghai
    networks:
      - mynetwork
    deploy:
      placement:
        constraints:
          - node.hostname == manager
          - node.role == manager

  redis7003:
    image: redis:alpine
    container_name: redis7003
    #设置主机名
    hostname: redis7003
    restart: always
    volumes:
      - /home/data/redis/7003/data:/data
      - /home/data/redis/7003/conf:/conf
    command: redis-server --appendonly yes --cluster-enabled yes --cluster-config-file /conf/nodes.conf --cluster-announce-ip 192.168.2.132 --cluster-announce-port 7003 --cluster-announce-bus-port 17003
    ports:
      - "7003:6379"
      - "17003:16379"
    environment:
      - TZ=Asia/Shanghai
    networks:
      - mynetwork
    deploy:
      placement:
        constraints:
          - node.hostname == worker1
  redis7004:
    image: redis:alpine
    container_name: redis7004
    #设置主机名
    hostname: redis7004
    restart: always
    volumes:
      - /home/data/redis/7004/data:/data
      - /home/data/redis/7004/conf:/conf
    command: redis-server --appendonly yes --cluster-enabled yes --cluster-config-file /conf/nodes.conf --cluster-announce-ip 192.168.2.132 --cluster-announce-port 7004 --cluster-announce-bus-port 17004
    ports:
      - "7004:6379"
      - "17004:16379"
    environment:
      - TZ=Asia/Shanghai
    networks:
      - mynetwork
    deploy:
      placement:
        constraints:
          - node.hostname == worker1

  redis7005:
    image: redis:alpine
    container_name: redis7005
    #设置主机名
    hostname: redis7005
    restart: always
    volumes:
      - /home/data/redis/7005/data:/data
      - /home/data/redis/7005/conf:/conf
    command: redis-server --appendonly yes --cluster-enabled yes --cluster-config-file /conf/nodes.conf --cluster-announce-ip 192.168.2.132 --cluster-announce-port 7005 --cluster-announce-bus-port 17005
    ports:
      - "7005:6379"
      - "17005:16379"
    environment:
      - TZ=Asia/Shanghai
    networks:
      - mynetwork
    deploy:
      placement:
        constraints:
          - node.hostname == worker2

  redis7006:
    image: redis:alpine
    container_name: redis7006
    #设置主机名
    hostname: redis7006
    restart: always
    volumes:
      - /home/data/redis/7006/data:/data
      - /home/data/redis/7006/conf:/conf
    command: redis-server --appendonly yes --cluster-enabled yes --cluster-config-file /conf/nodes.conf --cluster-announce-ip 192.168.2.133 --cluster-announce-port 7006 --cluster-announce-bus-port 17006
    ports:
      - "7006:6379"
      - "17006:16379"
    environment:
      - TZ=Asia/Shanghai
    networks:
      - mynetwork
    deploy:
      placement:
        constraints:
          - node.hostname == worker2

#声明网桥
networks:
  #定义服务网桥名称
  mynetwork:
    #指定网桥驱动,有bridge/overlay,默认是bridge
    driver: overlay
    #false-统自动创建网桥名,格式为: 目录名_网桥名,默认为false; true-使用外部创建的网桥,需要自己手动创建
    external: true

#挂载目录,声明服务使用的创建卷名
volumes:
  mysqldata:
    #false-系统自动创建的卷名,格式为: 目录名_卷名,默认为false; true-使用外部创建的卷面,需要自己手动创建
    external: false

配置说明:
这里使用了6个节点,3主3从,分别在manager/worker1/worker2上创建2个副本。
注意:挂载目录,如果不使用宿主机挂载目录,可以使用卷的方式挂载,卷挂载会自动创建,可以不用提前手动创建。

启动服务
sudo docker stack deploy -c redis-stack.yml redis

1.启动服务

[root@manager redis]# sudo docker stack deploy -c redis-stack.yml redis
Ignoring unsupported options: restart
Ignoring deprecated options:
container_name: Setting the container name is not supported.
Creating service redis_redis7001
Creating service redis_redis7002
Creating service redis_redis7003
Creating service redis_redis7004
Creating service redis_redis7005
Creating service redis_redis7006

2.查看启动节点

[root@manager redis]# sudo docker stack ls
NAME      SERVICES   ORCHESTRATOR
redis     6          Swarm

3.查看启动的堆栈

[root@worker2 redis_cluster]# docker stack ps redis
ID             NAME                IMAGE          NODE      DESIRED STATE   CURRENT STATE                ERROR     PORTS
bdofotp2fx91   redis_redis7001.1   redis:alpine   manager   Running         Running 2 minutes ago                  
81kmx5o61zn0   redis_redis7002.1   redis:alpine   manager   Running         Running about a minute ago             
wku68rakslzj   redis_redis7003.1   redis:alpine   worker1   Running         Running 49 seconds ago                 
oo7obcm33guk   redis_redis7004.1   redis:alpine   worker1   Running         Running about a minute ago             
xrt14qitd7ar   redis_redis7005.1   redis:alpine   worker2   Running         Running about a minute ago             
jc1xxv66fskg   redis_redis7006.1   redis:alpine   worker2   Running         Running 2 minutes ago                  
[root@worker2 redis_cluster]# 
验证测试

本地连接成功

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-FUXy0qLF-1650614722846)(file://C:\Users\Administrator\AppData\Roaming\marktext\images\2022-04-02-10-36-38-image.png)]

集群

进入manager(192.168.2.131)服务设置集群

1.登录manager节点,执行如下命令集群:

[root@manager ~]# docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS              PORTS      NAMES
71092cdaaf55   redis:alpine   "docker-entrypoint.s…"   2 minutes ago   Up About a minute   6379/tcp   redis_redis7001.1.j84ta2mmwttpl2lp6eoyksz71
695c0fc4545b   redis:alpine   "docker-entrypoint.s…"   2 minutes ago   Up About a minute   6379/tcp   redis_redis7002.1.8jt2yodv9ju8k4saumd7n5rif
[root@manager ~]# docker exec -it 710 /bin/sh
/data # redis-cli -h 192.168.2.131 -p 7001 --cluster create 192.168.2.131:7001 192.168.2.131:7002 192.168.2.132:7003 192.168.2.132:7004 192.168.2.133:7005 192.168.2
.133:7006 --cluster-replicas 1 --cluster-yes

2.查看集群

可以看到集群后,redis有3主3从,分别在manager、worker1、worker2节点上创建了2个服务。

[root@manager ~]# docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS          PORTS      NAMES
71092cdaaf55   redis:alpine   "docker-entrypoint.s…"   28 minutes ago   Up 28 minutes   6379/tcp   redis_redis7001.1.j84ta2mmwttpl2lp6eoyksz71
695c0fc4545b   redis:alpine   "docker-entrypoint.s…"   28 minutes ago   Up 28 minutes   6379/tcp   redis_redis7002.1.8jt2yodv9ju8k4saumd7n5rif
[root@manager ~]# docker exec -it 710 /bin/sh
/data # redis-cli -c
127.0.0.1:6379> cluster nodes
01011fe3d1e19db0bfab5d736f7fcb85f84e5a2b 192.168.2.131:7002@17002 slave 9fece13f2f1ff237b0e450cb5485c27cd4cc1522 0 1648894677000 5 connected
9a68f8ad7688f3c7f3a5474d64ffc5adc7448288 192.168.2.132:7004@17004 slave c1a9795720eb270d6bc77632236d30bfb1fe9728 0 1648894678000 1 connected
c1a9795720eb270d6bc77632236d30bfb1fe9728 192.168.2.131:7001@17001 myself,master - 0 1648894677000 1 connected 0-5460
9fece13f2f1ff237b0e450cb5485c27cd4cc1522 192.168.2.132:7005@17005 master - 0 1648894679923 5 connected 10923-16383
4b6cafd544d4353249eb7f7d58bed7bf0b1caeb1 192.168.2.132:7003@17003 slave 65c23a5ae11b985b270a5e34d7526648a3c826b8 0 1648894679000 7 connected
65c23a5ae11b985b270a5e34d7526648a3c826b8 192.168.2.133:7006@17006 master - 0 1648894678000 7 connected 5461-10922
127.0.0.1:6379> 
集群测试验证

1.Manager节点192.168.2.131:7001@17001 myself,master 登录redis:

redis-cli -c set username ‘zxq’ get username

[root@manager ~]# docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS         PORTS      NAMES
71092cdaaf55   redis:alpine   "docker-entrypoint.s…"   4 minutes ago   Up 4 minutes   6379/tcp   redis_redis7001.1.j84ta2mmwttpl2lp6eoyksz71
695c0fc4545b   redis:alpine   "docker-entrypoint.s…"   4 minutes ago   Up 4 minutes   6379/tcp   redis_redis7002.1.8jt2yodv9ju8k4saumd7n5rif
[root@manager ~]# docker exec -it 710 /bin/sh
/data # redis-cli -c
127.0.0.1:6379> set username "zxq"
-> Redirected to slot [14315] located at 192.168.2.132:7005
OK

2.Worker1节点192.168.2.132:7003@17003 master 登录redis:

[root@worker1 ~]# docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS          PORTS      NAMES
73da00eeb366   redis:alpine   "docker-entrypoint.s…"   24 minutes ago   Up 23 minutes   6379/tcp   redis_redis7004.1.mofccxkrtzb0erc36fwaj9l3b
8e4fed3c8855   redis:alpine   "docker-entrypoint.s…"   24 minutes ago   Up 23 minutes   6379/tcp   redis_redis7003.1.5qmpefwnglv6jvd62206owq3h
[root@worker1 ~]# docker exec -it 73d /bin/sh
/data # redis-cli -c
127.0.0.1:6379> get username
-> Redirected to slot [14315] located at 192.168.2.132:7005
"zxq"
192.168.2.132:7005> set nodes5 "7005"
OK

3.Worker1节点192.168.2.132:7004@17004 slave登录redis:

[root@worker1 ~]# docker exec -it 8e4 /bin/sh
/data # redis-cli -c
127.0.0.1:6379> get nodes5
-> Redirected to slot [15322] located at 192.168.2.132:7005
"7005"
192.168.2.132:7005> 

4.Worker2节点192.168.2.133:7006@17006 slave 登录redis:

[root@worker2 ~]# docker ps
CONTAINER ID   IMAGE                           COMMAND                  CREATED          STATUS          PORTS                                                                                                    NAMES
7505c9fde687   redis:alpine                    "docker-entrypoint.s…"   47 minutes ago   Up 47 minutes   6379/tcp                                                                                                 redis_redis7006.1.o6yly8n6h352g034jhx7nbvaf
2da9dc998c69   redis:alpine                    "docker-entrypoint.s…"   47 minutes ago   Up 47 minutes   6379/tcp                                                                                                 redis_redis7005.1.s6lfflko40afutyz9a5b77tib
[root@worker2 ~]# docker exec -it 750 /bin/sh
/data # redis-cli -c
127.0.0.1:6379> get username
-> Redirected to slot [14315] located at 192.168.2.132:7005
"zxq"
192.168.2.132:7005> get name
-> Redirected to slot [5798] located at 192.168.2.133:7006
"zxq"
192.168.2.133:7006> get nodes5
-> Redirected to slot [15322] located at 192.168.2.132:7005
"7005"
192.168.2.132:7005> 
redis集群完成

参考原文章地址:

docker swarm redis集群_starsky20的博客-CSDN博客_docker swarm搭建redis集群

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/732837.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-27
下一篇 2022-04-27

发表评论

登录后才能评论

评论列表(0条)

保存