Archive for the ‘ Linux ’ Category

RedHat Enterprise Linux 6 Beta

RedHat Enterprise Linux 6 Beta 1 has finally been released as a public beta. It is available as an ISO from the public RedHat FTP site.

A couple of days ago, I decided to play with the beta, and I discovered (as I had expected) that there are a lot of significant differences between RHEL5 and RHEL6.
The the main difference which I found to be very frustrating is that there is no longer any support for Xen dom0.

I had heard about RedHat’s decision to stop supporting Xen, but I did not think that this would mean they would stop shipping it with the distribution.

The loss of dom0 support means that you can no longer use RHEL as a Xen virtualization host, rather only as a guest under other Xen supporting distributions.

Xen was dropped in favor of Kernel-based Virtual Machine, which is a  virtualization infrastructure included with the Linux kernel. Linux KVM is a hardware-assisted virtualization infastructure which requires the CPU to have a special CPU feature called Intel-VT on Intel CPUs and AMD-V on AMD CPUs.

KVM has limited paravirtulization support, but I found in my very simple tests that fully paravirtualizaed guests inside Xen had much better performance.

This latest release of RHEL also means that my RHCE will soon expire. I am hopeing to get re-certified as soon as I can. At the same time, I am also considering taking the “Red Hat Certified Virtualization Administrator” course and exam, but I still have some time to think over that. :)

Nginx, Varnish, HAProxy, and Thin/Lighttpd

Over the last few days, I’ve been playing with Ruby on Rails again and came across Thin, a small, yet stable web server which will serve applications written in Ruby.

This is a small tutorial on how to get Nginx, Varnish, HAProxy working together with Thin (for dynamic pages) and Lighttpd (for static pages).

I decided to take this route as from reading in many places I found that separating static and dynamic content improves performance significantly.

Nginx

Nginx is a lightweight, high performance web server and reverse proxy. It can also be used as an email proxy, although this is not an area I have explored. I will be using Nginx as the front-end server for serving my rails applications.

I installed Nginx using the RHEL binary package available from EPEL.

Configuration of Nginx is very simple. I have kept it very simple, and made Nginx My current configuration file consists of the following:

user nginx;
worker_processes 1;

error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    log_format main '$remote_addr - $remote_user [$time_local] $request "$status" $body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_for"';

    sendfile on;
    tcp_nopush on;
    tcp_nodelay off;

    keepalive_timeout 5;

    # This section enables gzip compression.
    gzip on;
    gzip_comp_level 2;
    gzip_proxied any;
    gzip_types text/plain text/html text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;

    # Here you can define the addresses on which varnish will listen. You can place multiple servers here, and nginx will load balance between them.
    upstream cache_servers {
      server localhost:6081 max_fails=3 fail_timeout=30s;
    }

    # This is the default virtual host.
    server {
        listen 80 default;
        access_log /var/log/nginx/access.log main;
        error_log /var/log/nginx/error.log;
        charset utf-8;

        # This is optional. It serves up a 1x1 blank gif image from RAM.
        location = /1x1.gif {
          empty_gif;
        }

        # This is the actual part which will proxy all connections to varnish.
        location / {
          proxy_pass http://cache_servers/;
          proxy_redirect http://cache_servers/ http://$host:$server_port/;

          proxy_set_header Host $host;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }
}

Varnish

Varnish is a high performance caching server. We can use Varnish to cache content which will not be changed often.

I installed Varnish using the RHEL binary package available from EPEL as well. Initially, I only needed to edit /etc/sysconfig/varnish, and configure the address on which varnish will listen on.

DAEMON_OPTS="-a localhost:6081 \
             -T localhost:6082 \
             -f /etc/varnish/default.vcl \
             -u varnish -g varnish \
             -s file,/var/lib/varnish/varnish_storage.bin,10G"

This will make varnish listen on port 6081 for normal HTTP traffic, and port 8082 for administration.

Next, you must edit /etc/varnish/default.vcl to actually cache data. My current configuration is as follows:

backend thin {
  .host = "127.0.0.1";
  .port = "8080";
}

backend lighttpd {
  .host = "127.0.0.1";
  .port = "8081";
}

