Setting up Minikube and kubectl for Kubernetes Development

neotam Avatar

K8s Development Pack
Posted on :
,

Minikube is a tool that makes it easy to run and setup kubernetes cluster for development locally on laptop or desktop. Minikube runs a single node cluster inside given Virtual Machine. It is also possible to run minikube on host without VM on Linux. It’s goal is to enable fast local development and support all Kubernetes features that fit. This article walks you through installation of minikube and kubectl for kubernetes development for different platforms.

Minikube installation requires Hypervisor and corresponding driver for minikube. Minikube supports following drivers

  • virtualbox
  • vmwarefusion
  • kvm2
  • kvm
  • hyperkit
  • xhyve
  • hyperv
  • none

The derive “none” is the special one. Which works on only Linux. It is helpful if you want to run Minikube on host without VM. Especially it is helpful if minikube is configured to use inside virtual machine.

Install kubectl

The command line tool kubectl is used to manage application on Kubernetes cluster.

It is important to install kubectl which is compatible with kubernetes cluster version. Choose kubectl version that is within one minior version difference of targeted kuberntes cluster. i.e, v1.2 client should work with v1.1, v1.2 and v1.3 .
You can check kubectl version using command “kubectl version

Install kubectl on Windows

Install kubectl using Chocolaty

choco install kubernetes-cli

Install kubectl using Scoop

scoop install kubectl 

Install kubectl on macOS

Install kubectl using HomeBrew

brew install kubernetes-cli

Install kubectl using MacPorts

sudo port selfupdate
sudo port install kubectl

Install kubectl on Linux

Install kubectl using package manager

For Debian base distributions

sudo apt-get update && sudo apt-get install -y apt-transport-https
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 -a /etc/apt/sources.list.d/kubernetes.list
sudo apt-get update
sudo apt-get install -y kubectl

For Red Hat base distributions

cat < /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
EOF
yum install -y kubectl

Install kubectl from binaries

Current Stable version number can be found at,

https://storage.googleapis.com/kubernetes-release/release/stable.txt

Download Binaries of stable kubectl version

curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl

If you are looking for a specific version, replace $(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt) with version number that you want to install.

Once the file kubectl is downloaded make it executable

chmod +x ./kubectl

Move kubectl to one of executables PATH, i.e /usr/local/bin

sudo mv ./kubectl /usr/local/bin/kubectl 

Configure kubectl

Starting Minikube for the first time (command minikube start) will automatically create a context called “minikube” which is configured to point kubectl to the minikube cluster.

Default configuration files that kubectl reads can be found at,

  • ~/.kube/config on Linux
  • %USERPROFILE%/.kube/config on Windows

Install Minikube

Prerequisites

  • VT-x or AMD-v Virtualization must be enabled in BIOS
  • Hypervisor
  • kubectl on host

On Linux, the driver “none” (–vm-driver=none) is supported. We don’ need any kind of hypervisor but the docker (container tooling) if driver “none” is used. It is not recommended for production.

Install Minikube On Windows

Prerequisites

  • Hypervisor such as VirtualBox or HyperV
  • VT-x/AMD-v virtualization must be enabled in BIOS

VirtualBox is recommended. Minikube uses virtualbox driver by default on windows

Install Minikube Using Chocolatey

This is the easiest way to install Minikube on windows using Chocolatey.

choco install minikube kubernetes-cli

It is required to open power shell as administrator (run as administrator )

Install Minikube Using Windows Installer

Download the installer at minikube-installer-latest.exe and execute to install.

Install Minikube Manually

To manually install Minikube on Windows,

Install Specific Version

Specific version can be downloaded and installed from minikube-releases

Install Minikube On Linux

Install Minikube Using Binaries

Download Binaries and change permissions to make it executable

curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64 
chmod +x minikube  

Add Minikube executable to path

sudo cp minikube /usr/local/bin && rm minikube

Install Minikube On macOS

We can install Minikube on macOS using Hombrew

brew cask install minikube

It is also possible to install Minikube from binaries just in case specific version is required

(OR)

Download Minikube Binary, make it executable and add it to path

curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-darwin-amd64 
chmod +x minikube
 sudo mv minikube /usr/local/bin

kubectl Cluster Configuration for minikube

The command minikube start creates a context called “minikube” for kubectl. Minikube sets this context default on installation. This context contains the configuration to communicate with Minikube cluster.

Display kubectl configuration

kubectl config view

The command kubectl can be configured with multiple contexts to communicate with different kubernetes clusters. If you need to switch back to minikube context

kubectl config use-context minikube

Start Minikube

Starting Minikube for the first time will configure cluster and kubectl.

Start Minikube

minikube start

Above command uses the default driver of corresponding platform. We can instruct minikube to use specific driver when we start for the first time . For example, we want to use HyperV as hypervisor. Thus driver will be hyperv

minikube start --vm-driver=hyperv

Start Minikube with specific version

It is also possible to specify version of Kubernetes using command line argument
–kubernetes-version. For example to specify version 1.11.3 command would be

minikube start --kubernetes-version v1.11.3

Test Installed Minikube

To test if Minikube is installed successfully and the command line tool kubectl is pointing to installed Minikube

Check Minikube status

minikube status
minikube: Running
cluster: Running
kubectl: Correctly Configured: pointing to minikube-vm at 10.0.2.15

Check kubectl version

kubectl version
Client Version: version.Info{Major:"1", Minor:"10", GitVersion:"v1.10.2", GitCommit:"81753b10df112992bf51bbc2c2f85208aad78335", GitTreeState:"clean", BuildDate:"2018-04-27T09:22:21Z", GoVersion:"go1.9.3", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"10", GitVersion:"v1.10.0", GitCommit:"fc32d2f3698e36b93322a3465f63a14e9f0eaead", GitTreeState:"clean", BuildDate:"2018-03-26T16:44:10Z", GoVersion:"go1.9.3", Compiler:"gc", Platform:"linux/amd64"}

If you see both “Client Version” and “Server Version”. It means both kubectl (client) and Kubernetes (master) version are aligned and kubectl is able to communicate with k8s API Server.

Stop Cluster

minikube stop

This command shuts down the Minikube (Virtual Machine) but preserves cluster state and data. So, starting cluster again will restore the previous state and data.

Restarting cluster doesn’t require a drive parameter since minikube is already configured and previous state is stored.

Delete Cluster

minikube delete

This command is used to delete whole cluster. This command shuts down and deletes Minikube (Virtual Machine), deletes cluster state and data.

You might want to re-setup your Minikube if cluster is malfunctioning by deleting it using minikube delete .

Cleanup For Fresh Start

To cleanup installed old minikube

Delete Minikube Cluster

minikube delete

Remove Configurations

rm -rf ~/.minikube

Troubleshooting Minikube

  • Check compatibility of kubernetes server and client(kubectl)
  • If possible cleanup old minikube and it’s configuration if it is causing conflicts
  • Make sure system is connected to internet and domain names are getting resolved
  • Make sure ports that are required by minikube/kubernetes are free if driver “none” is used on Linux
  • Make sure container tooling like docker is compatible and up to date

Error : crictl not found in system path

crictl is a Container Runtime Interface (CRI) CLI. Which is required by minikube. Install crictl using following commands

VERSION="v1.13.0" 
wget https://github.com/kubernetes-sigs/cri-tools/releases/download/$VERSION/crictl-$VERSION-linux-amd64.tar.gz
sudo tar zxvf crictl-$VERSION-linux-amd64.tar.gz -C /usr/local/bin
rm -f crictl-$VERSION-linux-amd64.tar.gz

On Ubuntu you can get more details of a failure of ” minikube start” by using command journalctl

journalctl -r

Delete old cluster before “minikube start”. Following command could help

minikube delete && minikube start --kubernetes-version v1.11.3 --vm-driver=none --bootstrapper kubeadm

You can replace –vm-driver=none with supported driver for the hypervisor you are using. –kubernetes-version v1.11.3 is optional unless you are looking for specific Kubernetes version

Leave a Reply

Your email address will not be published. Required fields are marked *