Skaffold: K8s Development Made Easy
Skaffold: K8s Development Made Easy
In this post, look at Skaffold for automatic building and deploying our application to a Kubernetes cluster during development.
Join the DZone community and get the full member experience.Join For Free
Skaffold is a command line tool developed by Google, which aims to facilitate continuous development for Kubernetes applications. It will automate the task of building and deploying to a Kubernetes cluster whereas you, as a developer, can stay focused on writing code. It seems interesting enough to take a closer look at it!
- It detects source changes while you are developing
- It automatically builds and creates your artifacts (your Docker image) based on a Dockerfile or Jib
- It tags the artifacts
- It pushes and deploys the artifacts to your Kubernetes cluster
In order to get acquainted with Skaffold, we will run a local Kubernetes cluster with the help of minikube and we will deploy by means of kubectl, the command line interface tool for Kubernetes.
You might also like: The Complete Kubernetes Collection [Tutorials and Tools]
Before getting started, we need to install minikube, kubectl, and Skaffold if you have not done so already. We will be using Ubuntu 18.04.
Installation of minikube (version 1.6.2) is quite easy when working with Linux. If you are working with Windows, please take a look at one of our previous posts, it was quite of a struggle way then, but maybe things have improved in the meanwhile.
First, check whether our system has virtualization support enabled:
The output of the command is yes, which means we do not need to execute any extra steps.
Download and install minikube:
Installation instructions for kubectl can be found here. For Linux, we have to execute the steps below, with
kubectl version we verify the successful installation:
Installation instructions for Skaffold can be found here. The instructions are very similar to the one for kubectl.
Create Demo Application
We will create a simple Spring Boot demo application. We use Spring MVC and create one REST endpoint, which will return a greeting message.
Our pom contains the following dependencies and plugins:
HelloController contains one method, which returns the greeting message and the address of the host where it is executed.
Now that we have finished all the necessary preparations, it is time to start using Skaffold. Currently, we deliberately have left out some important configuration, which Skaffold will need, but this will allow us to verify which error messages are returned and how to solve them.
Skaffold will need a
skaffold.yaml file that contains the development workflow you want to use. The file can be generated when the
init command is executed from within your project directory.
Skaffold init does not create the Kubernetes manifest files for us, we will need to create them manually.
We will create a k8s deployment file with the help of kubectl. We copy the output of the command to file
deployment.yaml in the
k8s directory. The CLI parameter
--dry-run ensures us that the deployment is not executed yet, the parameter
-oyaml will output the configuration, which will allow us to just copy the contents.
skaffold init again, returns a new error:
We could have expected this, because we did not provide a Dockerfile or Jib configuration. We will make use of Jib after our positive experiences with it in our previous post. Add the Jib Maven Plugin to our
pom. We do not provide credentials this time, because we are not going to push the Docker image to a Docker registry.
In order to use Jib, we need to add the flag
--XXenableJibInit, see also this issue.
Skaffold Continuous Development
We have set up all necessary configuration in order to experiment with the
skaffold dev command. This will scan our project for any changes and automatically build and deploy them to our Kubernetes cluster. Run the following command:
Our application is being built and deployed to our Kubernetes cluster. We can also verify this with the minikube dashboard:
We cannot invoke our URL yet because we did not create a service yet. We will map port 8080 via a
NodePort. Generate the service yaml and add the contents (without the labels) to file
service.yaml in the
Also, add the
service.yaml as manifest file to the
Skaffold immediately notices this change and creates the service. This can be verified in the Skaffold console output:
Verify the creation of the service with kubectl:
NodePort that has been assigned is port 30272 (see PORT(S) column). We are now able to invoke our Rest endpoint:
Change the greeting text in the
Again, the change is automatically detected by Skaffold, and in the background, our application is being built and deployed. Invoke the URL again:
We can also use command
skaffold run in order to deploy on request:
Again, check the
NodePort the service is running and invoke the URL.
We once encountered the following error when running
skaffold dev and
We solved this by adding the following lines to the
skaffold.yaml file. Later on, it seemed not to be necessary anymore.
In this post, we looked at Skaffold for automatic building and deploying our application to a Kubernetes cluster during development. We only scratched the surface of what Skaffold is capable of, but we were very impressed by this tool. Definitely something to use and to keep an eye on.
Published at DZone with permission of Gunter Rotsaert , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.