sub vcl_recv {
    if (req.url ~ "^/static/") {
        set req.backend = lighttpd;
    } else {
        set req.backend = thin;
    }

    # Allow purging of cache using shift + reload
    if (req.http.Cache-Control ~ "no-cache") {
        purge_url(req.url);
    }

    # Unset any cookies and autorization data for static links and icons, and fetch from catch
    if (req.request == "GET" && req.url ~ "^/static/" || req.request == "GET" && req.url ~ "^/icons/") {
        unset req.http.cookie;
        unset req.http.Authorization;
        lookup;
    }

    # Look for images in the cache
    if (req.url ~ "\.(png|gif|jpg|ico|jpeg|swf|css|js)$") {
        unset req.http.cookie;
        lookup;
    }

    # Do not cache any POST'ed data
    if (req.request == "POST") {
        pass;
    }

    # Do not cache any non-standard requests
    if (req.request != "GET" && req.request != "HEAD" &&
        req.request != "PUT" && req.request != "POST" &&
        req.request != "TRACE" && req.request != "OPTIONS" &&
        req.request != "DELETE") {
        pass;
    }

    # Do not cache data which has an autorization header
    if (req.http.Authorization) {
        pass;
    }

    lookup;
}

sub vcl_fetch {
    # Remove cookies and cache static content for 12 hours
    if (req.request == "GET" && req.url ~ "^/static/" || req.request == "GET" && req.url ~ "^/icons/") {
        unset obj.http.Set-Cookie;
        set obj.ttl = 12h;
        deliver;
    }

    # Remove cookies and cache images for 12 hours
    if (req.url ~ "\.(png|gif|jpg|ico|jpeg|swf|css|js)$") {
        unset obj.http.set-cookie;
        set obj.ttl = 12h;
        deliver;
    }

    # Do not cache anything that does not return a value in the 200's
    if (obj.status >= 300) {
        pass;
    }

    # Do not cache content which varnish has marked uncachable
    if (!obj.cacheable) {
        pass;
    }

    # Do not cache content which has a cookie set
    if (obj.http.Set-Cookie) {
        pass;
    }

    # Do not cache content with cache control headers set
    if(obj.http.Pragma ~ "no-cache" || obj.http.Cache-Control ~ "no-cache" || obj.http.Cache-Control ~ "private") {
        pass;
    }

    if (obj.http.Cache-Control ~ "max-age") {
        unset obj.http.Set-Cookie;
        deliver;
    }

    pass;
}

HAProxy

HAProxy is a high performance TCP/HTTP load balancer. It can be used to load balance almost any type of TCP connection, although I have only used it with HTTP connections.

We will be using HAProxy to balance connections over multiple thin instances.

HAProxy is also available in EPEL. My HAProxy configuration is as follows:

global
  daemon
  log 127.0.0.1 local0
  maxconn 4096
  nbproc 1
  chroot /var/lib/haproxy
  user haproxy
  group haproxy

defaults
  mode http
  clitimeout 60000
  srvtimeout 30000
  timeout connect 4000

  option httpclose
  option abortonclose
  option httpchk
  option forwardfor

  balance roundrobin

  stats enable
  stats refresh 5s
  stats auth admin:123abc789xyz

listen thin 127.0.0.1:8080
  server thin 10.10.10.2:2010 weight 1 minconn 3 maxconn 6 check inter 20000
  server thin 10.10.10.2:2011 weight 1 minconn 3 maxconn 6 check inter 20000
  server thin 10.10.10.2:2012 weight 1 minconn 3 maxconn 6 check inter 20000
  server thin 10.10.10.2:2013 weight 1 minconn 3 maxconn 6 check inter 20000
  server thin 10.10.10.2:2014 weight 1 minconn 3 maxconn 6 check inter 20000
  server thin 10.10.10.2:2015 weight 1 minconn 3 maxconn 6 check inter 20000
  server thin 10.10.10.2:2016 weight 1 minconn 3 maxconn 6 check inter 20000
  server thin 10.10.10.2:2017 weight 1 minconn 3 maxconn 6 check inter 20000
  server thin 10.10.10.2:2018 weight 1 minconn 3 maxconn 6 check inter 20000
  server thin 10.10.10.2:2019 weight 1 minconn 3 maxconn 6 check inter 20000

