安装Terraform {#%E5%AE%89%E8%A3%85terraform}
安装依赖
apt install software-properties-common gnupg2 curl
</code> </pre>
导入 GPG 密钥
curl -fsSL https://apt.releases.hashicorp.com/gpg | apt-key add - </code> </pre> 添加存储库 ````````````````````bash apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main" </code> </pre> 安装 Terraform ```````````````````bash apt install terraform </code> </pre> 测试并检查Terraform版本 ``````````````````bash terraform version </code> </pre> 安装KVM {#%E5%AE%89%E8%A3%85kvm} ------------------------------ 安装kvm和libvirtd服务 `````````````````bash apt install qemu-kvm libvirt-daemon-system </code> </pre> 开启虚拟化功能 {#%E5%BC%80%E5%90%AF%E8%99%9A%E6%8B%9F%E5%8C%96%E5%8A%9F%E8%83%BD} -------------------------------------------------------------------------- ### Linux虚拟机 {#linux%E8%99%9A%E6%8B%9F%E6%9C%BA} 对于Ubuntu虚拟机,需要打开虚拟化功能,可以在VMWare的虚拟机\<设置-处理器\>中打开 ### Linux物理机 {#linux%E7%89%A9%E7%90%86%E6%9C%BA} 检查 KVM 模块是否已经加载 ````````````````bash lsmod | grep kvm </code> </pre> 输出应该包括 kvm_intel 或 kvm_amd(取决 CPU 类型)。如果没有输出,可以手动加载 KVM 模块: ```````````````bash sudo modprobe kvm sudo modprobe kvm_intel # 对于 Intel CPU sudo modprobe kvm_amd # 对于 AMD CPU </code> </pre> 检查 CPU 虚拟化支持 ``````````````bash egrep -c '(vmx|svm)' /proc/cpuinfo </code> </pre> 如果输出为 0,则表示虚拟化未启用,只能通过进入 BIOS/UEFI 设置启用虚拟化技术。 验证 QEMU 安装和路径 {#%E9%AA%8C%E8%AF%81-qemu-%E5%AE%89%E8%A3%85%E5%92%8C%E8%B7%AF%E5%BE%84} -------------------------------------------------------------------------------------- `````````````bash which qemu-system-x86_64 </code> </pre> 如果路径正确,输出应为 /usr/bin/qemu-system-x86_64。如果路径不正确或没有输出,可能需要重新安装 启动 libvirt 服务 {#%E5%90%AF%E5%8A%A8-libvirt-%E6%9C%8D%E5%8A%A1} -------------------------------------------------------------- ````````````bash systemctl start libvirt && systemctl enable libvirt </code> </pre> 创建并启动存储池 {#%E5%88%9B%E5%BB%BA%E5%B9%B6%E5%90%AF%E5%8A%A8%E5%AD%98%E5%82%A8%E6%B1%A0} ------------------------------------------------------------------------------------ ```````````bash # 创建默认存储池,--name可以自定义,这里使用default sudo virsh pool-define-as --name default --type dir --target /var/lib/libvirt/images 启动默认存储池 ======= sudo virsh pool-start default 设置默认存储池为自动启动 ============ sudo virsh pool-autostart default </code> </pre> 修改images目录的所有权和权限 {#%E4%BF%AE%E6%94%B9images%E7%9B%AE%E5%BD%95%E7%9A%84%E6%89%80%E6%9C%89%E6%9D%83%E5%92%8C%E6%9D%83%E9%99%90} ------------------------------------------------------------------------------------------------------------------------------ 修改所有权,使其归属 libvirt-qemu 用户和组,具体取决于系统,可能是 qemu 用户,可以通过/etc/passwd查看 ``````````bash chown -R libvirt-qemu:libvirt-qemu /var/lib/libvirt/images/ </code> </pre> 设置文件的权限 `````````bash chmod 0644 /var/lib/libvirt/images/ </code> </pre> 修改配置libvirt配置文件,编辑 /etc/libvirt/qemu.conf 文件 ````````bash security_driver = "none" dynamic_ownership = 1 </code> </pre> 这样可以防止新创建的qcow2存储文件默认属主属组为root,导致虚拟机libvirt-qemu缺少权限来启动虚拟机 应用tf脚本 {#%E5%BA%94%E7%94%A8tf%E8%84%9A%E6%9C%AC} ------------------------------------------------ 该脚本创建了一个1核1GB,10GB存储的虚拟机 ```````hcl terraform { required_providers { libvirt = { source = "dmacvicar/libvirt" version = "~> 0.6.3" } } } provider "libvirt" { uri = "qemu:///system" } 创建一个10GB的磁盘卷 ============ resource "libvirt_volume" "vm_disk" { name = "terraform_vm_disk.qcow2" pool = "default" format = "qcow2" size = 10 # 单位为GB } 创建云初始化的磁盘 ========= resource "libvirt_cloudinit_disk" "commoninit" { name = "commoninit.iso" pool = "default" user_data = \<\<-EOF #cloud-config hostname: terraform-kvm EOF } 创建虚拟机 ===== resource "libvirt_domain" "vm" { name = "terraform-kvm" memory = "1024" # 单位为MB,即1GB vcpu = 1 使用之前创建的磁盘卷 ========== disk { volume_id = libvirt_volume.vm_disk.id } 使用云初始化磁盘 ======== cloudinit = libvirt_cloudinit_disk.commoninit.id 配置网络接口 ====== network_interface { network_name = "default" } console { type = "pty" target_port = "0" target_type = "serial" } graphics { type = "spice" listen_type = "none" } } </code> </pre> 初始化脚本目录 ``````bash terraform init </code> </pre> 应用脚本 `````bash terraform apply </code> </pre> 删除资源 {#%E5%88%A0%E9%99%A4%E8%B5%84%E6%BA%90} -------------------------------------------- 在虚拟机已经成功运行的情况下,只需要进入到tf文件的目录,运行 ````bash terraform destroy </code> </pre> 如果虚拟机本身是shut down状态,需要在此基础上再通过virsh删除虚拟机 ```bash virsh undefine terraform-kvm </code> </pre> ``` ```` ````` `````` ``````` ```````` ````````` `````````` ``````````` ```````````` ````````````` `````````````` ``````````````` ```````````````` ````````````````` `````````````````` ``````````````````` ````````````````````