Getting started with the OpenStack API

View as Markdown

Find out how to manage your instances using the Python OpenStack client

Objective

In order to automate your operations on the Public Cloud, it is possible to use OpenStack APIs to generate different scripts.

Info

The Nova client was previously used to manage your instances and their disks. It is now deprecated and the commands have been merged into the Python OpenStack client.

For example, you can create an additional instance when your monitoring tools detect a peak load to avoid saturation on your infrastructure. It is also possible to schedule snapshot creation on a regular basis at the same time.

This guide explains how to manage your instances using the Python OpenStack client.

Requirements

Please refer to the following guides:

Instructions

You can obtain the list of possible commands by reading the embedded documentation:

admin@server-1:~$ openstack command list

You can filter the commands displayed by indicating the group.

admin@server-1:~$ openstack command list --group compute

You can also get information about a command by adding help before it:

admin@server-1:~$ openstack help flavor list 
usage: openstack flavor list [-h] [-f {csv,json,table,value,yaml}] [-c COLUMN]
                             [--quote {all,minimal,none,nonnumeric}] [--noindent]
                             [--max-width <integer>] [--fit-width] [--print-empty]
                             [--sort-column SORT_COLUMN]
                             [--sort-ascending | --sort-descending] [--public | --private | --all]
                             [--min-disk <min-disk>] [--min-ram <min-ram>] [--long]
                             [--marker <flavor-id>] [--limit <num-flavors>]
List flavors ...
Tip

You can obtain the Python Client documentation from the Python OpenStack Client documentation.

Basic Operations

Adding an SSH Public Key

As a first step, it is necessary to add a public SSH key to connect to the instances.

  • List commands associated with SSH keys.
admin@server-1:~$ openstack help | grep keypair         
  keypair create  Create new public or private key for server ssh access
  keypair delete  Delete public or private key(s)
  keypair list    List key fingerprints
  keypair show    Display key details
  • Add a public SSH key.
admin@server-1:~$ openstack keypair create --public-key ~/.ssh/id_rsa.pub SSHKEY
  • List available SSH keys:
admin@server-1:~$ openstack keypair list
+---------------+-------------------------------------------------+------+
| Name          | Fingerprint                                     | Type |
+---------------+-------------------------------------------------+------+
| SSHKEY        | 5c:fd:9d:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:3a | ssh  |
+---------------+-------------------------------------------------+------+

Listing the instance models

Next, obtain the ID of the model you want to use:

admin@server-1:~$ openstack flavor list
+--------------------------------------+-----------------+--------+------+-----------+-------+-----------+
| ID                                   | Name            |    RAM | Disk | Ephemeral | VCPUs | Is Public |
+--------------------------------------+-----------------+--------+------+-----------+-------+-----------+
| 0062dad0-f93c-4d7d-bde7-6add4ad6baaa | win-b2-15-flex  |  15000 |   50 |         0 |     4 | True      |
| 022f8ac5-b6a7-4365-9db8-c69775d67a2d | t2-180          | 180000 |   50 |         0 |    60 | True      |
| 07124b62-dd6d-4bf2-80d7-d9ea3c923cf3 | i1-180          | 180000 |   50 |         0 |    32 | True      |
| 0cb50da2-cd4d-4a14-8a22-bbc59d94c814 | c2-120-flex     | 120000 |   50 |         0 |    32 | True      |
| 0d338e52-cfba-4e32-914e-c2ea19d2a9df | d2-4            |   4000 |   50 |         0 |     2 | True      |
| 0dbcff05-2da0-40a6-87dc-96a1e98d9ffc | b2-30           |  30000 |  200 |         0 |     8 | True      |
| 11530c24-bc02-48c3-b272-802791795176 | i1-45           |  45000 |   50 |         0 |     8 | True      |
| 11fc4ed3-5198-4043-b093-063787a144e1 | c2-7            |   7000 |   50 |         0 |     2 | True      |
| 13d9146d-f519-4f8b-b87c-245d76bd21b0 | b2-120-flex     | 120000 |   50 |         0 |    32 | True      |
| ...                                  | ...             | ...    | ..   | ...       |       | ...       |
+--------------------------------------+-----------------+--------+------+-----------+-------+-----------+

Listing available images

