Cloning Instances

There are often occasions when you may need more than one of a particular instance, and want the installation and configuration of operating systems and software to be carried forward into additional instances. This can be achieved by using what are called golden images. They allow you to set a default state that your instances will then be created with, meaning that you can guarantee uniform configuration between multiple instances when they are first spun up.

Note

The method below assume that your operating system and other software installed on your instance reside entirely on the first volume attached to the instance, also known as the boot volume. There is no support for copying and starting instances with multiple attached volumes.

The steps required to create a golden image are:

  1. Prepare the source instance to be copied. The steps needed here depend on the OS and software installed.

  2. Shut down the instance.

  3. Create a new volume using the existing boot volume as a source.

  4. Create an image from the new volume.

  5. Delete the duplicated volume created above.

  6. Use the golden image you created in the previous step to then create your new instances.

Some steps can be performed using the dashboard, though some require the use of the command line. As such, we have only documented an example using the command-line tools:

Preparing the source instance

In general, we want any new instances that we create using a golden image to perform certain steps like it is a new instance despite the source we are using having already been launched and configured

For Linux operating systems, the tools in the OS images provided by Catalyst Cloud will detect that our new instance has been launched as a new copy, and reset the hostname and other details according to what the cloud has configured for these attributes.

For other software installed in the instance, you will need to add scripts to the system to reset the state when tooling detects a new instance has been created.

The cloud-init tool in most Linux images provides detection of a new instance and performs the reset of hostname and other details as described above. However, it can also be used to run scripts within the image without needing to have these passed into the instance.

Consult cloud-init’s documentation for more information on how to hook these scripts into the process.

Create a new volume copy

Note

You must have sourced an openrc file before you can use any of the command-line functions described here.

First, you need to identify the volume ID that you need to copy. This is done by retrieving the volumes attached to the instance you are cloning, then creating a copy of the first volume. For example:

$ openstack server show <UUID_OR_NAME_OF_SERVER> | grep volumes_attached

| volumes_attached            | id='0a8f8181-5c92-4367-ae26-XXXXXXXXXXXX'                |

$ openstack volume create --source 0a8f8181-5c92-4367-ae26-XXXXXXXXXXXX <TEMPORARY_NAME>

In the example above, the volume attached to the instance has the ID 0a8f8181-5c92-4367-ae26-XXXXXXXXXXXX, and so we created a new volume using that ID as our source.

Create image from volume copy

Next we create a new image from the volume copy. By default the image will not be shared anywhere except your own project, although it will be marked as shared.

$ openstack image create --volume <UUID_OF_COPY_VOLUME> <NAME_FOR_IMAGE>

Note

The name of the image should be something easily identified for what it contains. However it does not have to be unique. This can be used combined with some tools to enable selecting a “latest” image version by using the same name.

After this is complete, the image should be available which you can verify using the command below:

$ openstack image list --name <NAME_OF_IMAGE>

Delete the volume copy

Before you start launching instances, it is recommended that you clean up the volume that was created earlier, so that it does not get used for any other purposes. This is important as the copied volume has a dependency on the original volume it was sourced from. You cannot delete the original volume until all dependant copies are also deleted. It can be hard to identify where volumes have dependencies on each other and so it is recommended to clean up your volume immediately as you only need the image that was created going forward.

$ openstack volume delete <UUID_OF_COPY_VOLUME>

Create new instance using image

You can now create a new instance using the image you have created. The image will appear in the dashboard when creating an instance, or you can use the command-line for any image option with the openstack server create command.