XenonStack Recommends

DevOps

Building CI/CD Pipeline for Scala Application on Kubernetes

Gursimran Singh | 02 June 2023

Continuous Delivery Pipeline for Scala Application on Kubernetes

Overview of Scala Application

Scala is an object-oriented programming language. It provides support to functional programming. It also helps to remove bugs in complex applications. In this article, you get to know about Scala and Scala Applications on Kubernetes. Running Containers at any real-world scale requires container orchestration, and scheduling platform like Docker Swarm, Apache Mesos, AWS ECS but the most popular out of it is Kubernetes. Kubernetes is an open-source system for automating deployment and management of containerised applications. Continuous delivery is a branch of continuous integration. It concentrates on automating the software delivery process so that teams can quickly and confidently deploy their code to production at any point.

CI/CD Pipeline is a crucial part of the modern DevOps environment. Click to explore about, Continuous Integration and Continuous Delivery

What are the basics terminology for Scala Application?

To follow this guide you need -

Kubernetes

It is an open-source platform that automates container operations, and Minikube is best for testing Kubernetes.

Kubectl

Kubectl is a command-line interface to manage Kubernetes cluster either remotely or locally. To configure kubectl on your machine follow this link.

Shared Persistent Storage

Shared Persistent Storage is permanent storage that we can attach to the Kubernetes container so that we don`t lose our data even when container dies. We will be using GlusterFS as the persistent data store for Kubernetes container applications.

Scala Application Source Code

It's source code will be run inside a Kubernetes container.

DockerFile

Dockerfile contains a bunch of commands to build PHP Laravel application.

Container Registry

The Registry is an online image store for container images. Below mentioned options are few most popular registries.

Continuous delivery is a branch of continuous integration. It concentrates on automating the software delivery process. Source- Continuous Delivery Pipeline with Jenkins

How to write a Dockerfile for Scala Application?

At the time of writing this blog, I haven`t got any official image of Scala sbt so I decided to build a base image for Scala sbt. In this image, we are using OpenJDK 8 as a development environment and Scala sbt 0.13.

FROM openjdk: 8
RUN apt - get update\ && apt - get install apt - transport - https - y
RUN echo "deb https://dl.bintray.com/sbt/debian /" | tee - a / etc / apt / sources.list.d / sbt.list\ && apt - key adv--keyserver hkp: //keyserver.ubuntu.com:80 --recv 2EE0EA64E40A89B84B2DF73499E82A75642AC823 \
 && apt - get update\ && apt - get install sbt - y\ && rm - rf /
 var / lib / apt / lists /*

Building Scala Base Image

The Below mentioned command will build your scala base container image.

$ docker build -t scala-sbt:0.13

Dockerfile (Application)

As Scala sbt doesn't have any official image of Docker container so we will be using above created image of Scala sbt as a base image for Scala applications.

FROM scala - sbt: 0.13

MAINTAINER XenonStack

# Creating Home Directory in container
RUN mkdir - p / usr / src / app

# Setting Home Directory
WORKDIR / usr / src / app

# Copying src code to Container
COPY. / usr / src / app

# Compiling Scala Code
RUN sbt compile

# Exposing Port
EXPOSE 3000

# Running Scala Application
CMD["sbt", "run"]

Building Scala Application Image

The below-mentioned command will build your application container image.

$ docker build -t <name application="" of="" scala="" your="">:<version application="" of="">

Publishing Scala Application Container Image

Now we publish the container image on Private Docker Registry.
  • Login to Container Registry

$ docker login < dns link of private container registry >
 Username: xxxx
Password: xxxxx
  • Retag Container Images

$ docker tag <name application="" of="" your="">:<version application="" of="" your=""> <privater name="" registry="">/<name of="" repository="" your="">:<version application="" of="" your="">
  • Push Application container Images

$ docker push &amp;lt; privater registry name &amp;gt; /&lt;name of="" repository="" your=""&gt;:&lt;version application="" of="" your=""&gt;
Reference Link for Pull and Push to Quay.io docker registry. Similarly, we can push or pull any container image to any of below-mentioned container registry like Docker Hub, AWS ECR, Private Docker Registry, Google Container Registry etc.

Creating Deployment files in Scala Application for Kubernetes

Deploying application on kubernetes with ease using deployment and service files either in JSON or YAML format.
  • Deployment File
Following Content is for “ <name of application>.deployment.yml” file of python container application.

apiVersion: extensions / v1beta1
kind: Deployment
metadata:
 name: < name of application >
 namespace: < namespace of Kubernetes >
 spec:
 replicas: < number of application pods >
 template:
 metadata:
 labels:
 k8s - app: < name of application >
 spec:
 containers:
 -name: < name of application >
 image: < image name > : < version tag >
 imagePullPolicy: "IfNotPresent"
ports:
 -containerPort: 9000
  • Service File
Following Content is for “ <name of application>.service.yml” file of python container application.

apiVersion: v1
kind: Service
metadata:
 labels:
 k8s - app: < name of application >
 name: < name of application >
 namespace: < namespace of Kubernetes >
 spec:
 type: NodePort
ports:
 -port: 9000
selector:
 k8s - app: < name of application >
 

Deploying Scala Application in Kubernetes

Scala Container Application can be deployed either by kubernetes Dashboard or Kubectl (Command line). I`m explaining the command line that you can use in production kubernetes cluster.

$ kubectl create -f <name application="" of="">.deployment.yml
$ kubectl create -f <name application="" of="">.service.yml
Now we have successfully deployed it's Application on Kubernetes.

Verification of Dockerfile in Scala Application

We can verify application deployment either by using Kubectl or Kubernetes Dashboard. Below mentioned command will show you running pods of your application with status running/terminated/stop/created.

$ kubectl get po --namespace=<namespace kubernetes="" of=""> | grep <application name="">

Testing Dockerfile in Scala Application

Get the External Node Port using the below mentioned command. External Node Port is in the range from 30000 to 65000.

$ kubectl get svc--namespace = < namespace of kubernetes > | grep < application name >
Launch web Browser and open any of the below-mentioned URLs.
  • http://<kubernetes master ip address >: <application service port number>
  • http://<cluster ip address >: <application port number>
Develop and Deploy Python Application using Docker and Kubernetes and adopt DevOps in existing Python Applications. Source- Deploying Python Application on Docker & Kubernetes

Troubleshooting Dockerfile in Scala Application

  • Check Status of Pods.
  • Check Logs of Pods/Containers.
  • Check Service Port Status.
  • Check requirements/dependencies of the application.

How can XenonStack help you?

Our DevOps Consulting Services provides DevOps Assessment and Audit of your existing Infrastructure, Development Environment and Integration. Our DevOps Professional Services includes -

Continuous Integration & Continuous Deployment

Enable Secure Continuous Integration and Continuous Delivery Pipeline with Jenkins/Bamboo/TeamCity for delivery, automation, self-service environment, and On-demand. Deploy PHP Laravel Application to production by decreasing the time by the automation of entire delivery pipeline comprising build, deploy, test, and release.

Continuous Deployment Cloud Hosting

XenonStack Continuous Deployment Cloud Hosting Services for Scala Applications lets you clone your production to staging cluster in less than a minute. Develop, Deploy and Manage your PHP Laravel Application on leading Cloud Service Providers - AWS, Microsoft Azure, Google Cloud, and Container Environment - Kubernetes and Docker.

Application Modernization Services

Application Modernization Services enable the migration of monolithic applications to new Microservices architecture with Native Cloud Support including the integration of new functionality to create new value from the existing application.