Deploying ballerina code to the cloud

Ballerina is a programming language with built-in support for Docker and Kubernetes that enables developers to build cloud-based applications. This article provides an overview of Ballerina’s supported features in terms of the integration of Docker and Kubernetes which allows you to deploy on the container platform without additional samples.

Consider the following simple ballerina code that implements a simple Hello World service.

import ballerina/http;

service /hello on new http:Listener(9090) {
    resource function get sayHello(http:Caller caller, http:Request req)
            returns error? {
        check caller->respond("Hello, World!");
    }
}

The Ballerina compiler code is capable of creating Dockerfile, Docker Image, Kubernetes Deployment, Kubernetes Service, and Kubernetes HPA.

If the file is saved as hello_svc.bal, Configuring the source with the following command will produce the above mentioned patterns.

bal build --cloud=k8s hello_svc.bal
Compiling source
	hello_svc.bal

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 /Users/anuruddha/sample1/kubernetes

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

	hello_svc.jar

The yaml file can be accessed in the Kubernetes folder.

---
apiVersion: "v1"
kind: "Service"
metadata:
  labels:
    app: "hello_svc"
  name: "hello-svc-svc"
spec:
  ports:
  - name: "port-1-hello-sv"
    port: 9090
    protocol: "TCP"
    targetPort: 9090
  selector:
    app: "hello_svc"
  type: "ClusterIP"
---
apiVersion: "apps/v1"
kind: "Deployment"
metadata:
  labels:
    app: "hello_svc"
  name: "hello-svc-deployment"
spec:
  replicas: 1
  selector:
    matchLabels:
      app: "hello_svc"
  template:
    metadata:
      labels:
        app: "hello_svc"
    spec:
      containers:
      - image: "hello_svc:latest"
        lifecycle:
          preStop:
            exec:
              command:
              - "sleep"
              - "15"
        name: "hello-svc-deployment"
        ports:
        - containerPort: 9090
          name: "port-1-hello-sv"
          protocol: "TCP"
        resources:
          limits:
            memory: "256Mi"
            cpu: "500m"
          requests:
            memory: "100Mi"
            cpu: "200m"
      nodeSelector: {}
---
apiVersion: "autoscaling/v1"
kind: "HorizontalPodAutoscaler"
metadata:
  labels:
    app: "hello_svc"
  name: "hello-svc-hpa"
spec:
  maxReplicas: 2
  minReplicas: 1
  scaleTargetRef:
    apiVersion: "apps/v1"
    kind: "Deployment"
    name: "hello-svc-deployment"
  targetCPUUtilizationPercentage: 50

Hello_svc.bal Ports, protocols, and resource limits are automatically populated based on a program based on a file.

.

Write a Comment

Your email address will not be published. Required fields are marked *