Once a geek, forever a geek…

October 19, 2011
by Marius Voila
2 Comments

Data replication using lsyncd

One of my favorite Linux utilities I’ve discovered recently is lsyncd, a live syncing (mirror) daemon. Following the traditional Unix philosophy, it does data replication simply and it does it very well. Using some fancy inotify magic, lsyncd will spawn one or more processes to synchronize the targets after changes have been made.

After determining that a client would need multiple web-servers running in sync, I evaluated a few different tools that perform data replication and live syncing. This is one of the larger problems I’ve encountered with scaling web applications horizontally — storage management among multiple web services. In traditional deployments, it might make sense to use something like NFS or even DRBD, operating on the block device level. While this works for write-heavy systems under high load, it isn’t practical mirroring a whole block device for a high-traffic WordPress site — I just needed a basic client-server model.

This is where lsyncd really shines. The lsyncd configuration file is written in Lua and super easy to set up. Below is the lsyncd.conf that duplicates /var/www/mariusv on the master server to the 4 targets with the same path:

settings = {
   delay        = 1,
   maxProcesses = 5,
   statusFile   = "/tmp/lsyncd.status",
   logfile      = "/var/log/lsyncd.log",
}
 
targetlist = {
 "10.0.1.10:/var/www/mariusv",
 "10.0.1.11:/var/www/mariusv",
 "10.0.1.12:/var/www/mariusv",
 "10.0.1.13:/var/www/mariusv"
}
 
for _, server in ipairs(targetlist) do
  sync{ default.rsync,
    source="/var/www/mariusv",
    rsyncOpts="-rltvupgo",
    target=server
  }
end

To make the rsyncing seamless, you’ll need to send the public key generated on the master server to all of the duplicated nodes for easy access. With the client I was working with, we were also using Nginx to load balance across the web nodes. Since we only wanted writes going to the primary master server, we redirected all requests to /wp-admin to the primary node. Then, any changes made through the WordPress backend were synced over to the targets in less than 1 second. Here is the Nginx configuration:

upstream backend  {
  server 10.0.1.10; #master
  server 10.0.1.11; #www-01
  server 10.0.1.12; #www-02
  server 10.0.1.13; #www-03
}
 
upstream admin {
  server 10.0.1.10;
}
 
