How to Deploy on Kubernetes in Simple Words | by Pedram Ataee, PhD | Towards Data Science

I recently developed a contextualized most-similar word API service named Owl hosted on RapidAPI. It was a great journey for me so I decided to share various stages of its development with you. In this article, I want to share a step-by-step guide (Create, Connect, Deploy, and Expose) on how to deploy a containerized web application using Kubernetes. You can use this guideline in your data science projects as well as any other web applications that you work on. If you want to read more about the OWL API, you can read this article: How to Compute Word Similarity — A Comparative Analysis.
In this article, I want to share a step-by-step guide (Create, Connect, Deploy, and Expose) on how to deploy a containerized web application using Kubernetes.
During my journey to build the OWL API, I have faced many NLP challenges in order to build a better “most-similar word” service comparing to the current solutions. Beyond those challenges, I needed to deploy the OWL API such that it can manage a large number of API requests. I could have used a simple VPS (virtual private server) for deployment but I was certain that I will face scalability issues soon. That is why I decided to use Kubernetes technology.
Kubernetes is an open-source container orchestrator for the scalable deployment of containerized applications.
So, I assume you already have a containerized application ready to deploy. The question that you have is how to deploy it such that it can scale up when needed with minimum involvement.
You must create a Kubernetes cluster using the control panel of the cloud infrastructure that you work with. I have worked with OVH Cloud and Google Cloud recently. The process of creating Kubernetes clusters on both is straightforward but different. You should find their own guides where they described it thoroughly. For example, you can find the OVH guide to create a cluster in the link below.
docs.ovh.com
OVH is a French cloud computing company that offers VPS, dedicated servers, and other web services. They are one of the largest cloud providers in the world but not as famous as GCP or AWS. I also didn’t know them till several months ago but I highly recommend them since the OVH interface is user-friendly and its cost is more reasonable than its competitors such as AWS or GCP. If you don’t have a good reason to use AWS or GCP, I highly recommend the OVH Cloud.
To make sure your Kubernetes cluster can scale up when needed make sure to enable the autoscaling option in this step. Plus, choose the right number of nodes that you want to run in your cluster. The monthly cost would increase by an increase in the number of nodes that you add to the cluster.
The Kubernetes service that you can get in OVH is a Managed Kubernetes Service that mostly means scaling is automatedly managed when needed. The magic is done by a node called LoadBalancer, a node that distributes all the arriving tasks over a set of resources to make the overall process works more efficiently. There are different strategies for LoadBalancing that are not the focus of this article. So, I don’t describe it more here.
After creating a Kubernetes cluster, you must be able to download a “key” from the control panel to connect to the cluster from a remote machine. The key is mostly a YAML file that contains everything you need (e.g., cluster address and required credentials) to connect to the Kubernetes cluster. You don’t change or modify any value in this file. So, you just need to find out where must be downloaded from the control panel.
You need this file in order to build an authenticated connection between a remote machine and the Kubernetes cluster. The format of this key is different among cloud infrastructures such as GCP or OVH which is not important at this stage. The only thing that matters is that you must save this file in a safe place and set the value of the KUBECONFIG environment variable to the address of this file. If you use a Linux terminal, you can use the code below set the value of KUBECONFIG.
export KUBECONFIG=\\path\\to\\config\\kubeconfig.yml
As soon as you set the value of KUBECONFIG environment variable to the address of the corresponding file, you can start working with the Kubernetes cluster. The Kubernetes command-line tool or kubectl is all you need to start working with the cluster. The kubectl allows you to run commands against the Kubernetes cluster, e.g., to deploy applications or inspect cluster resources. How to install kubectl though? You can read this article written by its creators.
The kubectl (i.e., Kubernetes command-line tool) allows you to run commands against the Kubernetes cluster, e.g., to deploy applications or inspect cluster resources.
In the previous steps, you created a Kubernetes cluster and built an authenticated connection to work with it. Now, you must deploy a containerized application on the cluster.
Don’t get confused, please! The “manifest” is just a YAML file where the deployment details such as “how to map external ports?” or “how to set environment variables?” are determined. You can use the code below as a template for the deployment manifest.
apiVersion: v1kind: Servicemetadata:    name: MY-SERVICE    labels:        app: MY-APPspec:    type: LoadBalancer  ports:  - port: 80    targetPort: 80    protocol: TCP    name: http  - port: 443    targetport: 443    protocol: TCP    name: https  selector:    app: MY-APPapiVersion: apps/v1kind: Deploymentmetadata:    name: MY-DEPLOYMENT  labels:    app: MY-APPspec:    replicas: 1  selector:    matchLabels:      app: MY-APP  template:    metadata:      labels:        app: MY-APP    spec:      containers:      - name: MY-APP        image: IMAGE-NAME        ports:        - containerPort: 80
Several notes on the above deployment manifest are below.
Ports 80 and 443 are opened on the LoadBalanacer node. So, if you want to run the web application on https the corresponding port (i.e., 443) is open.
The MY-SERVICE , MY-APP and MY-DEPLOYMENT must be replaced with the names that you want to use in the project. What you assign here will be used in step 4 where you want to expose the cluster to the internet.
The IMAGE-NAME must be replaced with the complete name/address of the containerized application that you want to deploy. If you need to change the value of this variable on the fly, declare it as an environment variable (i.e., $IMAGE-NAME) and use the envsubst library.
To deploy, you must submit the deployment manifest file, described above, to the Kubernetes cluster using kubectl . For the first round of deployment, you must use kubectl apply command while in the next rounds of deployment you must use kubectl replace command instead. Why? Since you basically replace a new version with what already exists on the cluster! You can use the code below to submit a replace command.
kubectl replace -f path/to/manifest/manifest.yml
After you have done this step, run kubectl get services. If the service gets deployed properly, you will see some results like below.
NAME         TYPE         CLUSTER-IP   EXTERNAL-IP  PORT(S)     AGE kubernetes   ClusterIP     X.X.X.X     <none>       443/TCP      XMY-SERVICE   LoadBalancer  X.X.X.X     <none>          X         X
Congratulation! You are almost done. In step 3 you deployed the application. The only thing that remains is to expose the LoadBalancer node to the internet. In other words, you should get a public IP for the LoadBalancer node to let the traffic goes through it. Try the code below to open the internet traffic into the LoadBalancer node.
kubectl expose deployment MY-DEPLOYMENT --type=LoadBalancer --name=MY-SERVICE
It takes several seconds to get the Public IP. Then, try kubectl get services to find out the external IP that the service is available at! Now, you are completely done with the deployment process! 🎉
In various scenarios, you may need to delete the Kubernetes cluster. For example, you want to deploy the latest version and you do everything according to the book. However, the deployment doesn’t go through. In this case, there is a good chance the cluster has been locked in an unhealthy state that can not respond properly to your commands. You must delete the cluster. To delete a Kubernetes cluster you must delete both Service and Deployment objects. You can use commands such kubectl delete deployments MY-DEPLOYMENT or kubectk delete services MY-SERVICE .
Note that the deletion process always have some unwanted consequences such as changes in the external IP of the LoadBalancer node. So, always be cautious when you decide to delete a cluster.
One last thing. In this article, I aimed to help you deploy your application on a Kubernetes cluster as fast as possible. However, there is much more into Kubernetes that you can find in their official documentation. Hope you enjoyed this article!
If you like this post and want to support me...
Follow me on Medium!
Check out my books on Amazon!
Become a member on Medium!
Connect on Linkedin!
Follow me on Twitter!