-
Notifications
You must be signed in to change notification settings - Fork 1
Deploy a Second Instance using Already Resources in OpenStack
This section is intended to show how to deploy a second instance in OpenStack using already resources with Terraform. Consider all the steps outlined and it is necesssary the steps performed in section Deploy an Instance in MicroStack with Terraform. The test was done on an OpenStack based on MicroStack Canonical distribution. The deployment was performed in another machine named Deployment Machine with the following hardware requirements:
| Feature | Value |
|---|---|
| CPU | 2 |
| RAM | 4 GiB |
| Disk | 50 GB |
| OS Used | Ubuntu 20.04 LTS |
The basic connection architecture between OpenStack and Terraform is presented in the next figure:

The contents of this page are:
First, the system is updated and the necessary folders are created where the Terraform files will be placed. Execute the next commands for this:
sudo apt update && sudo apt upgrade -y
mkdir ~/terraform/scaling/ && cd ~/terraform/scaling/Now, Be sure that you have the OpenStack credentials in /etc/environment and ~/.bashrc files. If you don't know what are your OpenStack credentials please go to this link and read the configuration of the integration between OpenStack and Terraform. The aforementioned files must have:
/etc/environment
# Add to the end of file (/etc/environmet), the next OpenStack Credentials. Modify based on your RC values.
OS_AUTH_URL=http://localhost:5000/v3/
OS_PROJECT_ID=7625993ecf6d4b759984126f50d5ce4a
OS_PROJECT_NAME="default"
OS_USER_DOMAIN_NAME="Default"
OS_PROJECT_DOMAIN_ID="default"
OS_USERNAME="j.caviedes"
OS_PASSWORD=password1234
OS_REGION_NAME="microstack"
OS_INTERFACE=public
OS_IDENTITY_API_VERSION=3~/.bashrc
# Add to the end of file (~/.bashrc), the next OpenStack Credentials. Modify based on your RC values.
export OS_AUTH_URL=http://localhost:5000/v3/
export OS_PROJECT_ID=7625993ecf6d4b759984126f50d5ce4a
export OS_PROJECT_NAME="default"
export OS_USER_DOMAIN_NAME="Default"
export OS_PROJECT_DOMAIN_ID="default"
export OS_USERNAME="j.caviedes"
export OS_PASSWORD=password1234
export OS_REGION_NAME="microstack"
export OS_INTERFACE=public
export OS_IDENTITY_API_VERSION=3After modify the ~/.bashrc file, run the following command to make the change effective:
source ~/.bashrcCreate a instance.tf file and put into them the next code (replace the values with your MicroStack current configuration). Please see the next considerations before execute:
- A Debian 10 image must already be uploaded to OpenStack with the name
debian10. - You need a flavor with 2 vCPU and 4 GiB RAM, named
m1.medium. - A cloud init file must be defined.
- A network named
mgmtmust be defined before in OpenStack. - The floating IP
10.20.20.191must be available. - Consider that a security group is creating with this Terraform execution. If you have an already security group, please edit this file and use the already group.
- An already security group named
bustermust be available in OpenStack.
provider "openstack" {
}
data "openstack_images_image_v2" "debian-buster" {
name = "debian10"
most_recent = true
}
data "openstack_compute_flavor_v2" "m1-micro" {
name = "m1.medium"
}
resource "openstack_compute_instance_v2" "debian-buster" {
name = "debian-buster-scaling"
image_id = data.openstack_images_image_v2.debian-buster.id
flavor_id = data.openstack_compute_flavor_v2.m1-micro.id
security_groups = [
data.openstack_networking_secgroup_v2.buster.name
]
user_data = data.template_file.debian.template
metadata = {
prometheus = "true"
node_exporter = "true"
}
network {
name = "mgmt"
}
}
data "openstack_networking_floatingip_v2" "debian-buster-fip" {
address = "10.20.20.191"
}
resource "openstack_compute_floatingip_associate_v2" "debian-buster-fip" {
floating_ip = data.openstack_networking_floatingip_v2.debian-buster-fip.address
instance_id = openstack_compute_instance_v2.debian-buster.id
}
data "openstack_networking_secgroup_v2" "buster" {
name = "buster"
}Notice how instead of using resource, data is used to grab the resources that already exist in OpenStack. This allows Terraform to recycle resources instead of creating them from scratch. Also, see that the first part of file (named provider "openstack") is empty. The reason is that, if this part is empty, Terraform queries the environment variables (/etc/environment or ~/.bashrc) for the connection to OpenStack. Now, create a user_data.tf file in ~/terraform/scaling/ folder with the next content.
data "template_file" "debian" {
template = file("$HOME/terraform/files/debian.tpl")
}
data "template_cloudinit_config" "debian" {
gzip = false
base64_encode = false
part {
filename = "init.cfg"
content_type = "text/cloud-config"
content = data.template_file.debian.rendered
}
}The contents of the template file (in this case, located in $HOME/terraform/files/debian.tpl) are:
#cloud-config
package_upgrade: true
password: orion
chpasswd: { expire: False }
ssh_pwauth: True
runcmd:
- [ sh, -c, 'echo "nameserver 8.8.8.8" >> /etc/resolv.conf' ]
- sudo apt update && sudo apt install -y apache2
- sudo systemctl restart apache2
- sudo apt install -y prometheus-node-exporter
- sudo apt install -y stress-ng
- sudo mkdir -p /var/www/example.com
- sudo chown -R $USER:$USER /var/www/example.com
- sudo chmod -R 755 /var/www/example.com
- sudo echo -e "<html>\n\t<head>\n\t\t<title>Welcome to example.com\!</title>\n\t</head>\n\t<body>\n\t\t<h1>Success\! The example.com virtual host is working!</h1>\n\t</body>\n</html>" >> /var/www/example.com/index.html
- sudo sed -i 's/\\//g' /var/www/example.com/html/index.html
- sudo echo -e "<VirtualHost *:80>\n\tServerAdmin webmaster@localhost\n\tServerName example.com\n\tServerAlias www.example.com\n\tDocumentRoot /var/www/example.com\n\tErrorLog /var/log/apache2/error.log\n\tCustomLog /var/log/apache2/access.log combined\n</VirtualHost>" >> /etc/apache2/sites-available/example.com.conf
- sudo a2ensite example.com.conf
- sudo a2dissite 000-default.conf
- sudo apache2ctl configtest
- sudo systemctl restart apache2Note that in cloud init file we add some lines that define a SSH password (debian) to access the instance. In our case, we add this password to facilitate experiments. Also, note the initial installation of apache2, the prometheus-node-exporter and stress-ng packages. With these, it is possible to init a web server, export metrics for the monitoring with prometheus and grafana, and stress a web server if necesssary. The rest of the file is a configuration for a basic web server.
If you want to know more about prometheus and grafana monitoring for OpenStack instace resources, please see the section Using Prometheus and Grafana for Virtual Resources Monitoring.
We can now apply the configuration and see if it builds an OpenStack additional instance. For this, only execute the next lines in the ~/terraform/openstack directory. After the creation of the instance.tf, user_data.tf and debian.tpl files, perform the next step.
cd ~/terraform/openstack
terraform init
terraform applyWhen finish, the CLI shown Apply complete! Resources: 2 added, 0 changed, 0 destroyed in some part of log. You can see if the instance was created in the OpenStack UI. Also, the same configuration used to deploy a basic web server was used in this same cloud init file, so if you access the floating IP of the instance (10.20.20.191) from your browser, you should see the following message: