
Deploying Ballerina Code on the Cloud
Ballerina is a programming language that has built-in support for Docker and Kubernetes that enable developers to build applications running on the cloud. This article provides an overview of features supported by Ballerina in terms of Docker and Kubernetes integration that lets you deploy on a container platform without added artifacts.
Consider the following simple Ballerina code which 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 is capable of generating Dockerfile, Docker Image, Kubernetes deployment, Kubernetes Service, and Kubernetes HPA from the code.
If the file is saved as hello_svc.bal
, compiling the source with the following command will generate the above-mentioned artifacts.
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 at 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
The ports, protocols, and resource limits are automatically populated based on the program written based on the hello_svc.bal file.
Credit: Source link