Jolie Microservices on Kubernetes
Jolie is a "microservice-oriented" programming languages: it crystallizes the programming concepts of microservices as native language features.
Join the DZone community and get the full member experience.
Join For FreeThe Jolie programming language is (micro)service-oriented: it makes it easy to create services and plays well with containerization. Deploying a Jolie service in a Docker container is quite easy.
In this article, we are going to deploy a Jolie service on Kubernetes and invoke it in four easy steps.
This is a little demo on how to set up a Kubernetes environment on your PC (thanks to minikube) and build up a scalable clusterized version of a simple Jolie service.
What we are going to do now is to transform the example service for doubling numbers from the Jolie documentation into a powerful Kubernetes scalable app (doing exactly the same thing, but with the power of the cloud, so cooler).
First of all, let's set up our box!
Make sure that you have:
Ok, ready?
Now you can clone this Git repository and go ahead.
Step 1: Create a Docker Image Containing Your Jolie Application
This task is very simple, following the official Jolie documentation and using images available on Docker Hub.
This is our Dockerfile to build an image exposing the "twice-a-number" service on TCP port 8000:
FROM jolielang/jolie
EXPOSE 8000
COPY server.ol server.ol
COPY twiceInterface.iol twiceInterface.iol
CMD jolie server.ol
Before creating the image we have to configure Docker's environment to point to minikube, otherwise images wouldn't be available to your local cluster.
Linux
xxxxxxxxxx
$ eval $(minikube docker-env)
Win10 (on PowerShell)
xxxxxxxxxx
PS> & minikube docker-env | Invoke-Expression
Ok, now we can build our image (if not specified, commands are the same on both platforms; don't miss the final dot),
xxxxxxxxxx
$ docker build -t jolie-k8s-sample .
Now, verify that all went as expected:
xxxxxxxxxx
$ docker images
REPOSITORY TAG IMAGE ID
jolie-k8s-sample latest 2df927523a30 ...
Step 2: Create a Kubernetes Deployment
We are ready to use our brand new image in a Kubernetes deployment. We set the "replicas" parameter to 2, meaning that Kubernetes will always have two pods running our container.
xxxxxxxxxx
apiVersion apps/v1
kind Deployment
metadata
name jolie-sample-deployment
labels
app jolie-sample
spec
replicas2
selector
matchLabels
app jolie-sample
template
metadata
labels
app jolie-sample
spec
containers
name jolie-k8s-sample
image jolie-k8s-sample
ports
containerPort8000
imagePullPolicy IfNotPresent
To start deployment rollout, just type:
$ kubectl apply -f jolie-k8s-deployment.yml
And after a few seconds you will see your pods up and running:
xxxxxxxxxx
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
jolie-sample-deployment-655f8b759d-bmzk7 1/1 Running 0 4s
jolie-sample-deployment-655f8b759d-mq8cn 1/1 Running 0 4s
Step 3: Expose Deployment by a Service
Now we have two running instances of our (micro)service, but we lack a communication channel to reach the container's port 8000 from outside of the Kubernetes virtual network.
Kubernetically speaking we need a service.
Following the minikube tutorial, we type:
xxxxxxxxxx
$ kubectl expose deployment jolie-sample-deployment --type=LoadBalancer --port=8000
$ kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
jolie-sample-deployment LoadBalancer 10.109.47.147 <pending> 8000:30095/TCP 13s
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP
So now there's a service exposing the pod's port 8000 on the cluster's port 30095.
To make this visible from your host, you have to go through a "minikube service":
xxxxxxxxxx
$ minikube service jolie-sample-deployment
|-----------|-------------------------|-------------|-----------------------------|
| NAMESPACE | NAME | TARGET PORT | URL |
|-----------|-------------------------|-------------|-----------------------------|
| default | jolie-sample-deployment | | http://192.168.99.100:30095 |
|-----------|-------------------------|-------------|-----------------------------|
* Opening service default/jolie-sample-deployment in default browser...
The service is now visible and ready to receive invocations!
Step 4: Invoke Microservices From a Client
As a final step, we make sure that everything is working by calling the Jolie service running inside the Kubernetes cluster.
Open client.ol with a text editor and modify line 5, setting the IP and port of exposed the minikube service:
Location: "socket://192.168.99.100:30095"
Save and try the following:
xxxxxxxxxx
$ jolie client.ol
jolie-sample-deployment-655f8b759d-mq8cn -> 10
$ jolie client.ol
jolie-sample-deployment-655f8b759d-bmzk7 -> 10
$ jolie client.ol
jolie-sample-deployment-655f8b759d-mq8cn -> 10
$ jolie client.ol
jolie-sample-deployment-655f8b759d-bmzk7 -> 10
As you can, see we are getting the right response (client.ol has a hardcoded value 5 as an input) and so we have successfully demonstrated that Kubernetes can at least multiply an integer by another integer!
Eureka!
In our source code, we modified server.ol from the original to write out the hostname too:
getenv@Runtime( "HOSTNAME" )( hostname_env )
result = hostname_env + " -> " + number * 2
And that allow us to verify that:
- Our Jolie server is running on multiple pods.
- The infrastructure is hidden from the clients.
- The calls are automatically managed and balanced by the Kubernetes engine (the output shows each call is getting its response from a different pod).
- BONUS: if you need more power to twice number 5 you can scale up your deployment by running the following command.
$ kubectl scale deployment jolie-sample-deployment --replicas=<N
Enjoy your Jolie services on Kubernetes!
Published at DZone with permission of Marco Montesi. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments