Merge pull request #15 from lowescott/lxc

Commit initial version of LXC environment
This commit is contained in:
Scott Lowe 2016-01-08 15:22:28 -07:00
commit ee5408a187
6 changed files with 148 additions and 0 deletions

37
lxc/README.md Normal file
View file

@ -0,0 +1,37 @@
# Running LXC-Based OS Containers
These files were created to allow users to use Vagrant ([http://www.vagrantup.com](http://www.vagrantup.com)) and Ansible ([http://www.ansible.com](http://www.ansible.com)) to quickly and relatively easily experiment with LXC (pronounced "lex-see") containers. This configuration was tested using Vagrant 1.8.1, VMware Fusion 8.1.0, the Vagrant VMware plugin, and Ansible 1.9.1. Other versions of these products are likely to work, but haven't been tested.
## Contents
* **ansible.cfg**: This Ansible configuration file instructs Ansible to use an inventory file named `hosts` in the current directory, and to use the default vagrant user (`vagrant`) and default insecure Vagrant SSH private key.
* **hosts**: This file is created automatically when you run `vagrant status` or `vagrant up` (anytime Vagrant processes the `Vagrantfile`). This file relies on the presence of a "ip_addr" value in `machines.yml`. Do not edit this file directly---instead edit `machines.yml` to change the values that will be automatically written into this file.
* **machines.yml**: This YAML file contains a list of VM definitions and associated configuration data. It is referenced by `Vagrantfile` when Vagrant instantiates the VMs. Generally the only change this file needs is to ensure the "box" value correctly references the Vagrant box you're using. The "ip_addr" value is also required in order to build the Ansible inventory file; you may want to edit this to a value appropriate for your environment.
* **provision.yml**: This YAML file is an Ansible playbook that configures the VMs created by Vagrant when they are first provisioned. Ansible is called automatically by Vagrant; you do not need to invoke Ansible separately. No changes are needed to this file.
* **README.md**: The file you're currently reading.
* **Vagrantfile**: This file is used by Vagrant to spin up the virtual machines. This file is fairly extensively commented to help explain what's happening. You should be able to use this file unchanged; all the VM configuration options are stored in `machines.yml`.
## Instructions
These instructions assume you've already installed VMware Fusion, Vagrant, the Vagrant VMware plugin, and Ansible. Please refer to the documentation for those products for more information on installation or configuration.
1. Use `vagrant box add` to install an Ubuntu 14.04 x64 box for the "vmware_fusion" provider. I have a base box you can use for this purpose; to use my Ubuntu 14.04 x64 base box, add the box with `vagrant box add slowe/ubuntu-trusty-x64`. (In theory you should be able to use this Vagrant environment with VMware Workstation as well, but only VMware Fusion was tested.)
2. Place the files from the `lxc` directory of this GitHub repository (the "lowescott/learning-tools" repository) into a directory on your system. You can clone the entire "learning-tools" repository (using `git clone`), or just download the specific files from the `lxc` directory.
3. Edit `machines.yml` to ensure that the box specified in that file matches the Ubuntu 14.04 x64 base box you just installed and will be using. _I recommend that you do not change any other values in this file unless you know it is necessary._
4. From a terminal window, change into the directory where the files from this directory are stored and run `vagrant up` to bring up the VMs according to the instructions in `machines.yml` and `Vagrantfile`. (By default, it will create and power on only a single VM.)
5. Once Vagrant has finished creating, booting, and provisioning the VM (note you'll need Internet access for this step), log into the VM (named "lxc-01" by default) using `vagrant ssh`.
You now have a VM running LXC! You can use the various LXC-related commands (`lxc-create`, `lxc-start`, etc.) to create and manage containers on this VM.
## Additional Resources
[A Brief Introduction to Linux Containers with LXC](http://blog.scottlowe.org/2013/11/25/a-brief-introduction-to-linux-containers-with-lxc/)

84
lxc/Vagrantfile vendored Normal file
View file

@ -0,0 +1,84 @@
# -*- mode: ruby -*-
# vi: set ft=ruby :
# Specify Vagrant version, Vagrant API version, and desired clone location
Vagrant.require_version '>= 1.6.0'
VAGRANTFILE_API_VERSION = '2'
ENV['VAGRANT_DEFAULT_PROVIDER'] = 'vmware_fusion'
ENV['VAGRANT_VMWARE_CLONE_DIRECTORY'] = '~/.vagrant'
#ENV['VAGRANT_DEFAULT_PROVIDER'] = 'virtualbox'
#ENV['VAGRANT_DEFAULT_PROVIDER'] = 'vmware_appcatalyst'
# Require 'yaml' module
require 'yaml'
# Read YAML file with VM details (box, CPU, and RAM)
machines = YAML.load_file(File.join(File.dirname(__FILE__), 'machines.yml'))
# Create Ansible inventory file
# (current contents will be overwritten)
f = File.open('hosts','w')
machines.each do |machine|
f.puts machine['ip_addr']
end # machines.each
f.close
# Create and configure the VMs
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
# Always use Vagrant's default insecure key
config.ssh.insert_key = false
# Iterate through entries in YAML file to create VMs
machines.each do |machine|
config.vm.define machine['name'] do |srv|
# Don't check for box updates
srv.vm.box_check_update = false
srv.vm.hostname = machine['name']
srv.vm.box = machine['box']
# Configure default synced folder (disable by default)
if machine['sync_disabled'] != nil
srv.vm.synced_folder '.', '/vagrant', disabled: machine['sync_disabled']
else
srv.vm.synced_folder '.', '/vagrant', disabled: true
end #if machine['sync_disabled']
# Assign additional private network
if machine['ip_addr'] != nil
srv.vm.network 'private_network', ip: machine['ip_addr']
end # if machine['ip_addr']
# Configure CPU & RAM per settings in machines.yml (Fusion)
srv.vm.provider :vmware_fusion do |vmw|
vmw.vmx['memsize'] = machine['ram']
vmw.vmx['numvcpus'] = machine['vcpu']
if machine['nested'] == true
vmw.vmx['vhv.enable'] = 'TRUE'
end #if machine['nested']
end # srv.vm.provider vmware_fusion
# Configure CPU & RAM per settings in machines.yml (AppCatalyst)
srv.vm.provider :vmware_appcatalyst do |vmw|
vmw.vmx['memsize'] = machine['ram']
vmw.vmx['numvcpus'] = machine['vcpu']
#vmw.vmx['guestos'] = 'other3xlinux-64'
if machine['nested'] == true
vmw.vmx['vhv.enable'] = 'TRUE'
end #if machine['nested']
end # srv.vm.provider vmware_appcatalyst
# Configure CPU & RAM per settings in machines.yml (VirtualBox)
srv.vm.provider :virtualbox do |vb|
vb.memory = machine['ram']
vb.cpus = machine['vcpu']
end # srv.vm.provider virtualbox
end # config.vm.define
# Provision the VM with Ansible
config.vm.provision 'ansible' do |ansible|
ansible.playbook = 'provision.yml'
end # config.vm.provision
end # machines.each
end # Vagrant.configure

5
lxc/ansible.cfg Normal file
View file

@ -0,0 +1,5 @@
[defaults]
inventory = ./hosts
private_key_file = ~/.vagrant.d/insecure_private_key
remote_user = vagrant
host_key_checking = False

1
lxc/hosts Normal file
View file

@ -0,0 +1 @@
192.168.100.100

6
lxc/machines.yml Normal file
View file

@ -0,0 +1,6 @@
---
- name: lxc-01
box: slowe/ubuntu-trusty-x64
ram: 512
vcpu: 1
ip_addr: 192.168.100.100

15
lxc/provision.yml Normal file
View file

@ -0,0 +1,15 @@
---
- hosts: "all"
sudo: "yes"
remote_user: "vagrant"
tasks:
- name: "Install LXC"
apt:
state: "present"
update_cache: "yes"
name: "{{ item }}"
with_items:
- lxc
- lxc-templates
- lxctl