Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get instances IPs programatically in Google Compute Engine?

I need to connect to a replica set of mongodb in my app so I need to provide ip addresses of the mongo instances of the replica set in the code's config. The Ips are ephemeral so they might change in the future. How can I get the ips of the mongo instances programmatically so they can be deduced by the config without having to update the code every time an ip changes.

Or is the way to go to put the ips in the code and update manually?

I thought this was the way to do it: https://cloud.google.com/compute/docs/metadata#querying

But this url http://metadata.google.internal/computeMetadata/v1/ seems to not exist I can't query anything with it...

I could execute a shell command from the code with gcloud compute instances list and filter manually to get the ips, but I was wondering if there was some cleaner way.

My app would be running in an instance separate from the mongo instances.

like image 290
eloone Avatar asked Feb 24 '26 00:02

eloone


2 Answers

You mention your app is running in a separate instance, but you mention nothing about the network. So, my suggestion:

If your app instance(s) and mongo instance(s) are running in the same private network:

  • Notice that by default, GCE resolves all your instance names in your private network, to the respective private IP. For example, if you have some instances: instance-1, instance-2, instance-3, and they are all on the same network, then your app could simply connect to the names instead of the IP (this is the name you chose when creating the instance).
  • If any of your mongo instances reboots and changes its private IP, the name of your instance will keep resolving to the current (possibly different) private IP, so you don't have to worry.

If your app instance(s) and mongo instance(s) are NOT running in the same private network:

  • You can always make your public ephemeral IP, a static one. Just go to your VM Instance Details page, find the "External IP" section, click "edit", pick "New static IP address", and attach it to your server. They're free of charge, in case you mind, as long as they're being used.
  • Even though you may have static IPs in your mongo instances, I would anyway recommend you set up a domain pointing to those static IPs, so your app can just point to the domains. This is very useful for later in case you someday need to move your mongo servers, you can just set a different IP in the domain, and your app won't even notice. (Of course, after you migrate your mongo data...)

In both cases, you should always use names to configure the mongo replica set (not the IPs), for the same reason as above: if your instance reboots and changes its IP, your mongo RS won't work until you reconfigure it.

Hope it helps.

like image 100
maganap Avatar answered Feb 27 '26 01:02

maganap


Yes the best way is to use gcloud compute command.

You can only query metadata server from the instance and not from outside the Google Cloud Services.

You should consider a cron job inside your instances executing the following command returning the external IP and sending it to your application:

curl "http://metadata.google.internal/computeMetadata/v1/instance/network-interfaces/0/access-configs/0/external-ip" -H "Metadata-Flavor: Google"
like image 30
Paolo P. Avatar answered Feb 26 '26 23:02

Paolo P.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!