• [经验交流] Mesos-dns 和 Marathon-lb 简要使用方法


    Mesos-dns 和 Marathon-lb 是mesosphere 官网提供的两种服务发现和负载均衡工具。官方的文档主要针对DCOS,针对其它系统的相关中文文档不多,下面是我在Centos7上的安装说明和使用总结。

    1. Mesos服务发现与负载均衡

    默认情况下mesos marathon会把app发布到随机节点的随机端口上,mesos slavesapp越来越多的时候,想查找某组app就变得困难。

    mesos提供了两个工具:mesos-dnsmarathon-lbmesos-dns是一个服务发现工具,marathon-lb不仅是服务发现工具,还是负载均衡工具。

    2. mesos-dns

    Mesos-dns是 mesos 服务发现工具,能查找appIp,端口号以及master,leader等信息。

    2.1 安装

    从下述地址下载mesos-dns二进制文件:

    https://github.com/mesosphere/mesos-dns/releases

    重命名为mesos-dns

    chmod +x mesos-dns

    按照官方文档编写config.json,填入zk、master等相关信息 

     

    2.2 启动

    2.2.1 命令行方式

    mesos-dns -config config.json

    2.2.2 也可以用marathon部署

    #mesos-dns.json

    {
    "id": "mesos-dns",
    "cpus": 0.5,
    "mem": 128.0,
    "instances": 3,
    "constraints": [["hostname", "UNIQUE"]],
    "cmd": "/opt/mesos-dns/mesos-dns -config /opt/mesos-dns/config.json"
    }

    #向marathon发送部署内容

    curl -i -H 'Content-Type: application/json' 172.31.17.71:8080/v2/apps -d@mesos-dns.json

    图中的mesos-dns是通过marathon部署的mesos-dns,共两个实例。

     

    2.3 使用方法

    注:slave4是安装了mesos-dns的主机名

    2.3.1 查找appip

    dig test-app.marathon.mesos +short @slave4

    172.17.0.2

    2.3.2 查找app所在节点的IP

    dig test-app.marathon.slave.mesos +short @slave4

    172.31.17.33
    172.31.17.31
    172.31.17.32

    2.3.3 查找app服务端口号

    dig SRV _test-app._tcp.marathon.mesos +short @slave4

    0 0 31234 test-app-s3ehn-s11.marathon.slave.mesos.

    0 0 31846 test-app-zfp5d-s10.marathon.slave.mesos.

    0 0 31114 test-app-3xynw-s12.marathon.slave.mesos.

     

    3. marathon-lb

    Marathon-lb既是一个服务发现工具,也是负载均衡工具,它集成了haproxy,自动获取各个app的信息,为每一组app生成haproxy配置,通过servicePort或者web虚拟主机提供服务

    要使用marathonn-lb,每组app必须设置HAPROXY_GROUP标签。

    Marathon-lb运行时绑定在各组app定义的服务端口(servicePort,如果app不定义servicePort,marathon会随机分配端口号)上,可以通过marathon-lb所在节点的相关服务端口访问各组app

    例如:marathon-lb部署在slave5,test-app 部署在slave1,test-app 的servicePort是10004,那么可以在slave5的 10004端口访问到test-app提供的服务。

    由于servicePort 非80、443端口(80、443端口已被marathon-lb中的 haproxy独占),对于web服务来说不太方便,可以使用 haproxy虚拟主机解决这个问题:

    在提供web服务的app配置里增加HAPROXY_{n}_VHOST(WEB虚拟主机)标签,marathon-lb会自动把这组app的WEB集群服务发布在marathon-lb所在节点的80443端口上,用户设置DNS后通过虚拟主机名来访问。

    3.1 安装

    #下载marathon-lb镜像

    docker pull docker.io/mesosphere/marathon-lb

    可以通过docker run运行,也可以通过marathon部署到mesos集群里。

     

    3.2 运行

    3.2.1 命令行运行

    docker run -d --privileged -e PORTS=9090 --net=host docker.io/mesosphere/marathon-lb sse -m http://master1_ip:8080 -m http://master2_ip:8080 -m http://master3_ip:8080  --group external

    3.2.2 通过marathon部署 

    {
    "id": "marathon-lb",
    "instances": 3,
    "constraints": [["hostname", "UNIQUE"]],
    "container": {
    "type": "DOCKER",
    "docker": {
    "image": "docker.io/mesosphere/marathon-lb",
    "privileged": true,
    "network": "HOST"
    }
    },
    "args": ["sse", "-m","http://master1_ip:8080", "-m","http://master2_ip:8080", "-m","http://master3_ip:8080","--group", "external"]
    }

    curl -i -H 'Content-Type: application/json' 172.31.17.71:8080/v2/apps -d@marathon-lb.json

     

    3.3 使用方法

    下面使用marathon-lbhttp服务进行服务发现和负载均衡:

    3.3.1 发布app

    # 先创建app的json配置信息

    一定要加上HAPROXY_GROUP标签,对于web服务,可以加上VHOST标签,让marathon-lb设置WEB虚拟主机;

    对于web服务,servicePort设置为0即可,marathon-lb会自动把web服务集群发布到80、443上; 

    {
    "id": "test-app",
    "labels": {
    "HAPROXY_GROUP":"external",
    "HAPROXY_0_VHOST":"test-app.test.com"
    },
    "cpus": 0.5,
    "mem": 64.0,
    "instances": 3,
    "constraints": [["hostname", "UNIQUE"]],
    "container": {
    "type": "DOCKER",
    "docker": {
    "image": "httpd",
    "privileged": false,
    "network": "BRIDGE",
    "portMappings": [
    { "containerPort": 80, "hostPort": 0, "servicePort": 0, "protocol": "tcp"}
    ]
    }
    }
    }

    #发布app

    curl -i -H 'Content-Type: application/json' 172.31.17.71:8080/v2/apps -d@test-app.json

    3.3.2 访问app

    先设置DNS或者hosts文件:

    172.31.17.34 test-app.test.com

    用浏览器通过http和https访问虚拟主机,发现服务已经启动,实际上是marathon-lb内置的haproxy对test-app的三个实例配置的web服务集群:

    http://test-app.test.com

    https://test-app.test.com

    对于marathon-lb,可以同时部署多台,然后用DNS轮询或者keepalived虚拟IP实现高可用。



  • 相关阅读:
    微信分享
    angular 2
    angular 2
    angular 2
    angular 2
    ionic android升级检查
    ionic andorid apk 签名, 查看签名MD5
    微信支付 python版
    CSS3
    ionic 常见问题
  • 原文地址:https://www.cnblogs.com/hahp/p/5396302.html
Copyright © 2020-2023  润新知