通过Mesos默认调度框架Marathon创建简单使用样例

创建Marathon任务有2种方法:通过web-ui和使用json脚本的方法,下面就通过简单实例进行说明。

用webui创建一个top任务(非docker)

打开marathon-web-ui界面

  1. http://你安装mesos系统的机器ip地址:8080
创建新任务(点击+NewApp)

marathon1

填写本次任务的数据信息
  1. #任务名(很重要,参考后面nginx例子)
  2. ID dataman-top-test
  3. #任务需要使用cpu最小大小
  4. CPUs 0.1
  5. #任务需要使用内存最小容量
  6. Memory 16
  7. #任务需要使用磁盘大小
  8. Disk 0
  9. #需要同时跑几个任务
  10. Instances 1
  11. #执行器执行命令
  12. Command top -b
  13. #执行器
  14. Executor
  15. #容器调度Slave端口方法
  16. Ports
  17. #通过wget模式将容器外部资源动态的获取到容器内部的work_dir中
  18. URIs
  19. #约束
  20. Constraints
执行创建任务(点击+Create)

marathon2

marathon创建过程

marathon3

  1. 可以看到marathon的任务表中显示的任务状态
  2. 任务id(/dataman-top-test)
  3. 内存信息(16)
  4. cpu信息(0.1)
  5. 运行实例信息(0/1)
  6. 健康心跳状态(空)
  7. 状态(Deploying)
创建结束查看结果

marathon4

  1. 和创建过程信息一样,但是已经可以看到运行实力信息(1/1),状态是(Running)
进入单一任务详细状态

marathon5

  1. 点击任务名进入单一任务详细操作界面
  2. Suspend 将任务设置为空
  3. Scale 动态设置任务数量
  4. Refresh 刷新
  5. Restart App 重启任务
  6. Destroy App 删除任务
  7. version 任务已创建时间
  8. Updated 最新任务操作时间
查看任务数据信息

marathon6 点击Configuration任务创建的数据信息,同4.1.3类似

打开mesos-master

  1. http://你安装mesos系统的机器ip地址:5050
mesos-master总览

marathon7

mesos-master信息
  1. 左上角可以看到mesos-master信息,包括集群名、masterip、创建时间、集群启动时间等
LOG
  1. LOG可以查看mesos-master的实时运行日志
Slaves
  1. Activated #集群中存活的从机数量
  2. Deactivated #集群中死亡的从机数量
Task
  1. #这里说明这个统计是从Mesos启动后的累加值,并不是当前状态,仅供参考
  2. Staged #创建过任务的数量
  3. Started #正在开始任务的数量
  4. Finished #任务正常完成的数量
  5. Killed #任务手动取消的数量
  6. Failed #任务执行失败的数量
  7. Lost #任务丢失的数量
Resources
  1. #统计集群cpu和内存资源情况
  2. Total 总资源
  3. Used 使用资源
  4. Offered 申请资源
  5. Idle 空闲资源
Active Tasks
  1. #正在运行的任务统计
  2. 这里可以看到刚才创建的任务
Sandbox

marathon8

  1. #这里可以查看任务运行内部的动态日志,包括正确和错误的

stderr

  1. #日志样例
  2. I0701 14:35:19.243409 5881 exec.cpp:132] Version: 0.22.1
  3. I0701 14:35:19.246486 5883 exec.cpp:206] Executor registered on slave 20150701-140046-33620746-5050-5032-S0

stdout

  1. #日志样例
  2. 5508 root 20 0 4440 636 536 S 0.0 0.0 0:00.00 sh
  3. 5509 root 20 0 125208 9348 4932 S 0.0 0.2 0:00.06 docker
  4. 5564 root 20 0 141600 10904 4408 S 0.0 0.3 0:00.06 docker
  5. 5585 root 20 0 4440 652 548 S 0.0 0.0 0:00.01 sh
  6. 5591 root 20 0 85876 4056 2952 S 0.0 0.1 0:00.00 nginx
  7. 5592 www-data 20 0 86216 2016 604 S 0.0 0.0 0:00.09 nginx
  8. 5593 www-data 20 0 86216 1760 460 S 0.0 0.0 0:00.20 nginx
  9. 5594 www-data 20 0 86216 1760 460 S 0.0 0.0 0:00.17 nginx
  10. 5595 www-data 20 0 86216 1760 460 S 0.0 0.0 0:00.17 nginx
  11. 5596 root 20 0 4440 648 544 S 0.0 0.0 0:00.00 sh
  12. 5597 root 20 0 4440 644 544 S 0.0 0.0 0:00.00 sh
  13. 5598 root 20 0 736344 11648 10076 S 0.0 0.3 0:05.34 mesos-exec+
  14. ........
Completed Tasks
  1. #集群启动后完成的任务(不一定是成功,也有失败等状态)

用json脚本开放一个nginx网络服务

首先创建一个Nginx的dockerfile

  1. vi dockerfile
  2. FROM ubuntu
  3. MAINTAINER zpang zpang@dataman-inc.com
  4. #install nginx
  5. RUN apt-get update
  6. RUN apt-get install -y nginx
  7. # forward request and error logs to docker log collector
  8. RUN ln -sf /dev/stdout /var/log/nginx/access.log
  9. RUN ln -sf /dev/stderr /var/log/nginx/error.log
  10. #off nginx daemon
  11. RUN echo "daemon off;" >> /etc/nginx/nginx.conf

