How to create snapshot of instance in openstack

In this tutotrial, we will see how to create snapshot of instance and how create a new instance from snapshot in openstack. Some time we need multiple instances with same configuration or sometime we need to move one instance from one Openstack setup to another Openstack setup. In this case we can use snapshot feature of Openstack, which provide us to take a backup of our instance as an image and we can use it in future. So first off all we should know what is snapshot?

What is snapshot?

Snapshot is an Openstack feature which provides us to take backup of our instance, and we can deploy a new instance with the help of this snapshot as a same backup state. We can take a snapshot of our instance before any up-gradation or any changes which may be impact our instance. If our instance is going to down then we can use snapshot to create a new instance with same state.

In this tutorial we will cover these steps:-

  1. Take snapshot of instance.
  2. Download snapshot image.
  3. Boot a new instance from snapshot.
  4. Delete snapshot.

Take snapshot of instance

We find our instance where we want to take a snapshot, with the help of nova list command. See below example:-

[email protected]:~$ nova list
+--------------------------------------+---------------+--------+------------+-------------+----------------------------------------------------------------------------+
| ID                                   | Name          | Status | Task State | Power State | Networks                                                                   |
+--------------------------------------+---------------+--------+------------+-------------+----------------------------------------------------------------------------+
| 5aa3612d-9f25-4777-9275-2ff6900a8496 | cirros        | ACTIVE | -          | Running     | provider=172.21.144.217                                                    |
+--------------------------------------+---------------+--------+------------+-------------+----------------------------------------------------------------------------+
[email protected]:~$

To take a snapshot we need to stop our instance first. Then we can start snapshot of instance. So let’s stop our instance using nova stop command.

#Command:-nova stop <instance-name>

[email protected]:~$ nova stop cirros
Request to stop server cirros has been accepted.
[email protected]:~$

Check your instance properly shutdown or not with nova list command.

[email protected]:~$ nova list
+--------------------------------------+---------------+---------+------------+-------------+----------------------------------------------------------------------------+
| ID                                   | Name          | Status  | Task State | Power State | Networks                                                                   |
+--------------------------------------+---------------+---------+------------+-------------+----------------------------------------------------------------------------+
| 5aa3612d-9f25-4777-9275-2ff6900a8496 | cirros        | SHUTOFF | -          | Shutdown    | provider=172.21.144.217                                                    |
+--------------------------------------+---------------+---------+------------+-------------+----------------------------------------------------------------------------+
[email protected]:~$

You can see in above output, our cirros instance has been successfully shutdown.

Now we can take snapshot of our instance cirros. We can use below command to take snapshot of instance. You can see our snapshot has been started successfully in below output.

#Command:- nova image-create –poll <instance-name> <snapshot-name>

[email protected]:~$ nova image-create --poll cirros CirrosSnapshot

Server snapshotting... 25% complete

In below output we can see we have successfully completed snapshot of cirros instance.

[email protected]:~$ nova image-create --poll cirros CirrosSnapshot

Server snapshotting... 100% complete
Finished
[email protected]:~$

We can check cirros instance snapshot status using nova image list command.

[email protected]:~$ nova image-list
+--------------------------------------+-------------------------------------------------+--------+--------------------------------------+
| ID                                   | Name                                            | Status | Server                               |
+--------------------------------------+-------------------------------------------------+--------+--------------------------------------+
| b35c4a6c-5ce3-4f2a-a2cb-5143c883a0be | CirrosSnapshot                                  | ACTIVE | 5aa3612d-9f25-4777-9275-2ff6900a8496 |
| 23f9a666-1255-47ca-a296-955c6801db46 | cirros                                          | ACTIVE |                                      |
+--------------------------------------+-------------------------------------------------+--------+--------------------------------------+
[email protected]:~$

We can see our snapshot is active and now we can use it to deploy new instance.

Download snapshot image

We can download this image if we want to move this image on other openstack environment or we can use this snapshot to boot new instance with same state on same openstack environment.

