Rethink programming from code to cloud

Date: Oct 25, 2022

Related Tags:1. Release notes for Docker
2. Alibaba Cloud Container Service for Kubernetes (ACK)

Abstract: Ballerina is an open-source programming language designed for migrating code to the cloud while providing a unique developer experience. Its compiler can be extended to read annotations defined in the source code and generate artifacts to deploy the code to different clouds. These artifacts include Dockerfiles, Docker images, Kubernetes YAML files, or serverless functions.

In the early days, developers simply wrote programs, built them, and ran them. Today, developers also need to consider various ways of running, running on a machine as an executable (most likely a virtual machine), or packaging into a container; deploying the container into Kubernetes, or deploying it into a serverless environment or in the service mesh. However, these deployment methods are not part of a developer's programming experience. Developers have to write code in a certain way to work properly in a specific execution environment, so programming without these concerns is not an option.

Ballerina is an open-source programming language designed for migrating code to the cloud while providing a unique developer experience. Its compiler can be extended to read annotations defined in the source code and generate artifacts to deploy the code to different clouds. These artifacts include Dockerfiles, Docker images, Kubernetes YAML files, or serverless functions.

From code to Docker

Agile is the biggest benefit of microservice-based application development, and Docker plays a major role here. Docker helps us package an application and its dependencies into a binary image that can run on-premises, in a public cloud, or in a private cloud.

To create a Docker image, the developer must choose the appropriate base image, package and bundle all dependencies, copy the application binary, and set the execute command with the appropriate permissions to create the Dockerfile. Developers must follow a set of best practices if they want to create optimized images, otherwise, the built images will be large, less secure, and have many other drawbacks.

The Ballerina compiler is capable of creating optimized Docker images from application source code. The following code shows how to package the Ballerina hello service into a Docker container and run it.

Add @docker:Config {} to the service to generate the Dockerfile and Docker image. Add @docker:Expose {} to the listener object to expose the port for traffic to enter the container.

Ballerina fully supports Docker features:

1: Change the label and repository of the Docker image;
2: Push the Docker image to the Docker image repository;
3: Use Docker to remotely debug the Ballerina service;
4: Copy the file to the Docker image;
5: Multiple modules with Docker annotations;
6: Use Docker to run Ballerina service in HTTP and HTTPS;
7: Overwrite the generated Dockerfile CMD;
8: Docker Hello World Main function;
9: Use the Main function to copy the file.

From code to Kubernetes

Docker can help us package our application and perform some developer testing. However, if you want to run an application with multiple microservices in production, I recommend using a platform like Kubernetes. Kubernetes is an open-source platform for automating the deployment, scaling, and management of containerized applications. Kubernetes defines a unique set of basic modules that provide mechanisms for deploying, maintaining, and scaling applications. A Pod is a logical set of containers guaranteed to run in the same place on the host. A Kubernetes service provides functions such as discovery, routing, and load balancing for the set of pods it forms. A Kubernetes deployment is a set of Pods with mechanisms such as defined replica sets, health checks, and rolling updates. All these Kubernetes objects need to be defined as YAML files and deployed to the Kubernetes cluster.

Although developers want to run their applications on the Kubernetes platform, in many cases, the work of creating these YAML files is a headache for developers. The Ballerina compiler is able to create these YAML files when compiling source code. Let's modify the above example to generate Kubernetes objects.

Adding @kubernetes:Deployment{} to the Ballerina service generates the YAML for the Kubernetes deployment, which is required to deploy the hello application to Kubernetes. Add the @kubernetes:Service{} annotation to generate the YAML for the Kubernetes service. In this example, we set the serviceType to NodeNode and access the hello service through nodeIP:Port.

In addition to the hello.jar binary, the Ballerina compiler generates a Dockerfile, a Docker image, a hello.yaml file (with Kubernetes deployment and services), and a YAML file for the helm chart.

Ballerina provides comprehensive support to guarantee running applications on the Kubernetes platform:

