Terraform (henceforth TF) is an open-source infrastructure as code software tool that provides a consistent CLI workflow to manage hundreds of cloud services. Terraform codifies cloud APIs into declarative configuration files. For this page we refer to this GitHub project.
The necessary prerequisites are as follows:
- Install TF;
- Install Ansible;
- you already have a floating IP pool created;
- you have security groups enabled;
- you have a pair of keys generated that can be used to secure the new hosts.
Configuration
Inventory files
Create a directory for your cluster, mycluster
for istance, by copying the existing sample-inventory
and linking thehosts
script, used to build the inventory based on TF state (this will be the base for subsequent Terraform commands).
Code Block |
---|
language | bash |
---|
title | Inventory files |
---|
collapse | true |
---|
|
$ cp -LRp contrib/terraform/openstack/sample-inventory inventory/mycluster
$ cd inventory/mycluster
$ ln -s ../../contrib/terraform/openstack/hosts
$ ln -s ../../contrib |
OpenStack access and credentials
TF supports various authentication methods for OpenStack. The recommended authentication method is to describe credentials in a YAML fileclouds.yaml
, that can be stored in the current directory.
...
If you have multiple clouds defined in your clouds.yaml
file you can choose the one you want to use with the environment variable OS_CLOUD
.
Code Block |
---|
language | bash |
---|
title | environment variable OS_CLOUD |
---|
collapse | true |
---|
|
# Insert this line in the .bashrc file for variable persistence
export OS_CLOUD=openstack
# To apply the changes
$ source .bashrc |
Cluster variables
The construction of the cluster is driven by values found in ../../contrib/terraform/openstack/
variables.tf
. You can consult this file to find out which variables are available for configuration, accompanied by a brief description, which values they accept and their defaults. For your cluster, edit cluster.tfvars
.
Provisioning VMs
Initialization
Before TF can operate on your cluster you need to install the required plugins. This is accomplished as follows (this should finish fairly quickly telling you TF has successfully initialized and loaded necessary modules)
Code Block |
---|
language | bash |
---|
title | Initialization |
---|
collapse | true |
---|
|
# Launch from the path inventory/mycluster
$ terraform init ../../contrib/terraform/openstack |
Provisioning cluster
You can apply the Terraform configuration to your cluster with the following command issued from the usual path
Code Block |
---|
language | bash |
---|
title | Provisioning cluster |
---|
collapse | true |
---|
|
$ terraform apply -var-file=cluster.tfvars ../../contrib/terraform/openstack |
Info |
---|
|
If you chose to create a bastion host, this script will create ../../ contrib/terraform/openstack/k8s-cluster.yml with an ssh command for Ansible to be able to access your machines tunneling through the bastion's IP address. If you want to manually handle the ssh tunneling to these machines, please delete or move that file. If you want to use this, just leave it there, as ansible will pick it up automatically. |
Destroying cluster
You can destroy your new cluster with the following command
Code Block |
---|
language | bash |
---|
title | Destroying cluster |
---|
collapse | true |
---|
|
$ terraform destroy -var-file=cluster.tfvars ../../contrib/terraform/openstack |