Mesos-dns and Marathon-lb are two service discovery and load balancing tools provided by mesosphere's official website. The official documents are mainly for DCOS, and there are not many related Chinese documents for other systems. The following are my installation instructions and usage summary on Centos7.
1. Mesos service discovery and load balancing
By default , mesos marathon publishes apps to random ports on random nodes. When there are more and more mesos slaves and apps , it becomes difficult to find a group of apps .
mesos provides two tools: mesos-dns and marathon-lb . mesos-dns is a service discovery tool, and marathon-lb is not only a service discovery tool, but also a load balancing tool.
2. m esos-dns
Mesos-dns is a mesos service discovery tool, which can find the app 's IP, port number, master, leader and other information.
2.1 Installation
Download the mesos-dns binary from:
https://github.com/mesosphere/mesos-dns/releases
Renamed to mesos-dns
chmod +x mesos-dns
Write config.json according to the official document, fill in zk, master and other related information
2.2 Start
2.2.1 Command line mode
mesos-dns -config config.json
2.2.2 can also be deployed with 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"
}
# Send deployment content to marathon
curl -i -H 'Content-Type: application/json' 172.31.17.71:8080/v2/apps [email protected]
The mesos-dns in the figure is the mesos-dns deployed through marathon , with two instances in total.
2.3 How to use
Note: slave4 is the hostname where mesos-dns is installed
2.3.1 Find the ip of the app
dig test-app.marathon.mesos + short @ slave4
172.17.0.2
2.3.2 Find the IP of the node where the app is located
dig test-app.marathon.slave.mesos + short @ slave4
172.31.17.33
172.31.17.31
172.31.17.32
2.3.3 Find the app service port number
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 is both a service discovery tool and a load balancing tool. It integrates haproxy , automatically obtains the information of each app , generates haproxy configuration for each group of apps , and provides services through servicePort or web virtual host .
To use marathonn-lb , each group of apps must have the HAPROXY_GROUP tag set.
When Marathon-lb is running, it is bound to the service port ( servicePort, if the app does not define servicePort, marathon will randomly assign a port number ) defined by each group of apps, and each group of apps can be accessed through the relevant service port of the node where marathon-lb is located .
For example: marathon-lb is deployed on slave5, test-app is deployed on slave1, and the servicePort of test-app is 10004, then the service provided by test-app can be accessed on port 10004 of slave5.
Since servicePort is not port 80 and 443 (ports 80 and 443 have been exclusively used by haproxy in marathon-lb), it is not very convenient for web services. You can use haproxy virtual host to solve this problem:
Add the HAPROXY_{n}_VHOST (WEB virtual host) label to the app configuration that provides web services , and marathon-lb will automatically publish the WEB cluster services of this group of apps on ports 80 and 443 of the node where marathon-lb is located . User settings After DNS, it is accessed through the virtual host name.
3.1 Installation
#download marathon -lb image
docker pull docker.io/mesosphere/marathon-lb
It can be run through docker run or deployed to a mesos cluster through marathon .
3.2 Running
3.2.1 Command line operation
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 Deploy via 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 [email protected]
3.3 How to use
The following uses marathon-lb to perform service discovery and load balancing on the http service:
3.3.1 Publishing an app
# First create the json configuration information of the app
Be sure to add the HAPROXY_GROUP label. For web services, you can add the VHOST label to let marathon-lb set the WEB virtual host;
For web services, set servicePort to 0, and marathon-lb will automatically publish the web service cluster to 80 and 443;
{
"id": "test-app",
"labels": {
"HAPROXY_GROUP":"external",
"HAPROXY_0_VHOST":"test-app.gkkxd.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"}
]
}
}
}
#publish app _
curl -i -H 'Content-Type: application/json' 172.31.17.71:8080/v2/apps [email protected]
3.3.2 Accessing the app
First set up DNS or hosts file:
172.31.17.34 test-app.gkkxd.com
Use a browser to access the virtual host through http and https, and find that the service has been started, which is actually a web service cluster configured by the built-in haproxy of marathon-lb for the three instances of test-app:
For marathon-lb , multiple units can be deployed at the same time, and then use DNS polling or keepalived virtual IP to achieve high availability.