Attaching volumes to an instance

Now that we have a usable volume, we can attach this to any instance. We do this by using the nova volume-attach command in the Nova client.

Getting ready

To begin with, ensure you are logged in to the Ubuntu client that has access to the Nova client tools. If using the Vagrant environment that accompanies the book, you can access these tools from the controller node by running this command:

vagrant ssh controller

This recipe assumes you have created an openrc file. To create an openrc file on each node where you need it, open a text file named openrc and add the following contents:

export OS_TENANT_NAME=cookbook
export OS_USERNAME=admin
export OS_PASSWORD=openstack
export OS_AUTH_URL=https://192.168.100.200:5000/v2.0/
export OS_KEY=/path/to/cakey.pem
export OS_CACERT=/path/to/ca.pem

These packages can be installed using the following commands:

sudo apt-get update
sudo apt-get install python-novaclient

How to do it...

Carry out the following steps to attach a volume to an instance using the Nova client:

  1. If you have no instance running, spin one up. Once it is running, run the nova list command and note the instance ID:
    source openrc
    nova list --fields name
    

    The following output is generated:

    +--------------------------------------+-------+
    | ID                                   | Name  |
    +--------------------------------------+-------+
    | f9659289-82f1-435f-98fc-add99c7a611b | test1 |
    +--------------------------------------+-------+
    
  2. Using the instance ID, we can attach the volume to our running instance, as follows:
    nova volume-attach <instance_id> <volume_id> /dev/vdc
    
  3. The preceding command will output the name of the volume when successful. To view this, log into your running instance and view the volume that is now attached by running the following:
    sudo fdisk -l /dev/vdc
    
  4. We should see 1 GB of space available for the running instance. As this is like adding a fresh disk to a system, you need to format it for use and then mount it as part of your filesystem, as shown here:
    sudo mkfs.ext4 /dev/vdc
    sudo mkdir /mnt1
    sudo mount /dev/vdc /mnt1
    
  5. We should now see the newly-attached disk available at /mnt1, as shown here:
    df -h
    Filesystem   Size Used Avail Use% Mounted on
    /dev/vda    1.4G 602M 733M 46% /
    devtmpfs    248M  12K 248M  1% /dev
    none       50M 216K  50M  1% /run
    none      5.0M   0 5.0M  0% /run/lock
    none      248M   0 248M  0% /run/shm
    /dev/vdb    5.0G 204M 4.6G  5% /mnt
    /dev/vdc        1.0G  204M  784M   5% /mnt1
    

How it works...

Attaching a Cinder volume is no different from plugging in a USB stick on your own computer; similar to a USB stick, a Cinder volume can only be attached to a single host and must be formatted and mounted.

Under Nova client, the option volume-attach takes the following syntax:

nova volume-attach instance_id volume_id device

The instance_id parameter is the ID returned from the Nova list for the instance that we want to attach the volume to. The volume_id is the name of the device within the instance that we will use to mount the volume that can be retrieved using the nova volume-list. This device is the device that will be created on our instance that we use to mount the volume.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset