Skip to content

Instantly share code, notes, and snippets.

@SuperThunder
Last active March 26, 2023 23:22
Show Gist options
  • Save SuperThunder/9223da8e639fd0dd8e00568bfa826236 to your computer and use it in GitHub Desktop.
Save SuperThunder/9223da8e639fd0dd8e00568bfa826236 to your computer and use it in GitHub Desktop.
CKA Pluralsight Vagrantfile
# Usage:
# vagrant up control1
# vagrant up worker1 worker2 worker3
# IMPORTANT: after running kubeadm init or join, you need to set the correct node ip
# sudo vim /var/lib/kubelet/kubeadm-flags.env
# --node-ip=10.28.28.<XX> at the end of the flags string
# sudo systemctl daemon-reload && sudo systemctl restart kubelet
# Assumes 10GB of RAM and 9 cores are available :)
# The control plane could be scaled back to 2c / 2GB and workers 1c / 2GB (total 5c / 8GB)
# -*- mode: ruby -*-
# vi: set ft=ruby :
# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
ssh_pub_key = File.readlines("#{Dir.home}/.ssh/id_rsa.pub").first.strip
config.vm.provision "shell", inline: <<-SHELL
apt-get update -y
swapoff -a
(crontab -l 2>/dev/null; echo "@reboot /sbin/swapoff -a") | crontab - || true
echo #{ssh_pub_key} >> /home/vagrant/.ssh/authorized_keys
echo #{ssh_pub_key} >> /root/.ssh/authorized_keys
SHELL
config.vm.define "control1" do |control1|
control1.vm.box = "ubuntu/focal64"
control1.vm.network :private_network, ip: "10.28.28.21"
control1.vm.network :forwarded_port, guest: 22, host: 10122, id: "ssh"
control1.vm.base_mac = nil
control1.vm.provider "virtualbox" do |v|
v.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
v.memory = 4096
v.cpus = 3
end
#control1.customize ["modifyvm", :id, "--natdnsproxy1", "on"]
control1.vm.hostname = "control1"
end
config.vm.define "worker1" do |worker1|
worker1.vm.box = "ubuntu/focal64"
worker1.vm.network :private_network, ip: "10.28.28.31"
worker1.vm.network :forwarded_port, guest: 22, host: 10131, id: "ssh"
worker1.vm.base_mac = nil
worker1.vm.provider "virtualbox" do |v|
v.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
v.memory = 2048
v.cpus = 2
end
#worker1.customize ["modifyvm", :id, "--natdnsproxy1", "on"]
worker1.vm.hostname = "worker1"
end
config.vm.define "worker2" do |worker2|
worker2.vm.box = "ubuntu/focal64"
worker2.vm.network :private_network, ip: "10.28.28.32"
worker2.vm.network :forwarded_port, guest: 22, host: 10132, id: "ssh"
worker2.vm.base_mac = nil
worker2.vm.provider "virtualbox" do |v|
v.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
v.memory = 2048
v.cpus = 2
end
#worker1.customize ["modifyvm", :id, "--natdnsproxy1", "on"]
worker2.vm.hostname = "worker2"
end
config.vm.define "worker3" do |worker3|
worker3.vm.box = "ubuntu/focal64"
worker3.vm.network :private_network, ip: "10.28.28.33"
worker3.vm.network :forwarded_port, guest: 22, host: 10133, id: "ssh"
worker3.vm.base_mac = nil
worker3.vm.provider "virtualbox" do |v|
v.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
v.memory = 2048
v.cpus = 2
end
#worker1.customize ["modifyvm", :id, "--natdnsproxy1", "on"]
worker3.vm.hostname = "worker3"
end
# The most common configuration options are documented and commented below.
# For a complete reference, please see the online documentation at
# https://docs.vagrantup.com.
# Every Vagrant development environment requires a box. You can search for
# boxes at https://vagrantcloud.com/search.
#config.vm.box = "base"
# Disable automatic box update checking. If you disable this, then
# boxes will only be checked for updates when the user runs
# `vagrant box outdated`. This is not recommended.
# config.vm.box_check_update = false
# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine. In the example below,
# accessing "localhost:8080" will access port 80 on the guest machine.
# NOTE: This will enable public access to the opened port
# config.vm.network "forwarded_port", guest: 80, host: 8080
# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine and only allow access
# via 127.0.0.1 to disable public access
# config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"
# Create a private network, which allows host-only access to the machine
# using a specific IP.
# config.vm.network "private_network", ip: "192.168.33.10"
# Create a public network, which generally matched to bridged network.
# Bridged networks make the machine appear as another physical device on
# your network.
# config.vm.network "public_network"
# Share an additional folder to the guest VM. The first argument is
# the path on the host to the actual folder. The second argument is
# the path on the guest to mount the folder. And the optional third
# argument is a set of non-required options.
# config.vm.synced_folder "../data", "/vagrant_data"
# Provider-specific configuration so you can fine-tune various
# backing providers for Vagrant. These expose provider-specific options.
# Example for VirtualBox:
#
# config.vm.provider "virtualbox" do |vb|
# # Display the VirtualBox GUI when booting the machine
# vb.gui = true
#
# # Customize the amount of memory on the VM:
# vb.memory = "1024"
# end
#
# View the documentation for the provider you are using for more
# information on available options.
# Enable provisioning with a shell script. Additional provisioners such as
# Ansible, Chef, Docker, Puppet and Salt are also available. Please see the
# documentation for more information about their specific syntax and use.
# config.vm.provision "shell", inline: <<-SHELL
# apt-get update
# apt-get install -y apache2
# SHELL
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment