Building Serverless Kubernetes Clusters for Serverless Jobs

Image for post
Image for post

By Afzaal Ahmad Zeeshan.

In this article, I will describe the difference between offerings of serverless clusters and the serverless jobs that can run on managed clusters, and which type of the subscription is cost-effective for small-medium businesses and enterprises.

Service orchestration is the primary focus of cloud-native solutions and enterprises that deploy their solutions on private or public clouds. Alibaba Cloud offers several hosting options, and many deployment models are supported to enable customers to fully utilize the cloud. But this sometimes is not the case, as many customers are unaware of the potential of the cloud, and sometimes they do not use the right architectural design for their solutions. Think of this, many people use the lift-and-shift approach to migrate their legacy solutions from a VM-based hosting platform to a cloud; say Alibaba Cloud or any other cloud in this manner.

Going Serverless

  1. Your jobs only run at a specific time.
  2. Your jobs face a peak hour at a special occasion or time.
  3. Your jobs are trigger-based and only handle events as they come.
  4. Your solution and its internal services are asynchronous in nature.

In these cases, even microservices sometimes fail to provide the best solution on a cloud-hosted environment. This article will discuss the benefits of using Serverless Container Service for Kubernetes for Serverless jobs.

Serverless Jobs

A typical web application contains web route handlers, views, models and database manager. Apart from these services, the components for logging, backups, availability also make up a minor part of the application. In a microservices environment, each of these components is broken down and deployed separately. This helps achieve high-availability and better developer experience because your teams can work separately on the projects. You should check this blog out to learn more about the differences between monolith, microservices and serverless application development architectures.

Although Alibaba Cloud offers several platforms and hosting solutions for serverless jobs, especially the Alibaba Cloud Function Compute, it offers Node.js, Python and other SDKs. But we will focus on the containers-based jobs and the serverless approaches taken by the infrastructure too.

Serverless Infrastructure

This is what you get when you are creating a managed Kubernetes cluster on Alibaba Cloud:

Image for post
Image for post

As you see, you specify the number of nodes in the cluster. Note that in a pay-as-you-go model you can change the number of the nodes, but this require a manual configuration for the node count change, or you might need to use the Alibaba Cloud SDK to manage it manually.

On the other hand, if you select to create a serverless instance of the Kubernetes cluster, that would support your workloads in a serverless fashion. Note that you will not be asked for the node count in this.

Image for post
Image for post

The maximum count for this cluster would be 100. But the plus point of this approach is to support workloads of high demand, and shrink the cluster size down to zero, to prevent any charges.

Create a Serverless Kubernetes Cluster

I recommend that you select the recommended configurations and enable the logging, backups, internet access for the API.

Image for post
Image for post

Create the resource, you will be charged for any resources that are created as a part of this cluster. For example, the Private Zone service on Alibaba Cloud is a paid resource.

Image for post
Image for post

You will be shown this dialog before you create the resources. Check the pricing information for the resources to make sure that you have enough resources and credits to try out or to be charged for the services on your payment options. After you purchase the service, you will be shown the status of the resources being deployed.

Image for post
Image for post

Alibaba Cloud uses Resource Orchestration Service (ROS) to orchestrate the services here, and as the warning message tells, you should not delete the resources (compute, storage or network etc.) manually. You can have your resources deleted by the service itself when you delete the Kubernetes cluster. After a few minutes, your cluster would be available on the portal and you can access it to start deployments.

Image for post
Image for post

This Kubernetes cluster would contain only the bare Kubernetes services, and not containers/deployments or services. It will be your responsibility to create the services. Alibaba Cloud will then orchestrate them, and automatically scale the resources to accommodate the demands.

Connecting to the cluster

Image for post
Image for post

You should copy it under a “.kube” directory in your user account directory in a file named “config”. For example, on a Linux environment it has to be at “~/.kube/config”. On Windows platform you should keep it at your profile and then follow the directory path.

Note that the API is safeguarded by a certificate authority, so anyone with the access to the configuration of Kubernetes would be able to control your Kubernetes cluster — disable this feature if you do not wish to allow public access to these resources. You can always control the services from within the portal.

I have created a repository on GitHub that you can use to perform certain actions on Kubernetes clusters, such as creating the Pods, creating Deployments, exposing them as Services and much more. You can access the repository at, https://github.com/afzaal-ahmad-zeeshan/hello-kubernetes. I will use a file from the repository to create the deployment and expose a service and see how Alibaba Cloud automatically create them for us.

First you need to verify that everything is okay, after you copy the configuration content on your machine and install the kubectl command line utility, execute the following command to verify that your cluster is connected to your machine.

$ kubectl version

This would yield the version information of server and client.

Image for post
Image for post

Now that you have connected to the cluster, next step is to create a deployment. I will use the code as I have previously mentioned,

Image for post
Image for post

You would use the “hello-world/deployment.yaml” file from the repository. This file contains a deployment and a service that exposes the deployment. The deployment is a basic Node.js based web application.

apiVersion: apps/v1
kind: Deployment
metadata:
name: express-nodejs-deployment
labels:
runtime: nodejs
spec:
replicas: 3
selector:
matchLabels:
name: express-nodejs-pod
template:
metadata:
labels:
name: express-nodejs-pod
spec:
containers:
- name: express-nodejs
image: afzaalahmadzeeshan/express-nodejs:latest
ports:
- containerPort: 5000
resources:
limits:
memory: 256Mi
cpu: 250m
---
apiVersion: v1
kind: Service
metadata:
name: express-nodejs-service
spec:
ports:
- port: 80
targetPort: 5000
selector:
name: express-nodejs-pod

The key point to note here is that our deployment exposes the resources that it requires. You can explore the currently deployed pods and services on the Alibaba Cloud portal as well.

Image for post
Image for post

You can further explore the deployment and the number of pods in it by exploring the deployment.

Image for post
Image for post

The file that I used to create the deployment listed only 3 pods, so we see that we have those 3 pods created ¨C they are in pending state, since Kubernetes needs to pull the container images from Docker Hub and then deploy them on the cluster.

Your deployments also use a service to be exposed to the internet, you can find all the services currently exposed on the cloud under the Services tab.

Image for post
Image for post

The second service is the default service that allows us to connect to the Kubernetes cluster, and provides connection to other components of the cluster. Each time we create a new resource, Alibaba Cloud will automatically provision extra resources for us, and shrink the infrastructure down when no longer needing it.

Connecting using Alibaba Cloud Shell

Image for post
Image for post

The command-line interface is same, in case of Alibaba Cloud CLI you will not have access to local directories, so you would need to use Git version control to access the Kubernetes objects and the files ¨C but you can easily manage that by providing fully qualified URLs from a public GitHub repository.

Serverless on Kubernetes

Since our clusters are serverless, and the jobs and deployments are serverless in nature, we can easily achieve a complete serverless deployment on the cloud that provides (almost) unlimited scale for the resources and cuts the resources down to zero when they are no longer needed.
hs;

Original Source:

Follow me to keep abreast with the latest technology news, industry insights, and developer trends.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store