Skip to content

Instantly share code, notes, and snippets.

@ruzickap
Last active February 23, 2018 13:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ruzickap/0c31630b7a9ff8de042b3db587fb8927 to your computer and use it in GitHub Desktop.
Save ruzickap/0c31630b7a9ff8de042b3db587fb8927 to your computer and use it in GitHub Desktop.
Install Vagrant + libvirt and create Vagrantfile ale to run 3 VMs in KVM hypervisor
# Install Vagrant libvirt plugin (with all the dependencies like qemu, libvirt, vagrant, ...)
dnf install -y -q ansible git libvirt-client libvirt-nss python-netaddr python-virtualenv vagrant-libvirt
vagrant plugin install vagrant-libvirt
# Enable dns resolution of VMs taken from libvirt (https://lukas.zapletalovi.com/2017/10/definitive-solution-to-libvirt-guest-naming.html)
sed -i.orig 's/files dns myhostname/files libvirt libvirt_guest dns myhostname/' /etc/nsswitch.conf
# Start the libvirt daemon
service libvirtd start
# Create ssh key if it doesn't exist
test -f ~/.ssh/id_rsa.pub || ssh-keygen -f $HOME/.ssh/id_rsa -N ''
# Create directory structure
mkdir /var/tmp/kubernetes_cluster
cd /var/tmp/kubernetes_cluster
# Create Vagrantfile
cat > Vagrantfile << EOF
box_image = "peru/my_ubuntu-16.04-server-amd64"
node_count = 4
ssh_pub_key = File.readlines("#{Dir.home}/.ssh/id_rsa.pub").first.strip
Vagrant.configure(2) do |config|
config.vm.synced_folder ".", "/vagrant", :disabled => true
config.vm.box = box_image
config.vm.provider :libvirt do |domain|
domain.cpus = 2
domain.memory = 2048
domain.default_prefix = ''
end
(1..node_count).each do |i|
config.vm.define "kube0#{i}" do |config|
config.vm.hostname = "kube0#{i}"
end
end
config.vm.provision 'shell', inline: "install -m 0700 -d /root/.ssh/; echo #{ssh_pub_key} >> /root/.ssh/authorized_keys; chmod 0600 /root/.ssh/authorized_keys"
config.vm.provision 'shell', inline: "echo #{ssh_pub_key} >> /home/vagrant/.ssh/authorized_keys", privileged: false
end
EOF
# Create and start virtual machines
vagrant up
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment