Hot Reload Your Infrastructure As Code with Skaffold


If the Kubernetes bug has bitten you, there are usually some pain points that won’t go away. Setting up a functional development environment that is as close to production as possible may take some effort. Skaffold makes it somewhat effortless.

Skaffold

So why would you want to hot-reload your infrastructure? As tools for developing software have become much better over the years, it’s all about shortening those feedback loops until you can see the changes your code has done and reflect upon them. If feedback time goes down, so does your development time. And it’s about time we did the same for our infrastructure.

Photo by Xiong Yan on Unsplash

Continuous integration and continuous delivery/deployment have been making a lot of headroom the last few years and have enabled companies to ship code with shorter development cycles and pushing features to production faster. But that also assumes that you already have your development/staging and production environments already set up.

Sometimes your code depends on quite a lot of infrastructure. For developers to manage that, it quickly becomes a pain to get everything to work properly.

Docker Compose definitions are sometimes enough for the development cycle. Still, when it is more advanced setups, docker-compose quickly becomes hard to maintain. If you’re not using Docker Swarm in production changes, your production configuration differs significantly from your local development environment.

Kubernetes has become the de-facto standard for container orchestration and is offered by all major cloud providers, making it a perfect choice for companies and enterprises to define their deployments on Kubernetes and have the ability to reproduce their cluster set up anywhere and any time.

So Skaffold can use these definitions to empower your development experience and get a more fluid integration when running code on Kubernetes.

Setting up Skaffold

We will start with the GitOps project we created in the previous blog post as a starting point.

How to Set Up GitOps for a Full-Stack App with Flux & Helm on Kubernetes

You could use any Kubernetes definition or, in our case, a helm chart to set up a deployment. You might want to use Minikube, Microk8s, Docker Kubernetes, or some other local Kubernetes installation, but you can use any Kubernetes cluster.

If you’re running a Mac, you can install Skaffold with homebrew. In any other case, you can find the installation instructions here.

brew install skaffold

Since Skaffold does not support the automatic setup for helm charts, we will have to set it up manually. Your finished file skaffold.yaml should look something like this.

apiVersion: skaffold/v2beta13
kind: Config
build:
local: {}
artifacts:
- image: xxxxxxxxxx.dkr.ecr.us-east-1.amazonaws.com/flexcapacitor-ui
context: src/fluxcapacitor-ui
- image: xxxxxxxxxx.dkr.ecr.us-east-1.amazonaws.com/flexcapacitor-db
context: src/fluxcapacitor-db
- image: xxxxxxxxxx.dkr.ecr.us-east-1.amazonaws.com/flexcapacitor-api
context: src/fluxcapacitor-api

deploy:
helm:
releases:
- name: dev-release
namespace: dev-release-fluxcapacitor
createNamespace: true
chartPath: charts/fluxcapacitor
artifactOverrides:
api.image: xxxxxxxxxx.dkr.ecr.us-east-1.amazonaws.com/flexcapacitor-api
db.image: xxxxxxxxxx.dkr.ecr.us-east-1.amazonaws.com/flexcapacitor-db
ui.image: xxxxxxxxxx.dkr.ecr.us-east-1.amazonaws.com/flexcapacitor-ui

portForward:
- resourceType: service
resourceName: api
namespace: dev-release-fluxcapacitor
port: 4001
localPort: 4001
- resourceType: service
resourceName: neo4j
namespace: dev-release-fluxcapacitor
port: 7687
localPort: 7687
- resourceType: service
resourceName: ui
namespace: dev-release-fluxcapacitor
port: 3000
localPort: 3000

And then you will be able to run.

skaffold dev --port-forward

That will deploy your stack in a separate namespace and proxy the services back to your local machine. At the same time, it watches for changes to your application, then builds and deploys a new revision as soon as you make code changes — perfect for developing an application on a more complicated architecture, because you can make changes to the infrastructure to fit your use case.

It’s as simple as that!

Check out my other blog post on getting started with GitOps, another tool for infrastructure development. Or check out the DockerCon ’21 session where we put this to use.


Hot Reload Your Infrastructure As Code with Skaffold was originally published in Neo4j Developer Blog on Medium, where people are continuing the conversation by highlighting and responding to this story.