docker swarm 使用docker-compose.yml部署时,eureka注册中心如何配置?

docker swarm 中docker-compose.yml配置说明


前言

当时用docker swarm 对容器进行编排管理时,如果管理的是微服务,则不可避免要用到注册中心,但是注册中心不能简单的设置副本数为3来部署注册中心集群,接下来以eureka注册中心为例做说明。


一、docker-compose.yml如何配置注册中心?

需要按照配置3个服务的方式,来配置eureka注册中心。

二、配置说明

1.docker-compose.yml配置

version: '3.9'
services:
#  my-server:
#    image: ip:port/my-server
#    ports:
#      - "10000:100000"
#    networks:
#      - test-network
#      #environment:
#      # MODE: 'standalone'
#    volumes:
#      #使用绝对路径挂载数据卷
#      - /home/my/log/my-server:/app/log
#      - /etc/localtime:/etc/localtime
#    deploy:
#      #为外部客户端连接到swarm指定服务发现方式
#      #endpoint_mode: vip:Docker为服务分配了一个前端的虚拟IP,客户端通过该虚拟IP访问网络上的服务。Docker在客户端和服务的可用工作节点之间进行路由请求,而无须关系有多少节点正在参与该服务或这些节点的IP地址或者端口。这是默认设置。
#      #endpoint_mode: dnsrr:DNS轮询(DNSRR),Docker设置服务的DNS条目,以便对服务名称的DNS查询返回IP地址列表,并且客户端通过轮询的方式直接连接到其中之一。
#      endpoint_mode: vip
#      #指定服务的容器副本模式。
#      #global:每个swarm节点只有一个该服务容器。
#      #replicated:整个集群中存在指定份数的服务容器副本,为默认值。
#      mode: replicated
#      replicas: 2
#        #指定服务的标签。这些标签仅在服务上设置,而不在服务的任何容器上设置。
#        #labels:
#      #com.example.description: "This label will appear on the web service"
#      #指定constraints和preferences。constraints可以指定只有符合要求的节点上才能运行该服务容器,preferences可以指定容器分配策略。
#      placement:
#        #如果服务的容器副本模式为replicated(默认),可以指定每个节点上运行的最大容器副本数量。当指定的容器副本数量大于最大容器副本数量时,将引发no suitable node (max replicas per node limit exceed)错误
#        max_replicas_per_node: 1
#        constraints:
#          - "node.hostname!=host1"
#        #  - "engine.labels.operatingsystem==ubuntu 18.04"
#        #preferences:
#        #    - spread: node.labels.zone
#      restart_policy:
#        #重启策略。值可以为none、on-failure或any,默认为any。
#        condition: on-failure
#        #尝试重启的等待时间。指定为持续时间(durations)。默认值为0。
#        delay: 5s
#        #重启最多尝试的次数,超过该次数将放弃。默认为永不放弃。如果在window配置的时间之内未成功重启,则此次尝试不计入max_attempts的值。
#        max_attempts: 5
#        #在决定重启是否成功之前的等待时间。指定为持续时间(durations)。默认值为立即决定。
#        window: 10s
#      #配置资源限制
#      #resources:
#        #限制cpu的最大使用率和内存的最大使用率
#         #limits:
#          #最多使用20%的cpu
#          #  cpus: '0.20'
#          #最多使用64m内存
#          #  memory: 128M
#        #预留值
#         #reservations:
#            #预留10%的cpu
#         #   cpus: '0.10'
#            #预留20m内存
#         #   memory: 64M
#      #配置在更新失败的情况下如何回滚服务
#      rollback_config:
#        #一次回滚的容器数量。如果设置为0,则所有容器同时回滚。
#        parallelism: 1
#        #每个容器组之间的回滚所等待的时间。默认值为0s
#        delay: 0s
#        #回滚失败后的行为。有continue和pause两种,默认值为pause
#        failure_action: pause
#        #每次任务更新后监视失败的时间(ns|us|ms|s|m|h)。默认值为0s。
#        monitor: 0s
#        #在回滚期间能够容忍的最大失败率。默认值为0。
#        max_failure_ratio: 0
#        #设置回滚顺序。stop-first为在开启新任务之前停止旧任务,start-first为首先启动新任务,和正在运行任务短暂重叠,默认值为stop-first。
#        order: stop-first
#      #配置如何更新服务。该配置对滚动更新很有用
#      update_config:
#        #一次更新的容器数量
#        parallelism: 1
#        #更新一组容器之间的等待时间
#        delay: 0s
#        #更新失败后的行为。有continue、rollback和pause三种,默认值为pause。
#        failure_action: pause
#        #每次任务更新后监视失败的时间(ns|us|ms|s|m|h)。默认值为0s。。
#        monitor: 0s
#        #在更新期间能够容忍的最大失败率。
#        max_failure_ratio: 0
#        #设置更新顺序。stop-first为在开启新任务之前停止旧任务,start-first为首先启动新任务,和正在运行任务短暂重叠,默认值为stop-first。
#        order: stop-first
  eureka:
    image: ip:port/eureka
    ports:
      - "10001:10001"
    networks:
      - test-network
      #environment:
      # MODE: 'standalone'
    volumes:
      - /home/my/log/eureka:/app/log
      - /etc/localtime:/etc/localtime
    deploy:
      endpoint_mode: vip
      mode: replicated
      replicas: 1
        #labels:
      #com.example.description: "This label will appear on the web service"
      placement:
        max_replicas_per_node: 1
        constraints:
          - "node.hostname!=host1"
          - "node.hostname!=host2"
          - "node.labels.role != master"
        #  - "engine.labels.operatingsystem==ubuntu 18.04"
        #preferences:
        #    - spread: node.labels.zone
      restart_policy:
        condition: on-failure
        delay: 5s
        max_attempts: 5
        window: 10s
        #resources:
        #   limits:
        #     cpus: '0.20'
        #    memory: 64M
        #  reservations:
        #    cpus: '0.10'
        #   memory: 20M
      rollback_config:
        parallelism: 1
        delay: 30s
        failure_action: pause
        monitor: 0s
        max_failure_ratio: 0
        order: stop-first
      update_config:
        parallelism: 1
        delay: 30s
        failure_action: pause
        monitor: 0s
        max_failure_ratio: 0
        order: stop-first
  eureka1:
    image: ip:port/eureka
    ports:
      - "10002:10001"
    networks:
      - test-network
      #environment:
      # MODE: 'standalone'
    volumes:
      - /home/my/log/eureka:/app/log
      - /etc/localtime:/etc/localtime
    deploy:
      endpoint_mode: vip
      mode: replicated
      replicas: 1
        #labels:
      #com.example.description: "This label will appear on the web service"
      placement:
        max_replicas_per_node: 1
        constraints:
          - "node.hostname!=host1"
          - "node.hostname!=host2"
          - "node.labels.role != master"
        #  - "engine.labels.operatingsystem==ubuntu 18.04"
        #preferences:
        #    - spread: node.labels.zone
      restart_policy:
        condition: on-failure
        delay: 5s
        max_attempts: 5
        window: 10s
        #resources:
        #   limits:
        #     cpus: '0.20'
        #    memory: 64M
        #  reservations:
        #    cpus: '0.10'
        #   memory: 20M
      rollback_config:
        parallelism: 1
        delay: 30s
        failure_action: pause
        monitor: 0s
        max_failure_ratio: 0
        order: stop-first
      update_config:
        parallelism: 1
        delay: 30s
        failure_action: pause
        monitor: 0s
        max_failure_ratio: 0
        order: stop-first
  eureka2:
    image: ip:port/eureka
    ports:
      - "10003:10001"
    networks:
      - test-network
      #environment:
      # MODE: 'standalone'
    volumes:
      - /home/my/log/eureka:/app/log
      - /etc/localtime:/etc/localtime
    deploy:
      endpoint_mode: vip
      mode: replicated
      replicas: 1
        #labels:
      #com.example.description: "This label will appear on the web service"
      placement:
        max_replicas_per_node: 1
        constraints:
          - "node.hostname!=host1"
          - "node.hostname!=host2"
          - "node.labels.role != master"
        #  - "engine.labels.operatingsystem==ubuntu 18.04"
        #preferences:
        #    - spread: node.labels.zone
      restart_policy:
        condition: on-failure
        delay: 5s
        max_attempts: 5
        window: 10s
        #resources:
        #   limits:
        #     cpus: '0.20'
        #    memory: 64M
        #  reservations:
        #    cpus: '0.10'
        #   memory: 20M
      rollback_config:
        parallelism: 1
        delay: 30s
        failure_action: pause
        monitor: 0s
        max_failure_ratio: 0
        order: stop-first
      update_config:
        parallelism: 1
        delay: 30s
        failure_action: pause
        monitor: 0s
        max_failure_ratio: 0
        order: stop-first


networks:
  test-network:
    driver: overlay

其中配置了三个服务,服务名分别为eureka,eureka1,eureka2,每个eureka映射到宿主机的端口不一样。

2.微服务配置 

eureka:
  client:
    serviceUrl:
      defaultZone:  http://eureka:10001/eureka,http://eureka1:10002/eureka,http://eureka2:10003/eureka

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

驿外无主

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值