For download you can use this commands. where snapshot.raw is file name and your snapshot image id like below.

[email protected]:~$ glance image-download --file snapshot.raw b35c4a6c-5ce3-4f2a-a2cb-5143c883a0be
[email protected]:~$

You can see in below we have successfully downloaded snapshot image of cirros instance.

[email protected]:~$ pwd
/home/test
[email protected]:~$ ls -l snapshot.raw
-rw-rw-r-- 1 test test 22151168 Aug  7 20:38 snapshot.raw
[email protected]:~$ file snapshot.raw
snapshot.raw: QEMU QCOW Image (unknown version)
[email protected]:~$

Boot a new instance from the snapshot

Now I am going to use CirrosSnapshot image to create a new instance

First of all we need flavor, image and network ID to create a new instance. We can find these types of information using below commands.

Display openstack flavor list by using below command. We can see, we have a cirros-test flavor.

[email protected]:~$ openstack flavor list
+--------------------------------------+------------------+-------+------+-----------+-------+-----------+
| ID                                   | Name             |   RAM | Disk | Ephemeral | VCPUs | Is Public |
+--------------------------------------+------------------+-------+------+-----------+-------+-----------+
| 91e1107c-819a-4ff3-99ec-14ae91555f27 | cirros-test      |  1024 |    5 |         0 |     1 | True      |
+--------------------------------------+------------------+-------+------+-----------+-------+-----------+
[email protected]:~$

You can find network list using below command in your openstack setup where network ID will need to be mention at the time of boot new instance from snapshot image.

[email protected]:~$ openstack network list
+--------------------------------------+-------------------+--------------------------------------+
| ID                                   | Name              | Subnets                              |
+--------------------------------------+-------------------+--------------------------------------+
| 31483d3c-ae83-41fc-b7a8-0ab8112648c7 | provider          | fa60e6a0-cdcf-461e-aa9b-3f3f1d885e2d |
+--------------------------------------+-------------------+--------------------------------------+
[email protected]:~$

You can get image list using this commands. Image name will be mention at the time of boot new instance from snapshot image.

[email protected]:~$ nova image-list
+--------------------------------------+-------------------------------------------------+--------+--------------------------------------+
| ID                                   | Name                                            | Status | Server                               |
+--------------------------------------+-------------------------------------------------+--------+--------------------------------------+
| b35c4a6c-5ce3-4f2a-a2cb-5143c883a0be | CirrosSnapshot                                  | ACTIVE | 5aa3612d-9f25-4777-9275-2ff6900a8496 |
| 23f9a666-1255-47ca-a296-955c6801db46 | cirros                                          | ACTIVE |                                      |
+--------------------------------------+-------------------------------------------------+--------+--------------------------------------+
[email protected]:~$

In above output we can see. We have 2 images, one is cirros image and another is CirrosSnapshot image which we have taken snapshot of cirros instance.

Now I am going to use CirrosSnapshot image and cirros-test flavor to create a new instance with below command.

#Command:-nova boot –flavor <flavor-name> –nic net-id=<network-id> –image <snapshot-image-name> <new-instance-name>

