Deployer vos instances avec Terraform

De Ikoula Wiki
Révision datée du 6 septembre 2018 à 16:07 par Lhulotf79a8 (discussion | contributions) (Page créée avec « == Description == Nous allons voir comment déployer rapidement une ou plusieurs instance(s) Cloud Ikoula One via l'outils d'infrastructure as code nommé "Terraform" To... »)
(diff) ← Version précédente | Voir la version actuelle (diff) | Version suivante → (diff)
Jump to navigation Jump to search

Description

Nous allons voir comment déployer rapidement une ou plusieurs instance(s) Cloud Ikoula One via l'outils d'infrastructure as code nommé "Terraform"

Tout d'abord vous devrez installer Terraform si ce n'est pas déjà fait et disposer d'un compte Cloud Ikoula One. Terraform est disponible pour la plupart des OS au lien suivant : https://www.terraform.io/downloads.html

Terraform supporte plusieurs dizaines de provider dont la liste est disponible sur leur site (https://www.terraform.io/docs/providers/). Nous utiliserons le provider "cloudstack" pour déployer sur Cloud Ikoula One.


Déploiement d'une instance simple en zone basic

Nous allons écrire notre premier fichier de configuration Terraform permettant le déploiement d'une seule instance Cloud Ikoula en zone basic :

Il s'agit d'un fichier texte d'extension ".tf" (attention de ne pas créer plusieurs fichiers ".tf" au sein de votre répertoire de travail sinon Terraform les chargera tous), nous utiliserons le format Terraform classique mais il est également possible d'utiliser une syntaxe JSON.

Voici le contenu de notre fichier Terraform à adapter avec vos propres paramètres :

provider "cloudstack" {
  api_url   = "https://cloudstack.ikoula.com/client/api"
  api_key    = "< Votre clé API de votre compte/utilisateur Cloud Ikoula One  >"
  secret_key = "< Votre clé secrète de votre compte/utilisateur Cloud Ikoula One>"
}

resource "cloudstack_instance" "< Nom de votre choix pour votre ressource >" {
  zone = "< Nom de la zone basic Cloud Ikoula One de votre choix >"
  service_offering = "< Nom de l'offre de calcul Cloud Ikoula One de votre choix > "
  template = "< Nom du modèle Cloud Ikoula One de votre choix"
  name = "< Nom de votre choix pour votre instance Cloud Ikoula One"
  keypair = "< Nom de votre paire de clé SSH Cloud Ikoula One>"
  expunge = "true"
  security_group_ids = ["< ID de votre groupe de sécurité Cloud Ikoula One à utiliser >",]
}

Note : la ligne 'expunge = "true"' nous permettra de supprimer notre instance lors de l'appel "terraform destroy".


Une fois votre fichier de configuration enregistré, nous allons initialiser Terraform puis l'appliquer :

$ terraform init

Initializing provider plugins...

The following providers do not have any version constraints in configuration,
so the latest version was installed.

To prevent automatic upgrades to new major versions that may contain breaking
changes, it is recommended to add version = "..." constraints to the
corresponding provider blocks in configuration, with the constraint strings
suggested below.

* provider.cloudstack: version = "~> 0.1"

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.


Avant de lancer notre déploiement, nous pouvons vérifier ce qui sera exécuté sans rien appliquer via la commande "terraform plan" :

$ terraform plan
Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.


------------------------------------------------------------------------

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  + cloudstack_instance.TerraformVM1
      id:                            <computed>
      display_name:                  <computed>
      expunge:                       "true"
      group:                         <computed>
      ip_address:                    <computed>
      keypair:                       "MY_SSH_KEYPAIR"
      name:                          "TerraformVM1"
      network_id:                    <computed>
      project:                       <computed>
      root_disk_size:                <computed>
      security_group_ids.#:          "1"
      security_group_ids.3260590242: "84be7eef7-4pne-51c6-9abf6f3f9-c4zoek90887"
      service_offering:              "t1.pico"
      tags.%:                        <computed>
      template:                      "Debian 9 - Minimal - 64bits"
      zone:                          "US-FL-MIAMI02-Z2-BASIC"


Plan: 1 to add, 0 to change, 0 to destroy.

------------------------------------------------------------------------

Note: You didn't specify an "-out" parameter to save this plan, so Terraform
can't guarantee that exactly these actions will be performed if
"terraform apply" is subsequently run.

Si c'est bien ce que nous voulons faire alors nous pouvons appliquer via la commande "terraform apply" et saisir "yes" pour confirmer quand cela nous est demandé :

$ terraform apply

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  + cloudstack_instance.TerraformVM1
      id:                            <computed>
      display_name:                  <computed>
      expunge:                       "true"
      group:                         <computed>
      ip_address:                    <computed>
      keypair:                       "MY_SSH_KEYPAIR"
      name:                          "TerraformVM1"
      network_id:                    <computed>
      project:                       <computed>
      root_disk_size:                <computed>
      security_group_ids.#:          "1"
      security_group_ids.3260590242: "84be7eef7-4pne-51c6-9abf6f3f9-c4zoek90887"
      service_offering:              "t1.pico"
      tags.%:                        <computed>
      template:                      "Debian 9 - Minimal - 64bits"
      zone:                          "US-FL-MIAMI02-Z2-BASIC"


Plan: 1 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

cloudstack_instance.TerraformVM1: Creating...
  display_name:                  "" => "<computed>"
  expunge:                       "" => "true"
  group:                         "" => "<computed>"
  ip_address:                    "" => "<computed>"
  keypair:                       "" => "MY_SSH_KEYPAIR"
  name:                          "" => "TerraformVM1"
  network_id:                    "" => "<computed>"
  project:                       "" => "<computed>"
  root_disk_size:                "" => "<computed>"
  security_group_ids.#:          "" => "1"
  security_group_ids.3260590242: "" => "84be7eef7-4pne-51c6-9abf6f3f9-c4zoek90887"
  service_offering:              "" => "t1.pico"
  tags.%:                        "" => "<computed>"
  template:                      "" => "Debian 9 - Minimal - 64bits"
  zone:                          "" => "US-FL-MIAMI02-Z2-BASIC"
cloudstack_instance.TerraformVM1: Still creating... (10s elapsed)
cloudstack_instance.TerraformVM1: Still creating... (20s elapsed)
cloudstack_instance.TerraformVM1: Still creating... (30s elapsed)
cloudstack_instance.TerraformVM1: Still creating... (40s elapsed)
cloudstack_instance.TerraformVM1: Creation complete after 46s (ID: ba1220fc-b777-48e2-b63a-5d21ccc930ba)

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

Ici nous venons de déployer en quelques secondes, une instance nommée "TerraformVM1" avec le modèle "Debian 9 - Minimal - 64bits" dans la zone basic "US-FL-MIAMI02-Z2-BASIC" avec l'offre de calcul "t1.pico".

Nous pouvons voir celle-ci depuis notre interface Cloud Ikoula One :

Wiki terraform 1.png