Skip to content

Example Service

Corey Melanson edited this page Jul 19, 2018 · 1 revision

Overview

This is an example of a simple service which can be created with OpenNebula. It will provision the following:

  • 1x Security Group with a simple ruleset to allow SSH and ICMP
  • 1x VNET reserved from a master VNET, setup to use the above created Security Group
  • 1x Image from the Marketplace
  • 3x VMs using the above Image, VNET and Security Groups

How to Use

  1. If you haven't already, install Terraform and install the OpenNebula plugin

  2. Make a new directory and create the files as described below:

terraform.tfvars

#API URL and OpenNebula credentials
one_endpoint = "http://opennebula:2633/RPC2"
one_username = "OPENNEBULA_USERNAME"
one_password = "OPENNEBULA_PASSWORD"

#How many VMs to create
vm_count = 3

#VNET to reserve from
master_vnet="VNET NAME"

#Datastore to store the image in
datastore_id=103

#Name and URL of the gold image to download
gold_image_name="Ubuntu 18.04"
gold_image_url="http://marketplace.opennebula.org/appliance/ca5c3632-359a-429c-ac5b-b86178ee2390/download/0"

terraform.tf

variable "one_endpoint" {
  default = "http://localhost:2633/RPC2"
}
variable "one_username" {}
variable "one_password" {}

variable "vm_count" {
  default = 1
}

variable "master_vnet" {}
variable "datastore_id" {}
variable "gold_image_url" {}
variable "gold_image_name" {}

provider "opennebula" {
  endpoint = "${var.one_endpoint}"
  username = "${var.one_username}"
  password = "${var.one_password}"
}

#Load the cloud-init user-data from a file cloud-init.yaml
data "template_file" "cloudinit" {
  template = "${file("cloud-init.yaml")}"
}

#Find the master vnet to reserve from
data "opennebula_vnet" "StandardNet" {
  name = "${var.master_vnet}"
}

#Create basic VMs using the image and vnet we're reserving, along with our cloud-init user-data
resource "opennebula_vm" "demo" {
  count = "${var.vm_count}"
  name = "tfdemo${count.index + 1}"

  cpu = 0.5
  vcpu = 1
  memory = 1024

  context {
    TERRAFORM = "is awesome"
    HOSTNAME = "$NAME"
    NETWORK = "YES"
    USER_DATA = "${data.template_file.cloudinit.rendered}"
  }

  graphics {
    listen="0.0.0.0"
    type="vnc"
  }

  disk {
    image_id = "${opennebula_image.goldimage.id}"
  }

  nic {
    model = "virtio"
    network_id = "${opennebula_vnet.reservation.id}"
  }
}

#A basic Security Group which will allow port 22 and ICMP in and all protocols out
resource "opennebula_secgroup" "baseruleset" {
    name = "terrasec"
    description = "my description"
    rule {
        protocol = "ALL"
        rule_type = "OUTBOUND"
    }
    rule {
        protocol = "TCP"
        rule_type = "INBOUND"
        range = "22"
    }
    rule {
        protocol = "ICMP"
        rule_type = "INBOUND"
    }
}

#Make a VNET reservation from the master vnet using $vm_count IPs
resource "opennebula_vnet" "reservation" {
    name = "terravnetres"
    description = "my terraform vnet"
    reservation_vnet = "${data.opennebula_vnet.StandardNet.id}"
    reservation_size = "${var.vm_count}"
    security_groups = ["${opennebula_secgroup.baseruleset.id}"]
}

#Download the gold image to the datastore
resource "opennebula_image" "goldimage" {
    name = "${var.gold_image_name}"
    description = "Terraform image"
    datastore_id = "${var.datastore_id}"
    persistent = false
    path = "${var.gold_image_url}"
    dev_prefix = "vd"
    driver = "qcow2"
}

#Print out the IP addresses that OpenNebula assigned to our VMs
output "vm_ips" {
  value = "${join(",",opennebula_vm.demo.*.ip)}"
}

cloud-init.yaml

#cloud-config
users:
  - name: demo
    groups: sudo
    shell: /bin/bash
    sudo: ['ALL=(ALL) NOPASSWD:ALL']
    ssh-authorized-keys:
      - ssh-rsa key goes here
runcmd:
  - touch /tmp/demo.txt
  1. Initialize the Terraform plugins and test the configuration:
terraform init
terraform plan
  1. If there are no errors, apply the changes:
terraform apply
  1. At this point the VMs should be accessible if all went well.

  2. When you are done, delete the VMs:

terraform destroy
Clone this wiki locally