You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 7 Next »

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.

Requirements

All the files and folders used here come from the same GitHub repository cloned in the KubeSpray chapter. That said, 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 thehostsscript, used to build the inventory based on TF state (this will be the base for subsequent Terraform commands).

Inventory files
$ 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.yamlthat can be stored in the current directory.

clouds.yaml
# Find the "auth_url" and "project_id" parameters in the OpenStack "Project/API Access" tab
clouds:
  openstack:
    auth:
      auth_url: https://cloud-api-pub.cr.cnaf.infn.it:5000/v3/
      username: "yourUser"
      project_name: "yourProject"
      project_id: d2b42ee4145849819b41a9d8794a111d
      user_domain_id: "default"
      password: "yourP4ssw0rd"
    region_name: "sdds"
    interface: "public"
    identity_api_version: 3

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.

environment variable OS_CLOUD
# 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)

Initialization
# 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

Provisioning cluster
$ terraform apply -var-file=cluster.tfvars ../../contrib/terraform/openstack

Bastion host

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

Destroying cluster
$ terraform destroy -var-file=cluster.tfvars ../../contrib/terraform/openstack

Debugging

You can enable debugging output from Terraform by setting OS_DEBUG to 1 and TF_LOG to DEBUG before running the Terraform command.

  • No labels