Thursday, August 18, 2011

How to ip-migrate web sites using Varnish

This is a quick guide on how to migrate a bunch of web sites by using Varnish as a intermediary to allow for low-latency zero downtime migration between ip addresses or domain names.

Requirements: Varnish 2.x up and running, access to apache config, access to dns config.

1) Configure Varnish

Start out by defining your existing web sites as backends in the varnish config (default.vcl) by adding them in this format to the beginning of your vcl:

backend website1{
// NO CACHING
// website1.company.com
.host = "10.0.0.1";
.port = "80";
.connect_timeout = 160s;
.first_byte_timeout = 55s;
.between_bytes_timeout = 25s;
}

backend website2{
...
}

backend website3{
...
}

Then add some clever host/backend switching logic to your vcl:

sub vcl_recv {
# Find out which backend to use for the request
if (req.http.Host == "website1.company.com"){
set req.backend = website1;
return(pass);
}
else if (req.http.Host == "website2.company.com"){
set req.backend = website2;
return(pass);
}
else {
# maybe switch to your default backend here if you're using varnish in production
}
.....


You're now ready to start using varnish as your intermediary frontend router. Varnish will PASS any requests to these sites and you are now free to change the IP:s of the sites without the lag of DNS. You remembered to load and use your varnish config too, right?

2) Alter DNS for sites
Update your dns records, all www pointers should be changed to the ip address of your Varnish server.

3) Migrate!
When the changes have propagated (and you've verified that it works as intended) you are free to change ip:s or whatever you need on your sites, Varnish will maintain the dnsname -> backend connection as long as you remember to update your backend config in vcl. And, changes are instant - allowing fast rollbacks if needed.

Wednesday, August 17, 2011

How to use bash to find all used IP:s in your apache configs (following the debian config model)

for i in `ls /etc/apache2/sites-enabled/*.vhost`; do echo "Working file:$i"; echo "Servername:";grep "ServerName" $i | cut -c 16-40; echo "Ipaddress:";grep "<VirtualHost" $i|cut -c 14-27; done