Deploy Ballerina on Kubernetes

Let’s dockerize your Ballerina service and deploy it into Kubernetes.

In this section, you will be writing a simple Ballerina service and then, you will dockerize the application and deploy it in Kubernetes.

Setting up the prerequisites

To complete this tutorial, you need:

  1. A command terminal
  2. A text editor

    Tip: Preferably, Visual Studio Code with the Ballerina extension.

  3. A Ballerina installation
  4. Docker installed and configured in your machine
  5. A Docker Hub account
  6. Kubectl installed and configured in a Kubernetes cluster

Code to Cloud

Code to Cloud is a compiler extension, which is packed with Ballerina, which makes it easier to generate artifacts required for the cloud from your Ballerina code. Currently, you could generate Docker and Kubernetes artifacts from the Ballerina code. This process encourages you to write cloud-ready code from day one without any additional effort.

Writing the Ballerina service

Let’s write a Ballerina program that returns a Hello, World! string upon invoking the resource. Execute the bal new command to create a new Ballerina project.

$ bal new greeter

This command creates a new directory called greeter. Let’s move into this directory to see what’s in there.

├── Ballerina.toml
└── main.bal

You can replace the main.bal content with the following code.

import ballerina/http;

listener http:Listener httpListener = new (8080);
service / on httpListener {
    resource function get greeting() returns string { 
        return "Hello, World!"; 

To enable the code to cloud functionality in the Ballerina project, you need to add the cloud="k8s" property to the build-options in the Ballerina.toml file.


cloud = "k8s"
  1. Create a file named Cloud.toml in the package directory and add the content below. Make sure to replace the value of the repository field with your Docker hub repository name.


repository="wso2inc" # Docker hub repository name.
name="greeter" # container name

Now, when we build the Ballerina project, the cloud artifacts should be generated inside the target/ directory.

$ bal build
Compiling source

Generating executable

Generating artifacts...

        @kubernetes:Service                      - complete 1/1
        @kubernetes:Deployment                   - complete 1/1
        @kubernetes:HPA                          - complete 1/1
        @kubernetes:Docker                       - complete 2/2 

        Execute the below command to deploy the Kubernetes artifacts: 
        kubectl apply -f /home/example/greeter/target/kubernetes/greeter

        Execute the below command to access service via NodePort: 
        kubectl expose deployment greeter-deployment --type=NodePort --name=greeter-svc-local

Let’s push the created docker image into docker hub now. You should replace the wso2inc name with your repository name.

$ docker push wso2inc/greeter:latest
The push refers to repository []
latest: digest: sha256:c1acf5165848d70c347a970d6b5c32f63669cdbb0d4c1daca2c91cfbe32f61b2 size: 13718

Execute the commands displayed from the compiler above to deploy the Ballerina application into the Kubernetes cluster.

$ kubectl apply -f /home/example/greeter/target/kubernetes/greeter
service/greeter-svc created
deployment.apps/greeter-deployment created
horizontalpodautoscaler.autoscaling/greeter-hpa created

Info: For in-depth information about executing these deployed applications and the supported customizations in code to cloud, see Code to Cloud deployment.