<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Lakmal Warusawithana</title>
    <description>The latest articles on DEV Community by Lakmal Warusawithana (@lakwarus).</description>
    <link>https://dev.to/lakwarus</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F245582%2F827be581-3f55-4448-a690-5734886ddfeb.jpg</url>
      <title>DEV Community: Lakmal Warusawithana</title>
      <link>https://dev.to/lakwarus</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/lakwarus"/>
    <language>en</language>
    <item>
      <title>Code to Kubernetes</title>
      <dc:creator>Lakmal Warusawithana</dc:creator>
      <pubDate>Thu, 27 Feb 2020 19:22:45 +0000</pubDate>
      <link>https://dev.to/lakwarus/code-to-kubernetes-2cjp</link>
      <guid>https://dev.to/lakwarus/code-to-kubernetes-2cjp</guid>
      <description>&lt;p&gt;In the early 2000s, I was fascinated with Linux. I tried to install Linux everywhere: laptops, notebooks, MAC minis, PS2, LiveCD, PDAs, and POS terminals! But by the late 2000s, Linux became boring — it was now the default operating system to run servers. People moved on to virtual machines, where the OS didn’t matter.&lt;/p&gt;

&lt;p&gt;Then, in the early 2010s, I started looking into running our workload on containers. I spent a lot of time experimenting with LXC and OpenStack and exploring new Linux kernel features like cgroups and namespace. In the mid-2010s, Docker announced its first public release. Its layered file system, built-in registry, and simplified CLI exploded the industry. Everyone started talking about Docker. No one wanted to understand how it works, but instead, they just started working with it. &lt;/p&gt;

&lt;p&gt;With the emergence of microservices running the workload on a single container also became boring. Applications now needed to be deployed to multiple containers. Container orchestration, load balancing, auto-healing, monitoring, and management became interesting areas of research. Docker Swarm, DC/OS, and Kubernetes are a few projects in this space. I was lucky enough to be exposed to Kubernetes in its early stages and started to love it. I was fascinated by its flexible architecture and how it managed networking and routing and soon became a strong advocate. Now that we are in 2020 I think Kubernetes has won the battle and in my point of view is the next-gen Linux to run distributed workloads.&lt;/p&gt;

&lt;h2&gt;
  
  
  But through all the eras, developers have been neglected
&lt;/h2&gt;

&lt;p&gt;Deployment options like Linux, virtual machines, containers, and Kubernetes are not a part of the developer’s programming experience. Developers love their coding and should ideally focus on solving business problems. We have tried to fill the gap between development and deployment by introducing DevOps, but, in practice, these two worlds live separately.&lt;/p&gt;

&lt;p&gt;So how can we make Kubernetes boring and enhance the developer experience? After some out-of-the-box thinking into how we can distinctively connect these two worlds, Ballerina Kubernetes annotations were born. &lt;a href="https://ballerina.io/?utm_source=devto&amp;amp;utm_medium=article&amp;amp;utm_campaign=kubernetes_ballerina_devto_article_feb20"&gt;Ballerina&lt;/a&gt; is an open source programming language for cloud-era application developers. It is intended to be the core of a language-centric middleware platform. It has all the general-purpose functionality expected of a modern programming language, but it also has several unusual aspects that make it particularly suitable for its intended purpose.&lt;/p&gt;

&lt;p&gt;Note: I will only talk about how to run Ballerina applications on Kubernetes. If you are interested in the other capabilities of Ballerina please visit &lt;a href="https://ballerina.io/?utm_source=devto&amp;amp;utm_medium=article&amp;amp;utm_campaign=kubernetes_ballerina_devto_article_feb20"&gt;http://ballerina.io&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Ballerina Kubernetes Annotation
&lt;/h2&gt;

&lt;p&gt;Developers would prefer to stay within their IDE instead of writing Docker files, generating Docker images, and writing Kubernetes YAML files. But running their application on Kubernetes and testing production behavior is important. Unfortunately, running an application in a Kubernetes cluster currently requires a steep learning curve for developers. &lt;/p&gt;

