Easy Kubernetes Development with Google Skaffold

Skaffold is a command line tool from Google that brings automation to the deployment workflow of Kubernetes applications. This tool is tailored to provide developers with an experience of iteration and deployment which closely mirrors production. What this essentially means is that the developers will now be able to locally iterate on application source code without worrying about how it is updated in the later stages of validation and testing in the Kubernetes clusters.

Skaffold automates the traditional workflow and therefore handles building, pushing and deploying of applications running on Kubernetes clusters. Any directory with a Dockerfile can be deployed to Kubernetes cluster with this command line tool. What happens technically is that Skaffold builds the docker image locally, pushes it to a registry and deploys it using the skaffold CLI tool. For that reason, the work of developers becomes quite easy since they only need to launch Skaffold, run it in the background and develop their code while Skaffold updates the application continuously without the intervention of typing in any extra commands. At the same time, it can be employed when applications are being moved to production in the processes of continuous integration and continuous delivery due to its powerful tools. That is the inherent power that comes with Skaffold.

Main features

Skaffold solves a problem for developers and to better understand how it works, let us look at the features that it offers:

  • Skaffold is an automation tool and has the capability of detecting changes made on the source code while it runs and proceeds with the process of building, pushing and deploying automatically.
  • It supports multiple application components: Skaffold supports the workflows and tools that are already in your environment.
  • During implementation, Skaffold updates image tags in Kubernetes manifests which alleviates the developer from doing it manually. Thanks to the image tag management feature that comes with Skaffold.
  • Skaffold automatically detect changes in your source code and automatically build/push/deploy.
  • What is more interesting about Skaffold is that it has no server-side component. This reduces the configuration process and cluster overhead.

Where Skaffold saves the day

  • Deployment of applications on Kubernetes
  • Definition of build, push and deployment pipelines in the Continuous integration and Continuous delivery flow.
  • Quite useful in iterative builds in a loop of continuous build-deploy process.

Advantages of Skaffold

Skaffold guarantees easy app deployment compared to the traditional way. This shortens the time of moving from Development to Production.

The architecture that Skaffold is built upon is a pluggable type of architecture. What this allows one to have is the freedom to pick the tools in the development workflow that work best. This provides flexibility and high usability compared to other tools such as Draft.

When compared to other tools, Skaffold has the source-code watching capability which triggers build and push automatically without further commands to facilitate it. This is one of the major reasons one should consider using Skaffold.

Skaffold saves invaluable time in deployment which can be spent on other activities to improve the economic status of an organization.

Installing Skaffold

For this setup, you need a working Kubernetes cluster or alternatively use Minikube. Check out our previous guide on how to install and configure minikube on your local machine. Once you have Kubernetes environment ready, proceed to install Skaffold.

Installation of Skaffold has the following prerequisites:

  • Docker Engine
  • Docker image registry: Your docker client should be configured to push to an external docker image repository. If you’re using a minikube or Docker for Desktop cluster, you can skip this requirement.
  • kubectl — Kubernetes command-line tool

Installing Skaffold is a straightforward process since the package is distributed as a binary file. You only need to download the package, place it under your $PATH and make it executable. You can use wget or curl to pull the binary file.

Installing on Linux

Download the package:

# wget https://storage.googleapis.com/skaffold/releases/latest/skaffold-linux-amd64  -O /usr/local/bin/skaffold

Make the file executable:

# chmod +x  /usr/local/bin/skaffold

Installing on macOS

Download the package.

# wget https://storage.googleapis.com/skaffold/releases/latest/skaffold-darwin-amd64  -O /usr/local/bin/skaffold
# chmod +x /usr/local/bin/skaffold

Confirm that the directory with newly added skaffold package is in your PATH environment. You can confirm using:

# echo $PATH

Demo

Now that you have everything you need to start playing with Skaffold, let’s work on a demo project to help you grasp all the concepts.

Create Dockerfile

Skaffold needs Dockerfile to be used for building Docker image.

$ mkdir ~/skaffold-demo
$ cd ~/skaffold-demo
$ vim Dockerfile

The contents of my Dockerfile are as below:

FROM nginx
COPY index.html /usr/share/nginx/html/
WORKDIR /usr/share/nginx/html

The content of index.html file:

$ cat index.html
Hello world - Test Page 1

This will pull official Docker image for Nginx, copies index.html file to Nginx default document root, and finally sets working directory to /usr/share/nginx/html.

Create Kubernetes deployment

