Skaffold: K8s Development Made Easy – DZone Cloud | xxxSkaffold: K8s Development Made Easy – DZone Cloud – xxx
菜单

Skaffold: K8s Development Made Easy – DZone Cloud

一月 31, 2020 - MorningStar

Over a million developers have joined DZone.

{{announcement.body}}

{{announcement.title}}

Let’s be friends:

1024)” dc-slot=”ads.sl1.slot(articles[0], 0)” tags=”ads.sl1.tags(articles[0], 0)” size=”ads.sl1.size(articles[0], 0)” style=”border:0px;”>
1 && !articles[0].partner.isSponsoringArticle && (width > 1024)” dc-slot=”ads.sb2.slot(articles[0], 0)” tags=”ads.sb2.tags(articles[0], 0)” size=”ads.sb2.size(articles[0], 0)”>

Skaffold: K8s Development Made Easy

DZone ‘s Guide to

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 ·

Free Resource

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!

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?

  1. It detects source changes while you are developing
  2. It automatically builds and creates your artifacts (your Docker image) based on a Dockerfile or Jib
  3. It tags the artifacts
  4. 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:

Shell

 

xxxxxxxxxx
1

 

1

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

2

yes

The output of the command is yes, which means we do not need to execute any extra steps.

Download and install minikube:

Shell

 

xxxxxxxxxx
1

 

1

$ curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube_1.6.2.deb && sudo dpkg -i minikube_1.6.2.deb

Start minikube:

Shell

 

xxxxxxxxxx
1

14

 

1

$ minikube start

2

minikube v1.6.2 on Ubuntu 18.04

3

Automatically selected the 'virtualbox' driver (alternates: [none])

4

Downloading VM boot image ...

5

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

0

6

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

1

7

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

2

8

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

3

9

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

4

10

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

5

11

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

6

12

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

7

13

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

8

14

$ 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:

Shell

 

yes

0

1

 

1

yes

1

2

yes

2

3

yes

3

4

yes

4

5

yes

5

6

yes

6

Install Skaffold

Installation instructions for Skaffold can be found here. The instructions are very similar to the one for kubectl.

Shell

 

yes

7

1

 

1

yes

8

2

yes

9

3

xxxxxxxxxx

0

4

xxxxxxxxxx

1

5

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:

XML

 

xxxxxxxxxx

3

1

24

 

1

xxxxxxxxxx

4

2

xxxxxxxxxx

5

3

xxxxxxxxxx

6

4

xxxxxxxxxx

7

5

xxxxxxxxxx

8

6

xxxxxxxxxx

9

7

$ curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube_1.6.2.deb && sudo dpkg -i minikube_1.6.2.deb

0

8

$ curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube_1.6.2.deb && sudo dpkg -i minikube_1.6.2.deb

1

9

$ curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube_1.6.2.deb && sudo dpkg -i minikube_1.6.2.deb

2

10

$ curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube_1.6.2.deb && sudo dpkg -i minikube_1.6.2.deb

3

11

$ curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube_1.6.2.deb && sudo dpkg -i minikube_1.6.2.deb

4

12

$ curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube_1.6.2.deb && sudo dpkg -i minikube_1.6.2.deb

5

13

$ curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube_1.6.2.deb && sudo dpkg -i minikube_1.6.2.deb

6

14

$ curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube_1.6.2.deb && sudo dpkg -i minikube_1.6.2.deb

7

15

$ curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube_1.6.2.deb && sudo dpkg -i minikube_1.6.2.deb

8

16

$ curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube_1.6.2.deb && sudo dpkg -i minikube_1.6.2.deb

9

17

xxxxxxxxxx

0

18

xxxxxxxxxx

1

19

xxxxxxxxxx

2

20

xxxxxxxxxx

3

21

xxxxxxxxxx

4

22

xxxxxxxxxx

5

23

xxxxxxxxxx

6

24

xxxxxxxxxx

7

The HelloController contains one method, which returns the greeting message and the address of the host where it is executed.

Java

 

xxxxxxxxxx

8

1

16

 

1

xxxxxxxxxx

9

2

$ minikube start

0

3

$ minikube start

1

4

$ minikube start

2

5

$ minikube start

3

6

$ minikube start

4

7

$ minikube start

5

8

$ minikube start

6

9

$ minikube start

7

10

$ minikube start

8

11

$ minikube start

9

12

minikube v1.6.2 on Ubuntu 18.04

0

13

minikube v1.6.2 on Ubuntu 18.04

1

14

minikube v1.6.2 on Ubuntu 18.04

2

15

minikube v1.6.2 on Ubuntu 18.04

3