&lt;p&gt;The Ballerina Kubernetes annotation model was created to address this issue. A simple hello world sample can be used to understand the concepts, but these annotations can be used in complex examples as well. Let’s take the service below, which is written in Ballerina as an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import ballerina/http;
import ballerina/log;

service hello on new http:Listener(8080) {
   resource function hi(http:Caller caller, http:Request request) {
       var result = caller-&amp;gt;respond("Hello World!\n");
       if result is error {
           log:printError("Error occurred while sending response");
       }
   }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The sample program has a &lt;code&gt;hello&lt;/code&gt; HTTP service and within the service, there is a &lt;code&gt;hi&lt;/code&gt; resource function. The hello service is listening to port 8080 and when we invoke the hi resource it returns the “Hello World” HTTP response. When the source code is compiled it will generate a JAR. This will enable users to run the program with the java -jar command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$&amp;gt;&lt;/span&gt; ballerina build hello.bal 
Compiling &lt;span class="nb"&gt;source
    &lt;/span&gt;hello.bal
Generating executables
    hello.jar
&lt;span class="nv"&gt;$&amp;gt;&lt;/span&gt; java &lt;span class="nt"&gt;-jar&lt;/span&gt; hello.jar 
&lt;span class="o"&gt;[&lt;/span&gt;ballerina/http] started HTTP/WS listener 0.0.0.0:8080
&lt;span class="nv"&gt;$&amp;gt;&lt;/span&gt; curl http://localhost:8080/hello/hi
Hello World!
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;To annotate the above source code with Ballerina Kubernetes annotations, the ballerina/kubernetes module needs to be imported. Then the following annotations need to be added.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import ballerina/http;
import ballerina/log;
import ballerina/kubernetes;

@kubernetes:Service {
   name: "hello-world",
   serviceType: "NodePort"
}
@kubernetes:Deployment {
   name: "hello-world"
}
service hello on new http:Listener(8080) {
   resource function hi(http:Caller caller, http:Request request) {
       var result = caller-&amp;gt;respond("Hello World!\n");
       if result is error {
           log:printError("Error occurred while sending response");
       }
   }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The two annotations added above with a few properties are &lt;code&gt;@kubernetes:Deployment&lt;/code&gt; and &lt;code&gt;@kubernetes:Service&lt;/code&gt;. As shown below, once the code is compiled the Ballerina compiler will generate the Dockerfile, Docker image, and all necessary artifacts required to deploy the application.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$&amp;gt;&lt;/span&gt; ballerina build hello.bal 
Compiling &lt;span class="nb"&gt;source
    &lt;/span&gt;hello.bal

Generating executables
    hello.jar

Generating artifacts...

    @kubernetes:Service         - &lt;span class="nb"&gt;complete &lt;/span&gt;1/1
    @kubernetes:Deployment      - &lt;span class="nb"&gt;complete &lt;/span&gt;1/1
    @kubernetes:Docker          - &lt;span class="nb"&gt;complete &lt;/span&gt;2/2 
    @kubernetes:Helm            - &lt;span class="nb"&gt;complete &lt;/span&gt;1/1

    Run the following &lt;span class="nb"&gt;command &lt;/span&gt;to deploy the Kubernetes artifacts: 
    kubectl apply &lt;span class="nt"&gt;-f&lt;/span&gt; /Users/lakmal/hello/kubernetes

    Run the following &lt;span class="nb"&gt;command &lt;/span&gt;to &lt;span class="nb"&gt;install &lt;/span&gt;the application using Helm: 
    helm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;--name&lt;/span&gt; hello-world /Users/lakmal/hello/kubernetes/hello-world

&lt;span class="nv"&gt;$&amp;gt;&lt;/span&gt; docker images |grep hello
hello    latest        278a34c943fd        10 minutes ago      109MB
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;kubectl&lt;/code&gt; command can be used to deploy your application into a Kubernetes cluster.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$&amp;gt;&lt;/span&gt; kubectl apply &lt;span class="nt"&gt;-f&lt;/span&gt; /Users/lakmal/Documents/work/demo/DevWeekTX/kubernetes
service/hello-world created
deployment.apps/hello-world created

&lt;span class="nv"&gt;$&amp;gt;&lt;/span&gt; kubectl get all
NAME                               READY   STATUS    RESTARTS   AGE
pod/hello-world-6ff4b986f7-68b47   1/1     Running   0          15s

NAME                  TYPE        CLUSTER-IP      EXTERNAL-IP   PORT&lt;span class="o"&gt;(&lt;/span&gt;S&lt;span class="o"&gt;)&lt;/span&gt;          AGE
service/hello-world   NodePort    10.103.148.96   &amp;lt;none&amp;gt;        8080:31686/TCP   15s
service/kubernetes    ClusterIP   10.96.0.1       &amp;lt;none&amp;gt;        443/TCP          21d

NAME                          READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/hello-world   1/1     1            1           15s

NAME                                     DESIRED   CURRENT   READY   AGE
replicaset.apps/hello-world-6ff4b986f7   1         1         1       15s
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Since the &lt;code&gt;serviceType&lt;/code&gt; has been set to NodePort in the @kubernetes:Service annotation, it creates a NordPort type Kubernetes service. The application can be accessed via NodePort as shown below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$&amp;gt;&lt;/span&gt; curl http://localhost:31686/hello/hi
Hello World!
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;As you can see, using Ballerina annotations doesn’t interrupt the developer’s flow. The developer has to just code the annotations and the compiler takes care of the rest. Ballerina Kubernetes annotation reads the source code, populates the correct values and generates validated Kubernetes artifacts all while following best practices. The default values can be overridden by setting them as annotation properties. The IDE can even suggest what to put and where to put it in your source code. For example, see the following IDE suggestions under the @kubernetes:Deployment annotation:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mrJ5bs3d--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/q62mxp9evrdii9f7gws9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mrJ5bs3d--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/q62mxp9evrdii9f7gws9.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Ballerina annotations can generate artifacts for all major Kubernetes &lt;code&gt;kinds&lt;/code&gt; such as&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;@kubernetes:Deployment{}&lt;/li&gt;
&lt;li&gt;@kubernetes:Service{}&lt;/li&gt;
&lt;li&gt;@kubernetes:Ingress{}&lt;/li&gt;
&lt;li&gt;@kubernetes:HPA{}&lt;/li&gt;
&lt;li&gt;@kubernetes:Secret{}&lt;/li&gt;
&lt;li&gt;@kubernetes:ConfigMap{}&lt;/li&gt;
&lt;li&gt;@kubernetes:PersistentVolumeClaim{}&lt;/li&gt;
&lt;li&gt;@kubernetes:ResourceQuota{}&lt;/li&gt;
&lt;li&gt;@kubernetes:Job{}&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It also support Istio and OpenShift artifact generation as well:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;@istio:Gateway{}&lt;/li&gt;
&lt;li&gt;@istio:VirtualService{}&lt;/li&gt;
&lt;li&gt;@openshift:Route{}&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;More information on this can be found at &lt;a href="https://github.com/ballerinax/kubernetes"&gt;https://github.com/ballerinax/kubernetes&lt;/a&gt;. To quickly learn about the language features in Ballerina, check out these &lt;a href="https://ballerina.io/learn/by-example/?utm_source=devto&amp;amp;utm_medium=article&amp;amp;utm_campaign=kubernetes_ballerina_devto_article_feb20"&gt;Ballerina by Examples&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>ballerina</category>
      <category>kubernetes</category>
      <category>cloud</category>
      <category>devops</category>
    </item>
  </channel>
</rss>
