There is a general perception that Kubernetes is meant only for professionals with advanced infrastructure management skills. While there is a bit of truth in it, there are multiple ways to configure Kubernetes without getting overwhelmed.
With each major release, Kubernetes is getting closer to the developer community. In its current form, it is extremely simple to turn your existing Mac or Windows machine into a powerful Kubernetes development environment. I am playing with Kubernetes on the new 12” Gold MacBook with 8GB RAM and 256GB storage powered by a 1.1GHz Intel Core M3 processor. By no means this is meant to be a developer workstation but, with Minikube, I am able to carry a full-blown Kubernetes environment with me.
This tutorial aims at enabling Docker developers to easily get started with Kubernetes. It will walk you through all the prerequisites, and the basic concepts to deploy your first microservices application powered by Kubernetes. Though this is based on Mac, it will work with Microsoft Windows 10 with just a few modifications.
What Do We Need?
First things first, let’s make an inventory of the tools that we need.
VirtualBox: This is the core foundation on which we will configure the environment. Download the latest version of VirtualBox.
Docker Toolbox: This is the essential component of our toolchain. We will extensively use Docker Machine for the configuration. Download the stable version of Docker Toolbox.
DVM: It can get frustrating to deal with disparate Docker versions running within the same environment. Like Node Version Manager and Ruby Version Manager, this nifty tool makes it super simple to switch between multiple versions of Docker CLI. We will explore this tool in the later part of the tutorial. Follow these instructions on GitHub to get DVM on your machine.
Minikube: All good things in life come in small packages, and Kubernetes is not an exception. Minikube is a tiny VM that comes bundled with a single node Kubernetes cluster. The best thing is that it can run on any machine that is capable of launching VirtualBox VMs. Get the latest binary from GitHub.
Kubectl: Kubectl is the command line tool to interact with the Kubernetes cluster. Follow the instructions to download and install it.
Docker for Windows or Mac [Optional]: Since we are dealing with Docker Machine for creating Swarm cluster, we don’t really need native installers for Windows and Mac. But if you prefer, you can also have it run side-by-side. Make sure that you are grabbing the latest installer.
What Do We Get?
Most of the developers have an isolated development and testing environments for Docker and Kubernetes. This makes it frustrating to quickly build and deploy from source code. The Dockerfile targeting the core engine is always different from the Kubernetes environment. This forces us to push the images to the Docker Hub or a public registry and pull them in Kubernetes nodes, even when they are running within the same environment. While this provides a good isolation for production setup, it can get pretty annoying during development and testing.
In our environment, we will create a central registry that Docker and Kubernetes will share. Every image that we build will become instantly available to the Kubernetes cluster. This brings parity between Docker and Kubernetes environments.
This setup makes it easy to deploy applications in parallel running in Swarm and Kubernetes. Each time an image gets pushed to the registry, it immediately becomes available to both the Swarm cluster and Kubernetes.
This tutorial has some tips and tricks that will help you get a grip on managing disparate Docker environments.
The illustration below explains the setup and the configuration.
Let’s start by creating a central registry that we will access during development and testing. We will then configure Docker and Kubernetes to use the registry.
Step 1: Docker Private Registry
Create a new Docker Machine that acts as the registry:
Create the directory to hold images:
Point the Docker CLI to the machine:
Pull and run the registry image:
Finally, make note of the IP address of Docker Machine running the registry:
Step 2: Docker Dev Machine
Create a new Docker Machine that will be our default development environment:
Configure the Docker Engine to point to the private insecure registry:
Open /var/lib/boot2docker/profile in the editor and add the following line. Make sure that you included the IP address of the Docker Machine hosting the registry:
Optionally, you can run the following single line command to do the same:
Exit from the VM and restart the machine:
Point the Docker CLI to the dev machine:
Pull an image, tag it, and push it the private registry:
This step successfully configured the Docker engine to use the insecure private registry that we created in the previous step.
Step 3: Kubernetes Single Node Cluster
With the Docker private registry and development machine in place, let’s go ahead and configure Kubernetes. This step assumes that you followed the steps mentioned in the Minikube installation guide.
Start the Minikube VM:
A quick note on the configuration of private registry: Minikube can be pointed to the insecure registry during the creation of VM. It is exactly the same as the steps performed in configuring Docker Machine to use the private registry. In fact, Docker Machine also supports this configuration through the — engine-opt switch. Refer to the documentation for more details.
Check the status of Minikube:
Check if kubectl is correctly configured:
Step 4: Test the Workflow
With the testbed in place, let’s now try the entire round trip: Build an image on Docker development machine and deploy it in Kubernetes.
We will push the standard Apache image from the Docker Hub, modify it on the dev machine, commit and push the new image to the local registry.
The same image will be then deployed on Kubernetes.
Build on Docker
Set the CLI to Docker development machine:
Pull, tag, and push the image:
Deploy and Run in Kubernetes
Point the Docker CLI to minikube:
When you try to access the Docker engine on minikube, you will get an error about version mismatch between the client and server:
This is where we will take the help of Docker Version Manager. First, let’s check the Docker version on Minikube:
We can capture the server version into a variable, which will be passed to DVM:
Run the following command to switch to the correct version of Docker CLI:
Now you can use the same client to target Docker Dev machine and Kubernetes machine.
Let’s pull the image that we uploaded to the private registry:
Finally, let’s create a Kubernetes deployment and expose the container:
Grab the NodePort and access the service endpoint:
The above command should print “Kubernetes Rocks.”
Let’s clean up by delete the deployment and service:
You can download the scripts for all the steps from GitHub.
This setup has a few caveats:
It doesn’t use secure registry access but it can be easily configured
Images are not persisted on Mac or Windows host. If the Docker machine hosting the registry crashes, you will lose images. This can be avoided by mounting local volumes from the host machine and pointing the registry volume to it.
You can use Docker for Mac and configure the insecure registry option to pull images from the local registry.
If you want to get started with Kubernetes, please register for the free webinar that I am co-hosting, on Monday, August 29. We will show you how to setup your development environment, and deploy your first application.
The post Configuring the Ultimate Development Environment for Kubernetes appeared first on The New Stack.