This document explains the construction of a basic kubernetes cluster setup of:
- One Master Node
- Two Minion Nodes
Setting up Kubernetes was a complicated task until the recent release of Kubernetes 1.4. Kubernetes 1.4+ includes a tool called kubeadm which eases the process of setting up a cluster between the nodes.We are going to setup this cluster using the Kubernetes binaries instead of cloning from the repository.
We will create a Vagrantfile which will use the kubeadm tool to create the nodes and form a cluster between them.
To get started with the process, we would initially need the following be installed on the Ubuntu box:
1.Vagrant (>1.8)
2.VirtualBox 5.1
3.Git
We will setup the prerequisites required for the environment as below:
sudo apt-get update
apt-cache show vagrant
The output should be similar to:
Package: vagrant
Priority: optional
Section: universe/admin
Installed-Size: 2466
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Original-Maintainer: Antonio Terceiro <terceiro@debian.org>
Architecture: all
Version: 1.8.1+dfsg-1
Depends: bsdtar, bundler, curl, openssh-client, ruby-childprocess (>= 0.3.7), ruby-erubis (>= 2.7.0), ruby-i18n (>= 0.6.0), ruby-listen, ruby-log4r (>= 1.1.9), ruby-net-scp (>= 1.1.0), ruby-net-sftp, ruby-net-ssh (>= 1:2.6.6), ruby-rest-client, ruby-nokogiri, ruby-rb-inotify, ruby
Suggests: virtualbox (>= 4.0)
Filename: pool/universe/v/vagrant/vagrant_1.8.1+dfsg-1_all.deb
...
sudo apt-get install vagrant
sudo apt remove virtualbox
sudo nano -w /etc/apt/sources.list
deb http://download.virtualbox.org/virtualbox/debian xenial contrib
wget -q https://www.virtualbox.org/download/oracle_vbox_2016.asc -O- | sudo apt-key add -
sudo apt update
sudo apt install virtualbox-5.1
sudo apt-get install git
This file forms the heart of the entire process. We define the way the nodes have to been setup and form a cluster.
- Launch the Terminal.
- Get the K8s demo project by cloning it from the repository
git clone https://github.com/openebs/openebs.git
Change directory to the location where the Vagrantfile has been placed.The Vagrantfile is split into 5 sections:
- Define the characteristics of the Nodes.
- Specify the plugins needed by Vagrant to build the Nodes.
- Script to install common binaries for Master and Minion nodes.
- Script for the Master Node and Swarm.
- Script for the Minions and joining them into the Swarm.
# K8s Master Nodes
M_NODES = ENV['M_NODES'] || 1
# K8s Minion Nodes
H_NODES = ENV['H_NODES'] || 2
# Master node Memory & CPUs
M_MEM = ENV['M_MEM'] || 512
M_CPUS = ENV['M_CPUS'] || 1
# Minion Host Memory & CPUs
H_MEM = ENV['H_MEM'] || 1024
H_CPUS = ENV['H_CPUS'] || 1
required_plugins = %w(vagrant-cachier vagrant-triggers)
required_plugins.each do |plugin|
need_restart = false
unless Vagrant.has_plugin? plugin
system "vagrant plugin install #{plugin}"
need_restart = true
end
exec "vagrant #{ARGV.join(' ')}" if need_restart
end
$installer = <<SCRIPT
#!/bin/bash
echo Will run the common installer script ...
# Update apt and get dependencies
sudo apt-get update
sudo apt-get install -y unzip curl wget
# Install docker and K8s
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 http://apt.kubernetes.io/ kubernetes-xenial main
EOF
apt-get update
# Install docker if you don't have it already.
apt-get install -y docker.io
apt-get install -y kubelet kubeadm kubectl kubernetes-cni
SCRIPT
#Will be called during the VM creation
vmCfg.vm.provision "shell", inline: $installer, privileged: true
$kubeadminit = <<SCRIPT
#!/bin/bash
echo Will run the kubeadm init script ...
kubeadm init --api-advertise-addresses=$1 --token=$2
SCRIPT
#Will be called when setting up the Master Node
vmCfg.vm.provision :shell, inline: $kubeadminit, :args => "`#{master_ip_address}` #{token}", privileged: true
vmCfg.vm.provision :trigger, :force => true, :stdout => true, :stderr => true do |trigger|
trigger.fire do
master_hostname = "master-01"
get_ip_address = %Q(vagrant ssh #{master_hostname} -c 'ifconfig | grep -oP "inet addr:\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}" | grep -oP "\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}" | tail -n 2 | head -n 1')
get_master_ip_address = `#{get_ip_address}`
if get_master_ip_address == ""
info"The Kubernetes Master is down, bring it up and manually run:"
info"kubeadm --token=<token> <master_ip_address>"
info"in order to join the cluster."
else
info"Setting up the Minion using IPAddress: #{get_master_ip_address}"
info"Setting up the Minion using Token: #{token}"
@machine.communicate.sudo("kubeadm join --token=#{token} #{get_master_ip_address.strip}")
end
end
end
- Launch the Terminal.
- Run the following command.
vagrant up
Once the nodes have been setup SSH into the Master Node and run the following command.
sudo kubectl get nodes
The command should output the number of nodes in the cluster and their current state.
NAME STATUS AGE
host-01 Ready 52m
host-02 Ready 43m
master-01 Ready,master 1h
This project is licensed under the MIT License - see the LICENSE.md file for details
- Hat tip to anyone who's code was used
- Inspiration
- etc