Serverspace Black Friday
DC
26 de agosto de 2022
Actualizado junio 7, 2023

Cómo automatizar la implementación del servidor a través de Terraform?

VPS

Terraform

Terraform proveedor Serverspace le permite administrar los recursos de la nube mediante el enfoque Infraestructura como código. La administración se realiza mediante archivos de configuración especiales en los que describe el estado deseado de su infraestructura.

Instalación Terraform

Primero, instalemos Terraform en su sistema operativo. Recomendamos utilizar el Terraform manual del desarrollador.

Configuración del proveedor

    1. Crear una API clave para el proyecto que trabajará con Terraform.
    2. Cree y navegue hasta el directorio que se usará para trabajar con Terraform Proveedor.
    3. Cree y abra el archivo de configuración de provider.tf.
    4. Inserte la información del proveedor en el archivo, donde es tuyo API clave, y guarde los cambios:
    5. terraform {
      required_providers {
      serverspace = {
      source = "itglobalcom/serverspace"
      version = "0.2.2"
      }
      }
      }

      variable "s2_token" {
      type = string
      default = ""
      }

      provider "serverspace" {
      key = var.s2_token
      }

    6. Abra un símbolo del sistema y navegue hasta el directorio donde creó el archivo. Realice una inicialización para verificar que es correcto:
      terraform init

      Verá el siguiente mensaje si la inicialización es exitosa:

      Initializing the backend...
      Initializing provider plugins...
      - Finding itglobalcom/serverspace versions matching "0.2.0"...
      - Installing itglobalcom/serverspace v0.2.0...
      - Installed itglobalcom/serverspace v0.2.0 (self-signed, key ID 062XXXXXXXXXXXX)

      Partner and community providers are signed by their developers.
      If you'd like to know more about provider signing, you can read about it here: https://www.terraform.io/docs/cli/plugins/signing.html

      Terraform has created a lock file .terraform.lock.hcl to record the provider selections it made above. Include this file in your version control repository so that Terraform can guarantee to make the same selections by default when you run "terraform init" in the future.

      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.

Descripción de su infraestructura

Con Serverspace Terraform Proveedor, puede crear infraestructura con varias configuraciones. Puede encontrar documentación detallada en el Terraform Registro.

Echemos un vistazo al ejemplo de creación de una infraestructura simple con dos servidores conectados a través de una red aislada:

  • Servidor en Amsterdam llamado "servidor-1" con Ubuntu 20.04 sistema operativo. Configuración del servidor - 2 GB RAMde 2 CPUs, 40GB SSD unidad de arranque, 20 GB adicionales SSD unidad, 50 Mbit/s. SSH conexión al servidor. Almacenamiento de datos Redis instalado en el servidor.
  • Servidor en Amsterdam llamado "servidor-2" con Ubuntu 20.04 sistema operativo. Configuración del servidor - 8 GB RAMde 4 CPUs, 40GB SSD unidad de arranque, 70 Mbps.
  • Red aislada en Ámsterdam con dirección 192.168.0.0/24

