Running OpenSolaris and Zones in the Amazon Cloud - Part 3

Introduction

In Part 1 of this series of tutorials on OpenSolaris and Zones in AWS we described a method for creating zones within an EC2 instance running OpenSolaris.

In Part 2 we describe a method for backing up the zones using ZFS snapshots, sending a copy to a secondary EBS volume, and then performing a EBS snapshot of the secondary volume. We also provide examples of how to recover zones from our ZFS snapshots as well as recover from the secondary EBS volume if for some reason our primary EBS volume fails.

This is Part 3 of the series where we explain how to save a fully configured zones environment using EBS snapshots, which can then be cloned and up and running in minutes. We leverage Parametrized Launches as described in a previous blog entry.

Prerequisites

  • Basic understanding of AWS EC2 including: managing EBS volumes and snapshots.
  • Basic understanding of OpenSolaris including ZFS.
  • Zones up and running as described in Part 1 of this tutorial.
  • Ability to run the EC2 command line tools.
  • Although not a requirement, the example uses OpenSolaris and EC2 Parametrized Launches to greatly simplify the launch of a new instance.

Example EC2 environment

As described in Part 1, I created three EBS volumes, one for shared software, one for zones storage, and another one for zones backup. The EC2 environment is displayed below.

AWS EC2 Environment

Our goal is to perform the following steps:

  • Halt the zones
  • Detach the zones
  • Create a EBS snapshot of our zones ZFS pool.
  • Create a EBS volume from this snapshot.
  • Launch a new EC2 instance and bring up the zones.

We will follow the standard procedure for preparing to migrate a non-global Zone to a different machine. Instead of actually moving the zones to another EC2 instance we put the zones into a state as if we are going to migrate, we then take a EBS snapshot of the EBS volume that contains the zones ZFS pool. Once the EBS snapshot has been started we simply bring our zones back up on the current EC2 instance.

Our current zones list is shown below.

root:~# zoneadm list -cv
  ID NAME             STATUS     PATH               BRAND    IP
   0 global           running    /                  native   shared
   1 zone1            running    /zones/zone1       ipkg     excl
   2 zone2            running    /zones/zone2       ipkg     excl
root:~#

Prepare the Zones for Migration and Create a EBS Snapshot

The first step is to halt the zones.

root:~# zoneadm -z zone1 halt
root:~# zoneadm -z zone2 halt

 Next, we detach the zones.

root:~# zoneadm -z zone1 detach
root:~# zoneadm -z zone2 detach

At this point the zones are in a state ready for migration. We will export the zones ZFS pool, start an EBS snapshot, re-attach the zones, and boot them. In our example the EBS volume id for the zones ZFS pool is: vol-f0c93b99

root:~# zpool export zones
root:~# ec2-create-snapshot vol-f0c93b99

SNAPSHOT  snap-c03b89a9   vol-f0c93b99    pending 2009-09-30T23:07:29+0000

root:~# zoneadm -z zone1 attach
       Global zone version: entire@0.5.11,5.11-0.111:20090514T145840Z
   Non-Global zone version: entire@0.5.11,5.11-0.111:20090514T145840Z
                Evaluation: Packages in zone1 are in sync with global zone.
Attach complete.

root:~# zoneadm -z zone1 boot
root:~# zoneadm -z zone2 attach

       Global zone version: entire@0.5.11,5.11-0.111:20090514T145840Z
   Non-Global zone version: entire@0.5.11,5.11-0.111:20090514T145840Z
                Evaluation: Packages in zone2 are in sync with global zone.
Attach complete.

root:~# zoneadm -z zone2 boot

We now have a EBS snapshot with our saved zones and have re-started the zones on the original EC2 instance. Let's keep this instance up and running until we have tested launching a new instance with our saved zones.

Launch a new EC2 Instance and Start our Zones

Okay, we now have our saved zones sitting out on a EBS snapshot. We will now create a EBS volume from this snapshot, attach the volume to a new instance, and start up the zones on the new instance.

In this example, we assume that we have a AMI available which has the capability to run a user supplied script at startup. See OpenSolaris and EC2 Parametrized Launches for instructions.

The first step is to create a EBS volume from the snapshot taken in the previous section. In our example, the snapshot id is: snap-c03b89a9

root:~# ec2-create-volume --snapshot snap-c03b89a9 -z us-east-1c 
VOLUME  vol-a433c1cd  8  snap-c03b89a9  us-east-1c  creating  2009-09-30T23:29:09+0000

