1.概述:
目前marathon主要支持两种autoscaling:1.基于cpu、内存使用率的autoscaling;2.基于服务请求频率的autoscaling。下面将就这两种autoscaling的原理和用法进行详述。
2.基于CPU和内存使用率的autoscaling
2.1原理
app autoscaling的实现主要依靠一个运行在主机上名为marathon-autoscale.py的python脚本,不断监测app中cpu和内存的使用率,当使用率达到阈值时自动扩展app中instance的数量。这个脚本启动时主要配置好几个参数:Marathon的地址、要监测并自动扩展的app名字、这个app所有instance中平均内存使用率的上限、这个app所有instance中平均CPU使用率的上限、自动扩展的Instance数量的上限。
通过看脚本的代码可以看到脚本中有一个marathon的class,通过传入marathon的url来初始化它,如下图所示:
在main函数中可以看到使用marathon类创建了一个aws_marathon的对象,然后调用这个对象的get_app_detail函数返回一个所有APP的task和所在host的信息。之后主要就是通过这个两个信息来调用get_task_agentstatistics(task,host)函数从http://'+host +':5051/monitor/statistics.json获取每个instance上cpu和内存的使用情况。
以上分析得出结论,其实最终是调用mesos的监控机制,从mesos-slave上获取app中各instance上cpu和内存的使用率的。想了解Mesos的monitoring机制请看:http://mesos.apache.org/documentation/latest/monitoring/
2.2使用
1.创建app
on 10.134.29.141
• 1.编辑.json文件
vim nginx-autoscaling.json
{
"id": "nginx-autoscaling”,
"container": {
"type": "DOCKER",
"docker": {
"image":"10.133.19.25:5000/library/nginx:latest",
"network": "BRIDGE",
"portMappings": [
{ "hostPort": 0,"containerPort": 80, "servicePort": 10002 }
],
"forcePullImage":true
}
},
"instances": 1,
"cpus": 0.1,
"mem": 65,
"healthChecks": [{
"protocol": "HTTP",
"path": "/",
"portIndex": 0,
"timeoutSeconds": 10,
"gracePeriodSeconds": 10,
"intervalSeconds": 2,
"maxConsecutiveFailures": 10
}],
"labels":{
"HAPROXY_GROUP":"external,internal"
}
}
• 2.启动app
dcosmarathon app add nginx-autoscaling.json
2.启动autoscaler,on 10.134.29.134:
• 1.获取源码
git clonehttps://github.com/mesosphere/marathon-autoscale.git
cdmarathon-autoscale
• 2.运行autoscale脚本
pythonmarathon-autoscale.py
Enter the DNS hostname or IP of yourMarathon Instance : 10.134.29.134
Enter the Marathon Application Name toConfigure Autoscale for from the Marathon UI : nginx-autoscaling
Enter the Max percent of Mem Usageaveraged across all Application Instances to trigger Autoscale (ie. 80) : 5
Enter the Max percent of CPU Usageaveraged across all Application Instances to trigger Autoscale (ie. 80) : 5
Enter which metric(s) to triggerAutoscale ('and', 'or') : or
Enter Autoscale multiplier for triggeredAutoscale (ie 1.5) : 2
Enter the Max instances that should everexist for this application (ie. 20) : 10
3.验证:
• 1.进入docker
docker exec-it fb700c6045ad /bin/bash
• 2.加压
ddif=/dev/zero of=test bs=1M count=1000
此时查看会发现多了instance
3. 基于服务请求频率的autoscaling
3.1原理
这个的原理是启动一个marathon-lb-autoscale的app来查看Haproxy监测到的对某一app的请求频率。请求频率超过阈值便自动扩展。详细了解请见:
https://docs.mesosphere.com/administration/admin-tutorials/autoscaling-with-marathon/marathon-app-for-rate-based-autoscaling/
https://mesosphere.com/blog/2015/12/13/service-discovery-and-load-balancing-with-dcos-and-marathon-lb-part-2/
3.2使用
1.创建测试扩展的nginx app
2.创建marathon-lb-autoscaleapp
on 10.134.29.141:
• 1.编辑app配置文件.json
vimmarathon-lb-autoscale.json
{
"id":"marathon-lb-autoscale",
"args":[
"--marathon","http://10.134.29.134:8080",
"--haproxy","http://marathon-lb.marathon.mesos:9090",
"--target-rps", "100",
"--apps", "nginx_10002"
],
"cpus": 0.1,
"mem": 16.0,
"instances": 1,
"container": {
"type": "DOCKER",
"docker": {
"image":"docker.io/mesosphere/marathon-lb-autoscale",
"network": "HOST",
"forcePullImage": true
}
}
}
• 2.启动app
dcosmarathon app add marathon-lb-autoscale.json
3.验证
先将siege镜像用docker pull下载并上传到私有仓库中:
docker pull yokogawa/siege
docker tag docker.io/yokogawa/siege:latest10.133.19.25:5000/siege:latest
docker push10.133.19.25:5000/siege:latest(如果上传不成功的话需要上传的主机的docker启动参数加上--insecure-registry=10.133.19.25:5000)
• 1.创建一个siege app用来发送http请求进行测试:
vim siege.json
{
"id": "siege",
"args":[
"-d1",
"-r1000",
"-c100",
"http://10.134.29.142:10000/"
],
"cpus": 0.5,
"mem": 16.0,
"instances": 1,
"container": {
"type": "DOCKER",
"volumes": [],
"docker": {
"image": "10.133.19.25:5000/siege:latest",
"network": "HOST",
"privileged": false,
"parameters": [],
"forcePullImage": false
}
}
}
dcos marathon app add siege.json
• 2.加大request数量:
dcos marathon app update /siege instances=15
此时查看会发现多了nginxinstance