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

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


This content originally appeared on DEV Community and was authored by Khuram Murad

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

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


This content originally appeared on DEV Community and was authored by Khuram Murad


Print Share Comment Cite Upload Translate Updates
APA

Khuram Murad | Sciencx (2024-10-23T14:39:48+00:00) πŸš€ Creating a Kubernetes Cluster with Vagrant: A Step-by-Step Guide πŸš€. Retrieved from https://www.scien.cx/2024/10/23/%f0%9f%9a%80-creating-a-kubernetes-cluster-with-vagrant-a-step-by-step-guide-%f0%9f%9a%80/

MLA
" » πŸš€ Creating a Kubernetes Cluster with Vagrant: A Step-by-Step Guide πŸš€." Khuram Murad | Sciencx - Wednesday October 23, 2024, https://www.scien.cx/2024/10/23/%f0%9f%9a%80-creating-a-kubernetes-cluster-with-vagrant-a-step-by-step-guide-%f0%9f%9a%80/
HARVARD
Khuram Murad | Sciencx Wednesday October 23, 2024 » πŸš€ Creating a Kubernetes Cluster with Vagrant: A Step-by-Step Guide πŸš€., viewed ,<https://www.scien.cx/2024/10/23/%f0%9f%9a%80-creating-a-kubernetes-cluster-with-vagrant-a-step-by-step-guide-%f0%9f%9a%80/>
VANCOUVER
Khuram Murad | Sciencx - » πŸš€ Creating a Kubernetes Cluster with Vagrant: A Step-by-Step Guide πŸš€. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/10/23/%f0%9f%9a%80-creating-a-kubernetes-cluster-with-vagrant-a-step-by-step-guide-%f0%9f%9a%80/
CHICAGO
" » πŸš€ Creating a Kubernetes Cluster with Vagrant: A Step-by-Step Guide πŸš€." Khuram Murad | Sciencx - Accessed . https://www.scien.cx/2024/10/23/%f0%9f%9a%80-creating-a-kubernetes-cluster-with-vagrant-a-step-by-step-guide-%f0%9f%9a%80/
IEEE
" » πŸš€ Creating a Kubernetes Cluster with Vagrant: A Step-by-Step Guide πŸš€." Khuram Murad | Sciencx [Online]. Available: https://www.scien.cx/2024/10/23/%f0%9f%9a%80-creating-a-kubernetes-cluster-with-vagrant-a-step-by-step-guide-%f0%9f%9a%80/. [Accessed: ]
rf:citation
» πŸš€ Creating a Kubernetes Cluster with Vagrant: A Step-by-Step Guide πŸš€ | Khuram Murad | Sciencx | https://www.scien.cx/2024/10/23/%f0%9f%9a%80-creating-a-kubernetes-cluster-with-vagrant-a-step-by-step-guide-%f0%9f%9a%80/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.