Skip to main content

How to find the public IP address of each host in an Atlas deployment

Venkat Cherukuri avatar
Written by Venkat Cherukuri
Updated this week

Background

In certain situations, it may become necessary to know the IP address of each host in an Atlas deployment. This article outlines several approaches to obtaining these details.

If network traffic will be explicitly limited to and from a deployment using IP addresses, then it is important to understand circumstances in which the IP addresses in an Atlas deployment change. These circumstances are maintained in our Networking FAQ documentation.

Method 1: Query the DNS

Before obtaining the IP address of each node, their hostnames must first be determined. There are a few methods of doing this, but the easiest is to use DNS to query the cluster hostname SRV record.

Use DNS to query the cluster hostname SRV record

To retrieve the list of hostnames using DNS, query the SRV record of the cluster hostname. This can be done using any DNS querying tool such as nslookup or dig. The service and protocol prefix of _mongodb._tcp. must also be included when querying the SRV record.

As an example, given the cluster hostname of examplecluster.a01bc3.mongodb.net, a DNS query would need to lookup the SRV record for _mongodb._tcp.examplecluster.a01bc3.mongodb.net:

% dig _mongodb._tcp.examplecluster.a01bc3.mongodb.net srv ; <<>> DiG 9.10.6 <<>> _mongodb._tcp.examplecluster.a01bc3.mongodb.net srv ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 301 ;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 1 ;; OPT PSEUDOSECTION: ; EDNS: version: 0, flags:; udp: 1232 ;; QUESTION SECTION: ;_mongodb._tcp.examplecluster.a01bc3.mongodb.net. IN SRV ;; ANSWER SECTION: _mongodb._tcp.examplecluster.a01bc3.mongodb.net. 60 IN SRV 0 0 27017 examplecluster-shard-00-00.a01bc3.mongodb.net. _mongodb._tcp.examplecluster.a01bc3.mongodb.net. 60 IN SRV 0 0 27017 examplecluster-shard-00-01.a01bc3.mongodb.net. _mongodb._tcp.examplecluster.a01bc3.mongodb.net. 60 IN SRV 0 0 27017 examplecluster-shard-00-02.a01bc3.mongodb.net.

This query returned three hostnames for this record:

  • examplecluster-shard-00-00.a01bc3.mongodb.net

  • examplecluster-shard-00-01.a01bc3.mongodb.net

  • examplecluster-shard-00-02.a01bc3.mongodb.net

List all hostnames

  1. The Atlas UI: A list of all hosts can be retrieved in the Atlas UI by viewing the Overview or Metrics tab of a deployment. Alternatively, these individual hostnames can also be found by navigating to the Connect modal for the deployment, selecting Standard Connection and the oldest version of the Node.js driver from the driver selection drop down. This displays a standard mongodb:// connection string that contains each hostname individually rather than a DNS-seeded cluster hostname.

  2. The Atlas Administration API: A list of all hosts in a deployment can also be retrieved programmatically with the Administration API using the Return All Clusters in One Project endpoint. This endpoint returns a results array of all deployments, each of which contains the property connectionStrings.standard with the standard mongodb:// connection string that contains the hostname of each host.

Now that a list of hostnames for each host has been determined, you can use a ping command to determine the IP address of each. Optionally, you can use your preferred DNS querying tool from the last step to query the A record of each hostname.

% ping -c 3 examplecluster-shard-00-00.a01bc3.mongodb.net PING ec2-XX-XX-XX-XX.us-east-2.compute.amazonaws.com (XX.XX.XX.XX): 56 data bytes 64 bytes from XX.XX.XX.XX: icmp_seq=0 ttl=231 time=43.230 ms 64 bytes from XX.XX.XX.XX: icmp_seq=1 ttl=231 time=43.295 ms 64 bytes from XX.XX.XX.XX: icmp_seq=2 ttl=231 time=38.990 ms --- ec2-XX-XX-XX-XX.us-east-2.compute.amazonaws.com ping statistics --- 3 packets transmitted, 3 packets received, 0.0% packet loss round-trip min/avg/max/stddev = 38.990/41.838/43.295/2.014 ms

This shows that the IP address for host examplecluster-shard-00-00.a01bc3.mongodb.net is XX.XX.XX.XX. Repeat this step for each of the hostnames.

Method 2: Use an Atlas Admin API endpoint

You can use the Return All IP Addresses for One Project API endpoint to retrieve the IP addresses for all cluster nodes within a project. An API key pair with sufficient project permissions is required (the Project Read Only role at the very least).

The API response will look similar to the following:

{ "groupId": "123a4b5c6d78e910fg1h2i34", "services": { "clusters": [ { "clusterName": "myAtlasCluster", "futureInbound": [], "futureOutbound": [], "inbound": [ "40.177.XXX.XX", "40.176.XXX.XXX", "40.177.XX.XXX" ], "outbound": [ "40.177.XXX.XX", "40.176.XXX.XXX", "40.177.XX.XXX" ] } ] } }
Did this answer your question?