Thin

My Thin server is actually run on a separate Gentoo box. I installed Thin using the package in Portage.

To configure Thin, I used the following command:

thin config -C /etc/thin/config-name.yml -c /srv/myapp --servers 10 -e production -p 2010

This configures thin to start 10 servers, listening on port 2010 to 2019. If you want an init script for Thin, so you can start it at boot, run

thin init

This is will create the init script, and you can set it to start up at boot using the normal method (rc-update add thin default or chkconfig thin on).

You should now be able to access your rails app through http://nginx.servers.ip.address

Next, we must configure the static webserver.

Lighttpd

I decided to go with Lighttpd as it is a fast, stable and lightweight webserver which will do the job perfectly with little configuration.

You could also use nginx as the static server instead of using lighttpd, but I decided to separate it.

I decided to use the package from EPEL for Lighttpd, and found that most of the default configuration was as I wanted it to be. The only thing I needed to change was the port and address the server was listening on:

server.port = 8081
server.bind = "127.0.0.1"

And thats pretty much it! Now you just have to dump any static content into /var/www/lighttpd/ (the default location that the Lighttpd package in EPEL is configured to use) and reference any static links using “/static/document_path_of_file”, for example if I put an image into /var/www/lighttpd/images/ called “bg.png”, I can access it using http://servers_hostname/static/images/bg.png.

I have not really done any performance tests onto how well this works, and there are probably many things which I could have done better. This is the first time I made any attempt HTTP performance tuning, and so I am always looking for feedback or tips on how to make this better, so please do contact me if you have any suggestions! :)

RHEL5 iSCSI Target/Initiator

iSCSI is a protocol that allows you to use a disk in a remote machine, locally as a block device. It is a very popular SAN protocol, which allows the consolidation of storage into one large storage pool.

In iSCSI terminology, a target is a storage resource located on an iSCSI server and an initiator is a client which will be connecting to a target.

In this post I will demonstrate how to setup an iSCSI target and initiator on RedHat Enterprise Linux 5.

Target

RHEL5 has the stgt target daemon included with it. This is the server which allows initiators (clients) to connect to the disks using the iSCSI protocol. It is installable using yum:

yum install scsi-target-utils

To share a disk over iSCSI, you can either use a disk image, or a block device. I prefer to use LVM logical volumes as LVM allows easy management of free space.

Stgt’s configuration file lives in /etc/tgt/targets.conf, although most configuration can be done using the command line (although this is not persistant across reboots, so I prefer to use the configuration file).

To create a target, just add the following lines:

backing-store /dev/DiskArray/Archive1

Please note that you must replace iqn.2009-02.com.hamzahkhan:archive1 with your own domain name, and resource name. It must be in the iSCSI Qualified Name (IQN) format, iqn.yyyy-mm.{reversed domain name}:an_easy_to_remember_lablel.

The second line, backing-store /dev/DiskArray/Archive1 specifies the disk/disk image that is to become the target, I am using LVM logical volume although the procedure is exactly the same for disk images, and real physical disks.

By default, stgt will allow all IPs to connect to the target, which is highly insecure! To change this behaviour, it is possible to specify IP addresses which are allowed to use the targets. To do this, just place the following line under backing-store:

initiator-address 10.1.0.4

This will allow 10.1.0.4 to access the target.

Now, use chkconfig to make stgt start at boot, and start up the daemon.

/etc/init.d/tgtd start

chkconfig tgtd on

Thats all there is to it on the server side! All done :)

You can now connect to the target using any initiator, such as the one built into Windows Vista (although I have never tried using Windows).

Initiator

RedHat have included an iSCSI daemon which is also installable using yum:

yum install iscsi-initiator-utils

To connect to the target, edit /etc/iscsi/initiatorname.iscsi and change InitiatorName to something you prefer (Remember! it must be in the IQN format, iqn.yyyy-mm.{reversed domain name}:an_easy_to_remember_lablel. I usually use iqn.2009-02.com.hamzahkhan:hostname_of_box). Next start up iSCSId:

