Chapter 11. Creating a customized instance
Cloud users can specify additional data to use when they launch an instance, such as a shell script that the instance runs on boot.
The cloud user can use the following methods to pass data to instances:
- User data
-
Use to include instructions in the instance launch command for
cloud-initto run. - Instance metadata
A list of key-value pairs that you can specify when you create or update an instance.
You can access the additional data passed to the instance by using a config drive or the metadata service.
- Config drive
-
You can attach a config drive to an instance when it boots. The config drive is presented to the instance as a read-only drive. The instance can mount this drive and read files from it. You can use the config drive as a source for
cloud-initinformation. Config drives are useful when combined withcloud-initfor server bootstrapping, and when you want to pass large files to your instances. For example, you can configurecloud-initto automatically mount the config drive and run the setup scripts during the initial instance boot. Config drives are created with the volume label ofconfig-2, and attached to the instance when it boots. The contents of any additional files passed to the config drive are added to theuser_datafile in theopenstack/{version}/directory of the config drive.cloud-initretrieves the user data from this file. - Metadata service
Provides a REST API to retrieve data specific to an instance. Instances access this service at
169.254.169.254or atfe80::a9fe:a9fe.cloud-initcan use both a config drive and the metadata service to consume the additional data for customizing an instance. Thecloud-initpackage supports several data input formats. Shell scripts and thecloud-configformat are the most common input formats:-
Shell scripts: The data declaration begins with
#!orContent-Type: text/x-shellscript. Shell scripts are invoked last in the boot process. cloud-configformat: The data declaration begins with#cloud-configorContent-Type: text/cloud-config.cloud-configfiles must be valid YAML to be parsed and executed bycloud-init.Notecloud-inithas a maximum user data size of 16384 bytes for data passed to an instance. You cannot change the size limit, therefore use gzip compression when you need to exceed the size limit.
-
Shell scripts: The data declaration begins with
Vendor-specific data
The administrator can also pass data to instances when they are being created. This data might not be visible to you as the cloud user, for example, a cryptographic token that registers the instance with Active Directory.
The administrator uses the vendordata feature to pass data to instances. Vendordata configuration is read only and is located in one of the following files:
-
/openstack/{version}/vendor_data.json -
/openstack/{version}/vendor_data2.json
You can view these files using the metadata service or from the config drive on your instance. To access the files by using the metadata service, make a GET request to either 169.254.169.254/openstack/{version}/vendor_data.json or 169.254.169.254/openstack/{version}/vendor_data2.json.
To run openstack client commands on the cloud, you must specify the name of the cloud detailed in your clouds.yaml file. You can specify the name of the cloud by using one of the following methods:
Use the
--os-cloudoption with each command, for example:$ openstack flavor list --os-cloud <cloud_name>Use this option if you access more than one cloud.
Create an environment variable for the cloud name in your
bashrcfile:`export OS_CLOUD=<cloud_name>`
11.1. Customizing an instance by using user data Copy linkLink copied to clipboard!
You can use user data to include instructions in the instance launch command. cloud-init executes these commands to customize the instance as the last step in the boot process.
Prerequisites
-
The administrator has created a project for you and they have provided you with a
clouds.yamlfile for you to access the cloud. -
You have installed the
python-openstackclientpackage.
Procedure
Create a file with instructions for
cloud-init. For example, create a bash script that installs and enables a web server on the instance:$ vim /home/scripts/install_httpd #!/bin/bash yum -y install httpd python-psycopg2 systemctl enable httpd --nowLaunch an instance with the
--user-dataoption to pass the bash script:$ openstack server create \ --image rhel8 \ --flavor default \ --nic net-id=web-server-network \ --security-group default \ --key-name web-server-keypair \ --user-data /home/scripts/install_httpd \ --wait web-server-instanceWhen the instance state is active, attach a floating IP address:
$ openstack floating ip create web-server-network $ openstack server add floating ip web-server-instance 172.25.250.123Log in to the instance with SSH:
$ ssh -i ~/.ssh/web-server-keypair cloud-user@172.25.250.123Check that the customization was successfully performed. For example, to check that the web server has been installed and enabled, enter the following command:
$ curl http://localhost | grep Test <title>Test Page for the Apache HTTP Server on Red Hat Enterprise Linux</title> <h1>Red Hat Enterprise Linux <strong>Test Page</strong></h1>Review the
/var/log/cloud-init.logfile for relevant messages, such as whether or not thecloud-initexecuted:$ sudo less /var/log/cloud-init.log ...output omitted... ...util.py[DEBUG]: Cloud-init v. 0.7.9 finished at Sat, 23 Jun 2018 02:26:02 +0000. Datasource DataSourceOpenStack [net,ver=2]. Up 21.25 seconds
11.2. Customizing an instance by using metadata Copy linkLink copied to clipboard!
You can use instance metadata to specify the properties of an instance in the instance launch command.
Prerequisites
-
The administrator has created a project for you and they have provided you with a
clouds.yamlfile for you to access the cloud. -
You have installed the
python-openstackclientpackage.
Procedure
Launch an instance with the
--property <key=value>option. For example, to mark the instance as a webserver, set the following property:$ openstack server create \ --image rhel8 \ --flavor default \ --property role=webservers \ --wait web-server-instanceOptional: Add an additional property to the instance after it is created, for example:
$ openstack server set \ --property region=emea \ --wait web-server-instance
11.3. Customizing an instance by using a config drive Copy linkLink copied to clipboard!
You can create a config drive for an instance that is attached during the instance boot process. You can pass content to the config drive that the config drive makes available to the instance.
Prerequisites
-
The administrator has created a project for you and they have provided you with a
clouds.yamlfile for you to access the cloud. -
You have installed the
python-openstackclientpackage.
Procedure
Enable the config drive, and specify a file that contains content that you want to make available in the config drive. For example, the following command creates a new instance named
config-drive-instanceand attaches a config drive that contains the contents of the filemy-user-data.txt:$ openstack server create --flavor m1.tiny \ --config-drive true \ --user-data ./my-user-data.txt \ --image cirros config-drive-instanceThis command creates the config drive with the volume label of
config-2, which is attached to the instance when it boots, and adds the contents ofmy-user-data.txtto theuser_datafile in theopenstack/{version}/directory of the config drive.- Log in to the instance.
Mount the config drive:
If the instance operating system uses
udev:# mkdir -p /mnt/config # mount /dev/disk/by-label/config-2 /mnt/configIf the instance operating system does not use
udev, you need to first identify the block device that corresponds to the config drive:# blkid -t LABEL="config-2" -odevice /dev/vdb # mkdir -p /mnt/config # mount /dev/vdb /mnt/config