πŸš€ Creating a Kubernetes Cluster with Vagrant: A Step-by-Step Guide πŸš€

Khuram Murad - Oct 23 - - Dev Community

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:

  1. Vagrant - Make sure you have Vagrant installed on your machine. You can download it from Vagrant's official site.

  2. 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

Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

βš™οΈ Step 3: Start Your Cluster

In the terminal, navigate to your project directory and run:


vagrant up

Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

Then, initialize your Kubernetes cluster:


sudo kubeadm init --pod-network-cidr=10.244.0.0/16

Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

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! πŸŒπŸ’»

. . . . . .
Terabox Video Player