Linux/Proxmox: Difference between revisions

From Wiki
 
(49 intermediate revisions by the same user not shown)
Line 1: Line 1:
== Install Ubuntu to be used for cloning with cloud-init ==
== Download Ubuntu Cloud Image ==
* not working:
 
* https://cloud-images.ubuntu.com/releases/
** e.g.: ubuntu-22.04-server-cloudimg-amd64.img
* upload cloud-init image from proxmox
** gui -> local storage -> iso images
 
== Create VM from Cloud Image to be used as template  ==
<pre>
<pre>
# in case of apt-cacher-ng
iso folder:    ls -Flah /var/lib/vz/template/iso/
touch /etc/apt/apt.conf.d/00aptproxy
 
echo 'Acquire::http::Proxy "http://192.168.111.11:3142";' > /etc/apt/apt.conf.d/00aptproxy
qm create 9001 --name "ubuntu-2404-ci" --memory 2048 --cores 8 --net0 virtio,bridge=vmbr0
qm importdisk 9001 /var/lib/vz/template/iso/ubuntu-24.04-server-cloudimg-amd64.img lvm
qm set 9001 --scsihw virtio-scsi-single --scsi0 lvm:vm-9001-disk-0,discard=on,iothread=1
                                              #local:9001/vm-9001-disk-0.raw
 
qm set 9001 --boot order=scsi0
qm set 9001 --serial0 socket
qm set 9001 --vga serial0                      # qm set 9001 --vga std
qm set 9001 --cpu cputype=host
qm set 9001 --ostype l26                      # linux
qm set 9001 --agent 1                          # qemu guest agent
 
# set cloud-init definitions
qm set 9001 --scsi1 lvm:cloudinit              # drive with cloudinit information
qm set 9001 --ciuser "username"
qm set 9001 --cipassword "password"
qm set 9001 --ipconfig0 "ip=dhcp"             # qm set 9001 --ipconfig0 ip=192.168.111.123/24,gw=192.168.111.1
qm set 9001 --ciupgrade 1                      # upgrade packages on boot
</pre>
</pre>


== Manual rework to prepare template ==
<pre>
<pre>
apt update
# echo 'Acquire::http::Proxy "http://192.168.111.11:3142";' > /etc/apt/apt.conf.d/00aptproxy
apt install -y cloud-init cloud-initramfs-growroot qemu-guest-agent
 
sudo dpkg-reconfigure keyboard-configuration
apt install qemu-guest-agent
apt install mc
</pre>
</pre>
* install [[Linux/Docker/Installation|docker]]
* install https://gitea.marcluerssen.de/linux-public/.dotfiles
<pre>
apt clean


echo -n >/etc/machine-id
rm /var/lib/dbus/machine-id
ln -s /etc/machine-id /var/lib/dbus/machine-id
</pre>


== Use Ubuntu Cloud-Init Image ==
== Clone template to new VM ==
* upload cloud-init image from proxmox
<pre>
** gui -> local storage -> iso images
qm template 9001                    # turn vm 9001 into template
qm clone 9001 123 --name docker1    # deploy vm from template
qm resize 123 scsi0 20G
</pre>
* switch back from template to vm
<pre>
/etc/pve/qemu-server
-> template: 0
</pre>


== Terraform + Proxmox ==
* main.tf
<pre>
<pre>
iso folder:   /var/lib/vz/template/iso/
terraform {
  required_providers {
    proxmox = {
      source = "telmate/proxmox"
      version = "2.9.3"
    }
  }
}
 
provider "proxmox" {
  pm_api_url = "https://vm-host:8006/api2/json"
  pm_password = "password"
  pm_user = "root@pam"
  pm_tls_insecure = "true"
}
 
resource "proxmox_vm_qemu" "proxmox_vm" {
  name = "vm-01"
  desc = "Terraform VM"
  target_node = "vm-host"
 
  clone = "ubuntu-20.04-cloudinit"
  os_type = "cloud-init"
  agent = 1
 
  cores = 1
  sockets = 1
  cpu = "host"
  memory = 2048
 
  scsihw = "virtio-scsi-pci"
  boot = "c"
  bootdisk = "scsi0"
 
  disk {
    type = "scsi"
    storage = "local-lvm"
    size = "20G"
  }


qm create 9001 --memory 2048 --net0 virtio,bridge=vmbr0
  network {
    bridge = "vmbr0"
    firewall  = false
    model    = "virtio"
  }


qm importdisk 9001 /var/lib/vz/template/iso/focal-server-cloudimg-amd64.img local-lvm
  lifecycle {
    ignore_changes = [
      network
    ]
  }
}
</pre>


