Minikube
########

:date: 2017-03-21 12:38
:author: Russell Ballestrini
:tags: Guide, Kubernetes, Docker
:slug: minikube
:status: published

Minikube allows you to run a self contained, single node, Kubernetes cluster on your workstation.
Once installed and configured, you may use ``kubectl`` to interact with it, just like a production Kubernetes cluster.

Reference: https://kubernetes.io/docs/getting-started-guides/minikube/

install
==========

Requirements: Virtualbox

install kubectl
-----------------

Reference: https://kubernetes.io/docs/tasks/kubectl/install/

.. code-block:: bash

 curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/darwin/amd64/kubectl && chmod +x kubectl && sudo mv kubectl /usr/local/bin
 

install minikube
----------------------

Reference: https://github.com/kubernetes/minikube/releases

.. code-block:: bash

 curl -Lo minikube https://storage.googleapis.com/minikube/releases/v0.17.1/minikube-darwin-amd64 && chmod +x minikube && sudo mv minikube /usr/local/bin/
 
start
==========

Supported hypervisors: virtualbox, vmwarefusion, kvm, xhyve
 
.. code-block:: bash
 
  minikube start --vm-driver=virtualbox

If this is your first time starting minikube, it will perform the following:

.. code-block:: bash

 Starting local Kubernetes cluster...
 Starting VM...
 Downloading Minikube ISO 89.24 MB / 89.24 MB [==============================================] 100.00% 0s
 SSH-ing files into VM...
 Setting up certs...
 Starting cluster components...
 Connecting to cluster...
 Setting up kubeconfig... 
 Kubectl is now configured to use the cluster

Also, you should see a new VM running in VirtualBox, like this:

.. image:: /uploads/2017/virtualbox-running-minikube.png
   :width: 350
   :alt: virtualbox running minikube.

To verify that `kubectl` is configured to use minikube look at the config file (`~/.kube/config`).

Also try running:

* `kubectl get nodes`
* `kubectl get services`

You can also connect to the VirtualBox guest using SSH to have a look around.
In my case the Minikube VM was assigned ``192.168.99.100``.

.. code-block:: bash

 ssh -i ~/.minikube/machines/minikube/id_rsa docker@192.168.99.100

You can see all the containers running with:

.. code-block:: bash

 docker ps
 ps aux

Exit out, you really don't need to interact at this level

Instead we will treat Minikube as a "real" Kubernetes cluster and only use the ``kubectl`` tool.

demo
==========

create a deployment
----------------------

In this example we create an echoserver cluster. 

.. code-block:: bash

  kubectl run hello-minikube --image=gcr.io/google_containers/echoserver:1.4 --port=8080

this command will create -

1 ``deployment``:

.. code-block:: bash

 kubectl get deployments

1 ``replicaset``:

.. code-block:: bash

 kubectl get replicasets

1 ``pod``:

.. code-block:: bash

 kubectl get pods

To make the echoserver accessible externally, you need to ``expose`` the ``deployment``, like this:

.. code-block:: bash

 kubectl expose deployment hello-minikube --type=NodePort

The expose command creates -

1 ``service``:

.. code-block:: bash

 kubectl get services
 NAME            CLUSTER-IP   EXTERNAL-IP   PORT(S)          AGE
 kubernetes      10.0.0.1     <none>        443/TCP          2d
 hello-minikube  10.0.0.225   <nodes>       8080:31136/TCP   58m

To access the service, you connect to the Minikube's IP address on the exposed port.

In my case the Minikube VirtualBox IP is ``192.168.99.100`` and the exposed port is ``31136`` as listed above.

The minikube tool has a shortcut for this info, try:

.. code-block:: bash

 minikube service hello-minikube --url
 http://192.168.99.100:31136

Toss this into a web browser on your local machine and it should echo back!


scale a deployment
-----------------------

Scale up the ``deployment`` named ``hello-minikube`` by setting the number of ``replicas`` to ``3``:

.. code-block:: bash

 kubectl scale deployment hello-minikube --replicas=3

verify:

.. code-block:: bash

 kubectl get deployments
 kubectl get pods
 
delete a deployment
-----------------------
 
trash this demo (delete the ``deployment``, ``replicaset``, ``pods``, and ``service``):
 
.. code-block:: bash

 kubectl delete deployment hello-minikube

.. contents::