Finally, obtain the ID of the image you want to use:

admin@server-1:~$ openstack image list 
+--------------------------------------+-----------------------------------------------+--------+
| ID                                   | Name                                          | Status |
+--------------------------------------+-----------------------------------------------+--------+
| 814f0f1d-5c06-40c6-b582-842fbf4995d2 | AlmaLinux 10                                  | active |
| 2d1914bb-266b-4049-b4bd-c195d9e920bd | AlmaLinux 10 - UEFI                           | active |
| b442e71d-2ce4-43ad-a9c6-03d0ee2359ee | AlmaLinux 8                                   | active |
| 268b6be2-68f7-42c1-8d12-ba5fab40a84b | AlmaLinux 8 - UEFI                            | active |
| f9170d19-919e-49a0-b1a3-f217fe48ab0b | AlmaLinux 8 - cPanel                          | active |
| dbb258c0-a4ff-4b97-a695-6cd99763132c | AlmaLinux 9                                   | active |
| 57659466-a19d-418e-8d98-4cb74fe1d717 | AlmaLinux 9 - UEFI                            | active |
| 326ba25a-644c-4e6f-867a-f4fbc60b9c9b | AlmaLinux 9 - cPanel                          | active |
| bc6eb0f9-2a59-4ea6-86b8-8f6a2befba5c | Baremetal - Debian 11                         | active |
| 67b90767-19bc-4f4e-8982-77c227810f02 | Baremetal - Debian 12                         | active |
| fee70d7e-e32a-44ec-af2d-20201eeeaf6d | Baremetal - Debian 13                         | active |
| ...                                  | ...                                           | ...    |
+--------------------------------------+-----------------------------------------------+--------+

Creating an instance

With the information that has been previously obtained, it is now possible to create an instance:

admin@server-1:~$ openstack server create --key-name SSHKEY --flavor d2-2 --image "Ubuntu 22.04" InstanceTest
+-----------------------------+-----------------------------------------------------+
| Field                       | Value                                               |
+-----------------------------+-----------------------------------------------------+
| OS-DCF:diskConfig           | MANUAL                                              |
| OS-EXT-AZ:availability_zone |                                                     |
| OS-EXT-STS:power_state      | NOSTATE                                             |
| OS-EXT-STS:task_state       | scheduling                                          |
| OS-EXT-STS:vm_state         | building                                            |
| OS-SRV-USG:launched_at      | None                                                |
| OS-SRV-USG:terminated_at    | None                                                |
| accessIPv4                  |                                                     |
| accessIPv6                  |                                                     |
| addresses                   |                                                     |
| adminPass                   | xxxxxxxxxxxx                                        |
| config_drive                |                                                     |
| created                     | 2022-10-13T19:05:54Z                                |
| flavor                      | d2-2 (14c5fa3f-fdad-45c4-9cd1-14dd99c341ee)         |
| hostId                      |                                                     |
| id                          | xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx                |
| image                       | Ubuntu 22.04 (0ea24976-fb6c-46ef-acb5-0cb88b0493aa) |
| key_name                    | SSHKEY                                              |
| name                        | InstanceTest                                        |
| progress                    | 0                                                   |
| project_id                  | xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx                    |
| properties                  |                                                     |
| security_groups             | name='default'                                      |
| status                      | BUILD                                               |
| updated                     | 2022-10-13T19:05:55Z                                |
| user_id                     | xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx                    |
| volumes_attached            |                                                     |
+-----------------------------+-----------------------------------------------------+

After a few moments, verify the instance list to confirm the new instance appears:

admin@server-1:~$ openstack server list                                                                 
+--------------------------------------+--------------+--------+-------------------------------------+--------------+--------+
| ID                                   | Name         | Status | Networks                            | Image        | Flavor |
+--------------------------------------+--------------+--------+-------------------------------------+--------------+--------+
| xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx | InstanceTest | ACTIVE | Ext-Net=xxxx:xxxx::xxxx, 51.xx.xx.x | Ubuntu 22.04 | d2-2   |
+--------------------------------------+--------------+--------+-------------------------------------+--------------+--------+

Deleting an instance

You can delete an instance with the following command:

admin@server-1:~$ openstack server delete InstanceTest

Go further

Join our community of users.

Was this page helpful?