Skip to content

Instantly share code, notes, and snippets.

@vhugo
Created June 19, 2019 09:38
Show Gist options
  • Save vhugo/5ea63617ef88ce8f5b6ffd165884deae to your computer and use it in GitHub Desktop.
Save vhugo/5ea63617ef88ce8f5b6ffd165884deae to your computer and use it in GitHub Desktop.
Setup Kubernetes cluster locally

Setup Kubernetes cluster locally

The purpose of this document is to help Kubernetes beginners to set up their cluster locally (lab environment).

Kubernetes can be installed on-premise on VMs and bare metal, usually, the best option depends upon the purpose of the project, the resources available, the expertise and preference of tooling. It is possible to use Vagrant, VMware vSphere, KVM, or another Configuration Management (CM) tool in conjunction with a hypervisor software.

Several projects are already available to ease the process of setting up a local cluster with single or multiple nodes but the majority of these solutions boil down to multiple machines with the minimum required resources as described in the Kubernetes official documentation to bootstrap your cluster using kubeadm.

The idea for this document is to present some options so newcomers can find out what works best for them to get started experimenting with Kubernetes on their local machine. Feel free to update this document if you find any mistake or outdated information or if you know any other option that has worked for you.

Option 1: Kubernetes Vagrant multi-node cluster

The project k8s-vagrant-multi-node uses the make command with recipes for Vagrant integration with Virtualbox to set up a Kubernetes cluster.

Prerequisites

The minimum system requirements are a multicore processor and at least 4GB of RAM available and 10G of free disk space.

As the prerequisites described in the project page, make you have:

The installation process used in this documentation was performed on a MacOS and since most of the tools like make, grep, cut and rsync were already available in the system, there was no need to install them. If you are trying to install this option on a different OS or if you are missing any of those tools check their project page for more information on how to install them.

For Virtualbox and Vagrant go to each of their download page linked here, download the installer, execute it and follow the instructions there.

Create the cluster

Open your terminal and clone the project's repository to your local hard disk.

git clone https://github.com/galexrt/k8s-vagrant-multi-node.git

# change directory to the cloned repository
cd k8s-vagrant-multi-node

To start up with more resources than the default, use the environment variable before the make command. The following command line will create a 1x master and 3x workers each with 3GB of memory and 2 CPU:

NODE_MEMORY_SIZE_GB=3 NODE_CPUS=2 NODE_COUNT=3 make up -j 4

Note: Creating the cluster will cause a lot of data to be pulled down from the Internet. As such it will take some time. If you see network timeouts then it may be worth reducing the number of VMs built concurrently from 4 to 2 by changing the argument to make up to be -j 2.

Once the cluster has finished setup, you can use the following commands to check the status or ssh to the nodes.

# Get the Vagrant status for each of the nodes (including the masters).
make status

# Get the Vagrant status for just the nodes.
make status-nodes

# SSH into master
make ssh-master

# SSH into node1
make ssh-node-1

Destroy the cluster

To clean up the cluster do the following.

NODE_COUNT=3 make clean
make clean-data

Option 2: Kubernetes IN Docker

The project kind (Kubernetes IN Docker) is an easy way to create a Kubernetes cluster using Docker.

Prerequisites

The recent version of kind you can download in release page or if you have Go >= 1.12 already installed on your machine, run the following command:

GO111MODULE="on" go get sigs.k8s.io/kind@v0.3.0

Create the cluster

create a YAML file with the following configuration for 1x master, 2x workers nodes cluster

kind: Cluster
apiVersion: kind.sigs.k8s.io/v1alpha3
nodes:
- role: control-plane
- role: worker
- role: worker

and then run the following command:

kind create cluster --config ./my-cluster.yaml

For more information on how the cluster is created check out the project page

Destroy the cluster

kind delete cluster

Explore our new cluster

Now that we have our Kubernetes cluster running we can start using it.

Prerequisites

If you do not have kubectl installed yet and you are on MacOS or Linux, follow these steps, but if you are on Window check this link:

  1. Download the latest release, if you are on Linux just replace darwin to linux in the environment variable at the start of the command line:

    OS=darwin curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/${OS}/amd64/kubectl
    

    To download a specific version, replace the $(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt) portion of the command with the specific version.

    For example, to download version v1.14.0 on macOS, type:

    OS=darwin curl -LO https://storage.googleapis.com/kubernetes-release/release/v1.14.0/bin/${OS}/amd64/kubectl
    
  2. Make the kubectl binary executable.

    chmod +x ./kubectl
    
  3. Move the binary in to your PATH.

    sudo mv ./kubectl /usr/local/bin/kubectl
    
  4. Test to ensure the version you installed is up-to-date:

    kubectl version
    

For more information check out the documentation for kubectl installtion on the Kubernetes website

Get cluster information

Try to check the cluster information, run the following command:

kubectl cluster-info
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment