How to launch kubernetes cluster using vagrant?

Member

by orpha , in category: Third Party Scripts , 5 months ago

How to launch kubernetes cluster using vagrant?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by cali_green , 5 months ago

@orpha 

To launch a Kubernetes cluster using Vagrant, you can follow these steps:

  1. Install Vagrant on your machine if you haven't already. You can download Vagrant from the official website.
  2. Create a new directory for your Kubernetes cluster configuration and navigate to it in your terminal.
  3. Create a Vagrantfile in this directory. You can use the following example Vagrantfile to create a Kubernetes cluster with one master node and two worker nodes:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
Vagrant.configure("2") do |config|
  # Define master node
  config.vm.define "master" do |master|
    master.vm.box = "ubuntu/bionic64"
    master.vm.network "private_network", ip: "192.168.33.10"
    master.vm.provision "shell", path: "scripts/master.sh"
  end

  # Define worker nodes
  (1..2).each do |i|
    config.vm.define "worker#{i}" do |worker|
      worker.vm.box = "ubuntu/bionic64"
      worker.vm.network "private_network", ip: "192.168.33.1#{i+1}"
      worker.vm.provision "shell", path: "scripts/worker.sh"
    end
  end
end


  1. Create shell scripts for provisioning the master and worker nodes. You can customize these scripts based on your requirements. Here is an example shell script for provisioning the master node (master.sh):
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#!/bin/bash

# Update package list
apt-get update

# Install Docker
apt-get install -y docker.io
systemctl start docker
systemctl enable docker

# Install kubeadm, kubelet, and kubectl
apt-get install -y apt-transport-https curl
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -
cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
deb https://apt.kubernetes.io/ kubernetes-xenial main
EOF

apt-get update
apt-get install -y kubelet kubeadm kubectl


  1. Initialize the Kubernetes cluster by running the following commands in your terminal:
1
2
3
vagrant up
vagrant ssh master
sudo kubeadm init


  1. Follow the instructions provided by kubeadm to join the worker nodes to the cluster.
  2. Verify that the Kubernetes cluster has been successfully launched by running the following command:
1
kubectl get nodes


You should see the master and worker nodes listed in the output.


That's it! You have successfully launched a Kubernetes cluster using Vagrant.