Pages

Wednesday, October 29, 2014

Installing ubuntu cloudimages with uvtool

Introduction

With ubuntu 14.04 LTS, ubuntu released a new tool to simply install cloud images and create a virtual machine in minutes. The virtual machines it creates are also managable by libvirt. In this short tutorial I'm going to show how to install and use the uvtool and also briefly show how to use cloud-init with these images.

Install

To install uvtool in ubuntu 14.04 LTS you can simply use the package manager.
$ apt-get update
$ apt-get install uvtool
If you want to use uvtool for ubuntu 12,04 LTS you can use the ppa:
$ sudo add-apt-repository ppa:uvtool-dev/trunk
$ sudo apt-get update
$ sudo apt-get install uvtool

Cloudimages

Uvtool contains two main commands uvt-simplestreams-libvirt and uvt-kvm. The uvt-simplestreams-libvirt can be used to synchronize the ubuntu cloud images. To see which images are downloaded you can use:
$ uvt-simplestreams-libvirt query
release=trusty arch=amd64 label=release (20140927)
Uvtool creates a libvirt storage pool to store the cloud images.
$ virsh pool-list
 Name                 State      Autostart
-------------------------------------------
 default              active     yes
 uvtool               active     yes
To download all current cloudimages you can use uvt-simplestreams-libvirt sync. This will of-course take a long time and cause a lot of traffic. To selectively download the images you can use the filters release and/or arch. To download the latest trusty image for amd64 you would use:
$ uvt-simplestreams-libvirt sync release=trusty arch=amd64
or precise:
uvt-simplestreams-libvirt sync release=precise arch=amd64
This way you only download the images you need.
To see the volumes in the libvirt storage pool:
$ virsh vol-list uvtool
 Name                 Path
------------------------------------------------------------------------------
 x-uvt-b64-Y29tLnVidW50dS5jbG91ZDpzZXJ2ZXI6MTQuMDQ6YW1kNjQgMjAxNDA5Mjc= /var/lib/uvtool/libvirt/images/x-uvt-b64-Y29tLnVidW50dS5jbG91ZDpzZXJ2ZXI6MTQuMDQ6YW1kNjQgMjAxNDA5Mjc=

Create VM

Using the command uvt-kvm we can create a virtual machine based on the cloudimage we previously downloaded in the libvirt storage pool,
If you don't already created a pair of ssh-keys, create them with:
$ ssh-keygen
Create a trusty amd64 VM with the following command:
$ uvt-kvm create myvm release=trusty arch=amd64
This wil create and start a libvirt kvm virtual machine with default libvirt values.The command will return almost immediately. The creation of the VM though still continues in the background. If you want to know when the virtual machine is ready, use the uvt-kvm wait command.
$ uvt-kvm wait --insecure myvm
To find the ip of the just created machine (read the uvt-kvm manpage for the restrictions this command has):
$ uvt-kvm ip myvm
To stop en remove this virtual machine, issue the following command:
$ uvt-kvm destroy myvm
The destroy command will remove all persistent state associated with a VM, including VM-specific disk image files and the VM definition itself. It will not destroy the cloud images you downloaded with uvt-simplestreams-libvirt.
Although you can use uvt-kvm to create and remove virtual machine, this doesn't limit you to use the VM's with virsh.
$ virsh list
 Id    Name                           State
----------------------------------------------------
 8     myvm                         running
See also the ADVANCED USAGE section of the uvt-kvm's manpage.
Some more advanced create examples (see manpage for details):
# create a vm with 2 cpu cores, 1024 MiB memory and 32 Gib disk size.
uvt-kvm create --memory 1024 --disk 32 --cpu 2 myvm
# copies the script initscript.sh to the vm and executes once at startup, also installs the listed packages.
uvt-kvm create --run-script-once initscript.sh --packages heirloom-mailx,vim-nox myvm

Connect to the VM

You can connect to the running VM using the uvt-kvm ssh command or directly with ssh.

connect

$ uvt-kvm ssh myvm --insecure

search ip

uvt-kvm ip myvm
192.168.122.145

connect using ssh

ssh ubuntu@192.168.122.145

Cloud-init

It is possible to initialize your VM using cloud-init more directly. See http://cloudinit.readthedocs.org/en/latest/ for documentation
This overides default cloud-init from uvtool, that's why you also have to configure access like ssh keys or user/password combo.

Example:

