Getting Started with Kubernetes and Minikube

If you want to get started with Kubernetes and take some time exploring the concepts and features of what this orchestration tool can bring to your infrastructure, but don’t have an access to Cloud provider like Google Cloud Platform, AWS or Microsoft Azure, Minikube will be the fastest and easiest option for you.

Kubernetes is a cluster consisting of masters and workers nodes, and deploying it can take a lot of time and resource. Having a full fledge Kubernetes cluster only for testing purpose would be meaningless anyway. And that’s why Minikube can be very handy.

What is Minikube?


Minikube is an all-in-one solution that comes in a VM and provides all the features that a complete Kubernetes cluster has. In fact, in Kubernetes, you can have a node that has both the master and worker roles, and this is what Minikube is all about.

After installing Minikube, you will a one node Kubernetes cluster, and this node is a master and a worker at the same time.

What is Needed for Installing Minikube?


  • VT-x or AMD-v virtualization must be enabled in your computer’s BIOS.
  • A hypervisor must installed on your machine. This is because Minikube runs the Kubernetes cluster on a VM. VMware workstation, Microsoft Hyper-V, Oracle VirtualBox or KVM will do it.
  • A tool that is used to interact with your Kubernetes cluster called kubectl. By interacting, I mean creating resources like pods, deployments, services, …etc. kubectl is a command line interface for running commands against Kubernetes clusters. Kubernetes resources can also be created and managerd through the GUI Web console dashboard, but this tool stands far behind kubectl in terms of capabilities and management options.
  • An Internet connection for downloading the required binaries prior installation.

I am using a Windows 7 for my daily tasks, and using VMware Workstation 11 as a hosted hypervisor. I have one VM running CentOS 7 and I’ll take advantage of nested virtualization to install KVM, kubectl then Minikue as a nested VM on top of my virtualized CentOS server.

As such, the CPU virtualization is enabled on the VM settings. The VM has 4GB of vRAM, vCPU and 20GB of disk space.

Enable CPU virtualization for the CentOS VM

 

In fact, I could also have installed Minikube on my Windows 7 machine using Chocolatery, or Windows 10 using Hyper-V, but Linux is my something of choice when it comes to testing Open Source projects like Kubernetes or Docker.

A Side Note about Minikube Drivers


Several hypervisors can be used to run the Minikube VM, and in the Minikube jargon, these are called drivers. For instance, if you opt to use Hyper-V to run Minikube, you are using the hyperv driver. If instead, you want to use Oracle VirtualBox, the virtualbox driver needs to installed.

The installation steps will change depending on the driver you are using. More information can be found on the Kubernetes Official Documentation here.

This post will go through the installation of the kvm2 driver, Kubectl then Minikube on Linux CentOS 7. If you want to use Minikube on Windows. check this post instead.

Installing Minikube on Linux CentOS 7


Installing the kvm2 driver


To install the KVM2 driver, first install and configure the prereqs:

  • First, update your system
$ sudo yum update -y

  • Install libvirt and qemu-kvm on your system
$ sudo yum -y install qemu-kvm qemu-img virt-manager libvirt libvirt-python libvirt-client virt-install virt-viewer bridge-utils
  • Start and enable libvirtd service
$ sudo systemctl start libvirtd & sudo systemctl enable libvirtd

 

  • Add yourself to the libvirt group so you don’t need to sudo
$ sudo usermod -a -G libvirt $(whoami)
  • Update your current session for the group change to take effect
$ newgrp libvirt
  • If you are running a minimal CentOS installation, you will need install the x-window package to allow virt-manager to start. virt-manger can be used to manage the minikube VM from GUI.
$ sudo yum install "@X Window System" xorg-x11-xauth xorg-x11-fonts-* xorg-x11-utils -y
  • Install the Minikube kvm2 driver
$ curl -Lo docker-machine-driver-kvm2 https://storage.googleapis.com/minikube/releases/latest/docker-machine-driver-kvm2

This kvm2 driver will be downloaded and saved to a directory called docker-machine-driver-kvm2

$ ll
total 37764
drwxr-xr-x. 2 user user           6 Sep 18  2017 Desktop
-rw-r--r--. 1 user libvirt 38669136 Feb  6 08:15 docker-machine-driver-kvm2
drwxr-xr-x. 2 user user           6 Sep 18  2017 Documents
drwxr-xr-x. 2 user user           6 Sep 18  2017 Downloads
...
  • Make the kvm2 driver directory executable