Siga los pasos a continuación para configurar esta infraestructura:

    1. Cree y abra el archivo ssh_key.tf, que contendrá la parte pública de la clave ssh para crear el servidor.
    2. Inserte la información sobre la parte pública de la clave ssh en el archivo ssh_key.tf y guarde los cambios, reemplazando el contenido de la variable public_key con su valor:
      resource "serverspace_ssh" "terraform" {
      name = "terraform-key"
      public_key = "ssh-rsa AAAAB3Nza...JUDjlM= root@CentOS.local"
      }

      variable "pvt_key" {
      type = string
      default = ""
      }

    3. Cree y abra el archivo main.tf que contendrá la descripción de la infraestructura.
    4. Inserte la descripción de su infraestructura en el archivo.
      resource "serverspace_server" "server1" {
      image = "Ubuntu-20.04-X64"
      name = "server-1"
      location = "am2"
      cpu = 2
      ram = 2048

      boot_volume_size = 40*1024

      volume {
      name = "bar"
      size = 20*1024
      }

      nic {
      network = ""
      network_type = "PublicShared"
      bandwidth = 50
      }
      nic {
      network = resource.serverspace_isolated_network.my_net.id
      network_type = "Isolated"
      bandwidth = 0
      }

      ssh_keys = [
      resource.serverspace_ssh.terraform.id,
      ]

      connection {
      host = self.public_ip_addresses[0]
      user = "root"
      type = "ssh"
      private_key = file(var.pvt_key)
      timeout = "2m"
      }

      provisioner "remote-exec" {
      inline = [
      "export PATH=$PATH:/usr/bin",
      "sudo apt-get update",
      "sudo apt-get install -y redis-server",
      "exit 0"
      ]
      }

      }

      resource "serverspace_server" "server2" {
      image = "Ubuntu-20.04-X64"
      name = "server-2"
      location = "am2"
      cpu = 4
      ram = 8192

      boot_volume_size = 40*1024

      nic {
      network = ""
      network_type = "PublicShared"
      bandwidth = 70
      }
      nic {
      network = resource.serverspace_isolated_network.my_net.id
      network_type = "Isolated"
      bandwidth = 0
      }
      }

      resource"serverspace_isolated_network" "my_net" {
      location = "am2"
      name = "my_net"
      description = "Example for Terraform"
      network_prefix = "192.168.0.0"
      mask = 24
      }

    5. Para crear la infraestructura descrita anteriormente, ejecute el comando:
      terraform apply

      Aparecerá una ventana de diálogo:

      Terraform used the selected providers to generate the following execution plan.
      Resource actions are indicated with the following symbols:
      + create

      Terraform will perform the following actions:

      # serverspace_isolated_network.my_net will be created
      + resource "serverspace_isolated_network" "my_net" {
      + description = "Example for Terraform"
      + id = (known after apply)
      + location = "am2"
      + mask = 24
      + name = "my_net"
      + network_prefix = "192.168.0.0"
      }

      # serverspace_server.server1 will be created
      + resource "serverspace_server" "server1" {
      + boot_volume_id = (known after apply)
      + boot_volume_size = 40960
      + cpu = 2
      + id = (known after apply)
      + image = "Ubuntu-20.04-X64"
      + location = "am2"
      + name = "server-1"
      + public_ip_addresses = (known after apply)
      + ram = 2048
      + ssh_keys = (known after apply)

      + nic {
      + bandwidth = 0
      + id = (known after apply)
      + ip_address = (known after apply)
      + network = (known after apply)
      + network_type = "Isolated"
      }
      + nic {
      + bandwidth = 50
      + id = (known after apply)
      + ip_address = (known after apply)
      + network_type = "PublicShared"
      }

      + volume {
      + id = (known after apply)
      + name = "bar"
      + size = 20480
      }
      }

      # serverspace_server.server2 will be created
      + resource "serverspace_server" "server2" {
      + boot_volume_id = (known after apply)
      + boot_volume_size = 40960
      + cpu = 4
      + id = (known after apply)
      + image = "Ubuntu-20.04-X64"
      + location = "am2"
      + name = "server-2"
      + public_ip_addresses = (known after apply)
      + ram = 8192
      + ssh_keys = (known after apply)

      + nic {
      + bandwidth = 0
      + id = (known after apply)
      + ip_address = (known after apply)
      + network = (known after apply)
      + network_type = "Isolated"
      }
      + nic {
      + bandwidth = 70
      + id = (known after apply)
      + ip_address = (known after apply)
      + network_type = "PublicShared"
      }
      }

      # serverspace_ssh.terraform will be created
      + resource "serverspace_ssh" "terraform" {
      + id = (known after apply)
      + name = "terraform-key"
      + public_key = "ssh-rsa AAAAB3Nza...JUDjlM= root@CentOS.local"
      }

      Plan: 4 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.

    6. Introduzca sí para crear la infraestructura.
      Enter a value: yesserverspace_ssh.terraform: Creating...
      serverspace_isolated_network.my_net: Creating...
      serverspace_ssh.terraform: Creation complete after 1s [id=3181]
      serverspace_isolated_network.my_net: Creation complete after 8s [id=l2n403]
      serverspace_server.server2: Creating...
      serverspace_server.server1: Creating...
      serverspace_server.server1: Still creating... [10s elapsed]
      serverspace_server.server2: Still creating... [10s elapsed]
      ...
      serverspace_server.server1 (remote-exec): (Reading database ...
      serverspace_server.server1 (remote-exec): (Reading database ... 5%
      serverspace_server.server1 (remote-exec): (Reading database ... 10%
      ...
      serverspace_server.server1: Creation complete after 1m3s [id=l2s190038]
      Apply complete! Resources: 4 added, 0 changed, 0 destroyed.
    7. Para ver la infraestructura actual, ejecute el comando:
      terraform show

      Esto mostrará la configuración de infraestructura actual:


      # serverspace_isolated_network.my_net:
      resource "serverspace_isolated_network" "my_net" {
      description = "Example for Terraform"
      id = "l2n403"
      location = "am2"
      mask = 24
      name = "my_net"
      network_prefix = "192.168.0.0"
      }

      # serverspace_server.server1:
      resource "serverspace_server" "server1" {
      boot_volume_id = 58909
      boot_volume_size = 40960
      cpu = 2
      id = "l2s190038"
      image = "Ubuntu-20.04-X64"
      location = "am2"
      name = "server-1"
      public_ip_addresses = [
      "45.138.24.19",
      ]
      ram = 2048
      ssh_keys = [
      3181,
      ]

      nic {
      bandwidth = 0
      id = 59576
      ip_address = "192.168.0.1"
      network = "l2n403"
      network_type = "Isolated"
      }
      nic {
      bandwidth = 50
      id = 59575
      ip_address = "45.138.24.19"
      network_type = "PublicShared"
      }

      volume {
      id = 58910
      name = "bar"
      size = 20480
      }
      }

      # serverspace_server.server2:
      resource "serverspace_server" "server2" {
      boot_volume_id = 58911
      boot_volume_size = 40960
      cpu = 4
      id = "l2s190039"
      image = "Ubuntu-20.04-X64"
      location = "am2"
      name = "server-2"
      public_ip_addresses = [
      "31.44.3.68",
      ]
      ram = 8192
      ssh_keys = []

      nic {
      bandwidth = 0
      id = 59578
      ip_address = "192.168.0.2"
      network = "l2n403"
      network_type = "Isolated"
      }
      nic {
      bandwidth = 70
      id = 59577
      ip_address = "31.44.3.68"
      network_type = "PublicShared"
      }
      }

      # serverspace_ssh.terraform:
      resource "serverspace_ssh" "terraform" {
      id = "3181"
      name = "terraform-key"
      public_key = "ssh-rsa AAAAB3Nza...JUDjlM= root@CentOS.local"
      }

Felicidades por tu primera infraestructura como implementación de código.

Votar:
5 de 5
Calificación promedio: 5
Calificado por: 1
1101 CT Ámsterdam Países Bajos, Herikerbergweg 292
+31 20 262-58-98
700 300
ITGLOBAL.COM NL
700 300
Utilizamos cookies para hacer que su experiencia en el Serverspace mejor. Al continuar navegando en nuestro sitio web, usted acepta nuestros
Uso de Cookies y Normas sobre la privacidad.