Image for post
Image for post
Containers, containers everywhere. — Photo © Lode Van de Velde

Getting Started with Kubernetes via Minikube

Claudio Procida
Jul 24, 2016 · 6 min read

Kubernetes is an open-source production-grade container orchestration system for automating deployment, scaling, and management of containerized applications.

This tutorial is a simplified version of the Kubernetes Hello World Walkthrough, which uses minikube to run your service on a local Kubernetes cluster instead of Google Container Engine, so that you won’t need a cloud platform at all.

This tutorial uses Mac OS X, but the reader can easily follow on a different OS using the command flavors as noted in the various sections.

Prerequisites

  • Check that your system supports VT-x/AMD-v virtualization by running this command:

if the command returns an output, you’re good to go. Here’s the output on my machine:

If you’re on Linux, you would run instead:

  • Install kubectl for your OS and architecture:

Smoke test your minikube install

In order to validate our deployment and check that everything went well, let’s start the local Kubernetes cluster:

We can inspect the cluster for running pods:

…and cluster nodes:

Install the hello-minikube pod

The minikube project on GitHub offers a quick start demo which uses a pre-built Docker image hello-minikube. Since we started the minikube cluster already, we can skip the first step:

Now, let’s run the built-in hello-minikube pod. This will create a deployment for the pod:

We can inspect the pods and the deployments to verify these have been updated with the following commands:

In order to access the hello-minikube service, we must first expose the deployment to an external IP via the command:

Note we must use the type=NodePort because minikube doesn’t support the LoadBalancer service. We can check if the service was exposed by listing services:

Now we can either curl the service from the CLI, or hit it via the browser. In order to figure out its external IP address and port, we can use the command:

Note you can figure out the IP address, which is mutable and controlled by VirtualBox, via the command minikube ip, or inspecting the output of ifconfig:

Once we’re done with the hello-minikube service, we can delete its deployment and service, in order to free up resources, and verify they were in fact deleted:

Build and install a node service with Docker

This last example is a bit more complex and requires the creation of a small node server, build it into a Docker image with a Dockerfile, and run it in Kubernetes.

Let’s create a simple node project hello-node:

Let’s create a simple http server that returns a Hello World! response:

Let’s edit the Dockerfile to declare this image will use node 4.4, and the container will run the service by executing the server.js file:

Before issuing any Docker commands, let’s set the Docker environment. Similarly to running eval $(docker-machine env), we generate Docker environment variables for the minikube runtime using the command minikube docker-env:

Now, let’s build the image. This will take some time, as it will fetch images for dependencies like node 4.4 from the Docker hub. Once finished, you’ll have a new Docker image ready to deploy (note the trailing dot `.` in the command: this tells Docker to build the current directory):

Now we can deploy the hello-node pod to our local Kubernetes cluster via kubectl:

As before, we must expose the deployment to an external IP address and port in order to access it via curl:

Victory! We have successfully written, built, and deployed a simple node service as Docker image to our local Kubernetes cluster :-)

This toy project is just a starter to become productive with Kubernetes locally, before you start publishing pods to your cloud platform environment and incur in charges. It allows you to make your first baby steps without fear of scaling down your credit, while you run your experiments.

Cleanup

Let’s not forget to delete the service and deployment for hello-node, and shut down the minikube cluster once finished:

Thanks for reading through this tutorial! Let me know if you found it useful in the comments ;-)

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store