How to get instances of IP addresses programmatically in Google Compute Engine?

I need to connect to the mongodb replica set in my application, so I need to provide the ip addresses of the replica set mongo instances in the code config. Ips are ephemeral, so they may change in the future. How can I get the ips of mongo instances programmatically so that they can be inferred via config without having to update the code every time the ip changes.

Or is this a way to go to put 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/ doesn't seem to 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 ips, but I was wondering if there is a cleaner way.

My application will run in an instance, separate from mongo instances.

+3


source to share


2 answers


Yes, the best way is to use the gcloud compute command.

You can only query the metadata server from the instance and not from outside of Google cloud services.



You should consider the cron job inside your instances by running 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"

      

+4


source


You mentioned that your application runs in a separate instance, but you don't mention anything about networking. So my suggestion:

If the app instance and mongo instance are running on the same private network:

  • Please note that by default the GCE will resolve all of your instance names on your private network to their corresponding private IP address. For example, if you have multiple instances: instance-1, instance-2, instance-3, and they are all on the same network , then your application can simply connect to names instead of an IP address (this is the name you chose when you created copy).
  • If any of your mongo instances reboots and changes their private IP, your instance name will continue to resolve to your current (possibly different) private IP, so you don't have to worry.

If your app instance and mongo instances are NOT running on the same private network:



  • You can always make your public ephemeral IP address static. Just go to the details page for your VM instance, find the "External IP" section, click "Change", select "New Static IP Address" and attach it to your server. They are free if you don't mind as long as they are used.
  • Even though you may have static IPs in your mongo instances, I would still recommend that you set up a domain pointing to those static IPs, so your application can just point to domains. This is very useful for later ones, if you ever need to move your mongo servers, you can just set a different IP on the domain and your application won't even notice. (Of course, after transferring your mango data ...)

In both cases, you should always use names to configure the mongo replica set (and not IP addresses) for the same reason as above: if your instance reboots and changes its IP address, your mongo RS will not work while you do it do not reconfigure.

Hope it helps.

+4


source







All Articles