{{announcement.body}}
{{announcement.title}}

How Ballerina Makes Deploying to Kubernetes Easier for Developers

DZone 's Guide to

How Ballerina Makes Deploying to Kubernetes Easier for Developers

See how Ballerina's Kubernetes annotation allows developers to automate container deployment and focus on real problem solving.

· Cloud Zone ·
Free Resource

With the emergence of microservices, applications needed to be deployed on multiple containers. This fueled the need for container orchestration, load balancing, auto-healing, and monitoring, which exponentially increased the popularity of Kubernetes.

But developers have been neglected. Deploying on containers and Kubernetes is not part of the edit, build, run, and test programming cycle. Developers love coding, and should ideally focus on solving the business problems at hand. DevOps was introduced to fill this gap, but, in practice, these two worlds are quite separate. Ballerina — an open source programming language and platform for cloud-era application programmers — aims to connect these two worlds and make the developer experience better.

Ballerina Kubernetes Annotation

Developers prefer to stay within their IDEs instead of writing Docker files, generating Docker images, and writing Kubernetes YAML files. But running their application on Kubernetes and testing production behavior is important. Unfortunately, running an application in a Kubernetes cluster has a someonwhat steep learning. 

The Ballerina Kubernetes annotation model was created to address this issue. A simple "hello world" sample can be used to understand these concepts, but these annotations can be used in complex examples as well. Let’s take a look at the service below, written in Ballerina:

Java


You may also like: Getting Started With Ballerina.

The sample program has a "hello" HTTP service. Within the service, is a "hi" resource function. The hello service is listening to port 8080, and when we invoke the hi, resource it returns the “Hello World” HTTP response. When the source code is compiled, it will generate a JAR. This will enable users to run the program with the java -jar command.

Shell


To annotate the above source code with Ballerina Kubernetes annotations, the ballerina/kubernetes module needs to be imported. Then, the following annotations need to be added:

Java


The two annotations added above with a few properties are @kubernetes:Deployment and @kubernetes:Service. As shown below, once the code is compiled, the Ballerina compiler will generate the Dockerfile, Docker image, and all necessary artifacts required to deploy the application.

Shell


The kubectl command can be used to deploy your application into a Kubernetes cluster.

Shell


Since the serviceType has been set to NodePort in the @kubernetes:Service annotation, it creates a NordPort type Kubernetes service. The application can be accessed via the NodePort, as shown below:

Shell
 




xxxxxxxxxx
1


 
1
$> curl http://localhost:31686/hello/hi
2
Hello World!



In the above example, using Ballerina annotations doesn’t interrupt the developer’s flow. All the developer has to do is write the annotations, and the compiler takes care of the rest. Ballerina Kubernetes annotation reads the source code, populates the correct values, and generates validated Kubernetes artifacts, all while following best-practices.

The default values can be overridden by setting them as annotation properties. The IDE can even suggest what to put and where to put it in your source code. For example, see the following IDE suggestions under the @kubernetes:Deployment annotation:

Kubernetes deployment annotation

Kubernetes deployment annotation

Ballerina annotations can generate artifacts for all major types:

  • @kubernetes:Deployment{}.
  • @kubernetes:Service{}.
  • @kubernetes:Ingress{}.
  • @kubernetes:HPA{}.
  • @kubernetes:Secret{}.
  • @kubernetes:ConfigMap{}.
  • @kubernetes:PersistentVolumeClaim{}.
  • @kubernetes:ResourceQuota{}.
  • @kubernetes:Job{}.

It also supports Istio and OpenShift artifact generation:

  • @istio:Gateway{}.
  • @istio:VirtualService{}.
  • @openshift:Route{}.

More information on this can be found at https://github.com/ballerinax/kubernetes.

Extending the Ballerina Compiler

This is just the beginning of what Ballerina can potentially do with Kubernetes and cloud services. More can be done by writing extensions to the Ballerina compiler. Its extensible architecture and pluggable compiler extension support allow additional verification or processing if required. The compiler extension can make use of the metadata provided via annotations to introduce additional behavior to the compilation process.

The following diagram shows a high-level architecture of the Ballerina compiler:

Ballerina compiler

Ballerina compiler

The Ballerina compiler frontend has several phases. At the annotation processor phase, it triggers all the annotation extensions that are defined in the source code. Depending on the extension implementation, it can generate the necessary artifacts.

This step-by-step guide helps users develop their own compiler extensions to meet unique requirements: https://ballerina.io/learn/how-to-extend-ballerina/.

Summary

Ballerina is built to natively understand all major Kubernetes constructs and platforms like Openshift, Istio, Helm Chart, and K-native. Its powerful Kubernetes annotation capability allows developers to focus on building features, without having to worry about the complexities of deployment, such as creating Docker files and YAML artifacts. This helps boost productivity and enhance the developer experience by ensuring their development cycle is not broken during deployment.


Further Reading

Topics:
ballerina ,kubenetes ,developer ,microservice ,devops

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}