WSO2 Ballerina is a compiled, type-safe, concurrent programming language which is designed for microservice development and deployment. Kubernetes is an open source container orchestration system for automating deployment, scaling and management of the containerised applications. The Ballerina language enables the user to provide annotation level configuration for Kubernetes deployment. Here we will explain how to write simple Ballerina service and deploy it as a Kubernetes pod.

You need to begin with writing a simple Ballerina service. The below sample service will return “Hello World !” on request. Create a hello.bal file and add below sample code.

For the run sample, use the following command:

$ ballerina run hello.bal
Initiating service(s) in ‘hello.bal’
ballerina: started HTTPS/WSS endpoint


Run the above service with port 9090 and try using below command:

$ curl https://localhost:9090/helloWorld/sayHello -k

Hello World !

Now our initial Ballerina REST service is ready, it’s time to containerise and run this service with Kubernetes. Before starting implementation, you’ll need to follow below steps as a prerequisite.

  • install Kubernetes
  • install VirtualBox
  • install Minikube

(If you need support or want to find out more on how to install VirtualBox and Minikube, click here.)

After a successful installation, we can add Kubernetes configurations into Ballerina code as annotations.Ingress annotation provides ingress resource for service. Expose service with “” hostname.

//Endpoint annotations 
@kubernetes:Ingress { 
 hostname: “” 

Service annotation is used to add service type as Nodetype, this annotation modifies the service.yml file. In this example “hello” is the service name.

@kubernetes:Service {
name: “hello”,
serviceType: “NodePort”

Deployment annotation is then used to modify deployment.yml. Use below command.

$ minikube docker-env
export DOCKER_HOST=”tcp://″
export DOCKER_CERT_PATH=”/home/user/.minikube/certs”
export DOCKER_API_VERSION=”1.35″

Then add Docker host and Docker CertPath as below.

//Service annotations
@kubernetes:Deployment {
dockerHost: “tcp://”,
dockerCertPath: “/home/user/.minikube/certs”

After adding these annotations to Ballerina code it should appear like the below.

The next step is to try to build and run a Ballerina service as a Kubernetes pod. Follow the below commands for build and deployment. Make sure Minikube and VirtualBox are up and running.

$ sudo ballerina build hello.bal
Compiling source
Generating executable
@kubernetes:Service — complete 1/1
@kubernetes:Ingress — complete 1/1
@kubernetes:Secret — complete 1/1
@kubernetes:Deployment — complete 1/1
@kubernetes:Docker — complete 3/3
@kubernetes:Helm — complete 1/1

Run the following command to deploy the Kubernetes artefacts.

kubectl apply -f /home/{user}/Projects/Sample/Blog/target/kubernetes/hello

Run the following command to install the application using Helm.

helm install — name hello-deployment /home/{user}/Projects/user/Blog/target/kubernetes/hello/hello-deployment

Then run the below.

$ kubectl apply -f /home/user/Projects/Sample/Blog/target/kubernetes/hello
deployment.extensions/hello-deployment configured
ingress.extensions/helloep-ingress configured
secret/helloep-keystore configured
service/hello configured

Now the service is successfully deployed to the Minikube cluster, you can get pod details such as below.

$ kubectl get pods
hello-deployment-6c85f4b77c-wmj5h 1/1 Running 2 4h
$ kubectl get svc
hello NodePort <none> 9090:32272/TCP 5h
kubernetes ClusterIP <none> 443/TCP 5d
$ kubectl get ingress
helloep-ingress 80, 443 5d
$ kubectl get secrets
helloep-keystore Opaque 1 5d
$ minikube service hello — url

Now service can be tested using a curl command as below.

$ curl -k
Hello World !

The Kubernetes dashboard will also show the below. Using this dashboard you can monitor, delete, scale up the deployment.

This is a brief introduction using a simple service deployment. If you have found this interesting and want to find out more, visit the Ballerina website by clicking here.

Kapila Ranasinghe

Software Engineer
Mitra Innovation

Leave a Reply