16

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.

Shell

 

minikube v1.6.2 on Ubuntu 18.04

5

1

 

1

minikube v1.6.2 on Ubuntu 18.04

6

2

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.

Shell

 

minikube v1.6.2 on Ubuntu 18.04

8

1

25

 

1

minikube v1.6.2 on Ubuntu 18.04

9

2

Automatically selected the 'virtualbox' driver (alternates: [none])

0

3

Automatically selected the 'virtualbox' driver (alternates: [none])

1

4

Automatically selected the 'virtualbox' driver (alternates: [none])

2

5

Automatically selected the 'virtualbox' driver (alternates: [none])

3

6

Automatically selected the 'virtualbox' driver (alternates: [none])

4

7

Automatically selected the 'virtualbox' driver (alternates: [none])

5

8

Automatically selected the 'virtualbox' driver (alternates: [none])

6

9

Automatically selected the 'virtualbox' driver (alternates: [none])

7

10

Automatically selected the 'virtualbox' driver (alternates: [none])

8

11

Automatically selected the 'virtualbox' driver (alternates: [none])

9

12

Downloading VM boot image ...

0

13

Downloading VM boot image ...

1

14

Downloading VM boot image ...

2

15

Downloading VM boot image ...

3

16

Downloading VM boot image ...

4

17

Downloading VM boot image ...

5

18

Downloading VM boot image ...

6

19

Downloading VM boot image ...

7

20

Downloading VM boot image ...

8

21

Downloading VM boot image ...

9

22

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

00

23

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

01

24

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

02

25

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

03

Running skaffold init again, returns a new error:

Shell

 

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

04

1

 

1

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

05

2

$ 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.

XML

 

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

07

1

21

 

1

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

08

2

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

09

3

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

10

4

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

11

5

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

12

6

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

13

7

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

14

8

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

15

9

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

16

10

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

17

11

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

18

12

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

19

13

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

20

14

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

21

15

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

22

16

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

23

17

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

24

18

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

25

19

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

26

20

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

27

21

$ 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.

Shell

 

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

29

1

19

 

1

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

30

2

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

31

3

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

32

4

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

33

5

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

34

6

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

35

7

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

36

8

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

37

9

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

38

10

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

39

11

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

40

12

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

41

13

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

42

14

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

43

15

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

44

16

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

45

17

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

46

18

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

47

19

$ 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:

Shell

 

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

49

1

 

1

$ 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:

Shell

 

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

51

1

 

1

$ 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:

Shell

 

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

53

1

26

 

1

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

54

2

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

55

3

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

56

4

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

57

5

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

58

6

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

59

7

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

60

8

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

61

9

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

62

10

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

63

11

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

64

12

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

65

13

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

66

14

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

67

15

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

68

16

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

69

17

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

70

18

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

71

19

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

72

20

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

73

21

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

74

22

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

75

23

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

76

24

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

77

25

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

78

26

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

79

Also, add the service.yaml as manifest file to the skaffold.yaml file:

YAML

 

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

80

1

 

1

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

81

2

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

82

3

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

83

4

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

84

5

$ 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:

Shell

 

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

86

1

 

1

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

87

2

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

88

3

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

89

4

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

90

Verify the creation of the service with kubectl:

Shell

 

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

91

1

 

1

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

92

2

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

93

3

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

94

4

$ 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:

Shell

 

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

96

1

 

1

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

97

2

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

98

Change the greeting text in the HelloController:

Java

 

$ egrep -q 'vmx|svm' /proc/cpuinfo && echo yes || echo no 

99

1

 

1

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:

Shell

 

yes

01

1

 

1

yes

02

2

yes

03

We can also use command skaffold run in order to deploy on request:

Shell

 

yes

04

1

 

1

yes

05

2

yes

06

3

yes

07

4

yes

08

5

yes

09

6

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:

Shell

 

yes

11

1

 

1

yes

12

We solved this by adding the following lines to the skaffold.yaml file. Later on, it seemed not to be necessary anymore.

YAML

 

yes

13

1

 

1

yes

14

2

yes

15

3

yes

16

4

yes

17

5

yes

18

6

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

50+ Useful Kubernetes Tools

How to Implement Kubernetes

Topics:
devops ,skaffold ,cloud ,tutorial ,minikube ,kubernetes ,k8s

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

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.linkDescription }}

{{ parent.urlSource.name }}

· {{ parent.articleDate | date:’MMM. dd, yyyy’ }} {{ parent.linkDate | date:’MMM. dd, yyyy’ }}



Notice: Undefined variable: canUpdate in /var/www/html/wordpress/wp-content/plugins/wp-autopost-pro/wp-autopost-function.php on line 51