create file init.cfg:

#cloud-config

ssh_authorized_keys:
  - ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCrhB9U7WKdqwx4hDoLshqKfhEZHVzZ57pS8REkYnfl3ZH+uyKDHbH3pgIRF+Pa44VETK6DusYB93+tVWi/v5YfYfGqHUbkEyPFo81DQccLVUZA2+c/OJFTjIhOHAvAmWz3IrSZldRkCHGQJ5t707Q4aJqAkXaqQRhaTbgKJwdjE6ebcpPqdGeacLrlFlbNvMB2snn9S9VdyayHoj9VGTfnVoW2ylwvArJ0KunDq2IQOos0T040T6u11bXc6OMHkavMi/EyOpJ0ITD3Jw4Vz2cc5qxx7TuJvlbX5gFssRjt+4XCgtfgjwBhdLZHtwl25ZR/dk7ZuyLD1iE5uPGbWGaV mykey@host

write_files:
  - content: |
      Test file to see if cloud-init works
    path: /tmp/test.txt

packages:
  - pwgen
  - vim-nox
  - puppet
  - vim-puppet

Then issue the following commands for testing:

uvt-kvm create --user-data init.cfg myvm
uvt-kvm wait --insecure myvm
uvt-kvm ssh --insecure myvm
uvt-kvm destroy myvm

Thursday, October 25, 2012

Installing Puppet

Introduction

Puppet is an open source configuration management tool written in Ruby. With puppet you can bootstrap and maintain the configurations of computer systems using manifests. It's possible to use puppet as a standalone application or in a master/client configuration. This tutorial is about installing puppet in a master/client configuration on Ubuntu LTS. I'm going to show how to install the master and the client. In the tutorial replace "example.com" with your domain name.

Puppet Labs repository

The default Ubuntu repository already contains packages for puppet, but if you want the latest version you can use the packages from Puppet Labs. If you want the default distributions packages you can skip this part and go directly to "Installing master and client".
To use the Puppet Labs' packages you have to download the puppetlabs-release package:

Ubuntu lucid:
$ wget http://apt.puppetlabs.com/puppetlabs-release-lucid.deb
$ sudo dpkg -i puppetlabs-release-lucid.deb
$ sudo aptitude update

Ubuntu precise:
$ wget http://apt.puppetlabs.com/puppetlabs-release-precise.deb
$ sudo dpkg -i puppetlabs-release-precise.deb
$ sudo aptitude update

Installing master and client

The client runs in daemon mode and has a default run interval of 30 minutes. This means the client agent is going to check for new configuration at the master every 30 minutes and applies it to the client. The default host it connects to is 'puppet'. The easiest setup is to name the master "puppet.example.com". Setup your dns to point "puppet.example.com" to the ip of your master server. Make sure that on the client your resolv.conf contains "search example.com".

/etc/resolv.conf:
nameserver <ip1>
nameserver <ip2>
search example.com
...

Installing puppetmaster

On the machine that serves as the puppet master:
sudo aptitude install puppetmaster
 
To have the master start at boot we have to change /etc/default/puppetmaster:
$ sudo sed -i 's/START=no/START=yes/' /etc/default/puppetmaster
$ /etc/init.d/puppetmaster start

Installing puppetclient

On the nodes we install the puppet-agent:
$ sudo aptitude install puppet

To have the agent start at boot we have to change /etc/default/puppet:
$ sudo sed -i 's/START=no/START=yes/' /etc/default/puppet
$ /etc/init.d/puppet start

Certificates

To authorize the agent to the master, we have to sign the clients certificates. On the master:
$ puppet cert list
client1.example.com
$ puppet cert sign client1.example.com
Or if you have multiple clients you want to sign at once:
$ puppet cert list
client1.example.com
client2.example.com
client3.example.com
$ puppet cert sign --all

You can also choose to auto-sign the certificates, but be aware of the security risk this may present. On the master add the following line to the file /etc/puppet/autosign.conf:
*.example.com

First manifest

Now you have a working master/client puppet installation and we can start to write our first manifest. Edit the file /etc/puppet/manifests/site.pp and add your first node:
node 'client1.example.com' {
   file { "/tmp/test-file":
       replace => "no",
       ensure  => "present",
       content => "Example file from Puppet\n",
       mode    => 644,
   }
}

This is just a really simple example that adds the file /tmp/test-file to client1.example.com.