[email protected]:~$ nova boot --flavor cirros-test --nic net-id=31483d3c-ae83-41fc-b7a8-0ab8112648c7 --image CirrosSnapshot New-Cirros
+--------------------------------------+-------------------------------------------------------+
| Property                             | Value                                                 |
+--------------------------------------+-------------------------------------------------------+
| OS-DCF:diskConfig                    | MANUAL                                                |
| OS-EXT-AZ:availability_zone          |                                                       |
| OS-EXT-SRV-ATTR:host                 | -                                                     |
| OS-EXT-SRV-ATTR:hostname             | new-cirros                                            |
| OS-EXT-SRV-ATTR:hypervisor_hostname  | -                                                     |
| OS-EXT-SRV-ATTR:instance_name        | instance-00000679                                     |
| OS-EXT-SRV-ATTR:kernel_id            |                                                       |
| OS-EXT-SRV-ATTR:launch_index         | 0                                                     |
| OS-EXT-SRV-ATTR:ramdisk_id           |                                                       |
| OS-EXT-SRV-ATTR:reservation_id       | r-og670xmm                                            |
| OS-EXT-SRV-ATTR:root_device_name     | -                                                     |
| OS-EXT-SRV-ATTR:user_data            | -                                                     |
| OS-EXT-STS:power_state               | 0                                                     |
| OS-EXT-STS:task_state                | scheduling                                            |
| OS-EXT-STS:vm_state                  | building                                              |
| OS-SRV-USG:launched_at               | -                                                     |
| OS-SRV-USG:terminated_at             | -                                                     |
| accessIPv4                           |                                                       |
| accessIPv6                           |                                                       |
| adminPass                            | 9oGMt3AJeWzs                                          |
| config_drive                         |                                                       |
| created                              | 2018-08-08T06:06:35Z                                  |
| description                          | -                                                     |
| flavor                               | cirros-test (91e1107c-819a-4ff3-99ec-14ae91555f27)    |
| hostId                               |                                                       |
| host_status                          |                                                       |
| id                                   | 35813d11-2bc3-41be-94e3-cc157355b24b                  |
| image                                | CirrosSnapshot (b35c4a6c-5ce3-4f2a-a2cb-5143c883a0be) |
| key_name                             | -                                                     |
| locked                               | False                                                 |
| metadata                             | {}                                                    |
| name                                 | New-Cirros                                            |
| os-extended-volumes:volumes_attached | []                                                    |
| progress                             | 0                                                     |
| security_groups                      | default                                               |
| status                               | BUILD                                                 |
| tenant_id                            | 40a2ab4b966d42869c900d5475fbb7e3                      |
| updated                              | 2018-08-08T06:06:35Z                                  |
| user_id                              | 6f0e3f0521524a0e9469112bdb45635c                      |
+--------------------------------------+-------------------------------------------------------+
[email protected]:~$

We can see we have successfully launched new instance with snapshot image. We can check new instance status with the help of nova list command.

[email protected]:~$ nova list
+--------------------------------------+---------------+---------+------------+-------------+----------------------------------------------------------------------------+
| ID                                   | Name          | Status  | Task State | Power State | Networks                                                                   |
+--------------------------------------+---------------+---------+------------+-------------+----------------------------------------------------------------------------+
| 35813d11-2bc3-41be-94e3-cc157355b24b | New-Cirros    | ACTIVE  | -          | Running     | provider=172.21.144.212                                                    |
| 5aa3612d-9f25-4777-9275-2ff6900a8496 | cirros        | SHUTOFF | -          | Shutdown    | provider=172.21.144.217                                                    |
+--------------------------------------+---------------+---------+------------+-------------+----------------------------------------------------------------------------+
[email protected]:~$

You can see in above output we have 2 instances where cirros is our old instance and New-Cirros is our new instance which is create by cirros instance snapshot.

So we can create snapshot of any instance and we can create a new instance from snapshot.

Delete snapshot

If you want to delete your snapshot then you can use below command.

#Command:-nova image-delete <snapshot-name>

[email protected]:~$ nova image-delete CirrosSnapshot
[email protected]:~$

You can see we have successfully removed our CirrosSnapshot snapshot image from Openstack.

You can also verify with nova image-list command to check snapshot has been deleted or not?

[email protected]:~$ nova image-list
+--------------------------------------+-------------------------------------------------+--------+--------------------------------------+
| ID                                   | Name                                            | Status | Server                               |
+--------------------------------------+-------------------------------------------------+--------+--------------------------------------+
| 23f9a666-1255-47ca-a296-955c6801db46 | cirros                                          | ACTIVE |                                      |
+--------------------------------------+-------------------------------------------------+--------+--------------------------------------+
[email protected]:~$

You can see in above output we have only cirros image and our CirrosSnapshot image has been deleted.

That’s all we have successfully created new instance with snapshot image in openstack.

1 thought on “How to create snapshot of instance in openstack

Leave a Reply

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