未加星标

Discovering MongoDB withConsul

字体大小 | |
[数据库(综合) 所属分类 数据库(综合) | 发布者 店小二05 | 时间 2017 | 作者 红领巾 ] 0人收藏点击收藏

In order to build flexible and maintainable cloud infrastructure we need a way to connect services together with minimal effort. For a long time DNS has been the way to do that, but it also poses a different set of problems. How do you keep the DNS entries up to date? How do you make sure that the instances that are registered in DNS are actually healthy and able to servetraffic?

Consul from Hashicorp is a modern answer to these problems that is being widely adopted by teams of all sizes. It is a service discovery layer for your datacenter, as well as being a distributed key-value store and able to perform cross-datacenter replication and discovery. One of the wonderful things about it is the fact that the service discovery layer can be used via a built-in DNS interface. Services that are registered in Consul can have an associated health-check which will cause a failing instance to be removed from the list of DNS entries for that service. All-in-all it’s a pretty great system that is definitely worth considering if you haven’t already started using ityourself.

TheProblem

While re-architecting an ailing environment so that it would be easier to maintain and scale I decided to use Consul as the local DNS provider. Everything was going well until I started trying to access a MongoDB replica set through the DNS records that were being retrieved through Consul. For some reason I kept getting errors in my client logs stating that the connection to MongoDB was failing because the instance being connected to wasn’t themaster.

To try to remedy the failure I added the replica set configuration and the read preference to the connection parameters in the hopes that it would allow the client to properly discover the master node. No matter how I tweaked the parameters and checked and double checked the DNS records I couldn’t quite figure out where the problem was. After digging deeper and deeper into the problem I finally realized why I was having theissue.

Because the clients were using a connection of the form mongodb://mongodb.service.consul:27017/database they were just doing round-robin DNS lookups of the MongoDB servers. Sometimes they would win the game of roulette and reach a master node but other times they would hit a secondary node instead. Even if you have the read preference set to something like secondaryPreferred the client driver still needs to connect to the master first in order to be able to properly enumerate the replica set. Because of the intermittent nature of the failures the way to solve the problem wasn’t apparent atfirst.

TheSolution

Once I fully understood what the failure was, the solution became obvious. Since you can register arbitrary service names in Consul and associate a variety of different health checks, all I had to do was set one up that would only point to the node that was currently the master. In order to make that work I wrote a scripted health check that would run the following script every 10seconds:

#!/bin/bash ISMASTER=$(/usr/bin/mongo --quiet --eval 'db.isMaster().ismaster') if [ "$ISMASTER" = "true" ] then exit 0 else exit 2 fi

I then configured the Consul agent on the MongoDB instances with this servicedefinition:

{ "services": [ { "check": { "interval": "10s", "script": "/consul/scripts/mongo_is_master.sh" }, "name": "mongodb-master", "port": 27017, "tags": [ "mongodb", "master" ] } ] }

Whichever instance is passing that check will be registered as the mongodb-master.service.consul DNS record in Consul. That means that the clients just need to be configured to connect to mongodb://mongodb-master.service.consul:27017/database?replicaSet=rs0&readPreference=primaryPreferred and everyone will behappy.

Conclusion

This was an irritating problem that finally yielded to extended debugging. Hopefully I have been able to save someone else the pain of discovery by writing this post. If that someone is you then leave a comment and let me know. Thanks forreading!

本文数据库(综合)相关术语:系统安全软件

主题: MongoDB
分页:12
转载请注明
本文标题:Discovering MongoDB withConsul
本站链接:http://www.codesec.net/view/530627.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 数据库(综合) | 评论(0) | 阅读(30)