1: Kubernetes Hello World with liveness probe and hostname mapping;
2: Ballerina programs for multiple services running in multiple ports;
3: Secure Kubernetes Hello World;
4: Ballerina service with HTTP and HTTPS endpoints;
5: Kubernetes Hello World in Google Cloud Environment;
6: Mount the Secret to the deployment;
7: Mount the configuration map volume to the deployment;
8: Mount the persistent volume claim to the deployment;
9: Ballerina module with Kubernetes annotations;
10: Kubernetes Hello World with Ballerina functions;
11: Copy the external files into the Docker image;
12: Ballerina module with dependencies;
13: Deploy the Ballerina service in the namespace;
14: The resource quota for the namespace.

From code to Istio

A microservices architecture offers developers many of the advantages of agile development, helping to accelerate innovation. But microservices architecture also has its own complexities. Docker and Kubernetes address some of these complexities. A service mesh is a modern software architecture that reduces the complexity of platforms like Kubernetes. Istio is an open source service mesh implementation. Key areas of focus include: service discovery, load balancing, failover, metrics, and monitoring. Istio also supports complex operational requirements such as A/B testing, canary releases, rate limiting, access control, and end-to-end authentication, among others.

In addition to Kubernetes objects, Istio introduces some other unique concepts, chief among them VirtualService and Gateway. VirtualService defines a set of traffic routing rules to implement the complex operational requirements described above. Istio Gateway is a load balancer that runs at the edge of the mesh to receive incoming or outgoing HTTP/TCP connections.

If you add two annotations on the Ballerina listener object, Balerina will be able to generate objects that deploy VirtualService and Gateway. The following code snippet shows two annotations that define Istio support: Building this source code generates objects for Istio VirtualService and Gateway.

From code to Knative

Knative is a serverless platform, originally created by Google and now supported by more than 50 companies. Knative has built a serverless platform using the capabilities of the Kubernetes platform to help developers focus on writing code instead of worrying about the "boring but hard" work of building, deploying and managing applications. One of the key features of Knative is to automatically scale from zero replicas and adjust workloads based on demand.

Knative also has its own object model. Knative services are defined by routes and configurations, which have the same names as the services contained in the YAML file. Every time the configuration is updated, a new revision is created.

Ballerina is able to generate these necessary objects when compiling the source code, the only requirement is to add a simple comment to the code.

Adding this annotation generates the objects needed to deploy the application in serverless mode in a Knative cluster.

From code to AWS Lambda

AWS Lambda is an event-driven serverless computing platform. You can deploy Ballerina functions in AWS Lambda by simply adding @awslambda:Function.

CI/CD and GitHub Action

In a microservices architecture, continuous integration and continuous delivery (CI/CD) are critical to an agile environment where the system is constantly improving. There are various technologies that provide CI/CD functionality, recently GitHub introduced GitHub Actions. GitHub Actions provides a convenient mechanism to implement CI/CD pipelines in GitHub repositories using its workflow concept.

We can create a Ballerina development environment with CI/CD through Ballerina GitHub Action (available in GitHub Marketplace).

Support SaaS connector

We've discussed that Ballerina supports different technologies to automate cloud deployments. To get the full capabilities of the cloud, applications should be able to integrate with software-as-a-service (SaaS) offered by different cloud vendors.

Ballerina provides a simple workflow to connect and integrate these SaaS services. For example, the following code snippet shows how to initialize and send a tweet using the Twitter SaaS service:

Ballerina has many ready-made SaaS connectors:

1: Connect to Google Spreadsheets from Ballerina;
2: Connect to Gmail from Ballerina;
3: Connect to Bigquery from Ballerina;
4: Connect to Azure CV service via Ballerina;
5: Connect to Amazon S3 from Ballerina;
6: Connect to Amazon SQS service from Ballerina;
7: Connect to Salesforce from Ballerina.


Previously, developers simply wrote, built, and ran programs. Today, however, developers are faced with multiple options for how to operate.

Cloud-native platforms such as Docker, Kubernetes, service meshes, and serverless play an important role in modern platforms that support deployment automation.

For developers, however, these deployments are not programming.

Ballerina is an open source programming language designed for migrating from code to the cloud while providing a unique developer experience.

Ballerina's compiler can be extended to read annotations defined in source code and generate objects that deploy the code to different cloud environments, including Dockerfiles, Docker images, Kubernetes YAML files, or serverless functions.

Related Articles

Explore More Special Offers

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00

phone Contact Us