案例:用marathon-lb 绑定tomcat端口,并自动服务发现与负载均衡。
部署marathon-lb
在marathon上部署marathon-lb
访问marathon ui 地址: http://192.168.22.191:8080
创建应用-选择json模式-拷贝下面的内容
{
"id": "/marathon-lb",
"cmd": "apt-get install curl -y && /marathon-lb/run sse --marathon http://192.168.22.191:8080 --group external",
#设置HAPROXY_GROUP标签为 external
"cpus": 1,
"mem": 128,
"disk": 0,
"instances": 1,
"constraints": [
[
"hostname",
"LIKE",
"192.168.22.192" #指定部署的节点
]
],
"acceptedResourceRoles": [
"*"
],
"container": {
"type": "DOCKER",
"docker": {
"forcePullImage": false,
"image": "docker.io/mesosphere/marathon-lb",
#需执行docker pull mesosphere/marathon-lb命令下载镜像
"parameters": [],
"privileged": true
},
"volumes": []
},
"portDefinitions": [
{
"port": 10001,
"name": "default",
"protocol": "tcp"
}
]
}
部署成功的状态
部署tomcat
marathon上部署tomcat
json内容:
{
"id": "/test-tomcat",
"cmd": null,
"cpus": 0.5,
"mem": 128,
"disk": 0,
"instances": 2,
#启动容器的数量
"acceptedResourceRoles": [
"*"
],
"container": {
"type": "DOCKER",
"docker": {
"forcePullImage": false,
"image": "tomcat:8.5.32",
#提前执行docker pull tomcat:8.5.32命令下载镜像
"parameters": [],
"privileged": false
},
"volumes": [
{
"containerPath": "/usr/local/tomcat/logs", #容器里的目录
"hostPath": "/opt/test-tomcat", #主机的目录
"mode": "RW" #权限
#日志目录映射
}
],
"portMappings": [
{
"containerPort": 8080, #容器里的tomcat端口
"hostPort": 0, #主机的端口0表示随机
"labels": {},
"protocol": "tcp",
"servicePort": 10001 #服务端口
}
]
},
"healthChecks": [
{
"gracePeriodSeconds": 300,
"ignoreHttp1xx": false,
"intervalSeconds": 10,
"maxConsecutiveFailures": 3,
"path": "/",
"portIndex": 0,
"protocol": "HTTP",
"ipProtocol": "IPv4",
"timeoutSeconds": 20,
"delaySeconds": 15
}
],
"labels": {
"HAPROXY_GROUP": "external",
#设置HAPROXY_GROUP标签,需与marathon-lb一致
"HAPROXY_0_VHOST": "tomcat.marathon.mesos",
"HAPROXY_DEPLOYMENT_ALT_PORT": "10002"
},
"networks": [
{
"mode": "container/bridge"
}
],
"portDefinitions": []
}
部署成功后的状态
分别访问tomcat地址验证
http://192.168.22.193:27643
http://192.168.22.193:24308
查看marathon-lb状态
访问marathon-lb获取HAProxy的统计信息 http://192.168.22.192:9090/haproxy?stats
访问marathon-lb负载后的地址
http://192.168.22.192:10001
重启tomcat验证marathon-lb服务发现
重启tomcat后再访问以下两个地址观察
http://192.168.22.192:9090/haproxy?stats
http://192.168.22.192:10001