Centiq Blog

Centiq Blog

Nov 05
2009

Working with Clustered Nodes

Posted by: Steve Stringer in Infrastructure

Steve Stringer

 

When working with clustered nodes you will often want to run the same command on all the nodes in the system.  This may be to copy a file, start or stop a daemon or any type of operation.  On Linux there are many distributed shells that are designed to do just this.  But often I find it easier just run the list of nodes in to a for loop to do much the same thing.

Prerequisite check – the below tutorial assumes that you have password free root access via ssh keys to the clustered nodes.

To do this I first make a file called "nodes.list", this is a text file with the hostnames of all the nodes in the cluster.  I always find that it is best to put one hostname per line.  So the file may look like this. 

node001.mydomain
node002.mydomain
node003.mydomain
node004.mydomain

Store the file somewhere safe such as ~/cluster/nodes.list.  Next create a bash variable that contains the list.  I normally do this by adding the following lines in to ~/.bashrc

NODES="`cat ~/cluster/nodes.list`"
export NODES

After adding the variable to ~/.bashrc you can source it to import the changes to the file

# source ~/.bashrc

Check that this has worked by printing the variable.# echo $NODES

 If you printed the list of nodes then you are ready to continue.

With the variable wet you can start to issue commands to all the nodes.  For example, to print the time and date on all nodes you can run the following command.

 # for i in $NODES ; do ssh root@$i date ; done

You can also run multiple commands and use pipes.  For example, say you wanted to grep /var/log/messages for a pattern and display the last 5 results on each node.  You could run this command.

 # for i in $NODES ; do ssh root@$i "(grep kernel /var/log/messages | tail –n 5)" ; done

It is also nice to print the hostname of each node before the output for that node.  A simple way of printing the hostname of a system is running the hostname command with no arguments.  But be warned, if you are not careful it is easy to accidently rename all the nodes in the cluster, which is not advised.  That is why I prefer to print the $HOSTNAME variable.  Just insert a backslash in front of the dollar sign so the variable isn't substituted by the local host.  You can also add a trailing blank line so the output is nicely formatted.

 # for i in $NODES ; do ssh root@$i "(echo "$HOSTNAME" ; grep kernel /var/log/messages | tail –n 5 ; echo)" ; done

Please leave a comment if you found this article of use.

 

Hits: 3188
Trackback(0)
Comments (0)Add Comment

Write comment

security code
Enter the displayed characters


busy

Request more information

Want us to contact you right now?

Leave your details and we'll call you Immediately during work hours.

Name: *
Company:
Phone: *

Bloggers

Emily MalbonEmily Malbon:
Helpdesk and Support

Rebecca PritchardRebecca Pritchard:
Project Management

Robin WebsterRobin Webster:
UNIX

Steven CalvertSteven Calvert:
Storage

Steve StringerSteve Stringer:
Blade and SAP BWA

Glyn HeathGlyn Heath:
IT Industry

Tags

 Centiq Technical Wiki Site  Centiq is a 2012 IBM Beacon award winner and IBM Premier Business partner specialising in System x, Power and SAP BWA smarter_monitiq_logo v2 preferred partner_2012_solid_blue_vert_png accredit_uk_logo v2