Thursday, April 5, 2012

Installing memcached on RHEL6 to use with Drupal 7

Recently I decided to switch from memcache to memcached PHP extension to be used with Drupal 7. I followed a great guide from http://www.bxtra.net/articles/2011-03-22/how-to-install-memcached-on-centos-memcached-pecl-update-20110322 but made some modifications to work with RHEL 6 / CentOs 6.
# Get libvent
wget http://monkey.org/~provos/libevent-2.0.10-stable.tar.gz
tar -xvf libevent-2.0.10-stable.tar.gz
cd libevent-2.0.10-stable
./configure;make;make install;

# Get the latest version of libmemcached
wget https://launchpad.net/libmemcached/1.0/1.0.5/+download/libmemcached-1.0.5.tar.gz
tar xvfz libmemcached-1.0.5.tar.gz
cd libmemcached-1.0.5
./configure
make && make install

# Get ZLIB headers
yum install zlib-devel

# Remount /tmp with execute permissions 
mount -o remount,exec /tmp

pecl install memcached

# Revert back to a safer configuration
mount -o remount,noexec /tmp

The last step is to add the extension to /etc/php.d/
extension=memcached.so > /etc/php.d/memcached.ini
Now you can update your settings.php in Drupal to take advantage of all the tweaks:
$conf['memcache_options'] = array(

// Turn off compression, as this takes more CPU cycles than its 
// worth for most users
Memcached::OPT_COMPRESSION => FALSE,
// Turn on consistent distribution, which allows you 
// to add/remove servers easily
Memcached::OPT_DISTRIBUTION => Memcached::DISTRIBUTION_CONSISTENT,
// Highly recommended to enable this option if you want 
// to use consistent hashing
Memcached::OPT_LIBKETAMA_COMPATIBLE => TRUE, 
// Dials down the connection timeout to 50 millisecond
Memcached::OPT_CONNECT_TIMEOUT => 50,
// These are timeouts for the actual send and receive operations sent
// to the Memcached server, this time in microseconds. 
Memcached::OPT_SEND_TIMEOUT => 50000,
Memcached::OPT_RECV_TIMEOUT => 50000,
// A timeout that is used internally for polling the input and output
// streams. This one is back in milliseconds
Memcached::OPT_POLL_TIMEOUT => 50,
// Enable non-blocking sends. This means that PHP immediately regains
// control when sending a value over the wire.
Memcached::OPT_NO_BLOCK => TRUE,
// Nnumber of times to try to connect to a server 
// before ejecting it from a  pool. 
Memcached::OPT_SERVER_FAILURE_LIMIT => 20,
// The number of seconds to wait before trying to add 
// a down server back in to the pool
Memcached::OPT_RETRY_TIMEOUT => 360

);
This is mostly taken from http://blog.roundeights.com/36883753

Wednesday, April 4, 2012

Managing a collection of Apache web servers via Parallel SSH



Recently, I came across something that I considered to be a common problem, with a known solution and hit a snag. I needed to manage some basic operations across 12 Apache Web servers running RHEL6. I wanted to perform basic tasks such as restart Apache and memcache, as well perform some simple management tasks. It turned out to be a challenge, mostly due to lack of documentation. Which is why I'm writing this. Hopefully someone else will find this helpful.

First I downloaded PDSH - http://code.google.com/p/pdsh/wiki/UsingPDSH
wget http://pdsh.googlecode.com/files/pdsh-2.28.tar.bz2
Next untar the archive, of course
tar -xvf pdsh-2.28.tar.bz2
Next we want to configure, build and install PDSH on the system:
cd pdsh-2.28
./configure
make
make install 
Now we have PDSH all built and ready to go. All straight forward thus far, but next things get a touch tricky.
First, you need to define your hostlist file. Let's call it hostlist.txt and put it in /var/pdsh. We need to tell PDSH to use this as the default hostlist file location:
export WCOLL=/var/pdsh/hostlist.txt
Next, you want to edit your host file on the machine and create host names foe every server IP you want to manage:
vim /etc/hosts

192.168.1.10 devweb1
192.168.1.11 devweb2
192.168.1.12 devweb3
192.168.1.13 devweb4

Next you want to add these host names to your default host file in /var/pdsh/hostlist.txt
vim /var/pdsh/hostlist.txt
devweb1
devweb2
devweb3
devweb4
After this is done, you are almost done. You just have to make sure that you have a user that has key-based authentication set up with the servers that you want to manage. To test that SSH with the user you are planning to use to each one of the boxes and make sure you get in without entering a password:

If your SSH is set up correctly, you are ready to run basic commands in PDSH:
pdsh -R exec -w myuser@devweb[1-4] ssh -l %u %h date

devweb3: Wed Apr  4 14:38:49 CDT 2012
devweb1: Wed Apr  4 14:38:49 CDT 2012
devweb2: Wed Apr  4 14:38:49 CDT 2012
devweb4: Wed Apr  4 14:38:49 CDT 2012

This works, and that's great. Now you want to do something more complicated, such as restart Apache. This is where things get trickier. You need to log in individually to each one of your web servers and edit the SUDO file.
visudo

#Disable tty requirement, so PDSH can use sudo
#Defaults    requiretty

#next add permissions for your users for Apache 
#and Memcache with no password
Cmnd_Alias      APACHE = /etc/init.d/httpd start, 
                         /etc/init.d/httpd stop, 
                         /etc/init.d/httpd restart, 
                         /etc/init.d/httpd graceful

Cmnd_Alias      MEMCACHE = /etc/init.d/memcached restart,
                           /etc/init.d/memcached start, 
                           /etc/init.d/memcached stop

%myusergroup ALL = NOPASSWD: APACHE, MEMCACHE


And now you are good to go! Give the following a shot:
pdsh -R exec -w myuser@devweb[1-4] ssh -l %u %h sudo 
                                        /etc/init.d/httpd restart

Now you can manage a farm of any size very easily. Adjust the numbers in the brackets to make changes to a partial set of servers.