四、服务发现和haproxy
以下操作在三台服务上都有
1、安装haproxy
yum -y install haproxy
2、设置服务发现
mkdir -p /etc/marathon/conf
cp /etc/mesos/zk /etc/marathon/conf/master
cp /etc/marathon/conf/master /etc/marathon/conf/zk
sed -i 's|mesos|marathon|g' /etc/marathon/conf/zk
3、设置文件/etc/haproxy-marathon-bridge/marathons
mkdir /etc/haproxy-marathon-bridge/
[root@mesos-node1 conf]# cat /etc/haproxy-marathon-bridge/marathons
192.168.10.227:8080
192.168.10.228:8080
192.168.10.229:8080
[root@mesos-node1 conf]#
4、下载安装脚本
wget https://github.com/draculavlad/SetUpMesosphereOnCentOS7WithServiceDiscovery/blob/master/haproxy-marathon-bridge
5、添加执行权限
chmod +x /etc/init.d/haproxy-marathon-bridge
6、通过 haproxy-marathon-bridge脚本从Marathon生成一个HAProxy配置在localhost:8080运行:
./haproxy-marathon-bridge 192.168.10.227:8080 > /etc/haproxy/haproxy.cfg
7、将haproxy加入开机自启动并启动
systemctl start haproxy
systemctl enable haproxy
8、开启haproxy的定时任务,每一分钟加入一次任务。
/etc/init.d/haproxy-marathon-bridge install_cronjob
9、查看nginx.json文件
[root@mesos-node1 ~]# cat nginx.json
{
"id":"nginx",
"cmd": null,
"cpus":0.2,
"mem":32.0,
"instances": 1,
"constraints": [["hostname",
"UNIQUE",""]],
"container": {
"type":"DOCKER",
"docker": {
"image": "nginx",
"network": "BRIDGE",
"portMappings": [
{"containerPort": 80,
"hostPort": 0,"servicePort": 3030, "protocol": #此处我设置了固定的端口,这个端口可以按照要求来设定。
"tcp" }
]
}
}
}
[root@mesos-node1 ~]#
再查看/etc/haproxy/haproxy.cfg文件
[root@mesos-node1 ~]# cat /etc/haproxy/haproxy.cfg
global
log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
# turn on stats unix socket
stats socket /var/lib/haproxy/stats
defaults
mode tcp
log global
option dontlognull
option redispatch
retries 3
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout check 10s
maxconn 3000
frontend front-nginx-3030
bind *:3030
default_backend back-nginx-3030
backend back-nginx-3030
balance leastconn
server nginx-1 192.168.10.227:31894 check
[root@mesos-node1 ~]#
可以看到服务发现自动生成了这个访问端口,将它映射在随机生成的端口上,至此我们就可以用任意一台的IP地址来访问这个端口了。