qm set 9001 --scsihw virtio-scsi-pci --scsi0 local-lvm:vm-9000-disk-0
== Proxmox + Ansible ==
* on proxmox host:
<pre>
apt install sudo


qm set 9001 --ide2 local-lvm:cloudinit
adduser username
usermod -aG sudo username


qm set 9001 --boot c --bootdisk scsi0
sudo visudo
> username ALL = (ALL) NOPASSWD: ALL


qm set 9001 --serial0 socket --vga serial0
apt install python3-proxmoxer
</pre>
</pre>


== Links ==
* https://yetiops.net/posts/proxmox-terraform-cloudinit-saltstack-prometheus/




[[Category:Linux/Virtualization]]
[[Category:Linux/Virtualization]]
[[Category:Linux]]
[[Category:Linux]]

Latest revision as of 20:04, 5 November 2024

Download Ubuntu Cloud Image

Create VM from Cloud Image to be used as template

iso folder:    ls -Flah /var/lib/vz/template/iso/

qm create 9001 --name "ubuntu-2404-ci" --memory 2048 --cores 8 --net0 virtio,bridge=vmbr0
qm importdisk 9001 /var/lib/vz/template/iso/ubuntu-24.04-server-cloudimg-amd64.img lvm
qm set 9001 --scsihw virtio-scsi-single --scsi0 lvm:vm-9001-disk-0,discard=on,iothread=1
                                               #local:9001/vm-9001-disk-0.raw

qm set 9001 --boot order=scsi0
qm set 9001 --serial0 socket 
qm set 9001 --vga serial0                      # qm set 9001 --vga std
qm set 9001 --cpu cputype=host 
qm set 9001 --ostype l26                       # linux
qm set 9001 --agent 1                          # qemu guest agent

# set cloud-init definitions
qm set 9001 --scsi1 lvm:cloudinit              # drive with cloudinit information
qm set 9001 --ciuser "username"
qm set 9001 --cipassword "password"
qm set 9001 --ipconfig0 "ip=dhcp"              # qm set 9001 --ipconfig0 ip=192.168.111.123/24,gw=192.168.111.1
qm set 9001 --ciupgrade 1                      # upgrade packages on boot

Manual rework to prepare template

# echo 'Acquire::http::Proxy "http://192.168.111.11:3142";' > /etc/apt/apt.conf.d/00aptproxy

sudo dpkg-reconfigure keyboard-configuration
apt install qemu-guest-agent
apt install mc
apt clean

echo -n >/etc/machine-id
rm /var/lib/dbus/machine-id
ln -s /etc/machine-id /var/lib/dbus/machine-id

Clone template to new VM

qm template 9001                    # turn vm 9001 into template
qm clone 9001 123 --name docker1    # deploy vm from template
qm resize 123 scsi0 20G
  • switch back from template to vm
/etc/pve/qemu-server
 -> template: 0

Terraform + Proxmox

  • main.tf
terraform {
  required_providers {
    proxmox = {
      source = "telmate/proxmox"
      version = "2.9.3"
    }
  }
}

provider "proxmox" {
  pm_api_url = "https://vm-host:8006/api2/json"
  pm_password = "password"
  pm_user = "root@pam"
  pm_tls_insecure = "true"
}

resource "proxmox_vm_qemu" "proxmox_vm" {
  name = "vm-01"
  desc = "Terraform VM"
  target_node = "vm-host"

  clone = "ubuntu-20.04-cloudinit"
  os_type = "cloud-init"
  agent = 1

  cores = 1
  sockets = 1
  cpu = "host"
  memory = 2048

  scsihw = "virtio-scsi-pci"
  boot = "c"
  bootdisk = "scsi0"

  disk {
    type = "scsi"
    storage = "local-lvm"
    size = "20G"
  }

  network {
    bridge = "vmbr0"
    firewall  = false
    model     = "virtio"
  }

  lifecycle {
     ignore_changes = [
       network
     ]
  }
}

Proxmox + Ansible

  • on proxmox host:
apt install sudo

adduser username
usermod -aG sudo username

sudo visudo
> username ALL = (ALL) NOPASSWD: ALL

apt install python3-proxmoxer


Links