Nutanix_Home_Lab_Mentor_11 - itnett/FTD02H-N GitHub Wiki

For å bruke "cloud-init" i Nutanix miljøet ditt for å automatisere installasjon og konfigurering av dine VMs basert på en minimal distribusjon, kan du følge disse trinnene. Cloud-init er et verktøy som lar deg initialisere en VM ved å kjøre en serie med konfigurasjoner når VM-en starter for første gang. Dette er spesielt nyttig for å automatisere prosesser som pakkeinstallasjon, nettverkskonfigurasjon, og kjøring av skript.

1. Forberedelse av Cloud-Init konfigurasjonsfil

Her er et eksempel på en cloud-config YAML-fil som du kan bruke for å installere nødvendige pakker og konfigurere iptables-regler på en minimal distribusjon:

#cloud-config
package_update: true
package_upgrade: true
packages:
  - vim
  - curl
  - wget
  - iptables

runcmd:
  - iptables -A INPUT -p tcp --dport 22 -j ACCEPT
  - iptables -A INPUT -p tcp --dport 80 -j ACCEPT
  - iptables -A INPUT -p tcp --dport 443 -j ACCEPT
  - iptables -A INPUT -j DROP
  - systemctl restart iptables

2. Oppretting av en ISO-fil for Cloud-Init

For å kunne bruke denne cloud-config-filen, må den pakkes inn i en ISO-fil som Nutanix kan bruke som en "user-data" source. Her er trinnene for å gjøre det:

  1. Opprett katalogstruktur:

    På din lokale maskin, opprett en mappe for cloud-init:

    mkdir -p /tmp/cloud-init/
  2. Opprett meta-data og user-data:

    Lag to filer i denne mappen:

    • meta-data (Inneholder bare en VM ID)
    • user-data (Inneholder din cloud-config YAML)
    echo "instance-id: iid-local01" > /tmp/cloud-init/meta-data
    cat <<EOF > /tmp/cloud-init/user-data

#cloud-config package_update: true package_upgrade: true packages:

  • vim
  • curl
  • wget
  • iptables

runcmd:

  • iptables -A INPUT -p tcp --dport 22 -j ACCEPT
  • iptables -A INPUT -p tcp --dport 80 -j ACCEPT
  • iptables -A INPUT -p tcp --dport 443 -j ACCEPT
  • iptables -A INPUT -j DROP
  • systemctl restart iptables EOF

3. **Pakk inn ISO-fil**:

Bruk verktøyet `genisoimage` eller `mkisofs` for å lage en ISO-fil:

