Wednesday Jan 16, 2008

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 -Z
5) 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 zone 
So 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.
About

rnl

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today