Bookmark this page

Guided Exercise: Customizing an Instance at Launch with Cloud-init

In this exercise, you will customize two instances using cloud-init capabilities and features. You will log in to the instances to confirm cloud-init is up and running, and verify that cloud-init has correctly customized the two instances.

Outcomes

You should be able to customize an instance using a cloud-config script or file and verify cloud-init operations by checking the /var/log/cloud-init.log file or that the requested customization has occurred.

Confirm that the workstation and overcloud's virtual machines are started.

Log in to workstation as student using student as the password.

On workstation, run the lab cloudapps-cloudinit start command.

[student@workstation ~]$ lab cloudapps-cloudinit start

Procedure 8.1. Instructions

  1. Customize an instance using the Dashboard. Log in to the Dashboard using Example as the domain, developer1 as the user, and redhat as the password. Click the Project menu in the upper-right corner and ensure that finance is the current project. Launch an instance named finance-server1 with the rhel8 image, the default flavor, the finance-network1 network, the default security group, and the example-keypair key pair. Create a customization script that includes "Hello world!" in the /root/hello.txt file in the instance.

    1. On workstation, open a web browser and navigate to http://dashboard.overcloud.example.com. Log in to the Dashboard using Example as the domain, developer1 as the user, and redhat as the password.

    2. Navigate to ComputeInstances and then click Launch Instance.

    3. On the Details tab, enter finance-server1 as the Instance Name.

    4. On the Source tab, choose Image in the Select Boot Source field. Set Create New Volume to No. In the Available section, click the up arrow for the rhel8-web image.

    5. On the Flavor tab, in the Available section, click the up arrow for the default flavor.

    6. On the Networks tab, in the Available section, click the up arrow for the finance-network1 network.

    7. On the Security Groups tab, ensure that the default security group has been selected.

    8. On the Key Pair tab, ensure that the example-keypair key pair has been selected.

    9. On the Configuration tab, add the following content to the Customization Script field, and then click Launch Instance:

      #!/bin/sh
      echo 'Hello world!' > /root/hello.txt
  2. When the instance is active, attach a floating IP address to the finance-server1 instance.

    1. When the instance status changes to Active, attach a floating IP address to it. In the Actions menu for the finance-server1 instance, click Associate Floating IP.

      IP Address section, click + to create a new floating IP address. Click Allocate IP and then click Associate.

    2. Sign out of the Dashboard.

  3. On workstation, source the developer1-finance-rc file and create a user-data file, called install_httpd, to customize the instance. The script will install the web server and enable the service.

    [student@workstation ~]$ source developer1-finance-rc
    [student@workstation ~(developer1-finance)]$ vim /home/student/install_httpd
    #!/bin/bash
    # web server with a postgres backend
    yum -y install httpd python3-psycopg2
    systemctl enable httpd --now

    This script will install and enable the web server service.

  4. Customize an instance using the command line. As the developer1 user launch an instance named finance-server2 with the rhel8-web image, the default flavor, the finance-network1 network, the default security group, and the example-keypair key pair. Include the /home/student/install_httpd user-data script.

    1. Launch an instance using the --user-data option to perform the customization.

      [student@workstation ~(developer1-finance)]$ openstack server create \
      > --image rhel8-web \
      > --flavor default \
      > --nic net-id=finance-network1 \
      > --security-group default \
      > --key-name example-keypair \
      > --user-data /home/student/install_httpd \
      > --wait finance-server2
      ...output omitted...
    2. Verify that the status of the finance-server2 instance is active.

      [student@workstation ~(developer1-finance)]$ openstack server list \
      > -c Name -c Status -c Networks
      +-----------------+--------+------------------------------------------------+
      | Name            | Status | Networks                                       |
      +-----------------+--------+------------------------------------------------+
      | finance-server2 | ACTIVE | finance-network1=192.168.1.80                  |
      | finance-server1 | ACTIVE | finance-network1=192.168.1.168, 172.25.250.105 |
      +-----------------+--------+------------------------------------------------+
  5. When the instance state is active, create a new floating IP address and attach it to the instance. Verify that the floating IP has been assigned. Note that the IP addresses are provided from a pool and will be different each time.

    [student@workstation ~(developer1-finance)]$ openstack floating ip create \
    > provider-datacentre
    +---------------------+------------------------------------------+
    | Field               | Value                                    |
    +---------------------+------------------------------------------+
    | created_at          | 2020-07-29T01:54:06Z                     |
    | description         |                                          |
    | dns_domain          |                                          |
    | dns_name            |                                          |
    | fixed_ip_address    | None                                     |
    | floating_ip_address | 172.25.250.106                           |
    | floating_network_id | aa59f24c-117a-43d3-bcda-421e40e074b7     |
    | id                  | c7dee859-39be-490b-8ca4-e11b893dfb34     |
    | location            | Munch({'cloud': '', 'region_name':       |
    |                     | 'regionOne', 'zone': None, 'project':    |
    |                     | Munch({'id':                             |
    |                     | '5916c863837d4f9dbfe8ce523bef489d',      |
    |                     | 'name': 'finance', 'domain_id': None,    |
    |                     | 'domain_name': 'Example'})})             |
    | name                | 172.25.250.155                           |
    | port_details        | None                                     |
    | port_id             | None                                     |
    | project_id          | 5916c863837d4f9dbfe8ce523bef489d         |
    | qos_policy_id       | None                                     |
    | revision_number     | 0                                        |
    | router_id           | None                                     |
    | status              | DOWN                                     |
    | subnet_id           | None                                     |
    | tags                | []                                       |
    | updated_at          | 2020-07-29T01:54:06Z                     |
    +---------------------+------------------------------------------+
    [student@workstation ~(developer1-finance)]$ openstack server add floating ip \
    > finance-server2 172.25.250.106
    [student@workstation ~(developer1-finance)]$ openstack server list \
    > -c Name -c Networks
    +-----------------+--------+------------------------------------------------+
    | Name            | Status | Networks                                       |
    +-----------------+--------+------------------------------------------------+
    | finance-server2 | ACTIVE | finance-network1=192.168.1.80, 172.25.250.106  |
    | finance-server1 | ACTIVE | finance-network1=192.168.1.168, 172.25.250.105 |
    +-----------------+--------+------------------------------------------------+
  6. Log in to finance-server1 to verify that the cloud-init customization script created the /root/hello.txt file.

    1. Log in to finance-server1 with ssh using the example-keypair private key.

      [student@workstation ~(developer1-finance)]$ ssh -i ~/.ssh/example-keypair \
      > cloud-user@172.25.250.105
    2. Verify within /var/log/cloud-init.log to confirm that cloud-init ran.

      [cloud-user@finance-server1 ~]$ sudo less /var/log/cloud-init.log
      ...output omitted...
      ...util.py[DEBUG]: Cloud-init v. 18.5 finished at Wed, 29 Jul 2020 01:46:23 +0000. Datasource DataSourceOpenStack [net,ver=2].  Up 60.75 seconds
    3. Ensure that the /root/hello.txt file exists and has the correct content.

      [cloud-user@finance-server1 ~]$ sudo cat /root/hello.txt
      Hello world!
    4. Log out from finance-server1.

      [cloud-user@finance-server1 ~]$ exit
      logout
      Connection to 172.25.250.105 closed.
  7. Log in to finance-server2 to verify that the /home/student/install_httpd user-data script installed and enabled the httpd service.

    1. Determine the floating IP address allocated to the finance-server2 instance.

      [student@workstation ~(developer1-finance)]$ openstack server show \
      > finance-server2 -c addresses -f value
      finance-server2=192.168.1.80, 172.25.250.106
    2. Using ssh and the example-keypair private key, log in to finance-server2.

      [student@workstation ~(developer1-finance)]$ ssh -i ~/.ssh/example-keypair \
      > cloud-user@172.25.250.106
    3. Verify within /var/log/cloud-init.log to confirm that cloud-init ran.

      [cloud-user@finance-server2 ~]$ sudo less /var/log/cloud-init.log
      ...output omitted...
      ...util.py[DEBUG]: Cloud-init v. 18.5 finished at Wed, 29 Jul 2020 01:53:25 +0000. Datasource DataSourceOpenStack [net,ver=2].  Up 65.32 seconds
    4. Confirm that httpd is working.

      [cloud-user@finance-server2 ~]$ 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>
    5. On workstation, use the curl command to navigate to http://172.25.250.106. The connection must succeed.

      [student@workstation ~(developer1-finance)]$ curl http://172.25.250.106
      ...output omitted...

Finish

On the workstation machine, use the lab command to complete this exercise. This is important to ensure that resources from previous exercises do not impact upcoming exercises.

[student@workstation ~]$ lab cloudapps-cloudinit finish

This concludes the guided exercise.

Revision: cl110-16.1-4c76154