Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / containers / Kubernetes

An Introduction to Kubernetes with Minikube

5.00/5 (1 vote)
3 May 2017CPOL9 min read 20.5K  
A brief introduction to Kubernetes using MiniKube on a local environment

Introduction

In my new series on the Project Calico blog, I started off by sharing some of the challenges enterprises are facing today with their current infrastructures and architectures. This includes the need to be able to adapt, and to innovate quickly - all while trying to balance scale, security, and network resiliency.

The answer to these challenges, as first heavily promoted by Netflix, has been the move to Cloud Native architectures. Or in other words, the ability to set up, kill, and replace instances as needed, and with little to no consequence (imagine if someone shutdown your server today, could you say the same thing?).

According to the Cloud Native Computing Foundation, a cloud native system incorporates three things:

  • Applications and processes are containerized
  • These containers are dynamically orchestrated
  • The architecture is microservices oriented

This essentially means that your applications are stored as images that can quickly and easily be deployed, and managed. However, because you could have thousands of instances or deployments, it becomes very difficult to manage them manually - not to mention you probably don’t want to have to reset a service yourself every time it fails. For this reason, you’ll want to take advantage of an orchestrator like Kubernetes, Swarm, Apache Mesos, or Rancher Cattle.

Kubernetes (also referred to as k8s) was originally created and open sourced by Google before being donated to the Cloud Native Computing Foundation. Since being released, Kubernetes has quickly become one of the most popular and widely used orchestrators known for its fault-tolerance, scaling, discovery abilities - and its active community.

Unfortunately, it isn’t necessarily known for simplicity, and as you dig into Kubernetes, you’ll find that while it contains a robust API, there is a little bit of a learning curve. But that doesn’t mean that at its face, Kubernetes isn’t easy to get started with, and to start playing around with/ exploring.

And that’s exactly what we’ll be doing in this short tutorial. In just a few steps, we will be setting up Kubernetes locally (using MiniKube), launch the Kubernetes dashboard, start a new Ghost blogging platform deployment, make the service available publicly, and finally shell into the first container inside of a pod to make some quick updates.

Step 1. Install a Virtual Machine

To install Minikube, first you will need to make sure you have an appropriate virtual machine running on your system.

For Mac users running OS X, you can use xhyve driver, VirtualBox, or VMWare fusion. If you’re not sure, it may be easiest to start with VirtualBox.

For Linux users, you’ll need either VirtualBox or a KVM installation.

And finally for Windows users, VirtualBox or Hyper-V.

Step 2. Install kubectl

The next step is to install kubectl.

For Mac users, run the following in your Terminal:

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

For Linux users, run:

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

And for Windows users, run:

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

Then (for all users), run the following to make kubectl executable (note, you may need to change the path to where you want it to be stored locally):

chmod +x ./kubectl
sudo mv ./kubectl /usr/local/bin/kubectl

Step 3. Install Minikube

Finally, we are now ready to install Minikube. For this tutorial, we will be using Minikube v0.18.0, however, be sure to visit the Minikube repository on GitHub to ensure that this is the latest release.

For Mac users, again in your terminal run:

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

For Linux users, run:

curl -Lo minikube https://storage.googleapis.com/minikube/releases/v0.18.0/
minikube-linux-amd64 && chmod +x minikube && sudo mv minikube /usr/local/bin/

If you are using Windows, you will need to install the minikube-windows-amd64.exe and rename it to minikube.exe.

If you run into issues, visit the Minikube Installation Guide.

Step 4. Some Quick, Basic Tests

You should now have Minikube installed. To test if everything’s working, run the following command to start Minikube:

minikube start

If the installation was successful, you should see something like:

Starting local Kubernetes cluster...

Starting VM...

This process can take several minutes, as it continues to SSH files into the VM, set up certs, and start the miscellaneous components for you.

You will know it is setup when you see:

Connecting to cluster...

Setting up kubeconfig...

Kubectl is now configured to use the cluster.

Now we can test out kubectl and Minikube by running:

kubectl get pods

Which should return “No resources found” since we haven’t deployed anything yet.

Step 5. Deploy the Dashboard

The great thing about Minikube is that it comes with a nice shortcut for opening the Kubernetes Dashboard, or a web GUI we can use to create and manage our deployments.

To open the Kubernetes Dashboard, simply type:

minikube dashboard

Your computer should then open a new browser window that looks like this:

Image 1

Step 6. Create a New Deployment

To create a new deployment, you can click on “deploy a containerized app” or on “+ CREATE” in the upper right of the window.

You should now see a screen that looks like this:

Image 2

For the App Name, we’ll call this deployment “myblog”.

For the container image, we will be using the Ghost Blogging System’s container image. You can find the image name in the Docker Hub by looking at the pull command to the right.

Image 3

So for the Container Image, we will use the actual image name, which in this case is “ghost”.

You can leave the number of pods set to 1, as well as Service set to “none.”

