Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a web sever that connects to an internal database through a VPN. There are 2 IPs (primary and secondary) to the database from the web server. How can I setup my /etc/hosts file so that if the primary IP is not available then the secondary IP will be used?

Would this work for my hosts file?

141.131.286.1   abc.efg.datastore.com   #primary

141.131.286.237 abc.efg.datastore.com   #secondary
share|improve this question
    
add comment

2 Answers 2

The hosts file does not provide such mechanism. If you list two IPs for the same name, only the first one will be used. So, there is no such thing primary and secondary.

Also, the hosts file does not handle URLs. It just handles names like the oncs provided in the question. A URL contains complete path and protocol such as http//host/path/to/resource.

share|improve this answer
    
Edited title and naming in OPs question to eliminate the confusion about URLs/hosts. –  dmourati Jan 30 '13 at 17:56
add comment

You can't provide resilience or round robin load balancing via the /etc/hosts file - it is not designed for that purpose.

Instead, your options are ... (in no particular order)

  1. Configure your network properly, so that routes change when a link is dropped
  2. Use DNS round-robin load balancing (not A Good Idea TM) using a managed service (eg. loaddns.com or dnsmadeeasy.com etc.)
  3. Use a local L3 load balancer for the outbound traffic (HAProxy?) with the back-ends defined as necessary
  4. Build the resilience into your web application itself
share|improve this answer
    
DNS round-robin load balancing isn't usually resilient. One is selected and others are not tried. –  anttiR Sep 19 '12 at 17:09
    
Another option could be to use netcat or another software to forward the connection to an IP. Then change the forward if one IP is lost. –  anttiR Sep 19 '12 at 17:11
1  
@anttiR DNS RR alone has no resilience, but used via a managed DNS service provider it does. I've edited my answers and given some examples to be clearer. –  sonassi Sep 19 '12 at 17:19
    
I doubt it would work great with database. They have the tendency to fetch one IP and stick to it. An internet website on the other hand would work great. –  anttiR Sep 19 '12 at 17:30
    
That would depend on the resolver of the host machine. If the DNS resolver is set to be a non-caching service - or polls the DNS registrars DB directly, then it would work. But like I said, its not a good idea, its just an idea. –  sonassi Sep 19 '12 at 17:38
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.