/etc/init.d/iscsid start

and use iSCSI target descovery to find the targets on the server:

iscsiadm -m discovery -t st -p $SERVERS_IP

If all is well, it should output the names of all the targets that the initiator is allowed to connect to!

Next, we need to create the disk nodes. To do this, RedHat have provided a nice start up script. This script will login to all the targets that the iSCSI daemon knows about. We have already used the iscsiadm command to tell the iSCSI daemon which targets exist on the server, so using the script is all that is left:

/etc/init.d/iscsi start

Thats all there is to it! :)

You should have a new disk node in /dev/. You can use lsscsi (yum install lsscsi) to find the exact name if you have a lot of USB/SATA/SCSI drives connected to the machine already.

Now all you have to do is partition the disk, and dump your files onto it :)

Please remember, you must NEVER mount a partition on two machines at the same time. Doing so will cause data loss!

It IS possible to mount the same disk on multiple machines, but this requires a special clustered filesystem such as GFS.

Western Digital 1TB Hard Drive

A few months ago, I bought a Western Digital 1TB Hard Drive (http://www.wdc.com/en/products/products.asp?DriveID=336) for my server.

Since every WD drive I have bought in the past has served me very well, I assumed this drive would do the same…. but VERY annoyingly, I just got an email from smartmon tools telling me that there are an increasing number of bad sectors on the drive! :(

eBuyer has agreed to replace the drive since it is still under warrenty, but the problem is that I have the drive in an LVM volume group, so backing up the data is a little difficult.

It would be easy if I had another 1TB Hard Drive to add to the volume group, the pvmove all the data off the broken one, but I do not have a spare 1TB drive, and eBuyer (naturally), didn’t agree to sending me the new drive before I give the old drive back to them.

I also have 1TB Seagate drive in the volume group, which is performing very well, so very reluctantly, I might just ask eBuyer if they would let me switch it for a Seagate one.

Oh well, I guess for now, my only option would be to buy another 1TB drive, move all the data onto that drive, remove the old drive from the VG, get it replaced, then if I feel brave enough, add the new one to the VG. I’ll have quite a large volume group if I do that (3TB!).

Speaking of which, if anyone has any tips for boosting LVM performance when using large volume groups, please tell me! :)

Hackintosh!

So after my last blog post, I decided to try out OS X inside a virtual machine ….. but after four tries, I gave up and just wiped my hard drive and installed natively.

Installation went surprisingly well, and most of my hardware is “liked” by the hacked versions of OS X (I used iATKOS 5i). Everything that should be working, is working except for my sound card… which is partially working. I hear sound, but the front audio ports for headphones and microphones does not work, and neither does the back microphone socket, but I’m still quite happy with that considering OS X wasn’t designed to be run on my hardware (and apparently with some hacking, I can make those things work too).

So far I’ve been using OS X for two days, and I must say, I like it much more than I had expected.

My first computer was a Mac, and I used a Mac till OS 8.something (in 1999 my dad bought me a my first PC because Macs were, and still are quite pricy). I was originally quite reluctant to leave Mac OS, but eventually had to BUT I soon discovered Linux, and started using and loving it.

For the last 5/6 years I’ve been using Linux, and I only really used OS X two or three times at my Dad’s work place. I originally thought I would LOVE to have a Mac (with OS X), but a few days ago I changed my mind because I thought I had become too used to Linux (which I have!) and would not be able to use OS X to do everything I want.

I thought there would be lots of things that I would not like about OS X, but it turns out the list is actually a lot smaller than I thought.

There are a few things I don’t like about OS X, and some things I miss from Linux. For example:

  • I really hate the keyboard bindings. This is probably because I’m now used to Linux shortcuts etc. I managed to “fix” some of them (eg in the Terminal app I wanted page up/down to actually send the page up/down characters).
  • I STILL dislike iTunes, although it is much better than in Windows. I really miss MPD with gmpc.
  • I don’t really like the dock. This is probably one of the things that most people DO like, but I’m not really a fan of it.
  • I can’t seem to find a decent IRC client on it. X-Chat Aqua isn’t really quite as nice as it is on Linux, and Linkinus isn’t too good either in my opinion.
  • I don’t really like the fact that OS X doesn’t depend THAT much on log files. It does use them, but I don’t think the details it gives are always useful.
  • I kinda miss the ability to configure things from the command line. I don’t know if you can configure things from the command line in OS X, but from what I understand you can’t really do much system configuration from the command line except small hacks.

Overall, I like OS X, and will probably continue using it on my machine till I get a real Mac (hopefully in September).

Some of the things I like about OS X:

  • Undoubtably the thing I like most is how everything is so tightly integrated with each other. While this is also possible on Linux, it DOES need a lot of configuration to get it perfect. On OS X, it is all ready to go, out of the box. Linux is also heading that way, with things like d-bus interaction between apps has become more and more efficient, but not all apps take advantage of this yet.
  • I like the fact that everything looks the same, and isn’t “odd”. By this I mean there is no “KDE” look, or Gnome Look. Everything fits in fine with the UI. I know you can use special tools etc to make KDE apps fit into Gnome, and vice-versa, but again that requires configuration. Personally I didn’t ever bother doing that, although I didn’t really like how KDE apps didn’t fit into my Gnome desktop.
  • Close source applications work better on OS X than their Linux equivalents. For example Skype is on Linux and on Mac OS X, but the Mac version is MUCH more stable than the Linux version. I guess this is mainly due to all the sound systems that are available in Linux. Skype switched to ALSA recently from OSS, but now a lot of people want PulseAudio support too, or ESound support etc. There are too many choices I think, and I think that is causing a bit of chaos. This issue would probably be fixed a lot faster if Skype were open source, but I don’t think people should live in a dream world where everything is open source, sure it would be nice, but lets face it, thats never gonna happen. So in reality, the better choice will indeed be the one that works, and in my opinion, so far in OS X is the better choice.

On OS X, I’ve managed to actually have a pretty good quality conversation over Skype with my sister, which I haven’t been able to on Linux.

I don’t know if this makes me sound like I’m anti-Linux now, believe me I’m not! I LOVE Linux still (more than I like OS X!), in-fact I’m running it inside a VMware Fusion virtual machine right now and will wipe my machine and put Gentoo back on it as soon as I get a real Mac to sit beside my Gentoo machine.

I also thought I’d mention this: I know a lot of Linux users who say EVERYONE should use Linux, and there is no excuse for using Microsoft or Apple products. This goes to the people who think this way: You are all idiots :) .

