How to Use the Command 'Skaffold' (with Examples)
Skaffold is a command-line tool that facilitates continuous development for Kubernetes applications. It helps streamline the workflow for developers by automating various parts of the development cycle including building, deploying, and testing applications within a Kubernetes cluster. Skaffold monitors code changes and reflects them in real-time during application development, simplifying the iterative development process. It is especially useful in cloud-native development environments where Kubernetes plays a pivotal role. Below, we explore different use cases of the ‘skaffold’ command with examples to better understand its practical applications.
Use Case 1: Build the Artifacts
Code:
skaffold build -f skaffold.yaml
Motivation:
Building artifacts is one of the foundational steps in the development lifecycle of Kubernetes applications. The ‘skaffold build’ command is used when you want to compile your code and create deployable artifacts, such as container images, without deploying them to a Kubernetes cluster immediately. This is particularly useful in scenarios where you want to verify that your build process works correctly before integrating other stages like testing or deployment.
Explanation:
skaffold
: This is the command-line tool being used.build
: This sub-command triggers the process of building your application artifacts.-f skaffold.yaml
: This flag specifies the configuration file (‘skaffold.yaml’) that outlines how Skaffold should build the application. The file contains directives for building images, which builders to use, and additional build-time options.
Example Output:
Generating tags...
- my-app -> my-app:v1.0.0-123-gabc1234
Checking cache...
- my-app: Found Locally
Tags used in deployment:
- my-app -> my-app:v1.0.0-123-gabc1234
Use Case 2: Build and Deploy Your App Every Time Your Code Changes
Code:
skaffold dev -f skaffold.yaml
Motivation:
The ‘skaffold dev’ command is invaluable for developers who want a continuous feedback loop during the development process. It automatically watches for changes in your codebase and rebuilds and redeploys your application in the Kubernetes cluster. This continuous development mode is essential for rapid iteration, as it saves time and ensures that you are always testing the latest version of your code.
Explanation:
skaffold
: The name of the tool being executed.dev
: This sub-command initiates a development workflow that watches for file changes and redeploys necessary components in the cluster.-f skaffold.yaml
: Specifies the Skaffold configuration file that details how applications should be built, tested, and deployed.
Example Output:
Starting deploy...
- deployment.apps/my-app created
Watching for changes...
- Files changed: main.go
Rebuilding my-app...
Tags used in deployment:
- my-app -> my-app:v1.0.1-dirty
Completed in 8.356s
Use Case 3: Run a Pipeline File
Code:
skaffold run -f skaffold.yaml
Motivation:
Using the ‘skaffold run’ command is advantageous when you want to execute a predefined pipeline that typically includes building artifacts, deploying them, and perhaps running tests. Unlike continuous development mode, this use case might be more suited to CI/CD pipelines or environments where you want an orchestrated execution of build and deploy steps without the code watch feature.
Explanation:
skaffold
: Initiates the command-line tool.run
: This sub-command starts the continuous delivery pipeline configured in your Skaffold file.-f skaffold.yaml
: The configuration file specifying the pipeline steps including the build and deploy processes.
Example Output:
Building [my-app]...
Sending build context to Docker daemon 34.0MB
Step 1/10 : FROM golang:1.16-alpine as builder
...
Successfully tagged my-app:v1.0.2
Deploying application...
- service/my-app created
- deployment.apps/my-app updated
Use Case 4: Run a Diagnostic on Skaffold
Code:
skaffold diagnose -f skaffold.yaml
Motivation:
Diagnosing your Skaffold setup can be critical to discovering why certain operations might not work as expected. The ‘skaffold diagnose’ command provides insights into the configuration and current state of resources managed by Skaffold. It is particularly useful for debugging and troubleshooting by outlining what Skaffold attempts to do based on the provided configuration.
Explanation:
skaffold
: The command-line tool.diagnose
: This sub-command outputs a diagnostic report of your Skaffold environment, including configurations and operations that Skaffold would perform.-f skaffold.yaml
: Indicates the Skaffold configuration file whose setup you want to diagnose.
Example Output:
apiVersion: skaffold/v2beta12
kind: Config
metadata:
name: my-app
build:
artifacts:
- image: my-app
...
Use Case 5: Deploy the Artifacts
Code:
skaffold deploy -f skaffold.yaml
Motivation:
Manually deploying the artifacts to a Kubernetes cluster can be important when you want explicit control over when the deployment occurs, separate from the build process. This step is useful in production environments where builds are performed in a different context or time from deployments. Additionally, it facilitates scenarios like rolling out deployments after getting relevant approvals.
Explanation:
skaffold
: The CLI tool used.deploy
: This sub-command kicks off the deployment of pre-built artifacts to a Kubernetes cluster.-f skaffold.yaml
: Denotes the configuration file Skaffold uses to determine deployment specifications.
Example Output:
Starting deploy...
- deployment.apps/my-app created
Waiting for deployments to stabilize...
- deployment/my-app is ready.
Deployments stabilized in 10.563s
Conclusion:
Skaffold is a versatile tool that streamlines the development lifecycle in Kubernetes environments through an array of powerful commands. Each command serves a unique role in aiding developers—from building and deploying applications to running diagnostics. The practical examples outlined above illustrate how to efficiently leverage Skaffold’s capabilities, leading to a more productive and efficient development workflow.