利用Registrator和Consul实现mesos task的服务发现
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了利用Registrator和Consul实现mesos task的服务发现相关的知识,希望对你有一定的参考价值。
实现目的:
因为mesos中实际的工作节点是slave,框架marathon启动的任务(容器)都是在随机的slave上执行,所以在每台slave上启动Registrator,用来发现本机上的容器,它会把当前宿主机上的容器自动注册到consul.但是consul找一台salve启动就行,它会把自己选为leader,其他slave上启动Registrator的时候指定此leader就行
环境:
192.168.0.149 | Mesos-master、Zookeeper |
192.168.0.161 | Mesos-master、Zookeeper、Marathon |
192.168.0.174 | Mesos-master、Zookeeper |
192.168.0.239 | Mesos-slave、Consul-server、Registrator |
192.168.0.236 | Mesos-slave、Registrator |
部署:
定义IP
HOST_IP_1=192.168.0.149 HOST_IP_2=192.168.0.161 HOST_IP_3=192.168.0.174
192.168.0.149
启动Zookeeper
[[email protected] ~]# docker run -d --net="host" -e SERVER_ID=1 -e ADDITIONAL_ZOOKEEPER_1=server.1=${HOST_IP_1}:2888:3888 -e ADDITIONAL_ZOOKEEPER_2=server.2=${HOST_IP_2}:2888:3888 -e ADDITIONAL_ZOOKEEPER_3=server.3=${HOST_IP_3}:2888:3888 garland/zookeeper
启动Mesos-master
[[email protected] ~]# docker run --net="host" -p 5050:5050 -e "MESOS_HOSTNAME=${HOST_IP_1}" -e "MESOS_IP=${HOST_IP_1}" -e "MESOS_ZK=zk://${HOST_IP_1}:2181,${HOST_IP_2}:2181,${HOST_IP_3}:2181/mesos" -e "MESOS_PORT=5050" -e "MESOS_LOG_DIR=/var/log/mesos" -e "MESOS_QUORUM=2" -e "MESOS_REGISTRY=in_memory" -e "MESOS_WORK_DIR=/var/lib/mesos" -d garland/mesosphere-docker-mesos-master
192.168.0.161
启动Zookeeper
[[email protected] ~]# docker run -d --net="host" -e SERVER_ID=2 -e ADDITIONAL_ZOOKEEPER_1=server.1=${HOST_IP_1}:2888:3888 -e ADDITIONAL_ZOOKEEPER_2=server.2=${HOST_IP_2}:2888:3888 -e ADDITIONAL_ZOOKEEPER_3=server.3=${HOST_IP_3}:2888:3888 garland/zookeeper
启动Mesos-master
[[email protected] ~]# docker run --net="host" -p 5050:5050 -e "MESOS_HOSTNAME=${HOST_IP_2}" -e "MESOS_IP=${HOST_IP_2}" -e "MESOS_ZK=zk://${HOST_IP_1}:2181,${HOST_IP_2}:2181,${HOST_IP_3}:2181/mesos" -e "MESOS_PORT=5050" -e "MESOS_LOG_DIR=/var/log/mesos" -e "MESOS_QUORUM=2" -e "MESOS_REGISTRY=in_memory" -e "MESOS_WORK_DIR=/var/lib/mesos" -d garland/mesosphere-docker-mesos-master
启动Marathon
[[email protected] ~]# docker run -d -p 8080:8080 mesosphere/marathonv0.14.0 --master zk://${HOST_IP_1}:2181,${HOST_IP_2}:2181,${HOST_IP_3}:2181/mesos --zk zk://${HOST_IP_1}:2181,${HOST_IP_2}:2181,${HOST_IP_3}:2181/marathon
192.168.0.174
启动Zookeeper
[[email protected] ~]# docker run -d --net="host" -e SERVER_ID=3 -e ADDITIONAL_ZOOKEEPER_1=server.1=${HOST_IP_1}:2888:3888 -e ADDITIONAL_ZOOKEEPER_2=server.2=${HOST_IP_2}:2888:3888 -e ADDITIONAL_ZOOKEEPER_3=server.3=${HOST_IP_3}:2888:3888 garland/zookeeper
启动Mesos-master
[[email protected] ~]# docker run --net="host" -p 5050:5050 -e "MESOS_HOSTNAME=${HOST_IP_3}" -e "MESOS_IP=${HOST_IP_3}" -e "MESOS_ZK=zk://${HOST_IP_1}:2181,${HOST_IP_2}:2181,${HOST_IP_3}:2181/mesos" -e "MESOS_PORT=5050" -e "MESOS_LOG_DIR=/var/log/mesos" -e "MESOS_QUORUM=2" -e "MESOS_REGISTRY=in_memory" -e "MESOS_WORK_DIR=/var/lib/mesos" -d garland/mesosphere-docker-mesos-master
192.168.0.239
安装Mesos-slave
安装mesosphere源 [[email protected] ~]# rpm -ivh /mesosphere-el-repo-7-1.noarch.rpm 安装mesos [[email protected] ~]# yum -y install mesos 配置mesos [[email protected] ~]# echo zk://192.168.0.149:2181,192.168.0.161:2181,192.168.0.174:2181/mesos > /etc/mesos/zk [[email protected] ~]# echo 192.168.0.239 | sudo tee /etc/mesos-slave/hostname [[email protected] ~]# echo ‘docker,mesos‘ > /etc/mesos-slave/containerizers 启动mesos-slave [[email protected] ~]# systemctl start mesos-slave && systemctl enable mesos-slave [[email protected] ~]# systemctl disable mesos-master
启动Consul-server
[[email protected] ~]# docker run -d --name=consul --net=host gliderlabs/consul-server -bootstrap -bind=192.168.0.239
NOTE:一个节点启动就行,它会自己把自己选为leader
测试consul-server
[[email protected] ~]# curl 192.168.0.239:8500/v1/catalog/services {"consul":[]}
NOTE:也可以浏览器访问:http://192.168.0.239:8500/ui/
启动Registrator
[[email protected] ~]# docker run -d --name=registrator \ --net=host \ --volume=/var/run/docker.sock:/tmp/docker.sock \ gliderlabs/registrator:latest consul://192.168.0.239:8500
测试Registrator是否跟consul结合把服务自动注册到里面,启动一个redis容器测试
[[email protected] ~]# docker run -d -P --name=redis redis [[email protected] ~]# curl 192.168.0.239:8500/v1/catalog/services {"consul":[],"redis":[]}
consul现在有个服务叫做redis,我们能看到更多关于这个服务的,比如端口、IP
[[email protected] ~]# curl $(boot2docker ip):8500/v1/catalog/service/redis[{"Node":"boot2docker","Address":"10.0.2.15","ServiceID":"boot2docker:redis:6379","ServiceName":"redis","ServiceTags":null,"ServiceAddress":"","ServicePort":32768}]
所有slave的容器注册信息都可以在consul中看到
也可以用DNS API测试
[[email protected] ~]# dig @127.0.0.1 -p 8600 redis.service.consul ; <<>> DiG 9.9.4-RedHat-9.9.4-18.el7 <<>> @127.0.0.1 -p 8600 redis.service.consul ; (1 server found) ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 937 ;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0 ;; WARNING: recursion requested but not available ;; QUESTION SECTION: ;redis.service.consul. IN A ;; ANSWER SECTION: redis.service.consul. 0 IN A 192.168.0.239 ;; Query time: 1 msec ;; SERVER: 127.0.0.1#8600(127.0.0.1) ;; WHEN: Wed Mar 30 12:43:34 CST 2016 ;; MSG SIZE rcvd: 74
参考文章:
·https://github.com/sekka1/mesosphere-docker
·http://gliderlabs.com/registrator/latest/user/quickstart/
本文出自 “翟军铭的linux博客” 博客,请务必保留此出处http://zhaijunming5.blog.51cto.com/10668883/1758353
以上是关于利用Registrator和Consul实现mesos task的服务发现的主要内容,如果未能解决你的问题,请参考以下文章
Docker+Consul+Registrator实现服务自动注册与发现
使用Docker搭建consul集群+registrator实现服务自动注册。
第1266期基于Docker+Consul+Registrator+Nodejs实现服务治理
基于Consul+Registrator+Nginx实现容器服务自动发现的集群框架