使用dockerfile生成docker镜像

  1. #生成docker
  2. docker build -t ubuntu-nginx-base .
  3. #查看镜像
  4. docker images
  5. REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
  6. ubuntu-nginx-base latest c5dc79088bb8 41 hours ago 227.5 MB

生成Json启动脚本

  1. vi dataman-nginx-test.sh
  2. curl -v -X POST http://127.0.0.1:8080/v2/apps -H Content-Type:application/json -d \
  3. '{
  4. "id": "dataman-nginx-test",
  5. "cmd": "nginx",
  6. "cpus": 0.1,
  7. "mem": 128.0,
  8. "instances": 5,
  9. "container": {
  10. "type": "DOCKER",
  11. "docker": {
  12. "image": "ubuntu-nginx-base",
  13. "network": "BRIDGE",
  14. "portMappings": [
  15. { "containerPort": 80, "hostPort": 0, "servicePort": 10000, "protocol": "tcp" }
  16. ]
  17. }
  18. },
  19. "healthChecks": [
  20. { "protocol": "HTTP",
  21. "portIndex": 0,
  22. "path": "/",
  23. "gracePeriodSeconds": 5,
  24. "intervalSeconds": 20,
  25. "maxConsecutiveFailures": 3 }
  26. ]
  27. }'

参数说明:

  • http://127.0.0.1:8080/v2/apps #Marathon地址
  • id #任务名
  • cmd #启动命令
  • cpus #划分cpu资源
  • mem #划分内存资源
  • instances #实际运行任务总数量
  • container #容器数据
    • type #容器类型
    • image #容器镜像名
    • network #容器网络模式
    • protMappings #容器端口设置
      • containerPort #容器内部服务端口
      • hostPort #容器映射到主机端口
      • servicePort #一个辅助端口,用来做服务发现
      • protocol #容器网络支持协议
  • healthChecks #心跳检查设置
    • protocol #检查协议
    • portIndex #检查公共端口对应的服务,比如haproxy转发服务端口为80和443,第一个80对应的索引就是0,第二个443对应的索引就是1
    • path #检查地址
    • gracePeriodSeconds #一次健康检查以后,marathon认为服务健康不检查的时间段
    • intervalSeconds #检查间隔时间
    • maxConsecutiveFailures #失败检查重试次数,过次数后认为不可用

运行脚本生成任务

  1. sh dataman-nginx-test.sh
  2. #执行结果
  3. * Hostname was NOT found in DNS cache
  4. * Trying 127.0.0.1...
  5. * Connected to 127.0.0.1 (127.0.0.1) port 8080 (#0)
  6. > POST /v2/apps HTTP/1.1
  7. > User-Agent: curl/7.35.0
  8. > Host: 127.0.0.1:8080
  9. > Accept: */*
  10. > Content-Type:application/json
  11. > Content-Length: 1041
  12. > Expect: 100-continue
  13. >
  14. < HTTP/1.1 100 Continue
  15. < HTTP/1.1 201 Created
  16. < Cache-Control: no-cache, no-store, must-revalidate
  17. < Pragma: no-cache
  18. < Expires: 0
  19. < Location: http://127.0.0.1:8080/v2/apps/dataman-nginx-test
  20. < Content-Type: application/json
  21. < Transfer-Encoding: chunked
  22. * Server Jetty(8.y.z-SNAPSHOT) is not blacklisted
  23. < Server: Jetty(8.y.z-SNAPSHOT)
  24. <
  25. * Connection #0 to host 127.0.0.1 left intact
  26. {"id":"/dataman-nginx-test","cmd":"nginx","args":null,"user":null,"env":{},"instances":5,"cpus":0.1,"mem":128.0,"disk":0.0,"executor":"","constraints":[],"uris":[],"storeUrls":[],"ports":[0],"requirePorts":false,"backoffFactor":1.15,"container":{"type":"DOCKER","volumes":[],"docker":{"image":"ubuntu-nginx-base","network":"BRIDGE","portMappings":[{"containerPort":80,"hostPort":0,"servicePort":10000,"protocol":"tcp"}],"privileged":false,"parameters":[],"forcePullImage":false}},"healthChecks":[{"path":"/","protocol":"HTTP","portIndex":0,"command":null,"gracePeriodSeconds":5,"intervalSeconds":20,"timeoutSeconds":20,"maxConsecutiveFailures":3,"ignoreHttp1xx":false}],"dependencies":[],"upgradeStrategy":{"minimumHealthCapacity":1.0,"maximumOverCapacity":1.0},"labels":{},"version":"2015-07-01T10:37:19.979Z","deployments":[{"id":"ec0ccd2e-c5d9-4b07-87c9-e61cd411cdcd"}],"tasks":[],"tasksStaged":0,"tasksRunning":0,"tasksHealthy":0,"tasksUnhealthy":0,"backoffSeconds":1,"maxLaunchDelaySeconds":3600}

检查

nginx-1

  1. #这里需要注意的是因为配置了心跳监控,所以心跳监控的变成绿色了

检查容器nginx网络服务

nginx-2

  1. #点击这里会可以直接跳到nginx服务界面,说明服务正常

bamboo设置

bamboo主界面
  1. 进入bambooweb界面http://测试主机ip:8000/

bamboo1

添加bamboo规则转发nginx
  1. 转发规则默认2种:目录转发和域名转发,本次测试使用目录格式,需要将nginxweb服务端口转发到haproxy 80端口的根目录。

bamboo2

直接访问主机80端口
  1. 到游览器访问http://测试主机ip

bamboo3

参考文档

Mesos数人企业版