YJWANG

Terraform libvirt에 vm 배포하기 (Basic) 본문

91.IaC

Terraform libvirt에 vm 배포하기 (Basic)

왕영주 2020. 10. 2. 22:30

github

개인 서버 환경이라 volume base 위치나 data_pool 이름이 다를 수 있다. 이는 개인 환경에 맞춰서 설정하시거나 변수로 지정하여서 사용하시면 좋을 것 같기에 드리는 참고용 링크이다.

https://github.com/YoungjuWang/iaac/tree/master/yjwang_libvirt_tf

사전 준비

우선 terraform 설치libvirt plugin 설치는 아래 포스팅에서 참고하며 본 포스팅에서는 이 과정에 대해 설명하지 않는다.

https://yjwang.tistory.com/15

Terraform 작성 (basic)

.tf 확장자로 파일을 생성하며 아래 링크에 example 들이 많기에 참고하여 작성하기에 어려움은 없었다. pluginterraform 공식 홈페이지이니 참고하여 더 편하게 작성할 수 있었다.

https://github.com/dmacvicar/terraform-provider-libvirt
https://www.terraform.io/docs/configuration/index.html

vm(domain) 생성

disk와 network 없이 4 core에 4G 메모리를 갖는 test라는 이름의 서버를 생성하는 스크립트이다.

cat domain.tf

provider "libvirt" {
    uri = "qemu:///system"
}

resource "libvirt_domain" "test" {
# Resources
    name = "test"
    arch = "x86_64"
    vcpu  = "4"
    memory = "4096"

# Console
    console {
      type = "pty"
      target_type = "serial"
      target_port = "/dev/pts/1"
    }
}

이후 # terraform plan 을 통해 작성한 스크립트가 문제가 없는 지 확인하고 # terraform apply를 통해 적용한다.

이후 infra 가 필요 없어진 경우 # terraform destroy 통해서 자동으로 만든 항목을 제거할 수도 있다. 나는 이 점이 편리하게 느껴졌다.

vm (domain) 생성 with network, disk

위 예시와 다르게 vm을 이번엔 interface랑 disk를 붙여 생성해볼 것이다. 이정도만 해보시면 git및 제가 작성한 git을 참고하셔서 작성하시는데에 큰 무리 없을 것으로 봅니다.

provider "libvirt" {
    uri = "qemu:///system"
}

resource "libvirt_domain" "test" {
# Resources
    name = "test"
    arch = "x86_64"
    vcpu  = "4"
    memory = "4096"

# Disk
    disk {
      file = "/data/img/yjwang_10_centos78"
    }

# Network
    network_interface {
      network_name = "default"
    }

# Console
    console {
      type = "pty"
      target_type = "serial"
      target_port = "/dev/pts/1"
    }
}

예시 git엔 cloud-init으로 volume을 사용했으니 참고하시기 바랍니다.

반응형