Changing Servers - Redirect to new IP = No Downtime?
I am changing servers of my website. The IP of old server cannot be moved to the new one. To have no downtime I am planing to do the following, please someone confirm it will work:
- Setup the new server and listen on the new IP
- Old server redirect all traffic to the new IP
- Change DNS records to point to the new IP
My logic tells me that when I redirect to the new IP from my old box, the user will not see the domain name in the browser but will see the new IP. Is there a way to redirect to the new IP and send along the HOSTNAME with it so that the user will see the domain name in the browser?
Im doing this because the site is in constant use and simply changing DNS settings won't do as database won't be synced between the new and old servers during propagation.
Solution 1:
Here's the method that worked for me:
- Sync the files and databases with the new server.
- Perform a re-sync just before cut-off.
- Change the DNS to point to the new server.
- Forward the request coming to the old ip to the new server until DNS propagation completes.
Here's how I would do the step 4:
We will configure IPTables on a Linux server to redirect all the traffic coming on port 80, (which is the default web server port), to a server with the IP 122.164.34.240
. The first step is to set your Linux box to allow this kind of forwarding to take place. Open a terminal window, log in as root user and run the following command:
# echo 1 >/proc/sys/net/ipv4/ip_forward
The next step is to tell IPTables to redirect the traffic to the new server:
# iptables -t nat -A PREROUTING -p tcp --dport 80 -j DNAT --to-destination 122.164.34.240
Here’s where the IPTables magic happens. With the third and final step, we tell IPTables to rewrite the origin of connections to the new server’s port 80 to appear to come from the old server.
# iptables -t nat -A POSTROUTING -p tcp -d 122.164.34.240 --dport 80 -j MASQUERADE
The final step is required because if we don’t tell the web server of the new server that the connections are coming from the client machines, it would think that they are originating from the old server.
You may want to repeat this for the databases and email server port as well.
Solution 2:
You can also look into adding multiple A-records. For example, Google uses this, check their nslookup output:
Name: google.com
Addresses: 209.85.148.101
209.85.148.102
209.85.148.113
209.85.148.138
209.85.148.100
209.85.148.139
If you add multiple A-records to a domain, visitors will receive multiple IP's and try them in that order. If one fails, the client moves to the next one to try.
Setup the new IP as an extra A-record 24 hours up front, start new server, shutdown the old, remove IP.
Solution 3:
Another option is to use a VIP (virtual IP). So, your steps will be:
- Setup the new server and listen on the new IP.
- Add a VIP to the old server.
- Change DNS records to point to the VIP IP. Till now all traffic will still be sent to the old server but using the VIP.
- When ready to go, move the VIP to the new server.
- Optionally, you can change the DNS to the new server IP and remove the VIP (after some time) from DNS.