How to manage VMs with OpenStack command line tools

OpenStack is an industry-standard open-source cloud management platform. Using OpenStack, one can build public, private or hybrid clouds easily. Due to the purely open nature of the platform, major IT vendors including Red Hat, Rackspace, IBM and HP are betting on its future, actively contributing to OpenStack development.

In OpenStack, there are two different interfaces for managing cloud resources. One is via Horizon, web-based OpenStack dashboard, and the other is via OpenStack command line interface (CLI).

In this tutorial, I am going to demonstrate how to create or terminate VMs on OpenStack from the command line. This was tested in the Havana release of OpenStack. For earlier versions of OpenStack such as OpenStack Folsom, you can simply replace "neutron" with "quantum" in the command line used throughout the tutorial.

I assume that there is an OpenStack deployment already up and running somewhere. I am going to use OpenStack CLI clients to manage VMs on the existing OpenStack setup.

Install OpenStack CLI Clients

The first step is to install necessary OpenStack command line clients.

On Debian, Ubuntu or Linux Mint:

$ sudo apt-get install python-pip
$ sudo pip install python-novaclient python-keystoneclient python-neutronclient

On CentOS, Fedora or RHEL:

$ sudo yum install python-pip
$ sudo pip install python-novaclient python-keystoneclient python-neutronclient

Set Environment Variables

In order to be able to use OpenStack CLI clients, you need to set up necessary environment variables. For this purpose, create a script named openrc.sh as follows.

$ sudo vi openrc.sh
export OS_USERNAME=dan
export OS_PASSWORD=my_password
export OS_TENANT_NAME=demo
export OS_AUTH_URL=http://192.168.10.10:5000/v2.0

In the above, OS_USERNAME/OS_PASSWORD are OpenStack user name and its password. OS_TENANT_NAME is the name of the project created. OS_AUTH_URL is the URL of the Keystone endpoint. Keystone is an OpenStack service responsible for authentication among different OpenStack components. You should replace 192.168.10.10 with the IP address of the host where OpenStack Keystone is running in your deployment.

Now, set the environment variables by running openrc.sh as follows.

$ source openrc.sh

At this point, you are ready to run OpenStack command line clients. Verify that you do not encounter any error when running:

$ nova list

It shows an empty result, it means everything is okay.

Create a Network

Now you are ready to create a network to connect your VMs to.

Create a new network named "xmodulo".

$ neutron net-create xmodulo

Create a new subnet named "xmodulo_subnet", and add it to the network just created.

$ neutron subnet-create xmodulo 10.0.0.0/24 --name xmodulo_subnet

Check a list of available networks to verify that the network has been created successfully.

$ neutron net-list

In the output, make a note of the "id" of the network you created. This id will be used when you create a VM later.

Launch a VM

Before creating and launch a VM, you need to know a few pieces of information first.

Find out the type of VM that you want to create. For that, run the following command.

$ nova flavor-list

In this example, I am going to choose the smallest VM type "m1.nano", which has 64 MB, 1 vCPU and no disk. Make a note of this type name.

Next, choose the VM image to use for your VM. To get a list of all available VM images, use this command:

$ nova image-list

Make a note of the ID of the image that you want to use for your VM.

Next, choose the type of security group to use for your VM. A security group determines inbound access rules for your VM. To find out available security groups:

$ nova secgroup-list

To check the access rules of the "default" security group, use this command:

$ nova secgroup-list-rules default

In this example, I am going to choose a security group named "default" for the VM. This security group happens to have no rule in it.

Finally, let's create a VM using the information obtained thus far. Specify the VM type (--flavor), ID of VM image (--image), ID of a network (net-id=) that you have found out. Replace [vm-name] with the name of your VM, which needs to be unique.

$ nova boot [vm-name] --flavor m1.nano --image d2b830be-37df-4fa9-90b2-91c472d19aaa --security-groups default --nic net-id=1cbcddcf-3a7d-481f-b6f2-a97c6447c925

To verify that the VM has been successfully created and launched, run this command:

$ nova list

Stop, Suspend or Purge a VM

When you stop a running VM, it gets shutdown completely. On the other hand, when you suspend a VM, it is temporarily frozen, and can be restarted from the suspende state any time. In both cases, the VM image still remains in OpenStack.

To stop a VM:

$ nova stop [vm-name]

To suspend a VM:

$ nova suspend [vm-name]

If you want to purge the image of either stopped or suspended VM from OpenStack, use the command:

$ nova delete [vm-name]

Subscribe to Xmodulo

Do you want to receive Linux FAQs, detailed tutorials and tips published at Xmodulo? Enter your email address below, and we will deliver our Linux posts straight to your email box, for free. Delivery powered by Google Feedburner.

The following two tabs change content below.
Dan Nanni is the founder and also a regular contributor of Xmodulo.com. He is a Linux/FOSS enthusiast who loves to get his hands dirty with his Linux box. He likes to procrastinate when he is supposed to be busy and productive. When he is otherwise free, he likes to watch movies and shop for the coolest gadgets.
Your name can also be listed here. Write for us as a freelancer.

Leave a comment

Your email address will not be published. Required fields are marked *