$ chmod +x docker-machine-driver-kvm2
  • Copy the kvm2 driver directory to your path
$ sudo cp docker-machine-driver-kvm2 /usr/local/bin/
  • Once copied, you might want to delete it from your home directory to free up some space, as it is no longer needed
$ rm docker-machine-driver-kvm2

 

Installing Kubectl


On Linux, Kubectl can be compiled from source or installed from the released binaries, which is a faster and easier method to get this utility running in your machine. We’ll opt for this one.

  • Download kubectl binaries
$ curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl
  • Make the kubectl binaries directory executable
$ chmod +x ./kubectl
  • Add the kubectl executable to your path
$ sudo mv ./kubectl /usr/local/bin/kubectl
  • Check your installation
$ kubectl version
	
	Client Version: version.Info{Major:"1", Minor:"12", GitVersion:"v1.12.1", GitCommit:"4ed3216f3ec431b140b1d899130a69fc671678f4", GitTreeState:"clean", BuildDate:"2018-10-05T16:46:06Z", GoVersion:"go1.10.4", Compiler:"gc", Platform:"linux/amd64"}
	The connection to the server localhost:8080 was refused - did you specify the right host or port?

No worries If you see a message similar to the following, stating that the connection was refused. kubectl is checking if it has been correctly configured to connect to an existing Kubernetes cluster. Because Minikue is not installed yet, the errors above shows up. As long as you can see the Client version displayed, that means that kubectl installation went fine.

Installing Minikube


  • Download Minikube Binaries
$ curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
  • Install Minikube
$ sudo install minikube-linux-amd64 /usr/local/bin/minikube
  • Check Minikube Installation
$ minikube version
minikube version: v0.30.0
  • To avoid any configuration issue, temporary disable SELiux
# setenforce 0

Now that Minikube and KVM are installed, you just need to start the Minikube VM using the KVM2 driver.

$ minikube start --vm-driver kvm2

Starting local Kubernetes v1.10.0 cluster...
Starting VM...
Downloading Minikube ISO
 170.78 MB / 170.78 MB [============================================] 100.00{a30ec6af4236f4179fdacbfa14163c342445d6f2c9798eae9caee1ef22a84454} 0s
Getting VM IP address...
Moving files into cluster...
Downloading kubeadm v1.10.0
Downloading kubelet v1.10.0
Finished Downloading kubelet v1.10.0
Finished Downloading kubeadm v1.10.0
Setting up certs...
Connecting to cluster...
Setting up kubeconfig...
Starting cluster components...
Kubectl is now configured to use the cluster.
Loading cached images from config file.

During these steps, the Minikube ISO, kubeadm and kubelet are downloaded then started, an IP address is assigned to the Minikube VM, certifications have been generated and assign to the cluster, and kubectl has been configured automatically to connect to 111411

Minikube also supports a –vm-driver=none option that runs the Kubernetes components on the host and not in a VM. This option will allow to run Kubernetes without the need to have a hypervisor installed for running the VM. Note that Docker is required to use this driver though. If you use –vm-driver=none, be sure to specify a bridge network for docker. Otherwise it might change between network restarts, causing loss of connectivity to your cluster.

The output of the command is shown below for demonstration purpose

$ sudo minikube start --vm-driver none
Starting local Kubernetes v1.10.0 cluster...
Starting VM...
Getting VM IP address...
Moving files into cluster...
Downloading kubeadm v1.10.0
Downloading kubelet v1.10.0
Finished Downloading kubelet v1.10.0
Finished Downloading kubeadm v1.10.0
Setting up certs...
Connecting to cluster...
Setting up kubeconfig...
Starting cluster components...
Kubectl is now configured to use the cluster.
===================
WARNING: IT IS RECOMMENDED NOT TO RUN THE NONE DRIVER ON PERSONAL WORKSTATIONS
		The 'none' driver will run an insecure kubernetes apiserver as root that may leave the host vulnerable to CSRF attacks

