Vagrant

From Christoph's Personal Wiki
Revision as of 21:10, 29 March 2022 by Christoph (Talk | contribs) (Examples)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Vagrant is an open-source software product for building and maintaining portable virtual software development environments; e.g., for VirtualBox, KVM, Hyper-V, Docker containers, VMware, and AWS. It tries to simplify the software configuration management of virtualization in order to increase development productivity. Vagrant is written in the Ruby language, but its ecosystem supports development in a few other languages.

Common Vagrant commands

  • autocomplete — manages autocomplete installation on host
  • box — manages boxes: installation, removal, etc.
  • cloud — manages everything related to Vagrant Cloud
  • destroy — stops and deletes all traces of the vagrant machine
  • global-status — outputs status Vagrant environments for this user
  • halt — stops the vagrant machine
  • help — shows the help for a subcommand
  • init — initializes a new Vagrant environment by creating a Vagrantfile
  • login
  • package — packages a running vagrant environment into a box
  • plugin — manages plugins: install, uninstall, update, etc.
  • port — displays information about guest port mappings
  • powershell — connects to machine via powershell remoting
  • provision — provisions the vagrant machine
  • push — deploys code in this environment to a configured destination
  • rdp — connects to machine via RDP
  • reload — restarts vagrant machine, loads new Vagrantfile configuration
  • resume — resume a suspended vagrant machine
  • snapshot — manages snapshots: saving, restoring, etc.
  • ssh — connects to machine via SSH
  • ssh-config — outputs OpenSSH valid configuration to connect to the machine
  • status — outputs status of the vagrant machine
  • suspend — suspends the machine
  • up — starts and provisions the vagrant environment
  • upload — upload to machine via communicator
  • validate — validates the Vagrantfile
  • version — prints current and latest Vagrant version
  • winrm — executes commands on a machine via WinRM
  • winrm-config — outputs WinRM configuration to connect to the machine
Full list of available vagrant commands
$ vagrant list-commands

Examples

K0s

$ cat Vagrantfile

Vagrant.configure("2") do |config|
  config.vm.box = "hashicorp/bionic64"
  config.vm.network "private_network", ip: "192.168.33.11"
  config.vm.provision "shell", inline: <<-SHELL
    curl -sSLf https://get.k0s.sh | sudo sh
    sudo k0s default-config > /tmp/k0s.yaml
    sudo k0s install controller --single -c /tmp/k0s.yaml
    sudo systemctl start k0scontroller && sleep 10
    sudo cp /var/lib/k0s/pki/admin.conf /vagrant/kubeconfig
    sed -i "s/localhost/192.168.33.11/" /vagrant/kubeconfig
  SHELL
end

$ vagrant up

Rancher

The following is an example of bringing up Rancher (pre-v2.x):

# -*- mode: ruby -*-
# vi: set ft=ruby :

# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"

MASTER_MEMORY=1024
AGENT_MEMORY=1024

RANCH_MASTER_ADDRESS="172.19.8.100"
# Boxes are configured at 101, 102 and so on
RANCH_SUBNET="172.19.8"


RANCHER_SERVER_DOMAIN_NAME="ranch-svr.infracloud.io"
RANCHER_CLIENT_DEF="ranch-def.infracloud.io"
RANCHER_CLIENT_K8S="ranch-k8s.infracloud.io"


Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

    config.vm.define "ranch_server" do |rserver|
        rserver.vm.box = "ubuntu/xenial64"
        rserver.vm.network "private_network", ip: "#{RANCH_MASTER_ADDRESS}" 
        rserver.vm.hostname = "#{RANCHER_SERVER_DOMAIN_NAME}"
        rserver.vm.provider :virtualbox do |vba|
            vba.customize ["modifyvm", :id, "--memory", MASTER_MEMORY]
        end
        rserver.vm.provision "shell", inline: "wget -qO- https://get.docker.com/ | sh"          
        rserver.vm.provision "shell", inline: "sudo docker run -d --restart=always -p 8080:8080 rancher/server"     
    end

    config.vm.define "ranch_c_default" do |rclient|
        rclient.vm.box = "ubuntu/xenial64"
        rclient.vm.network "private_network", ip: "#{RANCH_SUBNET}.101"
        rclient.vm.hostname = "#{RANCHER_CLIENT_DEF}"
        rclient.vm.provider :virtualbox do |vba|
            vba.customize ["modifyvm", :id, "--memory", AGENT_MEMORY]
        end
        rclient.vm.provision "shell", inline: "wget -qO- https://get.docker.com/ | sh"      
        # K8S works with slightly older version of Docker hence downgrading
        #rclient.vm.provision "shell", inline: "sudo apt-get install -y --force-yes docker-engine=1.10.3-0~trusty"       
    end

    config.vm.define "ranch_c_k8s" do |rclient|
        rclient.vm.box = "ubuntu/xenial64"
        rclient.vm.network "private_network", ip: "#{RANCH_SUBNET}.102"
        rclient.vm.hostname = "#{RANCHER_CLIENT_K8S}"
        rclient.vm.provider :virtualbox do |vba|
            vba.customize ["modifyvm", :id, "--memory", AGENT_MEMORY]
        end
        rclient.vm.provision "shell", inline: "wget -qO- https://get.docker.com/ | sh"
        # K8S works with slightly older version of Docker hence downgrading
        #rclient.vm.provision "shell", inline: "sudo apt-get install -y --force-yes docker-engine=1.10.3-0~trusty"
    end

end
  • Start up resources:
$ vagrant up

External links