server {
  server_name www.mariusv.com mariusv.com;
 
  location / {
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass  http://backend;
  }
 
  location ~ /wp-admin/* {
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass  http://admin;
  }
}

You’ll want to install lsyncd from source on most distributions. Older versions (pre 2.x) contained gross XML style configuration; the most recent version, as of 8/30/2011, is 2.0.5.

October 13, 2011
by Marius Voila
1 Comment

Python Interface for AWS Route53

After taking a quick look at Boto’s Route53 interface, I realized that it was just way too cumbersome and awkward to be useful. I decided to make it a little more user-friendly, so without further ado, I introduce Area53. Now you can write code like the following:

>>> from area53 import route53
 
>>> # Creates the zone, example.com.
... zone = route53.create_zone('example.com')                 
 
>>> # Adds A record to the zone.
... zone.add_a('example.com', '182.12.142.12')               
 
>>> # Adds CNAME record to the zone.  
... zone.add_cname('www.example.com', 'example.com')         
 
>>> # Adds MX records to the zone.
... zone.add_mx(['10 mx1.example.com', '20 mx2.example.com'])

Now what just happended.

On line two we create the zone. Then we create an A record for the naked domain, followed by a cname for the ‘www’ subdomain which points back to the naked domain. Then we also added two MX records for our mail exchangers. Also, you don’t need to worry about trailing dots and fully qualified domain names, because that is handled automatically.

By the way, you can go grab the code on github anytime you want. There is a little bit more information there about how to install and dependencies, but it is pretty simple.

Now, we can grab a list of all zones like this:

>>>  route53.get_zones() # Get all hosted zones.
[<Zone:example.com.>, <Zone:mariusv.com.>]

Or we can grab our individual zone by name:

>>> for record in zone.get_records():
...     print record
...
<Record:A:example.com.:[u'182.12.142.12']>
<Record:CNAME:www.example.com.:['example.com.']>
<Record:MX:example.com.:[u'10 mx1.example.com.', u'20 mx2.ex'...>
<Record:NS:example.com.:[u'ns-1249.awsdns-28.org.', u'ns-902'...>
<Record:SOA:example.com.[u'ns-1249.awsdns-28.org. awsdns-hos'...>

And here is how you grab just the nameservers for your zone:

>>> zone.get_nameservers() # Get nameservers for the zone.
[u'ns-1249.awsdns-28.org.', u'ns-902.awsdns-48.net.']

You add the nameservers to your domain registrar to actually switch to Amazon’s DNS service. I would recommend migrating a non-critical domain to start to make sure that you understand the process before moving your big domain with hundreds of records over to Route53.

Now we can remove all these records and the zone itself like so:

>>> # Get the zone.
... zone = route53.get_zone('example.com') 
 
>>> # Delete A record from the zone.
... zone.delete_a('example.com')           
 
>>> # Delete CNAME record from the zone.
... zone.delete_cname('www.example.com') 
 
>>> # Delete MX records from the zone.
... zone.delete_mx()                       
 
>>> # Delete the zone itself.
... zone.delete()

If anyone has any questions, bugs or issues then feel free to ask. Hopefully, this will make your life with Route53 significantly easier.

October 8, 2011
by Marius Voila
2 Comments

This, really touches my heart..

The seas shall run dry,
And the rocks melt into sands,
Then shall I love you still, my dear,
When all these things are done.

PS: I do not own this photo. I tried but couldn’t find the source. (If you do, send a wink over here, if it’s yours, I salute you! )

Update: the story you can find here
and the picture belongs to oneasylum

September 20, 2011
by Marius Voila
0 comments

Connecting to Amazon EC2 server using ssh config file

I’ve recently moved my blog on the Amazon EC2 cloud infrastructure which has been an interesting process (more posts coming !)

I’m also big biG bIG BIG fan of SSH host shortnames. However, being used to a keypair from the servers to manage ssh login and sftp transfers, I couldn’t work out how to connect using the default .pem file supplied by Amazon EC2. PEM – or privacy enhanced email – is another cert-based system see the ubiquitous Wikipedia article for more tech juice.

I downloaded my server .pem file to my local ssh directory (you can find this in your user directory called .ssh – notice that the prefix of . makes it invisible normally). I could then login via ssh using

ssh -i ~/.ssh/server.pem me@mariusv.com

but I’m to lazy to writte that long line :D . After some time of thinkig the solution hits me…

Replace server.pem with your .pem file in the following instructions
Copy your server.pem file to ~/.ssh
Open terminal and you’ll be in your user directory
Edit (or create) the file config with: vim ~/.ssh/config
Hit “i” to go into insert mode
Add: IdentityFile ~/.ssh/server.pem
Hit: :wq! to quit and save

Change the permissions on your pem file with chmod 400 ~/.ssh/server.pem and you’re done!

Enjoy!

September 19, 2011
by Marius Voila
1 Comment

Split ISO image into multiple archives

The best solution for you, and it’s really easy! Just use the default GNU tools tar, split and cat. There is absolutely no need for any fancy gui tools or (god forbide!) software that you have to run in Wine!

Just type the following in a console :

tar cvzf - filename.iso | split -d -b 700m - filename.iso.tar.gz.

This wil produce the following files:

filename.iso.tar.gz.1
filename.iso.tar.gz.2
filename.iso.tar.gz.3

Burn to CD with your favorite burner, one file per disk.

Then later if you want to restore the iso, first copy all te parts in one directory, and then type:

cat filename.iso.tar.gz.* | tar xvzf -

That will give you back your original ISO.

I needed this a few days ago for a >10G backup that I wanted to put on a FAT32 external drive (maximum file size: 2G). Worked like a charm!

If you are concerned with space, replace the ‘z‘ option in tar with ‘j‘, and replace ‘gz‘ in the filenames with ‘bz2‘. Bz2 compression is usually a bit better than gz compression, but it’s slower.

And if you really want to save disk space, install the console version of 7-Zip, create a .7z archive, and pipe this trough split. I leave the exact syntax as an excercise to the reader. ;)

September 15, 2011
by Marius Voila
0 comments

OUTSTANDING…I’m speechless

I believe that this is the first singer in the history of mankind that her fans sing more beautifully than her that she is entirely astonished and her eyes are asking them all,”Who came to listen to whom here?”

Just notice how much she is in love with this crowd that she made and now she is listening to.

August 27, 2011
by Marius Voila
0 comments

Do not try

I am a senseless way.
Do not try to follow me, you worry me and I will hurt you.
Do not try to speculate … you will remain deeply disappointed by my circular aim.
Do not try in any way to love me. It would be the biggest mistake of your life!
Do not try to lie. Even if you succeed, you will believe you…
Try to accept, criticism, see, feel!
Then you could tell me what you want from me …

August 22, 2011
by Marius Voila
0 comments

Songs of the week — 09.19.2011

Actually, I haven’t listened to all that much new music of late, and that — combined with my busy days — are the cause of the missing posts. But I’m through with all that… for last week anyway. :)

Galaxies — Owl City
Stars — Switchfoot
Can’t Shut Up — Anthem Lights
Terminals — Relient K
Hello Hurricane — Switchfoot

August 13, 2011
by Marius Voila
0 comments

Songs of the week – 09.13.11

Hmm… I really don’t have anything cool to say about my collection of favorite songs this week. I guess they’re just what I listened to and enjoyed. Hope you do too!

The Redeemer — Sanctus Real
In Christ Alone — Owl City
Umbrella Beach — Owl City
Those Nights — Skillet
Small Rebellions — Jars of Clay