上一篇我們說談了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