`
m635674608
  • 浏览: 5043491 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

Service Discovery with Marathon, Mesos-DNS and HAProxy

 
阅读更多

Problem

You have a bunch of microservices deployed in your Mesos cluster, and want to make them available under service specific URLs via HTTP so they can call each other or be accessed from the outside world.


Overview

Components: Mesos-DNSMarathonHAProxy

  • Marathon starts applications as tasks in the Mesos cluster and uses health checks to keep track of their status.
  • Mesos-DNS polls Mesos Master for app/task - IP:PORT mapping
  • HAProxy configuration is done via servicerouter.py following the default service discovery recipe for Marathon with Mesos-DNS.

Pros

  • Allows logical (DNS) names to IP:PORT mappings.
  • DNS is mature and well understood.
  • Allows matching other request characteristics like HTTP headers.
  • Offers sticky sessions, HTTP to HTTPS redirection, SSL offloading, VHost support and templating capabilities.
  • Battle tested code in HAProxy for the heavy lifting.

Cons

  • Depends on cron job.
  • Some clients aggressively cache DNS entries (for example Java) which can lead to outdated IP addresses.
  • SRV records might not be understood in your environment.

Implementation steps

These steps assume you have already a running Mesos cluster with Marathon installed.

The rest of this guide focusses on Debian/Ubuntu, but can easily be adapted to any other OS.


1. Install HAProxy and Mesos-DNS

HAProxy

HAProxy can usually be installed in a recent enough version via your OS package manager, e.g.

$ apt-get install haproxy

Version 1.5.x is recommended, which is not the default on e.g. Ubuntu 14.04 LTS.
Check http://haproxy.debian.net/ for instructions how to get a recent HAProxy on older Ubuntu and Debian releases.

Mesos-DNS

We will do a deployment of Mesos-DNS via Marathon as described in the following. You might want to launch Mesos-DNS outside of Mesos/Marathon and
monitor/re-start out of band, alternatively—see also Mesos-DNS using systemd.

First create a Mesos-DNS configuration file and place it, for example, in /etc/mesos-dns/:

$ cat /etc/mesos-dns/config.js
{
  "zk": "zk://127.0.0.1:2181/mesos",
  "refreshSeconds": 60,
  "ttl": 60,
  "domain": "mesos",
  "port": 53,
  "resolvers": ["10.0.2.3"],
  "timeout": 5,
  "email": "root.mesos-dns.mesos"
}

Here, I’ve set it up as primary nameserver, but you can also re-use an existing Bind server and use Mesos-DNS only for the .mesos domain.

The second step for a Marathon-based deployment of Mesos-DNS is to create a Marathon app specification that looks as follows:

{
    "args": [
        "/mesos-dns",
        "-config=/config.json"
    ],
    "container": {
        "docker": {
            "image": "mesosphere/mesos-dns",
            "network": "HOST"
        },
        "type": "DOCKER",
        "volumes": [
            {
                "containerPath": "/config.json",
                "hostPath": "/etc/mesos-dns/config.js",
                "mode": "RO"
            }
        ]
    },
    "cpus": 0.2,
    "id": "mesos-dns",
    "instances": 1,
}

Note that in above Marathon app specification it’s important to use the same path under the hostPath key where you stored the
Mesos-DNS configuration file in the previous step. I’ve stored this app spec at ~/marathon-mesosdns.json but it really doesn’t matter where 
you put it.

Now you can post it to the Marathon API via curl:

$ curl -X POST -H "Content-Type: application/json" http://localhost:8080/v2/apps -d@~/marathon-mesosdns.json

2. Test Mesos-DNS setup

In order to test the setup, we now deploy a simple Python app to Marathon.
For that, we need a Marathon app spec which we call pyapp.json:

{
  "id": "peek",
  "cmd": "env >env.txt && python3 -m http.server 8080",
  "cpus": 0.5,
  "mem": 32.0,
  "container": {
    "type": "DOCKER",
    "docker": {
      "image": "python:3",
      "network": "BRIDGE",
      "portMappings": [
        { "containerPort": 8080, "hostPort": 0 }
      ]
    }
  }
}

Post it to the Marathon API via curl:

$ curl -X POST -H "Content-Type: application/json" http://localhost:8080/v2/apps -d@pyapp.json

Check the Marathon UI, and you should now see both Mesos-DNS and the Python app running:

Marathon

Test Mesos-DNS via dig:

$ dig _peek._tcp.marathon.mesos SRV
vagrant@mesos:~$ dig _peek._tcp.marathon.mesos SRV

; <<>> DiG 9.9.5-3ubuntu0.5-Ubuntu <<>> _peek._tcp.marathon.mesos SRV
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 57329
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; QUESTION SECTION:
;_peek._tcp.marathon.mesos.	IN	SRV

;; ANSWER SECTION:
_peek._tcp.marathon.mesos. 60	IN	SRV	0 0 31000 peek-27346-s0.marathon.mesos.

;; ADDITIONAL SECTION:
peek-27346-s0.marathon.mesos. 60 IN	A	10.141.141.10

;; Query time: 4 msec
;; SERVER: 127.0.0.1#53(127.0.0.1)
;; WHEN: Sat Oct 24 23:21:15 UTC 2015
;; MSG SIZE  rcvd: 160

This tells us that the app with the Marathon ID /peek and the logical FQHN peek.marathon.mesos
is running on host 10.141.141.10 and is available via port 31000.


3. Use servicerouter to configure HAProxy

To generate an HAProxy configuration from Marathon running at localhost:8080 with servicerouter.py do the following:

$ wget https://raw.githubusercontent.com/mesosphere/marathon/master/bin/servicerouter.py
$ sudo python servicerouter.py --marathon http://localhost:8080 --haproxy-config /etc/haproxy/haproxy.cfg
servicerouter: fetching apps
servicerouter: GET http://10.141.141.10:8080/v2/apps?embed=apps.tasks
servicerouter: got apps [u'/mesos-dns', u'/peek']
servicerouter: generating config
servicerouter: setting default value for HAPROXY_HEAD
servicerouter: setting default value for HAPROXY_HTTP_FRONTEND_HEAD
servicerouter: setting default value for HAPROXY_HTTP_FRONTEND_APPID_HEAD
servicerouter: setting default value for HAPROXY_HTTPS_FRONTEND_HEAD
servicerouter: setting default value for HAPROXY_FRONTEND_HEAD
servicerouter: setting default value for HAPROXY_BACKEND_REDIRECT_HTTP_TO_HTTPS
servicerouter: setting default value for HAPROXY_BACKEND_HEAD
servicerouter: setting default value for HAPROXY_HTTP_FRONTEND_ACL
servicerouter: setting default value for HAPROXY_HTTP_FRONTEND_APPID_ACL
servicerouter: setting default value for HAPROXY_HTTPS_FRONTEND_ACL
servicerouter: setting default value for HAPROXY_BACKEND_HTTP_OPTIONS
servicerouter: setting default value for HAPROXY_BACKEND_HTTP_HEALTHCHECK_OPTIONS
servicerouter: setting default value for HAPROXY_BACKEND_TCP_HEALTHCHECK_OPTIONS
servicerouter: setting default value for HAPROXY_BACKEND_STICKY_OPTIONS
servicerouter: setting default value for HAPROXY_BACKEND_SERVER_OPTIONS
servicerouter: setting default value for HAPROXY_BACKEND_SERVER_HTTP_HEALTHCHECK_OPTIONS
servicerouter: setting default value for HAPROXY_BACKEND_SERVER_TCP_HEALTHCHECK_OPTIONS
servicerouter: setting default value for HAPROXY_FRONTEND_BACKEND_GLUE
servicerouter: configuring app /mesos-dns
servicerouter: frontend at *:10000 with backend mesos-dns_10000
servicerouter: backend server at 10.141.141.10:31421
servicerouter: trying to resolve ip address for host 10.141.141.10
servicerouter: configuring app /peek
servicerouter: frontend at *:10001 with backend peek_10001
servicerouter: backend server at 10.141.141.10:31000
servicerouter: reading running config from /etc/haproxy/haproxy.cfg
servicerouter: running config is different from generated config - reloading
servicerouter: writing config to temp file /tmp/tmp_32M3e
servicerouter: moving temp file /tmp/tmp_32M3e to /etc/haproxy/haproxy.cfg
servicerouter: trying to find out how to reload the configuration
servicerouter: we seem to be running on a sysvinit based system
servicerouter: reloading using /etc/init.d/haproxy reload

This will refresh haproxy.cfg, and if there were any changes, then it will automatically reload HAProxy.
In order to keep HAProxy up to date, you can use a cron job that executes the servicerouter.py, say, every minute.

 

http://programmableinfrastructure.com/guides/service-discovery/mesos-dns-haproxy-marathon/

分享到:
评论

相关推荐

    docker-mesos-haproxy:Mesos HAProxy Docker 镜像

    Mesos HAProxy Bridge (CentoOS 7 + Supervisor) 这是一个带有 Mesos HAProxy 负载均衡器桥容器,用于实现 / 部署。 它基于 docker 镜像,我们在其中实现了插件,用于在配置文件更改时自动重新加载。 HAProxy 将在对...

    marathon-lb-1.4.3.tar.gz

    Marathon-LB通过动态地从Marathon获取应用实例信息,自动生成HAProxy配置,实现对Mesos集群中服务的负载均衡。 2. **Marathon-LB核心功能** - **动态配置**:Marathon-LB会实时监控Marathon中的应用实例变化,当有...

    mc:一个简单的Mesos-DNS客户端

    mc —一个简单的Mesos-DNS客户端 这是一个用Python编写的简单的客户端。 将其用作CLI工具,如下所示: $ ./mc.py localhost redis.marathon.mesos $ Discovered redis.marathon.mesos running on 10.141.141.10:...

    Mastering Mesos - Dipa Dubhashi

    5. **Mesos Marathon**:Marathon是Mesos上的一个持久化任务调度器,它可以处理应用程序的生命周期管理,包括启动、停止、更新和故障恢复,确保应用的高可用性。 6. **Mesos框架开发**:书中会详细介绍如何开发...

    apache-mesos-cookbook

    Work with Mesos and Docker Integrate Mesos with Spark and other big data frameworks Use networking features in Mesos for effective communication between containers Configure Mesos for high ...

    DCOS之marathon技术文档

    在Marathon之负载均衡篇中,文档详细描述了如何通过Marathon-Bridge、HAProxy、Bamboo等工具来实现负载均衡。 最后,Marathon之应用迁移篇则讲述了如何在Marathon环境中进行应用迁移。 整体来看,Marathon技术文档...

    mesos-marathon-demo:Mesosmarathon + 码头工人

    一旦你的 vagrant 机器在安装了 mesos 和 marathon 的情况下运行,你可以继续以下部分。 构建图像 为了能够使用 docker 执行示例,目录docker-image包含构建和创建 docker 实例所需的步骤。 运行图像 为了在马拉松...

    Docker-Mesos-Marathon:MesosMarathon 上的 Docker 框架

    使用 Mesos/Marathon/Demios 编排 Docker 介绍 关于 Mesos 云时代的应用 从静态分区到弹性共享 经过测试的物理环境 操作系统:Ubuntu 12.04.4 LTS amd64(3.11.0-15-generic / 8Cores / 8GB RAM) 流浪者:1.6.5 ...

    vagrant-mesos-do:Vagrant脚本可轻松在DigitalOcean上设置Apache Mesos集群

    mesos-worker-N:Docker和Marathon的Mesos Worker haproxy-1:具有动态配置的HA代理 在vagrant up ,群集即可使用。 Mesos配置为与docker容器一起使用。 所有计算机都使用UFW防火墙,并且只能通过ssh进行访问。 ...

    mesos-0.18.0-rc3.zip

    Mesos支持多种框架,如 Marathon(用于持续运行无状态任务)和 Chronos(用于定时任务调度)。0.18.0-rc3是发布前的测试版本,通常包含了新功能、性能优化以及错误修复。 【描述】中的"reactive-http.zip"指的是一...

    mesos-spark:Mesos-Spark 虚拟机

    我想在 CoreOS 上运行 marathon-mesos-deimos 设置( )。 我被 Ebay 使用主机 docker 设置( )。 我想同时享受 CoreOS 和 mesos 资源管理,未来希望 kubernetes ( )。 先决条件 舰队控制 流浪汉 虚拟机 Python ...

    airflow-mesos-executor

    1. 安装Mesos和Marathon(Mesos的调度框架)。 2. 安装和配置Airflow,选择Mesos Executor。 3. 设置环境变量和配置文件以指向Mesos集群。 4. 将Airflow的web服务器和scheduler部署在Mesos上,让它们能与Mesos ...

    marathon-dns:轻松自定义本地 DNS - 转 localhost

    npm install marathon-dns -g 完成后,打开您的项目文件 ( ~/.marathon ),并添加您想要的任何项目。 例如,以下项目文件: { "myawesomeapp": 3000 } 将设置http://myawesomeapp.dev指向http://localhost:3000...

    docker-mesos-marathon-screencast:Mesos 上的 Docker 集群中使用的脚本和 Marathon 截屏视频

    docker-mesos-marathon-screencast 的截屏视频中使用的脚本。入门如果您想为自己复制演示。 脚本包含在此 repo 中。 你会需要: 安装 使用您的主launch.sh的 IP 地址更新launch.sh 用你的主人的 IP 地址更新.mesos....

    marathon-lb:Marathon-lb是DCOS的服务发现和负载平衡工具

    马拉松磅 Marathon-lb是通过使用应用程序状态来管理HAProxy的工具。 HAProxy是一种快速,高效,经过考验的高可用性负载均衡器,具有许多高级功能,可为许多备受瞩目的网站提供支持。特征无状态设计:不直接依赖任何...

    mesos-1.4.3.tar.gz

    2. **多框架支持**:Mesos 支持多种框架,如 Marathon (持续服务)、Chronos (定时任务) 和 Spark (大数据处理),允许用户在同一集群上运行不同类型的作业。 3. **高可用性**:Mesos Master 可以配置为多副本模式,...

    mesos-flocker-integrations:Flocker和Mesos之间的集成

    在Mesos中,你可以通过Mesos-DNS或 Marathon(Mesos上的应用编排层)来调度Flocker的数据卷容器。通过Flocker的Mesos驱动程序,可以将Flocker的数据卷与Mesos任务关联起来,这样当Mesos调度器重新分配任务时,相关联...

    archive-mesos-coreos-cluster-example:CoreOS 上的多主机 mesos 集群

    可能还需要配置DNS或Service Discovery机制,如Marathon或Chronos,以实现服务的自动部署和扩展。 6. **运维工具**:Mesos自带了一些管理工具,如Mesos-DNS用于服务发现,以及Mesos UI用于监控集群状态。用户可能还...

    mesos-vagrant:一个用于玩弄 Apache Mesos 和 Marathon 的流浪开发环境

    流浪者一个用来玩 Apache Mesos 和 PAAS Marathon 的流浪开发环境。 该框架由 HA 中的 duel mesos masters、四个 mesos slaves、一个私有托管的 docker register 和 Jenkins docker build (hub101) 组成。 您可以在 ...

Global site tag (gtag.js) - Google Analytics