In Windows System, there is this file at C:\WINDOWS\system32\drivers\etc\hosts
. This file allows us to default a specific IP address to a host name.
The issue now is whether I can set multiple IP addresses to a host name. For example, can I do something like this:
192.168.244.128 gateway.net
192.168.226.129 gateway.net
And expect that the browser can resolve to both of them, see which one will work and thus point at that one?
If not, is there any other way to get the behavior I want?
Note: I am deploying this app in my own local area network, so there is no need for internet.
Normally you would not uses hosts to do this, but your DNS. Most DNS will provide what's called a "Round Robin" if you assign multiple A records to the one name in the zone.
What it would do then, is the first request comes through would receive
192.168.244.128
, the next would receive192.168.226.129
, so on and so forth. However, by design, your local machine will cache its DNS resolution, and will usually use the same IP address over and over, until it expires (Time To Live, TTL).Yes, you can do this, I've used it to test round-robin DNS scenarios without having to actually enter the hosts in a DNS.
When an application calls gethostbyname, it gets back the full list of IP addresses (possibly in random order - depending on the libraries / OS).
I think you are going about this the wrong way. Let me know if I'm making the wrong assumptions here.
Scenario:
What your trying to do is called round-robin DNS (aka poor mans load balancing) , but your trying to do this from the client side. This does not seem to have the desired effect (at least on my Windows XP box) even if i flush the DNS cache. Windows will only resolve to the first IP in the file. Nonetheless round-robin DNS is not fault tolerant, so this won't help you achieve what you want.
Suggested solution:
Hardware load balancer: Some brand names are Alteon, Big-IP, Barracuda. What this does is basically present a single IP for your users to connect to & it forwards the requests to the web servers. If one of the servers becomes unavailable, then it will no longer forward traffic to it. This is the expensive option.
Network Load Balancing Services: This is a Microsoft technology available on windows server, which will give you a single clustered IP. It achieves the same result as a hardware load balancer, but in a different way. All you need to do is configure it.
From http://www.unc.edu/atn/lsf/docs/7.0.5/lsf_config_ref/index.htm?hosts.5.html~main
IPv4 Example
In this example, hostA has 2 IP addresses and 3 aliases. The alias hostB specifies the first address, and the aliases hostC and host-C specify the second address. LSF uses the official host name, hostA, to identify that both IP addresses belong to the same host.
My suggestion is to use an internal DNS server with DNS round-robin and TTL=0. If you update the DNS record (also with automatic ip checking system) when an IP/server is down, you can have an high-availability system.
I have done this on a home network where I assign static IPs to both the wired and wireless network interfaces of a laptop, and from another machine's hosts file point a single hostname to both those ip addresses. It seems to work fine.
Based on this example I did the following 10.18.y.x 192.168.z.x hostA
Where hostA is the hostname of a server I intend to reach from both internal network (192.168.y.x) and VPN (10.8.z.x).
So it works well and I'm able to get samba working through VNP using hostname so it's fine for me to have my connected drives in windows in both case (LAN or VPN connected).
Best regards.
@ Plamen Dimitrov You'll need a device to handle that kind of balance, possibly a switch that can handle BGP in front of your FW- or use your FW if it's capable. If your Ciscos handle BGP look into implementing that. This way you can have both of your ISPs IP going into the Cisco/or switch and the target servers would have valid IPs of 24-bit public IPs (DIFFERENT FROM THE 2 ISP IPs). At that point (you're using 3 different public IP blocks, the one for your servers MUST BE 24 BIT), you'd need to have each of your ISPs know about this solution and they'd have to be willing to support the BGP solution between them, which most will do. Now, when your FQDN resolves, it will resolve to your 24 bit block of IPs, even if 1 ISP lines goes down. The goal is, your 24-bit addresses will always be available due to your ISPs agreeing to route that 24bit address through their lines. This works for VPN too since all you're using for VPN are the 24bit addresses, not either of the IPs from the ISP that you plugged into your cisco/or switch. At that point you have to be wary of BGP FLAPPING where if your lines go up and down a lot, due to a line getting DOS, the DNS addresses will change so much, on the internet, that the DNS servers will AUTOMATICALLY REMOVE THE IPs THAT LEAD TO YOUR 24 BIT BLOCK OF IPs. That's a DOS attack on BGP solutions.
Slight clarification here, at least in the Windows world: You CAN have two IP addresses for a single name in a hosts file. When queried, all addresses are returned. The app (i.e. browser) will attempt to connect to each in turn until a connection is made. IOW, it will try all addresses before timing out. (This is a common misunderstanding as many people believe it will only try the "first" address.)
You can verify this with the following experiment:
Add two or more addresses for a host in the hosts file, one real and the others fake. (Make the fake addresses smaller alphabetically.) Open a browser and try to connect to the hostname. Run netstat -no at the same time. You should see connection attempts to the fake address. (stuck in SYN_SENT) The browser will eventually get to the good address and will connect. There is a delay in connecting, but it WILL work.
You can't do this with the hosts file.
You can't do this with DNS either: you can serve multiple IPs for a single name, but the browser will pick just one of those multiple IPs, try it, and if this host is currently down, the browser will display a connection error.
One possible solution is to set up a proxy server and configure these two IPs as the parents for the domain they serve. At least in the case of Squid, the proxy will try one server and, if it fails, try the second server. Then configure your browser to use this proxy server.