DZone
Microservices Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Microservices Zone > Generate Kubernetes YAML Right From Your App Code

Generate Kubernetes YAML Right From Your App Code

Learn how a new programming language, Ballerina, can compile into Docker and Kubernetes artifacts, letting you generate YAML from your microservices app code.

Dmitry Sotnikov user avatar by
Dmitry Sotnikov
CORE ·
Jul. 11, 18 · Microservices Zone · Tutorial
Like (27)
Save
Tweet
15.38K Views

Join the DZone community and get the full member experience.

Join For Free

A new programming language, Ballerina, can compile right into Docker and Kubernetes artifacts.

Kubernetes is rapidly becoming the platform to run your microservices. Unfortunately, for most existing programming languages this means that developers or operations need to produce hundreds of lines of YAML configuration files defining the deployment.

Ballerina is a cloud-native programming language aimed at making microservice development and deployment easy. Let us see how with just a little bit of annotations Ballerina code can be deployed into Docker and Kubernetes.

Suppose, I have a simple service designed in Ballerina. This is an HTTP service called hello on port 9090, it has a single POST resource called hi that accepts a string name in the request and sends "Hello, name" back:

import ballerina/http;

endpoint http:Listener listener {
    port: 9090
};

service<http:Service> hello bind listener {
    @http:ResourceConfig {
        methods: ["POST"]
    }
    hi (endpoint caller, http:Request request) {
        string name = check request.getTextPayload();
        _ = caller->respond("Hello, " + name);
    }
}

If I compile and run it with Ballerina, I can invoke the service with curl (or some other HTTP client):

$ ballerina build demo.bal

$ ballerina run demo.balx
ballerina: initiating service(s) in 'demo.bal'
ballerina: started HTTP/WS endpoint 0.0.0.0:9090

$ curl -X POST -d "Ballerina" localhost:9090/hello/hi
Hello, Ballerina

Now, if I want to turn this into a Kubernetes deployment, I can simply add @kubernetes annotations right inside the service code.

I will add one to the endpoint definition asking Ballerina build process to turn it into a Kubernetes NodePort, and another one to my hello service itself to turn it into a Docker image and Kubernetes deployment.

import ballerina/http;
import ballerinax/kubernetes;

@kubernetes:Service {
    serviceType: "NodePort",
    name: "ballerina-demo"  
}
endpoint http:Listener listener {
    port: 9090
};

@kubernetes:Deployment {
    image: "demo/ballerina-demo",
    name: "ballerina-demo"
}
service<http:Service> hello bind listener {
    @http:ResourceConfig {
        methods: ["POST"]
    }
    hi (endpoint caller, http:Request request) {
        string name = check request.getTextPayload();
        _ = caller->respond("Hello, " + name);
    }
}

If I do ballerina build now, instead of a local executable file, it will generate me the Docker image and Kubernetes YAMLs that I can deploy right into my cloud:

$ kubectl get pods
No resources found.

$ ballerina build demo.bal
@kubernetes:Deployment                   - complete 1/1
@kubernetes:Docker                       - complete 3/3
Run following command to deploy kubernetes artifacts:
kubectl apply -f kubernetes/

$ tree
.
├── demo.bal
├── demo.balx
└── kubernetes
    ├── demo_deployment.yaml
    ├── demo_svc.yaml
    └── docker
        └── Dockerfile

2 directories, 5 files

$ kubectl apply -f kubernetes/
deployment.extensions "ballerina-demo" created
service "ballerina-demo" created

$ kubectl get pods
NAME                              READY     STATUS    RESTARTS   AGE
ballerina-demo-57ccd9f69c-j4klz   1/1       Running   0          6s

$ kubectl get svc
NAME             TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)          AGE
ballerina-demo   NodePort    10.100.95.68   <none>        9090:31885/TCP   11s
kubernetes       ClusterIP   10.96.0.1      <none>        443/TCP          25d

$ curl -X POST -d "Kubernetes" localhost:31885/hello/hi
Hello, Kubernetes

Note that we did not have to modify the logic of the service, add or edit any external files, or change our build process.

We only added Kubernetes-specific annotations to the existing code and the build process took care of the rest.

Besides the Services and Deployment annotations that we used, there are a few more for other configuration aspects such as autoscaling, passing config and other files, and so on:

Image title

Ballerina is an open-source project. You can find its source code repository at https://github.com/ballerina-platform/ballerina-lang.

This Kubernetes integration is just one of the features that Ballerina has.

You can learn more about Ballerina, see the examples, and download the runtime and tooling at https://ballerina.io.

Kubernetes YAML Ballerina (programming language) app microservice

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How to Integrate Zoom in React Application?
  • Handling Sensitive Data: A Primer
  • GraphQL: A Deep Dive Into Benefits, Use Cases, and Strategies
  • Top 10 Criteria to Select the Best Angular Development Company

Comments

Microservices Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo