Skip to content

Instantly share code, notes, and snippets.

@yudaykiran
Last active February 5, 2017 07:20
Show Gist options
  • Save yudaykiran/f9bf7e5efca91d2e4993d1a6959e3854 to your computer and use it in GitHub Desktop.
Save yudaykiran/f9bf7e5efca91d2e4993d1a6959e3854 to your computer and use it in GitHub Desktop.

Automating Kubernetes Cluster Setup with Vagrant for Ubuntu 16.04

This document explains the construction of a basic kubernetes cluster setup of:

  1. One Master Node
  2. Two Minion Nodes

Getting Started

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.

Prerequisites

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 

Installing

We will setup the prerequisites required for the environment as below:

Vagrant:

Update the packages info from repositories
sudo apt-get update
Check the Vagrant package info (optional)
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
...
Install Vagrant
sudo apt-get install vagrant

VirtualBox

Remove an existing copy of VirtualBox if you have one
sudo apt remove virtualbox
Open the /etc/apt/sources.list file:
sudo nano -w /etc/apt/sources.list
Append the following line to the file
deb http://download.virtualbox.org/virtualbox/debian xenial contrib
Press Ctrl+O to save the file. Then press Ctrl+X to close the file.
Get the Oracle GPG public key and import it into Ubuntu 16.04
wget -q https://www.virtualbox.org/download/oracle_vbox_2016.asc -O- | sudo apt-key add -
Run the following commands to install VirtualBox
sudo apt update

sudo apt install virtualbox-5.1

Git

sudo apt-get install git

Vagrantfile

This file forms the heart of the entire process. We define the way the nodes have to been setup and form a cluster.

Running the Vagrant file

  1. Launch the Terminal.
  2. 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:

  1. Define the characteristics of the Nodes.
  2. Specify the plugins needed by Vagrant to build the Nodes.
  3. Script to install common binaries for Master and Minion nodes.
  4. Script for the Master Node and Swarm.
  5. Script for the Minions and joining them into the Swarm.

Define the characteristics of the Nodes

# 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 for Vagrant

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

Install the common binaries for Master and Minion nodes.

$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

Master Node Setup

$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

Minion Nodes Setup - uses vagrant-triggers

 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

Running the Vagrant file

  1. Launch the Terminal.
  2. 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

License

This project is licensed under the MIT License - see the LICENSE.md file for details

Acknowledgments

  • Hat tip to anyone who's code was used
  • Inspiration
  • etc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment