Using Kubernetes for Local Development
⚡️ Updated on July 3, 2018 to reflect changes introduced by Skaffold 0.9.
In the past years, I’ve been using Kubernetes in production both at RisingStack - the Node.js consulting company I’ve co-founded - and at GoDaddy at large scale. One of the common challenges I’ve seen is the lack of proper tooling on how one can develop their services locally against a real Kubernetes cluster. In practice, it meant that the developers ran their local instances against development or staging environments, which is far from ideal: if don’t have those services running locally, debugging them can be challenging.
A few days ago, I bumped into Skaffold, a command line tool that facilitates continuous development for Kubernetes applications. In this article, I am going to show you how you can start developing a Node.js app with the help of Skaffold and Kubernetes.
Skaffold is a command line tool that facilitates continuous development for Kubernetes applications. You can iterate on your application source code locally then deploy to local or remote Kubernetes clusters. Skaffold handles the workflow for building, pushing and deploying your application. It can also be used in an automated context such as a CI/CD pipeline to leverage the same workflow and tooling when moving applications to production. - The official Skaffold docs
In practice, Skaffold can:
- detect changes in the source code and automatically build, push and deploy
- automatically update image tags, so you don’t have to do that manually in the Kubernetes manifest files
- build/deploy/push different applications at once, so it is a perfect fit for microservices too,
- support both development and production environment, by running the manifests only once, or continuously watching for changes.
- Make sure that you have Minikube installed - (Skaffold works with any Kubernetes clusters, for the sake of simplicity I picked Minikube)
- Make sure that you have kubectl installed
- Make sure that you have docker installed
Once you have all the dependencies installed, visit the Skaffold Releases page, and grab a build that suits your system, then put it on the system path.
Let’s start with a simple Node.js application - no magic here, just a dummy HTTP server using Express:
const express = require('express')
Next, let’s create a
Dockerfile to containerize the above Node.js application:
Now you can create a dockerized Node.js application by issuing the
docker build . command. However, with that approach, you would only have the image built, but it wouldn’t be running inside a Kubernetes cluster. This is where Skaffold helps out.
YAML files to describe workflows. For the above application, ours may look like this:
As you can see, we have two main parts of the configuration file, the first one being the
build section, the second one being the
deploy section. In the build section, we can define what artifacts (most probably Docker images) we’d like to build, while in the deploy section we can define resources (like services or deployments) we’d like to see in Kubernetes.
paths array tells Skaffold where the Kubernetes manifests are, while with the parameters, you can inject variables into the manifests. To get a more detailed description, please refer to the annotated skaffold example.
With these last steps, you’ve finished the setup process of Skaffold. Chances are, if you are already deploying applications using Kubernetes, you can simply reuse your
Dockerfile and Kubernetes manifests, so you’ll only have to write the Skaffold
To start using Skaffold, you will have to start up Minikube (using
minikube start), then run Skaffold:
Once you start modifying your files, Skaffold will automatically re-deploy to Kubernetes. To access your running service, you can run
minikube service [service-name]. In our example, it is
minikube service node-app. This will open your default browser with the Node.js application you’ve just deployed.
I hope that this tutorial will save you some time when developing services with Kubernetes - as I am fairly new to Skaffold too, please let me know in the comments below in case I missed anything! You can check out the whole example project on my GitHub.