目的
卒業研究の一環で、最終的にKubernetesの自動クラスタ構築を行う事が出来るソリューションを作らなければならない。プロビジョニングはterraformで行いたいと考えている。
インストール
以下参考
$ sudo apt-get -y update && sudo apt-get install -y gnupg software-properties-common
wget -O- https://apt.releases.hashicorp.com/gpg | \
gpg --dearmor | \
sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg
gpg --no-default-keyring \
--keyring /usr/share/keyrings/hashicorp-archive-keyring.gpg \
--fingerprint
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] \
https://apt.releases.hashicorp.com $(lsb_release -cs) main" | \
sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update
sudo apt-get -y install terraform
/etc/libvirt/qemu.confの設定変更
security_driver = "none"
に変更する。
libvirtのリスタートは以下コマンドより
sudo systemd restart libvirt-bin
確認
$ terraform -v
Terraform v1.5.0
on linux_amd64
libvirt-providerのインストール
provider.tf
terraform {
required_providers {
libvirt = {
source = "dmacvicar/libvirt"
version = "0.7.1"
}
}
}
操作する対象が別ホストの場合
provider.tf
provider "libvirt" {
uri = "qemu+ssh://shoma@172.24.20.3/system"
}
構成ファイルの最小構成
main.tf
provider "libvirt" {
uri = "qemu+ssh://shoma@172.24.20.3/system"
}
resource "libvirt_volume" "os-image" {
name = "terraform-disk.qcow2"
source = "https://cloud-images.ubuntu.com/bionic/current/bionic-server-cloudimg-amd64.img"
format = "qcow2"
}
resource "libvirt_domain" "domain-testvm" {
name = "ubuntu-terraform"
memory = 2048
vcpu = 2
disk { volume_id = libvirt_volume.os-image.id }
network_interface {
network_name = "default"
}
console {
type = "pty"
target_port = "0"
}
graphics {
type = "spice"
listen_type = "address"
autoport = "true"
}
}
確認
root@r620-shoma-001:/home/shoma/terraform# virsh list
Id Name State
----------------------------------------
223 ubuntu-terraform-1 running
複数台のVMを作成する
main.tf
provider "libvirt" {
uri = "qemu+ssh://shoma@172.24.20.3/system"
}
resource "libvirt_volume" "os-image" {
count = 3
name = "${format("terraform-vm%02d.qcow2", count.index + 1)}"
source = "https://cloud-images.ubuntu.com/bionic/current/bionic-server-cloudimg-amd64.img"
format = "qcow2"
}
resource "libvirt_domain" "terraform-vm" {
count = 3
name = "${format("ubuntu-terraform%02d", count.index + 1)}"
memory = 2048
vcpu = 2
disk { volume_id = libvirt_volume.os-image[count.index].id }
network_interface {
network_name = "default"
}
console {
type = "pty"
target_port = "0"
}
graphics {
type = "spice"
listen_type = "address"
autoport = "true"
}
}
確認
root@r620-shoma-001:/home/shoma/terraform# virsh list
Id Name State
----------------------------------------
237 ubuntu-terraform02 running
238 ubuntu-terraform03 running
239 ubuntu-terraform01 running
参考文献