Your next step is to create Kubernetes deployment yaml file. The contents of my file is:

$ cat k8s-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: hello-world
spec:
selector:
matchLabels:
app: hello-world
template:
metadata:
labels:
app: hello-world
spec:
containers:
- name: hello-world
image: nginx-skaffold

Create Kubernetes Service

$ cat k8s-service.yaml 
apiVersion: v1
kind: Service
metadata:
name: hello-world
spec:
selector:
app: hello-world
ports:
- port: 8080
protocol: TCP
targetPort: 80
type: LoadBalancer

Create Skaffold configuration

The Skaffold configuration file is where the workflow is configured. In this file you define the tools you will be using and their configuration. Skaffold configuration file used in this demo has the following content.

$ cat skaffold.yaml 
apiVersion: skaffold/v1alpha2
kind: Config
build:
artifacts:
- imageName: nginx-skaffold
workspace: .
local: {}

deploy:
kubectl:
manifests:
- k8s-*

From the file definitions, it can be seen that Skaffold workflow has three main phases.

1. Build

In the build phase, Skaffold builds an artifact using the tool of your choice. Artifacts are Docker images that you would like to deploy into your Kubernetes cluster when changes are made to them.

2. Push

In the push phase, Skaffold ensures that your image is uploaded to the registry referenced in the image name. Before running Skaffold you should ensure that you are able to push to your configured image registry.

When using local Kubernetes clusters like Minikube or Docker for Desktop, the push is skipped because the image is already available locally.

3. Deploy

The deploy step ensures that the most recent artifacts are running in the cluster. You can use different tools for deployment, for example kubectl or helm. Each deployment type has parameters that allow you to define how you want your app to be installed and updated.

You have to run skaffold dev command to initiate skaffold deployment on your local Kubernetes environment.

$ skaffold dev

You should receive an output like below:

$  skaffold dev          
Starting build...
Found [minikube] context, using local docker daemon.
Sending build context to Docker daemon 8.704kBStep 1/4 : FROM nginx ---> c5c4e8fa2cf7
Step 2/4 : COPY index.html /usr/share/nginx/html/index.html
---> Using cache
---> b2743e4ff68a
Step 3/4 : WORKDIR /usr/share/nginx/html
---> Using cache
---> 4ee8631f078b
Step 4/4 : EXPOSE 80
---> Using cache
---> 51a516564d41
Successfully built 51a516564d41
Successfully tagged 87de02f02ad14494edd89e60779062fd:latest
Successfully tagged nginx-skaffold:51a516564d41fc9a39d714abcba1464ee301c5feb9de6ab858bc047a74ad3a79
Build complete in 231.654336ms
Starting deploy...
Deploying k8s-deployment.yaml...
Deploying k8s-service.yaml...
Deploy complete in 685.787798ms
Watching for changes...

To access your running service, you need to run:

$ minikube service [service-name]

In our example, this should be:

$ minikube service hello-world

This opens nginx page on your default browser and displays the content that was added to index.html file.

Whenever you make any changes to the index.html file, Skaffold should update your deployed application continually:

$ cat index.html

Hello world - Test Page 2

Upon saving the changes, you should see Skaffold doing redeployment automatically

Starting build...
Found [minikube] context, using local docker daemon.
Building [nginx-skaffold]...
Sending build context to Docker daemon 3.072kB
Step 1/3 : FROM nginx
---> 06144b287844
Step 2/3 : COPY index.html /usr/share/nginx/html/
---> fd751a98ac09
Step 3/3 : WORKDIR /usr/share/nginx/html
---> 0a4009d6de20
Successfully built 0a4009d6de20
Successfully tagged f15dd87578a0fb660a2e66cc3da0cf96:latest
WARN[1350] Using digest instead of git commit: Running [git rev-parse --short HEAD]: stdout , stderr: fatal: not a git repository (or any of the parent directories): .git
, err: exit status 128: exit status 128
Build complete in 1.012115486s
Starting deploy...
deployment.apps/hello-world configured
service/hello-world unchanged
Deploy complete in 1.073736335s
Watching for changes...

Reload the webpage to see new changes


Conclusion

Skaffold is a command line tool designed to facilitate continuous development of Kubernetes applications. By using Skaffold, it is easy to iterate on your application source code locally and later deploy to a remote or local Kubernetes clusters.

Leverage this tool to improve your productivity by ensuring you have quick automated deployments of Kubernetes applications. You can read more about Skaffold on its website or Github repository