DEV Community

Cover image for SNAP+LXD+TERRAFORM
Anderson Gama
Anderson Gama

Posted on • Updated on

SNAP+LXD+TERRAFORM

Instalação do LXD e utilização com o Terraform localmente

Instalar o SNAP caso não tenha

sudo apt install -y snapd
Enter fullscreen mode Exit fullscreen mode

Instalar o LXD

sudo snap install lxd --channel=4.1/stable --classic
Enter fullscreen mode Exit fullscreen mode

Inicializar a configuração do LXD

sudo lxd init
Enter fullscreen mode Exit fullscreen mode
Would you like to use LXD clustering? (yes/no) [default=no]: 
Do you want to configure a new storage pool? (yes/no) [default=yes]: 
Name of the new storage pool [default=default]: 
Name of the storage backend to use (ceph, btrfs, dir, lvm) [default=btrfs]: 
Create a new BTRFS pool? (yes/no) [default=yes]: 
Would you like to use an existing block device? (yes/no) [default=no]: 
Size in GB of the new loop device (1GB minimum) [default=15GB]: 5GB
Would you like to connect to a MAAS server? (yes/no) [default=no]: 
Would you like to create a new local network bridge? (yes/no) [default=yes]: 
What should the new bridge be called? [default=lxdbr0]: 
What IPv4 address should be used? (CIDR subnet notation, “auto” or “none”) [default=auto]: 
What IPv6 address should be used? (CIDR subnet notation, “auto” or “none”) [default=auto]: 
Would you like LXD to be available over the network? (yes/no) [default=no]: yes
Address to bind LXD to (not including port) [default=all]: 
Port to bind LXD to [default=8443]: 
Trust password for new clients: 
Again: 
Would you like stale cached images to be updated automatically? (yes/no) [default=yes] 
Would you like a YAML "lxd init" preseed to be printed? (yes/no) [default=no]: yes
Enter fullscreen mode Exit fullscreen mode
config:
  core.https_address: '[::]:8443'
  core.trust_password: mobile
networks:
- config:
    ipv4.address: auto
    ipv6.address: auto
  description: ""
  name: lxdbr0
  type: ""
storage_pools:
- config:
    size: 5GB
  description: ""
  name: default
  driver: btrfs
profiles:
- config: {}
  description: ""
  devices:
    eth0:
      name: eth0
      network: lxdbr0
      type: nic
    root:
      path: /
      pool: default
      type: disk
  name: default
cluster: null
Enter fullscreen mode Exit fullscreen mode

Copiar o perfil padrão

lxc profile copy default ubuntu-profile
Enter fullscreen mode Exit fullscreen mode

Customizar o perfil criado

lxc profile set ubuntu-profile boot.autostart true
lxc profile set ubuntu-profile boot.autostart.delay 1
lxc profile set ubuntu-profile limits.cpu 1
lxc profile set ubuntu-profile limits.memory 64MB
lxc profile device set ubuntu-profile root size 512MB
Enter fullscreen mode Exit fullscreen mode

Caso deseje alterar a porta do LXD

lxc config set core.https_address :8443
Enter fullscreen mode Exit fullscreen mode

Caso deseje alterar a senha do LXD

lxc config set core.trust_password mobile
Enter fullscreen mode Exit fullscreen mode

Adicionar acesso local ao LXD

lxc remote add mobile 127.0.0.1 --accept-certificate
lxc remote set-default mobile
Enter fullscreen mode Exit fullscreen mode

Copiar imagem do Ubuntu para o LXD

lxc image copy images:ubuntu/focal mobile: --alias focal --auto-update
Enter fullscreen mode Exit fullscreen mode

Configuração do Terraform para uso com o LXD

cd /tmp
Enter fullscreen mode Exit fullscreen mode
curl -s https://api.github.com/repos/sl1pm4t/terraform-provider-lxd/releases/latest | jq '.assets | .[] | .browser_download_url' | grep "linux_amd64"
Enter fullscreen mode Exit fullscreen mode

Resultado: "https://github.com/sl1pm4t/terraform-provider-lxd/releases/download/v1.3.0/terraform-provider-lxd_v1.3.0_linux_amd64.zip"

wget -c https://github.com/sl1pm4t/terraform-provider-lxd/releases/download/v1.3.0/terraform-provider-lxd_v1.3.0_linux_amd64.zip
Enter fullscreen mode Exit fullscreen mode
mkdir -p ~/terraform/teste/.terraform/plugins/linux_amd64/
Enter fullscreen mode Exit fullscreen mode
unzip terraform-provider-lxd_v1.3.0_linux_amd64.zip -d ~/terraform/teste/.terraform/plugins/linux_amd64/
Enter fullscreen mode Exit fullscreen mode
cd ~/terraform/teste/
Enter fullscreen mode Exit fullscreen mode
nano -c provider.tf
Enter fullscreen mode Exit fullscreen mode
provider "lxd" {
  generate_client_certificates = true
  accept_remote_certificate    = true

  lxd_remote {
    name     = "mobile"
    scheme   = "https"
    address  = "127.0.0.1"
    password = "mobile"
    default  = true
  }
}
Enter fullscreen mode Exit fullscreen mode
nano -c lxd_container.tf
Enter fullscreen mode Exit fullscreen mode
resource "lxd_container" "ubuntu" {
  name      = "ubuntu"
  image     = "focal"
  profiles  = ["ubuntu-profile"]
  ephemeral = false

  config = {
    "boot.autostart" = true
  }

  limits = {
    cpu = 1
  }
}
Enter fullscreen mode Exit fullscreen mode
terraform init
terraform plan
terraform apply
Enter fullscreen mode Exit fullscreen mode

Top comments (0)