zoukankan      html  css  js  c++  java
  • marathon的高可用服务自动发现和负载均衡

    上一篇我们说谈了docker+zookeeper+mesos+marathon集群,本篇我们来谈谈marathon的集群和自动发现服务。

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

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

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

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

    2. mesos-dns(生产环境最好不要用)

    Mesos-dns是 mesos 服务发现工具,能查找app的Ip,端口号以及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

    2.3 使用方法

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

    2.3.1 查找app的ip

    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所在节点的80和443端口上,用户设置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://docker-master1:8080 -m http://docker-master2:8080 -m http://docker-master3: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://docker-master1:8080", "-m","http://docker-master2:8080", "-m","http://docker-master3:8080","--group", "external"]
    }  
    curl -X POST http://docker-master1:8080/v2/apps -d@/root/marathon-lb.json -H "Content-type:application/json"

    3.3 使用方法

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

    3.3.1 发布app

    # 先创建app的json配置信息

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

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

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

    发布APP

    curl -X POST http://docker-master1:8080/v2/apps -d@/root/nginx.json -H "Content-type:application/json"

    3.3.2 访问app

    先设置DNS或者hosts文件:

    192.168.20.213 test.nginx.com

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

    http://test.nginx.com

    https://test.nginx.com

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

    3.3.3 查看haproxy配置文件和状态

    http:/test.nginx.com:9090/_haproxy_getconfig

    http://test.nginx.com:9090/haproxy?stats

  • 相关阅读:
    【最短路】The 2019 Asia Nanchang First Round Online Programming Contest Fire-Fighting Hero (Dijkstra)
    【积累】The 2019 Asia Nanchang First Round Online Programming Contest The Nth Item (矩阵快速幂 k进制快速幂)
    【线段树】The Preliminary Contest for ICPC Asia Xuzhou 2019 Colorful String(回文树+线段树+状压/bitset)
    allure参数说明及代码示例
    Idea+maven+testng+reportng生成测试报告
    ubuntu 16.04 镜像下载
    new AndroidDriver报错java.lang.NoSuchMethodError: com.google.common.base.Throwables.throwIfUnchecked
    Appium常用的API
    Appium常用的定位方法
    adb 指令总结
  • 原文地址:https://www.cnblogs.com/harlanzhang/p/9856359.html
Copyright © 2011-2022 走看看