Go ahead and click “Deploy.

You should now see two different sections on your screen, one for Deployments and one for Replica sets. Under Deployments, you should see myblog, and under replica sets, you should see something that starts with myblog followed by a dash and a series of numbers.

Under Pods, you should either see 0/1 or 1/1 (depending on if the pods are successfully running yet).

If you see 0/1, refresh the screen until you see 1/1.

Now, run the following in the command line:

kubectl get deployments

You should see your myblog deployment, or something like:

NAME      DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
myblog    1         1         1            1           2m

Now let’s check on the pod we created:

kubectl get pods

Hopefully, you should see the pod up and running, or something that looks like this:

NAME                      READY     STATUS    RESTARTS   AGE

myblog-2479762429-7n5xw 1/1 Running 0 3

Some of the key things we’re looking for here is if the pod is ready (#/# should equal 100%), the pod status (should be running) and the number of restarts of the pod.

If Status is “CrashLoopBackOff” or the number of restarts is greater than 0, it means that your pod failed to start for some reason or another.

Step 7. Expose the Service

However, just because we’ve launched the service, doesn’t mean it is accessible via our web browser… something that is probably important when launching a new blog.

In this case, we need to tell Kubernetes to open a port for this pod (Ghost is expecting port 2368), and to give it an accessible IP.

To do this, we’ll run the following command exposing the deployment: myblog:

kubectl expose deployment/myblog --port=2368 --type=NodePort

If successful, you should see something like this:

service "myblog" exposed

Step 8. Open the Service

Now that the service has been exposed, we can try opening it in a browser window. The easiest way to do this is by taking advantage of the minikube service shortcut like so:

minikube service myblog

This should open your Ghost blog in a new browser window (just like it did with the dashboard). If everything worked, you should see your new blog up and running:

Image 4

To learn more about Ghost as well as to set up your new (locally hosted) blog, you can click on the “Welcome to Ghost” blog post.

Step 9. Shell into the First Container

The last thing we want to try to do is shell into the container running our blog and make some quick changes (note this is just for learning/ testing purposes only).

To do this, let’s first get the pod name. You can get the pod name from the Kubernetes Dashboard (minikube dashboard) under “Pods” or from the terminal using the command kubectl get pods.

Image 5

Now we can use kubectl exec to shell into the first container in this pod, like so:

kubectl exec myblog-2479762429-7n5xw -it -- /bin/bash

You’ll notice you’re now inside the first container as root:

root@myblog-2479762429-7n5xw:/usr/src/ghost#

Now you can browse your container’s directories, modify the Ghost theme, upload additional themes, and more.

Keep in mind the Ghost container does not come with Vim installed by default, so if you want to install Vim, you will need to run the following commands:

apt-get update

apt-get install vim

To exit the Ghost container shell, simply run the exit command:

exit

Step 10. Killing Deployments/ Pods

Now, we will delete our deployments and pods to free up the resources they are currently taking (this is also important when hosting deployments and pods on services like AWS/ GKE to avoid charges).

To do this, we will run the following commands (we could also do this from the Kubernetes Dashboard but the command line is usually faster):

kubectl delete deployments --all

kubectl delete pods --all

Keep in mind - using these commands will delete ALL deployments and pods. You can also delete deployments and pods one by one by adding in the name of the deployment or the pod instead of adding the “--all” flag.

Now if we get our deployments (kubectl get deployments) and pods (kubectl get pods), we should once again see No Resources Found.

Finally, we can stop Minikube:

minikube stop
ProTip: Try running these steps again only using "httpd" for the container image and expose port 80 instead of 2368. You should now have a running and exposed Apache container!

Recap

To recap, Kubernetes is a container orchestration solution designed to make the management of containers, pods, and clusters easier. It also helps keep your pods running, managing faults, and enabling your applications to scale as needed.

In this simple tutorial, we took a quick look at installing kubectl, Minikube, the Kubernetes Dashboard, setting up a deployment, exposing a deployment, shelling into a container, and finally deleting deployments and pods.

Of course, this is just scratching the surface of the power of both containerization and Kubernetes, and was focused on a very simple, localized deployment (versus say a high availability, mission critical deployment).

It’s also important to understand that moving to a cloud native infrastructure (regardless of which orchestrator you choose) brings with it new challenges including networking and security. As you start evaluating Kubernetes, I also recommend that you start evaluating tools and plugins to not only give you the most out of Kubernetes, but ensure your applications have built in networking and security redundancies. One of the more popular open source plugins, Project Calico will provide you with both scalable container networking (using a pure L3 approach) as well as seamless cluster isolation.

In my next post, I’ll try to cover using Kubernetes in more of a production environment such as AWS or GKE, as well as dive more into how to ensure scalable networking and container/ cluster security.

You can also find a template for setting up Kubernetes on Amazon with this AWS Kubernetes Quick Start.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)