Dockerfile and the MyFirstApp-0.0.1-SNAPSHOT.jar should be under the same folder.

Create Dockerfile

Understanding the elements of a basic Dockerfile.

FROM specifies the base image on which container is built. In this case we use java:8 which uses Java container as both the build and runtime environment.

ADD element copies the standalone Jar to app.jar.

ENTRYPOINT specifies a command that will be executed when the container starts. In this case “java –jar app.jar”.

Note from Kubernetes:
When using a single VM of Kubernetes, it’s really handy to reuse the minikube’s built-in Docker daemon; as this means you don’t have to build a docker registry on your host machine and push the image into it - you can just build inside the same docker daemon as minikube which speeds up local experiments. Just make sure you tag your Docker image with something other than ‘latest’ and use that tag while you pull the image. Otherwise, if you do not specify version of your image, it will be assumed as :latest, with pull image policy of Always correspondingly, which may eventually result in ErrImagePull as you may not have any versions of your Docker image out there in the default docker registry (usually DockerHub) yet.

To start Kubernetes cluster use “minikube start”. This should start a local Kubernetes cluster on your laptop. You can see a minikube VM entry added in Oracle VM Virtual Box Manager.

Oracle VM Virtual Box - minikube instance

Once you start minikube the console would look like this. Now we are ready to deploy applications on kubernetes.

minikube start

Get the minikube IP using the command "minikube ip". We will run the containerized app on this IP.

minikube IP

Run “minikube docker-env” to get the environment of built-in docker daemon.

minikube docker-env

Run the last command displayed above to configure the shell for the docker environment.

Set the docker environment

Verify by running “docker ps” command. You should see several kubernetes related containers already running.

docker ps listing

Go to the folder where we copied the Spring Boot standalone Jar. We already have the Dockerfile here. Run the command "docker build –t myfirstapp:v1 ." to build the docker image. It is important to specify the version tag when working with Kubernetes environment. This command should pull the java libraries, add the Spring Boot application, set the entry point and build the container for us.

Build docker image

Verify if the container is listed by running the "docker images" command. We will see an entry for the myfirstapp container.

docker images listing

Now it is time to start our container in kubernetes. We will leverage kubectl to run this container.

kubectl run myfirstapp –image=myfirstapp:v1 –port=8080

kubectl run

Launch the minikube dashboard and verify if the application is created. Run the command “minikube dashboard” this should launch a browser and display the dashboard. You can see the app running in dashboard.

minikube dashboard launch

Kubernetes dashboard

Some useful commands. Use “kubectl get pods” to get the list of pods in the cluster. Use “kubectl get services” to get list of services running in the cluster.

kubectl get pods

kubectl get services

Next step is to expose this application for external access as a service. Use the command “kubectl expose deployment myfirstapp –type=NodePort”.

kubectl expose service

Run the “kubectl get services” command again to see if myfirstapp is listed.

kuebctl get services listing exposed service

Verify if the application works. Using the browser. Type the URL http://192.168.99.103:31608/sayhello to invoke the API. (or) Run the command “minkube service myfirstapp”. This should automatically launch the browser. Append the URL sayhello to get the message.

minikube service myfirstapp

Launch myfirstapp API from kubernetes IP

Now we have successfully deployed and run a Spring Boot application as a container on Kubernetes.

This section provides a quick introduction on how to develop a simple micro service which exposes a REST API using Spring Boot.

The intent is to do a simple “/sayhello” API which returns “Hello” as the output.

Open the Spring Tool Suite (STS) and click on Spring Starter Project.

Create a Spring Starter Project

Fill in the application details. For this example we would use Java 8 and Maven as the build system.

Spring Starter Project application details

Since we are exposing APIs from this application enable Web Services under Web. Refer the snapshot below.

Spring Starter Project Enable Web Services

Finish the application wizard and you would see the Spring project created and ready for use in Spring Tool Suite IDE. Under Maven dependencies all the dependent jars are already loaded. Also notice that pom.xml is created.

@SpringBootApplication is a convenience annotation. The @SpringBootApplication annotation is equivalent to using @Configuration, @EnableAutoConfiguration and @ComponentScan with their default attributes if you are familiar with Spring annotations.

Otherwise we have one line of code in main program which launches the application.

Now let us introduce a new class HelloController which exposes the /sayhello API. This is achieved using @RestController and @RequestMapping annotations as shown below.

Next step is to package this application into a standalone application using Maven. If you notice the generated pom.xml file you will see the necessary build plugins already added since we selected Maven as the build system during project creation.

Go to the workspace folder where the application is stored in your workspace and run “mvn package” command. This builds and packages the application. You should see “MyFirstApp-0.0.1-SNAPSHOT.jar” bundled with all dependencies.

Maven standalone application package

You can test if the application starts up by running the command “java –jar MyFirstApp-0.0.1-SNAPSHOT.jar”. The same browser test can be done on the exposed API.

Spring Tool Suite (STS)

The Spring Tool Suite (STS) is an Eclipse based development environment that is customized for developing Spring applications. The Spring Tool Suite (STS) is a ready to use distribution of the latest Eclipse releases with the Spring IDE components pre-installed.

This
page provides a quick reference on the steps needed to run a Spring Boot
application as a Docker container inside Kubernetes on Windows. This would be
ready reference for anyone wanting to experiment with micro services
architecture.

How to run a Spring
Boot application as a Docker container inside Kubernetes on Windows -
Introduction

This section provides a quick introduction to Spring Boot,
Dockers and Kubernetes.

SpringBoot

Spring
Boot makes it easy to create stand-alone, production-grade Spring based
Applications that you can “just run”. Spring Bootis a rapid
application development platform. It uses various components ofSpring, but has
additional elements like the ability to package your application as a runnable
jar, which includes an embedded tomcat (or jetty) server.

Docker is an open platform for developing, shipping, and
running applications. Docker provides the ability to package and run an
application in a loosely isolated environment called a container. Container
based virtualization uses the kernel on host’s operating system to run multiple
guest instances. Each container has its own root file system, processes,
memory, devices and network ports.

Kubernetes
is an open-source system for automating deployment, scaling and management of
containerized applications across a cluster of nodes. Kubernetes is a powerful system, developed
by Google, for managing containerized applications in a clustered environment.
It aims to provide better ways of managing related, distributed components
across varied infrastructure.

Let us look into a simple example to understand Scala Traits. Scala Traits are defined using the keyword trait and looks like a class definition. In this example we define a trait called Vehicle which can have both abstract methods and concrete methods.

Scala Traits and Diamond Problem

For C++ programmers whenever topic of multiple inheritance is discussed the focus is on the topic of diamond problem. Here classes Derived1 and Derived2 inherit from Base and Derived3 inherits fromboth Derived1 and Derived2. If both Derived1 and Derived2 override a method in Base there is ambiguity in Derived3 on which implementation should be inherited. In C++ developers need to override each conflicted method at the bottom of the diamond to resolve the ambiguity.

Diamond Problem with Multiple Inheritance

Java doesn't support multiple inheritance prior to Java 8 and hence avoid the diamond problem. But in Java 8 with the introduction of default methods on interfaces Java is subject to diamond problem. Again the conflicted method needs to be implemented at the bottom of diamond to resolve ambiguity.

Scala provide a simple solution to resolve the diamond problem by considering the order in which Traits are inherited. The implementation in the super type that is furthest to the right is considered. If the class/ trait inheriting provides a overridden implementation then this is considered.