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.
You mention your app is running in a separate instance, but you mention nothing about the network. So, my suggestion:
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.
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With