What it is
Skaffold is a command-line tool that facilitates continuous development for Kubernetes applications. It automates the build, push, and deploy workflow, allowing you to iterate rapidly on your code and see changes reflected in your cluster almost instantly.
Installation
Linux
curl -Lo skaffold https://storage.googleapis.com/skaffold/releases/latest/skaffold-linux-amd64 \
&& chmod +x skaffold \
&& sudo mv skaffold /usr/local/bin/
macOS
curl -Lo skaffold https://storage.googleapis.com/skaffold/releases/latest/skaffold-darwin-amd64 \
&& chmod +x skaffold \
&& sudo mv skaffold /usr/local/bin/
Windows (PowerShell)
Invoke-WebRequest -OutFile skaffold.exe https://storage.googleapis.com/skaffold/releases/latest/skaffold-windows-amd64.exe
# Move skaffold.exe to your PATH, e.g., C:\Program Files\skaffold
Core Concepts
- Skaffold Configuration (
skaffold.yaml): This is the central configuration file where you define your application’s build, test, and deploy steps, as well as Skaffold’s behavior. - Build: Skaffold can build container images using various tools like Docker, Jib, Bazel, or buildpacks.
- Test: You can define tests that run after your application is built and deployed.
- Deploy: Skaffold deploys your application to Kubernetes using tools like
kubectl, Helm, or Kustomize. - Development Loop: Skaffold watches your source files for changes. Upon detecting a change, it rebuilds your image, redeploys your application, and can even forward ports for live debugging.
Commands / Usage
Starting the Development Loop
-
skaffold dev: Starts the Skaffold development loop. This command builds, deploys, and then continuously watches for changes.skaffold devExplanation: This is the primary command for live development. Skaffold will build your application, deploy it to Kubernetes, and then monitor your source files for changes, automatically rebuilding and redeploying upon detection.
-
skaffold dev --port-forward: Starts the development loop and forwards specified ports from your deployed application to your local machine.skaffold dev --port-forwardExplanation: Useful for accessing services running inside your Kubernetes cluster locally, like a web server or database.
-
skaffold dev --watch-poll: Starts the development loop using polling instead of filesystem events for detecting changes.skaffold dev --watch-pollExplanation: Fallback mechanism if filesystem watching is unreliable in your environment.
-
skaffold dev --build-concurrency 4: Sets the number of parallel build jobs.skaffold dev --build-concurrency 4Explanation: Speeds up builds by running multiple build operations simultaneously if your
skaffold.yamldefines multiple deployable artifacts. -
skaffold dev --build-artifact <artifact-name>: Only build and deploy the specified artifact.skaffold dev --build-artifact my-apiExplanation: Allows you to focus on a specific part of your application during development.
-
skaffold dev --sync: Enables file sync for faster updates without rebuilding the entire image.skaffold dev --syncExplanation: For languages that support it (e.g., Go, Python), Skaffold can copy changed files directly into the running container, significantly speeding up hot-reloading.
Building and Deploying
-
skaffold build: Builds your application’s container images without deploying them.skaffold buildExplanation: Useful for CI/CD pipelines to just build images and push them to a registry.
-
skaffold build -t my-app:latest: Builds your application and tags the resulting image asmy-app:latest.skaffold build -t my-app:v1.0Explanation: Allows explicit image tagging.
-
skaffold build --file-path ./Dockerfile: Specifies a custom Dockerfile path for building.skaffold build --file-path ./backend/DockerfileExplanation: When you have multiple Dockerfiles or non-standard naming.
-
skaffold deploy: Deploys your application to Kubernetes using the configured deployer.skaffold deployExplanation: Deploys the application based on the
skaffold.yamlconfiguration. -
skaffold run: Builds and deploys your application. This is a one-time operation, not a continuous loop.skaffold runExplanation: Equivalent to running
skaffold buildfollowed byskaffold deploy. Good for initial deployments or in CI/CD.
Debugging and Port Forwarding
-
skaffold debug: Starts the development loop, builds, deploys, and attaches a debugger to your application.skaffold debugExplanation: Sets up the necessary port forwarding and debugger configurations for live debugging.
-
skaffold debug --debugger-port 5005: Attaches the debugger to a specific port.skaffold debug --debugger-port 5005Explanation: Useful when your application’s debugger listens on a non-default port.
-
skaffold port-forward: Starts port forwarding for a running application without initiating a full development loop.skaffold port-forwardExplanation: Manually forwards ports defined in your
skaffold.yamlor specified via flags.
Application Management
-
skaffold delete: Deletes the deployed resources from your Kubernetes cluster.skaffold deleteExplanation: Cleans up the Kubernetes resources managed by Skaffold.
-
skaffold apply: Builds and deploys your application, similar toskaffold run, but also handles updates and deletions.skaffold applyExplanation: A more idempotent command that ensures the cluster state matches the Skaffold configuration.
-
skaffold render: Renders the deploy manifests (e.g.,kubectlmanifests, Helm charts) without deploying them.skaffold renderExplanation: Useful for inspecting the generated Kubernetes manifests before they are applied.
Configuration and Status
-
skaffold config list: Displays the current Skaffold configuration.skaffold config listExplanation: Shows global configuration settings like default Kubernetes context or registry.
-
skaffold config set <key> <value>: Sets a Skaffold configuration value.skaffold config set default-repo my-docker-registry.com/my-projectExplanation: Configures Skaffold’s global behavior.
-
skaffold status: Reports the current status of your Skaffold deployment.skaffold statusExplanation: Checks if Skaffold is running, what’s deployed, and if any errors occurred.
Workspace Management
-
skaffold init: Initializes askaffold.yamlfile for your project.skaffold initExplanation: Scans your project for common patterns (e.g., Dockerfiles, Helm charts) and generates a basic
skaffold.yaml. -
skaffold init --enable-build --enable-deploy: Initializes Skaffold with specific features enabled.skaffold init --enable-build=docker --enable-deploy=kubectlExplanation: Guides you through creating a
skaffold.yamlby asking questions about your project setup. -
skaffold fix: Attempts to automatically fix common issues in yourskaffold.yaml.skaffold fixExplanation: Helps resolve syntax errors or misconfigurations in your Skaffold configuration file.
Common Patterns
-
Hot Reloading Python/Node.js Apps with File Sync:
skaffold dev --syncExplanation: Skaffold will copy changed files directly into the running container, enabling very fast iteration for interpreted languages.
-
Building and Pushing Images for CI:
skaffold build --pushExplanation: Builds all images defined in
skaffold.yamland pushes them to the configured registry. The--pushflag is often implicit when runningskaffold buildin a CI environment configured with a default registry. -
Deploying a Helm Chart: Ensure your
skaffold.yamlis configured for Helm:deploy: helm: releases: - name: my-app chartPath: ./charts/my-appThen run:
skaffold devExplanation: Skaffold will build your image, tag it, update the Helm chart with the new image tag, and then deploy the chart.
-
Deploying with
kubectland Overlays: Ensure yourskaffold.yamlis configured forkubectlwith Kustomize:deploy: kubectl: manifests: - k8s/deployment.yaml - k8s/service.yaml kustomize: paths: - kustomize/overlays/devThen run:
skaffold devExplanation: Skaffold builds your image, tags it, patches the Kubernetes manifests with the new image tag (or uses Kustomize overlays), and applies them.
-
Debugging a Java Application: Configure your
skaffold.yamlfor debugging:deploy: kubectl: manifests: - k8s/deployment.yaml profiles: - name: debug activation: - kubeContext: docker-desktop # or your context build: artifacts: - image: my-java-app docker: dockerfile: Dockerfile.debug deploy: kubectl: # Add debug args to your deployment manifest or use a separate debug manifest # Example: Add JVM debug args to deployment specThen run:
skaffold dev -p debugExplanation: Skaffold will build using
Dockerfile.debug, deploy, and ensure the necessary debugging ports are forwarded and accessible for your IDE. -
Forwarding Multiple Ports:
skaffold dev --port-forward=8080:80,5432:5432Explanation: Manually specifies which local ports should be forwarded to which container ports.
Gotchas
- Image Tagging: By default, Skaffold tags images with a unique digest. If you need predictable tags (e.g.,
latestor a specific version) for your deploy step, you must configure this inskaffold.yamlunder thebuild.artifacts.imageorbuild.tagPolicy. skaffold devvsskaffold run:devis for continuous development with automatic rebuilding/redeploying.runis a one-time build and deploy.- Default Repository: If you don’t specify a default repository in
skaffold.yamlor viaskaffold config set default-repo, Skaffold might try to push to Docker Hub using your username, or fail if no default is set and no registry is specified in the image name. - File Sync Limitations: File sync only works for certain languages and requires specific configurations. It doesn’t rebuild the image, only copies files into the running container.
- Kubernetes Context: Skaffold uses your current
kubectlcontext by default. Ensure you are targeting the correct cluster. You can override this with the--kube-contextflag or by configuring it inskaffold.yaml. skaffold initDefaults: Theskaffold initcommand makes assumptions. Always review and adjust the generatedskaffold.yamlto fit your project’s specific needs.- Build Artifacts: If you have multiple artifacts defined in
skaffold.yaml,skaffold devwill build and deploy all of them by default. Use--build-artifactor--deploy-artifactto target specific ones. - Remote Build Issues: When building remotely (e.g., Kaniko, Jib), ensure the necessary permissions and configurations are in place for Skaffold to communicate with the builder and the registry.