StackStalk
  • Home
  • Java
    • Java Collection
    • Spring Boot Collection
  • Python
    • Python Collection
  • C++
    • C++ Collection
    • Progamming Problems
    • Algorithms
    • Data Structures
    • Design Patterns
  • General
    • Tips and Tricks

Sunday, April 16, 2017

Spring Boot micro service as a Docker container inside Kubernetes on Windows – Containerize the micro service and deploy

 April 16, 2017     No comments   

This section provides the steps needed to run the Spring Boot micro service as a Docker container inside Kubernetes.

  • Copy the MyFirstApp-0.0.1-SNAPSHOT.jar built in the previous section to another folder say C:\MyFirstApp.
  • Let us now create a Docker container using this standalone jar. We need to introduce the Docker file. Create a file with name Dockerfile in C:\MyFirstApp and add the following lines. 
FROM java:8
ADD MyFirstApp-0.0.1-SNAPSHOT.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

  • Dockerfile and the MyFirstApp-0.0.1-SNAPSHOT.jar should be under the same folder.
Create Dockerfile
  • Understanding the elements of a basic Dockerfile.
  1. 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.
  2. ADD element copies the standalone Jar to app.jar.
  3. 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.
    • Share This:  
    Newer Post Older Post Home

    0 comments:

    Post a Comment

    Follow @StackStalk
    Get new posts by email:
    Powered by follow.it

    Popular Posts

    • Avro Producer and Consumer with Python using Confluent Kafka
      In this article, we will understand Avro a popular data serialization format in streaming data applications and develop a simple Avro Produc...
    • Monitor Spring Boot App with Micrometer and Prometheus
      Modern distributed applications typically have multiple microservices working together. Ability to monitor and manage aspects like health, m...
    • Server-Sent Events with Spring WebFlux
      In this article we will review the concepts of server-sent events and work on an example using WebFlux. Before getting into this article it ...
    • Implement caching in a Spring Boot microservice using Redis
      In this article we will explore how to use Redis as a data cache for a Spring Boot microservice using PostgreSQL as the database. Idea is to...
    • Python FastAPI microservice with Okta and OPA
      Authentication (AuthN) and Authorization (AuthZ) is a common challenge when developing microservices. In this article, we will explore how t...
    • Spring Boot with Okta and OPA
      Authentication (AuthN) and Authorization (AuthZ) is a common challenge when developing microservices. In this article, we will explore how t...
    • Getting started with Kafka in Python
      This article will provide an overview of Kafka and how to get started with Kafka in Python with a simple example. What is Kafka? ...
    • Getting started in GraphQL with Spring Boot
      In this article we will explore basic concepts on GraphQL and look at how to develop a microservice in Spring Boot with GraphQL support. ...

    Copyright © StackStalk