We note that the new volume id is: vol-a433c1cd

Next, we build the script that will do the following:

    • Attach the new volume created from our EBS snapshot.
    • Create the zones network as described in Part 1.
    • Import the ZFS pool.
    • Attach the zones.
    • Boot the zones.

    #!/usr/bin/bash

    ebsvolumeids="vol-a433c1cd"
    zpools="zones"

    ec2autorundir="/var/ec2"
    ec2keysdir="/usr/local/aws/.ec2"
    ec2keysbucket="skodell.ec2.keys"

    ntpdate 0.north-america.pool.ntp.org

    # set the environment for EC2
    # ec2autorun.setec2 file is passed
    # to instance in payload file
    . ${ec2autorundir}/ec2autorun.setec2

    # create the directory to hold the AWS keys
    if ! [[ -d ${ec2keysdir} ]]
    then
        /usr/bin/mkdir -p ${ec2keysdir}
    else
        /usr/bin/rm -r ${ec2keysdir}/\*
    fi
    builtin cd ${ec2keysdir}
    chmod 700 ${ec2keysdir}

    # get the AWS keys
    for i in `s3cmd list ${ec2keysbucket}`
    do
        if ! [[ $i == "--------------------" ]]
        then
            s3cmd get ${ec2keysbucket}:$i $i
        fi
    done

    # get the ec2 instance id and instance type
    ec2instanceid=`curl http://169.254.169.254/latest/meta-data/instance-id --silent`
    ec2instancetype=`curl http://169.254.169.254/latest/meta-data/instance-type --silent`
    ec2publichostname=`curl http://169.254.169.254/latest/meta-data/public-hostname --silent`

    # set the starting device number for the ebs volumes
    case ${ec2instancetype} in
    'm1.small')
        ebsvolumedev=2
     ;;
    'm1.large')
        ebsvolumedev=3
        ;;
    'm1.xlarge')
        ebsvolumedev=5
        ;;
    \*)
        ebsvolumedev=5
     ;;
    esac

    # attach the volumes
    for volid in ${ebsvolumeids}
    do
        ec2-attach-volume -i ${ec2instanceid} -d ${ebsvolumedev} ${volid}
        let ebsvolumedev=${ebsvolumedev}+1
    done

    if ! [[ ${ebsvolumeids} == "" ]]
    then
        # while until all of the volumes report that they are attached
        ebsvolsattached=0
        while [[ ${ebsvolsattached} -eq 0 ]]
        do
            ebsvolsattached=1
            ebsvolumestatus=`ec2-describe-volumes | egrep ATTACHMENT | egrep ${ec2instanceid} | cut -f5`   
            for volstatus in ${ebsvolumestatus}
            do
                echo "Vol Status is: ${volstatus}"
                if ! [[ ${volstatus} == "attached" ]]
                then
                    ebsvolsattached=0
                fi
            done
            sleep 1
        done
    fi

    # import the zfs pools for this instance
    for zpoolid in ${zpools}
    do
        zpool import ${zpoolid}
    done

    # setup VLAN for zones
    dladm create-etherstub etherstub0
    dladm create-vnic -l etherstub0 vnic0
    dladm create-vnic -l etherstub0 vnic1
    dladm create-vnic -l etherstub0 vnic2

    dladm show-etherstub
    dladm show-vnic

    ifconfig vnic0 plumb
    ifconfig vnic0 inet 192.168.0.1 up
    ifconfig vnic0

    routeadm -u -e ipv4-forwarding

    # create the zones
    zonecfg -z zone1 create -a /zones/zone1
    zonecfg -z zone2 create -a /zones/zone2

    zoneadm -z zone1 attach
    zoneadm -z zone2 attach

    zoneadm -z zone1 boot
    zoneadm -z zone2 boot

    # start ipfilter
    cp ${ec2autorundir}/ec2-ipnat.conf /etc/ipf/ipnat.conf
    svcadm enable network/ipfilter


    Once we have the script created, we launch a new instance with this scrpt as described in OpenSolaris and EC2 Parametrized Launches.

    References

    Comments:

    Thanks - great series of articles and a nice introduction to EC2.

    Posted by Andy on October 14, 2009 at 07:32 PM PDT #

    How can we get status of snapshot that its created [completed] or not.
    Can you please guide on this.

    Posted by mahesha on November 16, 2010 at 01:40 AM PST #

    Post a Comment:
    • HTML Syntax: NOT allowed
    About

    Sean ODell

    Search

    Categories
    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