import ballerina/http;

// This code is completely focused on the business logic and it does not specify anything related to operations.
listener http:Listener helloEP = new(9090);

service http:Service /helloWorld on helloEP {
    resource function get sayHello() returns string {
        return "Hello, World from service helloWorld ! \n";
    }
}

Kubernetes

Ballerina supports generating Docker and Kubernetes artifacts from code without any additional configuration. This simplifies the experience of developing and deploying Ballerina code in the cloud. Code to Cloud builds the containers and required artifacts by deriving the required values from the code. If you want to override the default values taken by the compiler, you can use a Cloud.toml file.

For more information, see Code to Cloud deployment.

import ballerina/http;
listener http:Listener helloEP = new(9090);

This code is completely focused on the business logic and it does not specify anything related to operations.

service http:Service /helloWorld on helloEP {
    resource function get sayHello() returns string {
        return "Hello, World from service helloWorld ! \n";
    }
}
# Before you build the package, you need to override some of the default values taken by the compiler. To do this, create a filed named `Cloud.toml` 
in the package directory, and add the content below to it.
# For all the supported key value properties, see [Code to Cloud deployment](/learn/run-ballerina-programs-in-the-cloud/code-to-cloud-deployment/).
[container.image]
repository="wso2inc"
name="hello"
tag="v0.1.0"
# Additionally, if you are using Minikube, execute the command below to reuse the Docker daemon from Minikube.
`eval $(minikube docker-env)`
# Execute the `bal build` command to build the Ballerina package. Code to Cloud generates only one container per package.
`bal build`
Compiling source
        wso2/hello:0.1.0
Creating balas
        target/bala/hello-2020r2-any-0.1.0.bala
Running Tests
        wso2/hello:0.1.0
        No tests found
Generating executables
        target/bin/hello.jar
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/wso2/project/target/kubernetes/hello-0.1.0
        Execute the below command to access service via NodePort:
        kubectl expose deployment wso2-hello-0--deployment --type=NodePort --name=wso2-hello-0--svc-local
# First, let’s try executing the Docker image separately.
# Verify if the Docker image is generated. (If you used the `minikube docker-env`, skip this and go to the Kubernetes deployment directly.)
`docker images`
REPOSITORY                    TAG                 IMAGE ID            CREATED             SIZE
wso2inc/hello                       v0.1.0              60d95f0928b2        About a minute ago   228MB
# Run the generated Docker image.
`docker run -d -p 9090:9090 wso2inc/hello:v0.1.0`
c04194eb0b4d0d78cbc8ca55e0527d381d8ab4a1a68f8ea5dd3770a0845d5fbb
# Access the service.
`curl http://localhost:9090/helloWorld/sayHello`
Hello, World from service helloWorld !
# Execute the Kubernetes service now.
`kubectl apply -f /home/wso2/project/target/kubernetes/hello-0.1.0`
service/helloep-svc created
deployment.apps/wso2-hello-0--deployment created
horizontalpodautoscaler.autoscaling/wso2-hello-0--hpa created
# Verify the Kubernetes pods.
`kubectl get pods`
NAME                                          READY   STATUS    RESTARTS   AGE
wso2-hello-0--deployment-7d4d56457b-7jlzx   1/1     Running   0          57s
# Expose via Nodeport to test in the developer environment.
`kubectl expose deployment wso2-hello-0--deployment --type=NodePort --name=wso2-hello-0--svc-local`
service/wso2-hello-0--svc-local exposed
# Get the IP address and port of the Kubernetes service.
`kubectl get svc`
NAME                        TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
wso2-hello-0--svc-local   NodePort    10.111.61.112    <none>        9090:32437/TCP   4m17s
# If you are using Minikube, the IP address should be changed according to the output of the `minikube ip` command.
`minikube ip`
192.168.49.2
# Access the deployed service via cURL.
`curl http://192.168.49.2:32437/helloWorld/sayHello`
Hello, World from service helloWorld !