When using the none driver, the kubectl config and credentials generated will be root owned and will appear in the root home directory.
You will need to move the files to the appropriate location and then set the correct permissions.  An example of this is below:

		sudo mv /root/.kube $HOME/.kube # this will write over any previous configuration
		sudo chown -R $USER $HOME/.kube
		sudo chgrp -R $USER $HOME/.kube

		sudo mv /root/.minikube $HOME/.minikube # this will write over any previous configuration
		sudo chown -R $USER $HOME/.minikube
		sudo chgrp -R $USER $HOME/.minikube

This can also be done automatically by setting the env var CHANGE_MINIKUBE_NONE_USER=true
Loading cached images from config file.

Minikube is now installed and started. Check again the kubectl installation to confirm that it is able to see and connect to Minikube

$ kubectl version
Client Version: version.Info{Major:"1", Minor:"12", GitVersion:"v1.12.1", GitCommit:"4ed3216f3ec431b140b1d899130a69fc671678f4", GitTreeState:"clean", BuildDate:"2018-10-05T16:46:06Z", GoVersion:"go1.10.4", 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"}

You can also check the Minikube VM status by running the minikube status command. This will also display the IP that was assigned to the VM.

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

You can also install virt-manager, which is the graphical tool to manage KVM virtual machines, to check that the Minikube VM is effectively up and running.

$ virt-manager

Minikue VM up and running

If you’re not sure or forgot the IP address of the Minikube VM, you can display again with this command

$ minikube ip
192.168.39.186

To ssh to the Minikube VM, use the minikube ssh command

$ minikube ssh
                         _             _
            _         _ ( )           ( )
  ___ ___  (_)  ___  (_)| |/')  _   _ | |_      __
/' _ ` _ `\| |/' _ `\| || , <  ( ) ( )| '_`\  /'__`\
| ( ) ( ) || || ( ) || || |\`\ | (_) || |_) )(  ___/
(_) (_) (_)(_)(_) (_)(_)(_) (_)`\___/'(_,__/'`\____)

$ hostname
minikube

 

Working with Kubernetes


Run the Kubernetes dashborad

Kubernetes is usually managed with kubectl, but you can also the Web console dashboard. Fortunately, this will also be installed automatically during Minikube installation.

To display the Kubernetes Web console, use below command

$ minikube dashboard
Opening http://127.0.0.1:34121/api/v1/namespaces/kube-system/services/http:kubernetes-dashboard:/proxy/ in your default browser...
START /usr/bin/firefox "http://127.0.0.1:34121/api/v1/namespaces/kube-system/services/http:kubernetes-dashboard:/proxy/"

The Kubernetes Dashboard

Running Kubernetes resources using Kubectl

Kubectl is the tool to use if you want to seriously get into Kubernetes and have all the advantages that it brings to the table, and once you get used of this tool, you won’t trade for it for the world.

To create a deployment, the kubectl run is used, so let’s get some on how to this

$ kubectl run --help | less	
	
Create and run a particular image, possibly replicated.

Creates a deployment or job to manage the created container(s).

Examples:
  # Start a single instance of nginx.
  kubectl run nginx --image=nginx

  # Start a single instance of hazelcast and let the container expose port 5701 .
  kubectl run hazelcast --image=hazelcast --port=5701

  # Start a single instance of hazelcast and set environment variables "DNS_DOMAIN=cluster" and "POD_NAMESPACE=default" in the container.
  kubectl run hazelcast --image=hazelcast --env="DNS_DOMAIN=cluster" --env="POD_NAMESPACE=default"

As you can see, the kubectl help is simply, very helpfull when it comes to clear and concise examples. Here, to run an nginx image to our freshly created Kubernetes lab cluster, run the following command:

$ kubectl run nginx --image=nginx
kubectl run --generator=deployment/apps.v1beta1 is DEPRECATED and will be removed in a future version. Use kubectl create instead.
deployment.apps/nginx created

To check that your deployment is running, use below command

$ kubectl get deploy

NAME      DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
nginx     1         1         1            1           54s

Keep in mind, that there is another method to create Kubernetes resources, which by creating manifest files then running them with kubectl. And this the way to go in most of production environments, as this method provides more capabilities and flexibility, but I won’t go through the steps on how to do it in this post. The scope being just to install and run Minikube.

That’s it for this post. You have now a running cluster where you can test and manage your Kubernetes resources. Enjoy!

 

Leave a Comment

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