Some people say Apple and Microsoft products should be avoided because they are buggy. Sure Windows IS buggy, and sure OS X probably has some bugs too (I haven’t found any yet!). BUT truthfully, can anyone say that Linux applications are bug free? The only difference is that you have the ability to fix the bugs yourself…. which is quite a useless ability if you are a normal user who doesn’t give a damn about how the internal works, and doesn’t have a clue what C++ is!

To be quite honest, A LOT of Linux applications have A LOT of bugs. NetworkManager is quite buggy, so I stopped using it and manually setup wpa_supplicant to connect to my wireless network, but I don’t think my Dad can do that! A normal user like my father needs GUIs to do everything, they are easier to use for someone who has no clue how to use a CLI (and doesn’t want to learn how to use it!). Naturally Linux IS becoming more and more user friendly, and I think there WILL be a point where I can safely install Linux on either of my parents computers, and not have to worry about them not knowing how to do something, but till that day comes I REALLY think it is pointless and ignorant for people to tell everyone to boycott Apple and Microsoft, and switch to Linux unless they are willing to understand the internals a little and figure out how to manually edit things from the CLI, which over 90% of the world’s population probably isn’t :) .

Besides… Apple products are really nice in my opinion, not very buggy and they work very well! (I love my iPhone <3!!!)

Lol like most of my posts, I wrote this while super sleepy so it probably makes no sense, oh well.

iPhone / Linux / Mac OS X / Windows

Last week my phone contract ended, so I decided to upgrade to an iPhone 3G.

