π Creating a Kubernetes Cluster with Vagrant: A Step-by-Step Guide π
Khuram Murad
Posted on October 23, 2024
Are you looking to dive into the world of Kubernetes but unsure where to start? Setting up a local cluster can be a bit daunting, but fear not! Vagrant makes it incredibly easy to create a reproducible environment for practicing your Kubernetes skills. Hereβs a simple guide to get you started:
π οΈ Prerequisites:
Vagrant - Make sure you have Vagrant installed on your machine. You can download it from Vagrant's official site.
VirtualBox - You'll also need VirtualBox as the provider for Vagrant. Download it here.
π¦ Step 1: Set Up Your Vagrantfile
Create a new directory for your project and inside it, run:
vagrant init
This command will generate a Vagrantfile
. Open it in your favorite text editor.
π Step 2: Configure Your Cluster
Replace the content of the Vagrantfile
with the following configuration:
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/bionic64"
# Define your master node
config.vm.define "master" do |master|
master.vm.hostname = "k8s-master"
master.vm.network "private_network", type: "dhcp"
master.vm.provision "shell", inline: <<-SHELL
sudo apt-get update
sudo apt-get install -y docker.io
sudo systemctl start docker
sudo systemctl enable docker
# Install kubeadm, kubelet, and kubectl
sudo apt-get install -y apt-transport-https ca-certificates curl
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
echo "deb https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list
sudo apt-get update
sudo apt-get install -y kubelet kubeadm kubectl
sudo systemctl enable kubelet
SHELL
end
# Define your worker nodes (add more if needed)
(1..2).each do |i|
config.vm.define "worker#{i}" do |worker|
worker.vm.hostname = "k8s-worker#{i}"
worker.vm.network "private_network", type: "dhcp"
worker.vm.provision "shell", inline: <<-SHELL
sudo apt-get update
sudo apt-get install -y docker.io
sudo systemctl start docker
sudo systemctl enable docker
# Install kubeadm, kubelet, and kubectl
sudo apt-get install -y apt-transport-https ca-certificates curl
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
echo "deb https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list
sudo apt-get update
sudo apt-get install -y kubelet kubeadm kubectl
sudo systemctl enable kubelet
SHELL
end
end
end
βοΈ Step 3: Start Your Cluster
In the terminal, navigate to your project directory and run:
vagrant up
This command will start all defined virtual machines and provision them according to the configuration.
π Step 4: Initialize Kubernetes
Once all nodes are up and running, SSH into the master node:
vagrant ssh master
Then, initialize your Kubernetes cluster:
sudo kubeadm init --pod-network-cidr=10.244.0.0/16
Follow the instructions provided at the end of the initialization process to set up kubectl
access.
π Step 5: Deploy a Pod Network
For Kubernetes pods to communicate, you'll need to deploy a pod network. For example, you can use Flannel:
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/k8s-manifests/kube-flannel.yml
And thatβs it! π You now have a local Kubernetes cluster running on Vagrant where you can practice and experiment.
Feel free to share your experiences or ask questions in the comments below! Happy K8s learning! ππ»
Posted on October 23, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.