- {{node.title}}
{{node.type}} · {{ node.urlSource.name }} by {{node.authors[0].realName }}
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.
Mar. 10, 20 · Cloud Zone ·
Comment (0)
Join the DZone community and get the full member experience.
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!
Introduction
In November 2019 a generally available release was announced promising to save developers time by automating the development workflow. What will Skaffold do for us?
- 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.
It is advised to take a look at the official Skaffold documentation and examples for more in-depth information. The sources we are using in this post are available at GitHub.
You might also like: The Complete Kubernetes Collection [Tutorials and Tools]
Prerequisites
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.
Install Minikube
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:
xxxxxxxxxx
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
yes
The output of the command is yes, which means we do not need to execute any extra steps.
Download and install minikube:
xxxxxxxxxx
$ curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube_1.6.2.deb && sudo dpkg -i minikube_1.6.2.deb
Start minikube:
xxxxxxxxxx
$ minikube start
minikube v1.6.2 on Ubuntu 18.04
Automatically selected the 'virtualbox' driver (alternates: [none])
Downloading VM boot image ...
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
0
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
1
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
2
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
3
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
4
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
5
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
6
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
7
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
8
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
9
Install Kubectl
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:
yes
0
yes
1
yes
2
yes
3
yes
4
yes
5
yes
6
Install Skaffold
Installation instructions for Skaffold can be found here. The instructions are very similar to the one for kubectl.
yes
7
yes
8
yes
9
xxxxxxxxxx
0
xxxxxxxxxx
1
xxxxxxxxxx
2
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:
xxxxxxxxxx
3
xxxxxxxxxx
4
xxxxxxxxxx
5
xxxxxxxxxx
6
xxxxxxxxxx
7
xxxxxxxxxx
8
xxxxxxxxxx
9
$ curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube_1.6.2.deb && sudo dpkg -i minikube_1.6.2.deb
0
$ curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube_1.6.2.deb && sudo dpkg -i minikube_1.6.2.deb
1
$ curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube_1.6.2.deb && sudo dpkg -i minikube_1.6.2.deb
2
$ curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube_1.6.2.deb && sudo dpkg -i minikube_1.6.2.deb
3
$ curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube_1.6.2.deb && sudo dpkg -i minikube_1.6.2.deb
4
$ curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube_1.6.2.deb && sudo dpkg -i minikube_1.6.2.deb
5
$ curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube_1.6.2.deb && sudo dpkg -i minikube_1.6.2.deb
6
$ curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube_1.6.2.deb && sudo dpkg -i minikube_1.6.2.deb
7
$ curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube_1.6.2.deb && sudo dpkg -i minikube_1.6.2.deb
8
$ curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube_1.6.2.deb && sudo dpkg -i minikube_1.6.2.deb
9
xxxxxxxxxx
0
xxxxxxxxxx
1
xxxxxxxxxx
2
xxxxxxxxxx
3
xxxxxxxxxx
4
xxxxxxxxxx
5
xxxxxxxxxx
6
xxxxxxxxxx
7
The HelloController
contains one method, which returns the greeting message and the address of the host where it is executed.
xxxxxxxxxx
8
xxxxxxxxxx
9
$ minikube start
0
$ minikube start
1
$ minikube start
2
$ minikube start
3
$ minikube start
4
$ minikube start
5
$ minikube start
6
$ minikube start
7
$ minikube start
8
$ minikube start
9
minikube v1.6.2 on Ubuntu 18.04
0
minikube v1.6.2 on Ubuntu 18.04
1
minikube v1.6.2 on Ubuntu 18.04
2
minikube v1.6.2 on Ubuntu 18.04
3
minikube v1.6.2 on Ubuntu 18.04
4
Use Skaffold
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.
Generate skaffold.yaml
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.
minikube v1.6.2 on Ubuntu 18.04
5
minikube v1.6.2 on Ubuntu 18.04
6
minikube v1.6.2 on Ubuntu 18.04
7
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.
minikube v1.6.2 on Ubuntu 18.04
8
minikube v1.6.2 on Ubuntu 18.04
9
Automatically selected the 'virtualbox' driver (alternates: [none])
0
Automatically selected the 'virtualbox' driver (alternates: [none])
1
Automatically selected the 'virtualbox' driver (alternates: [none])
2
Automatically selected the 'virtualbox' driver (alternates: [none])
3
Automatically selected the 'virtualbox' driver (alternates: [none])
4
Automatically selected the 'virtualbox' driver (alternates: [none])
5
Automatically selected the 'virtualbox' driver (alternates: [none])
6
Automatically selected the 'virtualbox' driver (alternates: [none])
7
Automatically selected the 'virtualbox' driver (alternates: [none])
8
Automatically selected the 'virtualbox' driver (alternates: [none])
9
Downloading VM boot image ...
0
Downloading VM boot image ...
1
Downloading VM boot image ...
2
Downloading VM boot image ...
3
Downloading VM boot image ...
4
Downloading VM boot image ...
5
Downloading VM boot image ...
6
Downloading VM boot image ...
7
Downloading VM boot image ...
8
Downloading VM boot image ...
9
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
00
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
01
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
02
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
03
Running skaffold init
again, returns a new error:
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
04
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
05
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
06
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.
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
07
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
08
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
09
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
10
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
11
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
12
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
13
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
14
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
15
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
16
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
17
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
18
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
19
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
20
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
21
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
22
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
23
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
24
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
25
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
26
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
27
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
28
In order to use Jib, we need to add the flag --XXenableJibInit
, see also this issue.
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
29
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
30
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
31
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
32
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
33
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
34
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
35
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
36
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
37
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
38
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
39
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
40
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
41
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
42
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
43
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
44
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
45
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
46
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
47
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
48
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:
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
49
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
50
Our application is being built and deployed to our Kubernetes cluster. We can also verify this with the minikube dashboard:
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
51
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
52
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 k8s
directory:
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
53
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
54
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
55
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
56
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
57
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
58
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
59
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
60
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
61
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
62
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
63
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
64
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
65
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
66
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
67
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
68
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
69
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
70
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
71
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
72
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
73
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
74
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
75
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
76
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
77
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
78
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
79
Also, add the service.yaml
as manifest file to the skaffold.yaml
file:
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
80
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
81
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
82
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
83
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
84
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
85
Skaffold immediately notices this change and creates the service. This can be verified in the Skaffold console output:
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
86
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
87
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
88
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
89
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
90
Verify the creation of the service with kubectl:
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
91
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
92
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
93
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
94
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
95
The NodePort
that has been assigned is port 30272 (see PORT(S) column). We are now able to invoke our Rest endpoint:
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
96
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
97
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
98
Change the greeting text in the HelloController
:
$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no
99
yes
00
Again, the change is automatically detected by Skaffold, and in the background, our application is being built and deployed. Invoke the URL again:
yes
01
yes
02
yes
03
We can also use command skaffold run
in order to deploy on request:
yes
04
yes
05
yes
06
yes
07
yes
08
yes
09
yes
10
Again, check the NodePort
the service is running and invoke the URL.
Troubleshooting
We once encountered the following error when running skaffold dev
and skaffold run
:
yes
11
yes
12
We solved this by adding the following lines to the skaffold.yaml
file. Later on, it seemed not to be necessary anymore.
yes
13
yes
14
yes
15
yes
16
yes
17
yes
18
yes
19
Conclusion
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.
Further Reading
Like This Article? Read More From DZone
Comment (0)
Published at DZone with permission of Gunter Rotsaert , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Cloud Partner Resources
- {{ node.blurb }}
{{ editionName }}
{{ parent.title || parent.header.title}}
{{ parent.tldr }}
{{ parent.linkDescription }}
{{ message }}
{{ $dialog.title }}