Robert Lor's Weblog
Postgres and Solaris Virtualization
Many people today use virtualization technology to consolidate applications to fewer and more powerful systems to improve system utilization, save datacenter space and power. One way to achieve this on Solaris is through Zones . Combined with Solaris Resource Management , a Solaris Zone provides a virtualized environment where resources such as CPU and memory can be controlled.
To demonstrate how this works, I will show a simple example of how to run PostgreSQL in Solaris Zone and adjust CPU cap as appropriate for this application. You can also restrict other resources such as memory or number of processes in a zone, but this example only covers CPU capping.
Note: To follow this example, you need a machine with Solaris Express Developer Edition 9/07 or later installed.
1) Create a new zone. Here is a script you can use to automate the zone creation. Save it to a file called create_zone.sh, and run it like this ./create_zone.sh pgzone /zones By default, this script assign the zone 20% of a CPU. You can adjust this number(ncpus) as necessary.
#!/bin/ksh
PrintMsg() {
CURRENTTIME=`date`
echo "$CURRENTTIME: $*"
}
if [[ -z "$1" || -z "$2" ]]; then
PrintMsg "Usage: $0 "
PrintMsg "Example: $0 pgzone /zones"
exit 2
fi
if [[ ! -d $2 ]]; then
PrintMsg "$2 does not exist or is not a directory"
exit 1
fi
name=$1
dir=$2
PrintMsg "Creating a new zone"
zoneadm -z $name list > /dev/null 2>&1
if [ $? != 0 ]; then
PrintMsg "Configuring $name"
commands=$dir/$name.config
rm -f $commands
echo "create" > $commands
echo "set zonepath=$dir/$name" >> $commands
echo "set autoboot=true" >> $commands
echo "set scheduling-class=FSS" >> $commands
echo "add capped-cpu" >> $commands
echo "set ncpus=0.2" >> $commands
echo "end" >> $commands
echo "add capped-memory" >> $commands
echo "set physical=256m" >> $commands
echo "set swap=256m" >> $commands
echo "end" >> $commands
echo "set cpu-shares=10" >> $commands
echo "set max-lwps=500" >> $commands
echo "commit" >> $commands
zonecfg -z $name -f $commands 2>&1 | \
sed 's/^/ /g'
else
PrintMsg "$name already configured"
fi
# Installing
if [ `zoneadm -z $name list -p | \
cut -d':' -f 3` != "configured" ]; then
PrintMsg "$name already installed"
else
PrintMsg "Installing $name"
mkdir -pm 0700 $dir/$name
chmod 700 $dir/$name
zoneadm -z $name install > /dev/null 2>&1
PrintMsg "Setting up sysid for $name"
cfg=$dir/$name/root/etc/sysidcfg
rm -f $cfg
echo "network_interface=NONE {hostname=$name}" > $cfg
echo "system_locale=C" >> $cfg
echo "terminal=xterms" >> $cfg
echo "security_policy=NONE" >> $cfg
echo "name_service=NONE" >> $cfg
echo "timezone=US/Pacific" >> $cfg
echo "root_password=Qexr7Y/wzkSbc" >> $cfg # 'l1a'
fi
PrintMsg "Booting $name"
zoneadm -z $name boot
2) Open a terminal. As root, log into the zone using zlogin (e.g zlogin pgzone).
3) Once you're in the zone, do the following:
a. As root, su to postgres:
# su - postgres
b. Create PostgreSQL DB cluster:
$ /usr/postgres/8.2/bin/initdb -D /var/postgres/8.2/data
c. As root, use the SMF's svcadm command to start PostgreSQL:
# /usr/sbin/svcadm enable postgresql:version_82
d. Create and load a db called bench
$ /usr/postgres/8.2/bin/createdb bench
$ /usr/postgres/8.2/bin/pgbench -i -s 5 bench
e. Run this Cartesian joint multiple times (try 5) to generate CPU load
$ /usr/postgres/8.2/bin/psql -d bench -c "select count(*) from accounts foo, accounts bar;" &
4) In the global zone, using another terminal window, run the following command to see cpu usage for each zone. Note the zone that Postgres is running should cap to around 20% if you have a single CPU system.
# prstat -Z5) You can dynamically adjust the amount of CPU assigned to the zone using the prctl command. In another terminal window, run:
# prctl -n zone.cpu-cap -i zoneSo in a nutshell, that's how you can use Solaris Zones and Resource Management to improve system utilization in a virtualization environment. As I mention in the beggining, you can also cap other resouources as well which make the combination of Solaris Zones and resource management very powerful.
Posted at 03:58PM Jan 16, 2008 by Robert Lor in PostgreSQL | Comments[4]
I tried your example on an E25K running
Solaris 10 08/07 as you specified. When
I try to "add capped-cpu" I get an error.
Any ideas why?
Thanks,
Mark
Posted by Mark Langenbahn on January 31, 2008 at 10:48 PM CST #
@Mark: My apologies. I had thought that "add capped-cpu" made it into Solaris 10 08/07, but I just verified that it didn't. It may happen in the next Solaris 10 update, but if you want to try this now, please use Solaris Express.
Posted by Robert Lor on February 04, 2008 at 02:03 PM CST #
Robert, Mark
CPU Caps are part of Solaris 05/08 and you are in luck ... it is available now ... upgrade and enjoy..
http://www.sun.com/software/solaris/get.jsp
Posted by Hein Van Der Merwe on April 15, 2008 at 12:37 PM CDT #
@Mark: Do you know if it's possible to install a patch to obtain the new functionality of this new add capped-cpu resource from u4 8/07? Without doing a full Live upgrade to u5 5/08.
Posted by Matt on June 04, 2008 at 10:12 PM CDT #