Week 11 Lab: Automating Cloud Deployment
Module: Operating Systems 3 (Virtualisation & Cloud Technologies)
Estimated Time: 60 Minutes
Lab Type: Scripting / CLI
Lab Overview
By the end of this lab, you will have moved beyond "ClickOps". You will write code that deploys a web server automatically.
Objectives:
1. Write "User Data": Create a Cloud-Init YAML file to install software.
2. Script the CLI: Write a Bash script to launch an instance.
3. Parse Output: Use jq to extract the Floating IP automatically.
Prerequisites:
- MicroStack installed.
jqinstalled on your Ubuntu VM (sudo apt install jq).- Existing
private-netandexternalnetworks. - Important: Ensure Port 80 is open! Run:
bash openstack security group rule create --proto tcp --dst-port 80 default
Part 1: The Cloud-Init Configuration (User Data)
Cloud-Init runs on the first boot. We tell it what to do using YAML.
-
Create the Config File: On your Ubuntu VM (running MicroStack), create a file named
web-setup.yaml.bash nano web-setup.yaml -
Add the Configuration:
#cloud-config
package_update: true
packages:
- apache2
runcmd:
- echo "<h1>Hello from Automation!</h1>" > /var/www/html/index.html
- systemctl enable apache2
- systemctl start apache2
-
Field Report:
- Filename created:
[ _____________________________ ] - What software are we installing?:
[ _____________________________ ]
- Filename created:
Part 2: The Deployment Script (Bash + OpenStack CLI)
We will write a script that does everything: Launch -> Wait -> Assign IP.
-
Create the Script:
bash nano deploy.sh -
Add the Logic (Type this carefully):
#!/bin/bash
# Variables
VM_NAME="auto-web-01" # Hint: Use "auto-web-$(date +%s)" to avoid name collisions!
IMAGE="ubuntu" # Or 'cirros' if RAM is tight
FLAVOR="m1.tiny"
NET="private-net"
KEY="lab-key"
SEC_GROUP="default"
echo "--- Starting Deployment of $VM_NAME ---"
# 1. Launch Instance
echo "1. Launching Server..."
openstack server create \
--flavor $FLAVOR \
--image $IMAGE \
--network $NET \
--key-name $KEY \
--security-group $SEC_GROUP \
--user-data web-setup.yaml \
--wait \
$VM_NAME
# 2. Get Floating IP
echo "2. Requesting Floating IP..."
fip=$(openstack floating ip create external -f json | jq -r .floating_ip_address)
echo " Got IP: $fip"
# 3. Assign IP
echo "3. Assigning IP to Server..."
openstack server add floating ip $VM_NAME $fip
echo "--- Deployment Complete! ---"
echo "Access your site at: http://$fip"
- Make executable:
chmod +x deploy.sh
Part 3: Execution and Testing
- Run the script:
bash ./deploy.sh -
Wait:
- The script will finish in ~30 seconds.
- BUT: The VM needs time to boot and run the Cloud-Init script (installing Apache takes ~2 minutes).
-
Verify:
-
Pro Tip: Run
ssh ubuntu@<IP> sudo cloud-init status --waitto see when it finishes! - Open
http://<THE_FLOATING_IP>in your browser. - Success Condition: You see "Hello from Automation!".
Important Observation: This script is not idempotent. If you run it a second time, it will fail because the server
auto-web-01already exists. This is why we need tools like Heat and Ansible. -
Field Report:
- Floating IP Assigned:
[ _____________________________ ] - Did the Apache Page load?
[ Yes / No ]
- Floating IP Assigned:
Lab Checkpoint
- I have installed
jqto parse JSON. - I have created a valid Cloud-Init YAML file.
- I have successfully run a Bash script to deploy a full stack.
Reflection:
- Why did we need
jqin the script? Could we have done it withgrep? - If we ran this script twice, what would happen? (Hint: It would fail on the second run because the name
auto-web-01already exists. How could we fix that?). - Troubleshooting: If Apache didn't start, where would you look? (Expected:
/var/log/cloud-init-output.loginside the VM).
Looking Ahead: In the next lab, we will replace this Bash script with a Heat Template, making our deployment declarative and repeatable.
Instructor Signature: ___ Date: ___