```bash
genisoimage -output /tmp/cloud-init.iso -volid cidata -joliet -rock /tmp/cloud-init/

Dette vil lage en ISO-fil /tmp/cloud-init.iso som inneholder dine meta-data og user-data filer.

3. Last opp ISO til Nutanix og bruk den som CD-ROM

  1. Last opp ISO til Nutanix Prism:

    • Gå til Prism UI.
    • Naviger til Settings > Image Configuration.
    • Klikk på Upload Image og last opp cloud-init.iso med en passende beskrivelse.
    • Sørg for at bildet er satt som en ISO under "Type".
  2. Knytt ISO til en VM:

    • Når du oppretter en ny VM i Nutanix, knytt ISO-en som CD-ROM.
    • Gå til VM Configuration, legg til en CD-ROM under Disks og velg cloud-init.iso fra listen over tilgjengelige bilder.
  3. Start VM-en:

    Når VM-en starter, vil den bruke cloud-init-konfigurasjonen fra ISO-filen for å utføre alle instruksjonene som er definert i user-data.

4. Validering

Etter at VM-en har startet, kan du sjekke om pakkene er installert og om iptables-reglene er konfigurert som forventet:

# Sjekk installerte pakker
dpkg -l | grep -E 'vim|curl|wget|iptables'

# Sjekk iptables-regler
iptables -L

Oppsummering

Du har nå konfigurert en cloud-init-prosess for dine Nutanix-baserte VMs, som automatiserer oppsett av grunnleggende pakker og iptables-regler. Dette oppsettet kan utvides til å inkludere flere konfigurasjoner etter behov, og gir en kraftig metode for å automatisere VM-initialisering i ditt Nutanix-miljø.

To automate the creation of Linux VMs with Nutanix using Cloud-Init, we can leverage the Nutanix API v3. Below is a detailed guide that shows how to create and configure a VM with Cloud-Init on Nutanix. This will include setting up a VM from a minimal distribution ISO, automating the installation of necessary packages, and configuring services like vim, curl, iptables, etc.

1. Prerequisites

  • Nutanix Cluster with AHV (Acropolis Hypervisor)
  • Nutanix Prism Central
  • A minimal Linux distribution ISO uploaded to Nutanix Image Service
  • Nutanix API v3 access
  • Cloud-Init installed on your minimal Linux ISO

2. Create Cloud-Init User Data

The Cloud-Init user data will automate the package installation and configuration when the VM first boots.

Here’s an example cloud-config YAML file:

#cloud-config
package_update: true
package_upgrade: true
packages:
  - vim
  - curl
  - wget
  - iptables

runcmd:
  - iptables -A INPUT -p tcp --dport 22 -j ACCEPT
  - iptables -A INPUT -p tcp --dport 80 -j ACCEPT
  - iptables -A INPUT -p tcp --dport 443 -j ACCEPT
  - iptables -A INPUT -j DROP
  - systemctl restart iptables

This cloud-config will update and upgrade the packages, install the necessary tools, and configure the iptables rules.

3. Encode Cloud-Init Data in Base64

Nutanix API requires the Cloud-Init user data to be in base64 format.

You can encode your Cloud-Init data using a command like this:

cat cloud-config.yaml | base64

4. Build the Nutanix API Request

The JSON payload for creating a VM with Nutanix API v3 should include the Cloud-Init data encoded in base64.

Here’s an example JSON payload:

{
  "spec": {
    "name": "cloudinit_vm_from_api",
    "resources": {
      "power_state": "ON",
      "num_vcpus_per_socket": 2,
      "num_sockets": 1,
      "memory_size_mib": 2048,
      "disk_list": [
        {
          "device_properties": {
            "device_type": "DISK",
            "disk_address": {
              "device_index": 0,
              "adapter_type": "SCSI"
            }
          },
          "data_source_reference": {
            "kind": "image",
            "uuid": "{{cloud_init_image_uuid_pc}}"
          }
        }
      ],
      "nic_list": [
        {
          "nic_type": "NORMAL_NIC",
          "is_connected": true,
          "ip_endpoint_list": [
            {
              "ip_type": "DHCP"
            }
          ],
          "subnet_reference": {
            "kind": "subnet",
            "name": "{{subnet_name}}",
            "uuid": "{{subnet_uuid}}"
          }
        }
      ],
      "guest_tools": {
        "nutanix_guest_tools": {
          "state": "ENABLED",
          "iso_mount_state": "MOUNTED"
        }
      },
      "guest_customization": {
        "cloud_init": {
          "user_data": "I2Nsb3VkLWNvbmZpZwpwYWNrYWdlX3VwZGF0ZTogdHJ1ZQpwYWNrYWdlX3VwZ3JhZGU6IHRydWUKcGFja2FnZXM6CiAgLSB2aW0KICAtIGN1cmwKICAtIHdnZXQKICAtIGlwdGFibGVzCgpydW5jbWQ6CiAgLSBpcHRhYmxlcyAtQSBSRVR1Uk4gLXAgdGNwIC0tZHBvcnQgMjIgLWogQUNDRVBUIAogIC0gaXB0YWJsZXMgLUEgUkVUUk4gLXAgdGNwIC0tZHBvcnQgODAgLWogQUNDRVBUIAogIC0gaXB0YWJsZXMgLUEgUkVUUk4gLXAgdGNwIC0tZHBvcnQgNDQzIC1qIERST1AKICAtIHN5c3RlbWN0bCByZXN0YXJ0IGlwdGFibGVzCg=="
        },
        "is_overridable": false
      }
    },
    "cluster_reference": {
      "kind": "cluster",
      "name": "{{cluster_name}}",
      "uuid": "{{cluster_uuid}}"
    }
  },
  "api_version": "3.1.0",
  "metadata": {
    "kind": "vm"
  }
}

5. Replace Placeholders with Actual Values

Replace the following placeholders in the JSON payload:

  • {{vm_name}}: Desired name for the VM.
  • {{cloud_init_image_uuid_pc}}: UUID of the minimal Linux ISO with Cloud-Init support.
  • {{subnet_name}} and {{subnet_uuid}}: Subnet name and UUID where the VM will connect.
  • {{cluster_name}} and {{cluster_uuid}}: Nutanix cluster name and UUID.

6. Send the API Request

Use a tool like curl or Postman to send the API request to Nutanix Prism Central:

curl -k -X POST -u admin:password \
    -H "Content-Type: application/json" \
    -d @create_vm.json \
    https://<prism_central_ip>:9440/api/nutanix/v3/vms

7. Monitor the VM Creation Process

Once the API request is sent, the VM will be created, powered on, and customized with the Cloud-Init script you provided. You can monitor the VM through Nutanix Prism Central UI or by watching the VM console.

8. Wrapping Up

This process automates the creation and configuration of VMs using Nutanix API v3 and Cloud-Init. This setup is ideal for environments where you need to quickly deploy and configure VMs with minimal manual intervention.

To automate the deployment of multiple VMs using Nutanix with Cloud-Init, follow the detailed guide provided. This setup will allow you to quickly spin up VMs, configure them using Cloud-Init, and ensure they are ready to perform their intended roles.

1. Prerequisites

  • Nutanix Cluster with AHV (Acropolis Hypervisor)
  • Nutanix Prism Central: Ensure you have the API credentials and network configurations ready.
  • Minimal Linux Distribution ISO: Uploaded to the Nutanix Image Service with Cloud-Init installed.
  • Nutanix API v3 Access
  • Cloud-Init Configurations: Prepare your configuration in a YAML format.

2. Create Cloud-Init User Data

The cloud-config will automate the initial setup of the VMs:

#cloud-config
package_update: true
package_upgrade: true
packages:
  - vim
  - curl
  - wget
  - iptables

runcmd:
  - iptables -A INPUT -p tcp --dport 22 -j ACCEPT
  - iptables -A INPUT -p tcp --dport 80 -j ACCEPT
  - iptables -A INPUT -p tcp --dport 443 -j ACCEPT
  - iptables -A INPUT -j DROP
  - systemctl restart iptables

3. Encode Cloud-Init Data in Base64

Convert the YAML configuration into base64 format:

cat cloud-config.yaml | base64

Copy the base64 encoded string for use in the JSON payload.

4. Build the Nutanix API Request

Create a JSON payload for the Nutanix API v3 that includes the Cloud-Init data.

JSON Payload Template:

{
  "spec": {
    "name": "cloudinit_vm_from_api",
    "resources": {
      "power_state": "ON",
      "num_vcpus_per_socket": 2,
      "num_sockets": 1,
      "memory_size_mib": 2048,
      "disk_list": [
        {
          "device_properties": {
            "device_type": "DISK",
            "disk_address": {
              "device_index": 0,
              "adapter_type": "SCSI"
            }
          },
          "data_source_reference": {
            "kind": "image",
            "uuid": "{{cloud_init_image_uuid_pc}}"
          }
        }
      ],
      "nic_list": [
        {
          "nic_type": "NORMAL_NIC",
          "is_connected": true,
          "ip_endpoint_list": [
            {
              "ip_type": "DHCP"
            }
          ],
          "subnet_reference": {
            "kind": "subnet",
            "name": "{{subnet_name}}",
            "uuid": "{{subnet_uuid}}"
          }
        }
      ],
      "guest_tools": {
        "nutanix_guest_tools": {
          "state": "ENABLED",
          "iso_mount_state": "MOUNTED"
        }
      },
      "guest_customization": {
        "cloud_init": {
          "user_data": "base64_encoded_cloud_config"
        },
        "is_overridable": false
      }
    },
    "cluster_reference": {
      "kind": "cluster",
      "name": "{{cluster_name}}",
      "uuid": "{{cluster_uuid}}"
    }
  },
  "api_version": "3.1.0",
  "metadata": {
    "kind": "vm"
  }
}

Replace the placeholders:

  • {{cloud_init_image_uuid_pc}}: UUID of the minimal Linux ISO in Nutanix Image Service.
  • {{subnet_name}} and {{subnet_uuid}}: Your target subnet’s name and UUID.
  • {{cluster_name}} and {{cluster_uuid}}: Your Nutanix cluster’s name and UUID.
  • base64_encoded_cloud_config: Replace with your actual base64 encoded Cloud-Init configuration.

5. Automate VM Creation via API

Use curl to send the API request:

curl -k -X POST -u admin:password \
    -H "Content-Type: application/json" \
    -d @create_vm.json \
    https://<prism_central_ip>:9440/api/nutanix/v3/vms

6. Monitor the VM Creation Process

Watch the progress in Nutanix Prism Central or by accessing the VM console directly.

7. Wrapping Up

The above steps automate the deployment and initial configuration of VMs using Cloud-Init on Nutanix. You can extend this by creating additional scripts to automate more VMs or different configurations. This method is ideal for rapidly deploying and configuring infrastructure in a repeatable manner, ensuring consistency across your environment.

⚠️ **GitHub.com Fallback** ⚠️