Till now, my main two phones have been the SE P910i and K800i, both of which have served me very very well for what I was using them for. There are a few reasons I decided to switch over to an iPhone. Firstly, I was originally using my K800i with a 2GB M2 memory stick as my MP3 player. As I soon noticed, 2GB isn’t really enough sometimes. I think the iPhone’s 16GB worth of space will give much more freedom. Also, I usually use my PSP to watch movies/TV shows when ever I go on the train or bus. I really like using the PSP for that purpose, the screen size is not bad, and the quality is pretty good too. BUT, then I have to carry around 2 phones and a PSP which can be quite irritating. So considering the size of the iPhone screen isn’t much smaller than the PSPs, it would be a perfect replacement to watch videos on, and have only 1 gadget to carry around. Thirdly, I often use my P910i to go on IRC or MSN from places where I can’t access a computer (Grandmothers house etc). I really liked using my P910i for this purpose, it was easy to type on and the application I use on it for MSN/Jabber/Yahoo is quite nice too (IM+). But from what I have seen, iPhone alternatives are just as good, or even better and have less bugs (afaik, IM+ for UIQ2 phones is no longer developed :( ).

In “preperation” for my iPhone getting delivered tomorrow, I decided to actually “try” out iTunes. After 3 hours of playing with it, I can say this much: I hate it :D .

The main reason for this is that I cannot make it “watch” a folder. All of my music is stored on my server, where my brothers dump music too. Quite often my brothers have put music there without me knowing, and so a “folder watch” feature would be very nice in iTunes. On Linux, I use MPD as my music player. I have a crontab script which makes MPD update its database every hour, so if there is anything new on my server, it will automatically be added to my library. After some googling I found iTunes Library Updater (http://itlu.ownz.ch/wordpress/) which works I guess but it still requires quite a bit of user interaction.

After Googling a bit more, I got the impression that even in Mac OS X I would have this problem which got me thinking: I would feel quite uncomfortable in OS X, just as I do in Windows.

I never really thought about it before but most of what I do on Linux, I do through the command line (Which is why I wanted a Mac for so long I guess). I find the command line much more efficiant than using a GUI, and imo Mac OS X is most popular due to its idiot-proof GUIs etc. Now that I think about it, when I first go on any machine, I open some sort of terminal. On Windows, I fire up Putty before anything else and on Linux Gnome Terminal is on my auto-startup apps list.

I accept that Linux does have a lot of things missing, but I think that OS X and Windows both have just as many things missing from them, and so far I think Linux is fixing these missing things faster than Apple or Microsoft are. (I guess thats mainly because most of these “issues” with OS X and Windows don’t effect the everyday user, but do effect Geeks!).

I was originally intending on listing reasons why I think I wouldn’t feel comfortable in OS X, but that will come another day (after I have actually tried out OS X).

A lot of you know I’ve wanted a Mac for AGES, but I can now say that I’m not sure if I really want to switch to OS X… of course I will try it out, who knows, I might be wrong!

Now to find an easy way to put music/videos onto the iPhone from Linux….

Back from the dead!

I finally decided to put my blog back up!

Well, its not actually back up really. As some of you might know, I’m learning Ruby and I will re-create my site completly in Ruby on Rails! So for now, this blog is temporary, and I will attempt to “import” all the posts from here, into my new Ruby on Rails blog once it is ready. :D

So far I LOVE Ruby/RoR! I managed to get a fully functional Photo gallery within 15mins!

I’ve also been playing with various RedHat products and also been learning various things needed to take the Cisco CCNA exam :) .

I’m trying to learn how to set up RADIUS, LDAP and Kerberos and thought it would be a good idea to try out RedHat Directory Server :) . Although I would like to understand how to set up OpenLDAP manually, I think RedHat have done a really good job at creating a LDAP server which does everything by its self :D . It would be really handy in a large organisation.

From looking around the RedHat site, I also found RedHat Enterprise IPA, which seems like a very interesting project. So I will be trying that out later too! :)

I’m also playing with Active Directory 2003 (Microsoft are giving Windows Server 2003 away for free to students!), and I will try to “connect” RedHat Directory Server and Enterprise IPA, from what I understand, it can be done :o .

Hopefully I’ll be taking Cisco CCNA exam soon, so I’ve been reading various books that “teach” the stuff that I need to know to pass the exam :O.