How to deploy a RESTful API Application in Kubernetes

Read Time:3 Minute, 26 Second

How to deploy a RESTful API Application in Kubernetes

Introduction

Kubernetes is a powerful DevOps tool for achieving scalable software applications deployments. Kubernetes is a tool for automating the deployment of multiple instances of an application, scaling, updating and monitoring the health of deployments.

This article will help you deploy your REST API in Kubernetes. First, you’ll need to set up a local Kubernetes cluster, then create a simple API to deploy.

 

Set Up Local Kubernetes

There’s a couple options for running Kubernetes locally, with the most popular ones including minikube, k3s, kind, microk8s. In this guide, any of these will work, but we will be using k3s because of the lightweight installation.

Install k3d, which is a utility for running k3s. k3s will be running in Docker, so make sure you have that installed as well.

curl -s https://raw.githubusercontent.com/rancher/k3d/main/install.sh | bash

 

        

Create a Simple API

Create a simple API using Express.js.

mkdir my-backend-api && cd my-backend-api

touch server.js

npm init

npm i express –saveq

// server.js

const express = require(“express”);

const app = express();

app.get(“/user/:id”, (req, res) => {

  const id = req.params.id;

  res.json({

    id,

    name: John Doe #${id}

  });

});

app.listen(80, () => {

  console.log(“Server running on port 80”);

});

Deploy

Now, deploy the image to your local Kubernetes cluster. Use the default namespace.

Create a deployment:

kubectl create deploy my-backend-api –image=andyy5/my-backend-api

Check that everything was created and the pod is running:

kubectl get deploy -A

kubectl get svc -A

kubectl get pods -A

Once the pod is running, the API is accessible within the cluster only. One quick way to verify the deployment from our localhost is by doing port forwarding:

Replace the pod name below with the one in your cluster

kubectl port-forward my-backend-api-84bb9d79fc-m9ddn 3000:80

Now, you can send a curl request from your machine

curl http://localhost:3000/user/123

 

Manage external access in a cluster

To correctly manage external access to the services in a cluster, we need to use ingress. Close the port-forwarding and let’s expose our API by creating an ingress resource.

An ingress controller is also required, but k3d by default deploys the cluster with a Traefik ingress controller (listening on port 80).

Create an Ingress resource with the following YAML file:

kubectl create -f ingress.yaml

kubectl get ing -A

// ingress.yaml

apiVersion: networking.k8s.io/v1

kind: Ingress

metadata:

  name: my-backend-api

  annotations:

    ingress.kubernetes.io/ssl-redirect: “false”

spec:

  rules:

  – http:

      paths:

      – path: /user/

        pathType: Prefix

        backend:

          service:

            name: my-backend-api

            port:

              number: 80

Final thoughts

As one can see it is quite quick to let the traffic in or even deploy an autoscaling cluster onto Google Cloud Platform. There are some caveats mostly resulting from a different structure of a production cluster and a cluster one would run locally to test their applications. Without an ingress controller, like NGINX, there are a number of extra steps to bear in mind. With minikube clusters, one needs to enable ingress and when it comes to Docker Desktop one shall make sure the service resource is a LoadBalancer. Implementing an ingress controller seems to be the most suitable option as there is little overhead when moving from local deployments to production deployments

Source: https://blog.educationecosystem.com/how-to-deploy-a-restful-api-application-in-kubernetes/

Tag Cloud

Java Java Logical Programs OTP Generation in Java python Recursion youtube video ASCII Upper and Lower Case blockchain javascript graph learn to code software development Successful Software Engineers breadth first search Java Array Programs Java Programs Uncategorized android ios programming kotlin web-development django data sql cybersecurity database swiftui serverless aws swift rust react background-position gradients loader mask grid nth-child pseudo elements indieweb WordPress Print Array without brackets C++ factorial Java String Programs Final Keyword Static Variable Axie Infinity Cryptokitties NFT games tool inserting MISC Tips Codes python code python projects python3 system info python project Bigginers How to Do Integrations Payment Gateways PHP checkout page in php Implement stripe payment gateway in Step by step in PHP integrate stripe gatway in php mysql payment gateway integration in php step by step payment gateway integration in php step by step with source code payment gateway integration in website PHP Integrate Stripe Payment Gateway Tutorial PHP shopping cart checkout code shopping cart in php stripe php checkout PHP/MySQL/JSON best international payment gateway does google pay accept international payments how to accept international payments in india paytm payment gateway razorpay codeigniter github razorpay custom checkout github razorpay get payment details razorpay integration in codeigniter github razorpay international payments Razorpay payment gateway integration in CodeIgniter razorpay payment gateway integration in php code Razorpay payment gateway integration with PHP and CodeIgniter Razorpay payment gateway setup in CodeIgniter Library & Frameworks Tips & Tricks UI/UX & Front-end coding birds online html code for google sign in login with google account in PHP login with google account using javascript login with google account using javascript codeigniter login with google account using php login with google account using php source code
Weekly Quiz Winners Previous post Weekly Quiz Winners
iShadeed’s Container Queries Lab Next post iShadeed’s Container Queries Lab

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.