Saturday, December 4, 2021

TLDR Intro to Skaffold

Introduction

Skaffold is a tool aiding developers with the build and deployment lifecycle of k8s applications. The core idea is to add a skaffold.yaml file defining the various pipeline stages, such as build, test, deploy, portForward.  The following image shows the skaffold stages:


Specifically, the commands:

  • init generates a skaffold.yaml file after detecting Docker, Jib and Buildpacks builders;
  • file sync - copies changed files to an already deployed containers to avoid the need to rebuild, redeploy and restart the corresponding pod; sync can be: i) manual (with src and dest fields defined a priori), ii) infer (with dest inferred by the builder, e.g. from docker add commands) and iii) auto (both src and dest configured for all files).
  • build wraps specific build tools to unify and ease local, in-cluster (e.g. using kaniko) and remote builds, as well as the management of target images on registries; As shown in this example dependencies between artifacts can also be specified, so that a base image is firstly built and then inherited by another artifact:

    apiVersion: skaffold/v2beta26
    kind: Config
    build:
      artifacts:
      - image: app
        context: app
        requires:
        - image: base
          alias: BASE
      - image: base
        context: base
    deploy:
      kubectl:
        manifests:
        - app/k8s-pod.yaml

    Specifically, the base image builds the Dockerfile in the base folder (as specified by the context), while the app folder contains and defines the Dockerfile for the app one. This also allows the app image to copy artifacts as in a docker multistage build.
  • test to perform checks after building and before the deploy stage
  • tag to define how built images should be tagged, such as using a digest of source files (inputDigest), a gitCommit, a datetime, or a formatting of environment variables (envTemplate)

    build:
      tagPolicy:
        envTemplate:
          template: "{{.FOO}}"
      artifacts:
      - image: gcr.io/k8s-skaffold/example

    using the variable FOO;

  • deploy 
    • performs a render step to generate actual K8s manifests, Helm templates or Kustomize overlays
    • deploys the rendered manifests to the cluster
    • waits until deployed resources stabilize, as specified by healthchecks
Beside those basic building blocks there exist shortcuts to trigger end-to-end pipelines:
  • run - to build and deploy the workflow defined in the skaffold.yaml only once (as a CI/CD tool)
  • dev - initially behaving like run for the end-to-end build and deployment runs, but with skaffold watching the source files for changes, so that they can be re-built, pushed, test and re-deployed to the target cluster upon any changes; 
  • debug - behaves like skaffold dev but also configures containers for debugging by exposing debugging ports that can be port-forwarded to the local machine and debugged from an IDE.

See the full skaffold specification reference here.


---
Cheers!


References

  • https://skaffold.dev/
  • https://github.com/GoogleContainerTools/skaffold/tree/main/examples

No comments:

Post a Comment