Skip to content

Infrastructure-as-a-Service > Clusters

Warning

Il faut ajouter sa clef SSH dans son agent SSH avant d'utiliser la cli terraform.

~/projet-cloud $ eval "$(ssh-agent)"
~/projet-cloud $ ssh-add

Dépendances

~/projet-cloud/pkgx.yaml
env:
  OS_CLOUD: 'resinfo-discotech@stratuslab.production.virtualdata' #(1)
  TF_VAR_VM_PREFIX: 'k3s' #(2)
  TF_VAR_IMAGE_NAME: 'jammy-server-cloudimg-amd64.img' #(3)
  TF_VAR_SSH_USER: 'ubuntu' #(4)
  TF_VAR_FLAVOR_NAME: 'vd.4' #(5)
  TF_VAR_PUBLIC_NETWORK: 'public-2' #(6)
  TF_VAR_KEYPAIR: 'mykey' #(7)
dependencies:
  jq: ^1.7
  yq: ^4.25.2
  terraform.io: ^1.5
  task: ^3.30.1
  openssh.com: ^9.5
  1. Le nom de l'entrée dans la config client OpenStack.
  2. Prefixe le nom de toutes les VMs.
  3. Image disque utilisée pour le déploiement des VMs.
  4. Le login utilisateur sous lequel se connecter à la VM.
  5. Le nom du gabarit (CPU/RAM/DISK) de VM à déployer.
  6. Le nom du réseau fournissant des IPs publiques.
  7. Le nom de la clef SSH à utiliser pour se connecter.

Tâches

~/projet-cloud/Taskfile.yaml
version: '3'

tasks:

  default:
    silent: true
    cmds:
    - task --list

  deploy:
    desc: deploy
    cmds:
    - |
      bash <<-'EOS'
      source <(pkgx --shellcode)
      terraform apply --auto-approve
      EOS

Activation

~ $ cd projet-cloud
   ~/.pkgx/github.com/kkos/oniguruma/v6.9.8
   ~/.pkgx/stedolan.github.io/jq/v1.7.0
env +stedolan.github.io/jq^1.7 +github.com/mikefarah/yq^4.35.2 +terraform.io^1.5 +taskfile.dev^3.30.1 +gnu.org/coreutils^9.4
~/projet-cloud/ $

Terraform

~/projet-cloud/ $ ls -al | grep tf 

Variables

~/projet-cloud/terraform__variables.tf
variable VM_PREFIX {
  type = string
}

variable IMAGE_NAME {
  type = string
}

variable SSH_USER {
  type = string
}

variable COMPUTE_COUNT {
  type = number
  default = 3
}

variable FLAVOR_NAME {
  type = string
}

variable PUBLIC_NETWORK {
  type = string
}

variable KEYPAIR {
  type = string
}

Resources

~/projet-cloud/resource__openstack_compute_instance_v2.tf
resource openstack_compute_instance_v2 bootstrap {
  name = "manager-1"
  image_name = var.IMAGE_NAME
  flavor_name = var.FLAVOR_NAME
  key_pair = var.KEYPAIR
  network {
    name = var.PUBLIC_NETWORK
  }

  connection {
    type = "ssh"
    user = var.SSH_USER
    host = self.access_ip_v4
  }

  provisioner remote-exec {
    inline = [
      "hostname"
    ]
  }
}

resource openstack_compute_instance_v2 compute {
  count = var.COMPUTE_COUNT
  name = "compute-${count.index+1}"
  image_name = var.IMAGE_NAME
  flavor_name = var.FLAVOR_NAME
  key_pair = var.KEYPAIR
  network {
    name = var.PUBLIC_NETWORK
  }

  connection {
    type = "ssh"
    user = var.SSH_USER
    host = self.access_ip_v4
  }
  provisioner remote-exec {
    inline = [
      "hostname"
    ]
  }
}

Sorties

~/projet-cloud/terraform__outputs.tf
output bootstrap_user {
  value = var.SSH_USER
}

output bootstrap_ip {
  value = openstack_compute_instance_v2.bootstrap.access_ip_v4
}

output compute_user {
  value = [ for i in range(var.COMPUTE_COUNT): var.SSH_USER ]
}

output compute_ip {
  value = openstack_compute_instance_v2.compute.*.access_ip_v4
}

Execution

~/projet-cloud/ $ task list
~/projet-cloud/ $ task deploy