Skip to content
atiqbitstream edited this page Mar 10, 2025 · 1 revision

Welcome to the MicroDeployCE wiki!

Deploying a Microservices-Based Architecture

๐ŸŽฏ Goal:

  • Deploy multiple microservices using IBM Cloud Code Engine
  • Understand containerized microservices deployment
  • Set up an API Gateway to route traffic between services

๐Ÿ› ๏ธ Step 1: Define Your Microservices

We'll deploy two microservices:

  1. User Service โ€“ Manages user accounts
  2. Order Service โ€“ Handles customer orders

Each microservice will be a Node.js + Express API.


๐Ÿ“ Step 2: Create the User Service

  1. Create a directory for the User Service
  `mkdir user-service && cd user-service` 
  1. Initialize a Node.js project
   npm init -y
  1. Install Express
  npm install express
  1. Create server.js
 `const express = require("express");
 const app = express();
 
 app.get("/users", (req, res) => {
     res.json([{ id: 1, name: "John Doe" }, { id: 2, name: "Jane Doe" }]);
 });
 
 app.listen(8080, () => console.log("User Service running on port 8080"));
  1. Create a Dockerfile
   `FROM node:16
   WORKDIR /app
   COPY package.json ./
   RUN npm install
   COPY . .
   CMD ["node", "server.js"]
  1. Build and push the container image to IBM Cloud Container Registry (ICR)
   ibmcloud cr login
   docker build -t us.icr.io/mycodeengine/user-service:latest .
   docker push us.icr.io/mycodeengine/user-service:latest
  1. Deploy the User Service on Code Engine

    sh

    CopyEdit

    ibmcloud ce application create --name user-service --image us.icr.io/mycodeengine/user-service:latest

  2. Get the public URL and test the service

   ibmcloud ce application get --name user-service --output url

๐Ÿ“ Step 3: Create the Order Service

Follow the same steps as above but replace "user-service" with "order-service".

The Order Service API (server.js):

const express = require("express");
const app = express();

app.get("/orders", (req, res) => {
   res.json([{ id: 101, item: "Laptop" }, { id: 102, item: "Phone" }]);
});

app.listen(8080, () => console.log("Order Service running on port 8080"));

Once done, build and push the image:

`docker build -t us.icr.io/mycodeengine/order-service:latest .
docker push us.icr.io/mycodeengine/order-service:latest

Deploy on IBM Code Engine:

`ibmcloud ce application create --name order-service --image us.icr.io/mycodeengine/order-service:latest

We'll use Kong API Gateway deployed on IBM Cloud Kubernetes Service (IKS) to manage traffic between microservices.


๐Ÿš€ Step 4: Setting Up Kong API Gateway on IBM Cloud Kubernetes Service (IKS)

๐ŸŽฏ Goal:

  • Deploy Kong API Gateway on IKS
  • Route requests from a single public endpoint to User Service and Order Service
  • Secure API requests

๐Ÿ› ๏ธ Step 1: Set Up IBM Cloud Kubernetes Cluster

  1. Create a Kubernetes cluster on IBM Cloud
  ibmcloud ks cluster create classic --name my-iks-cluster

๐Ÿ“Œ Wait for the cluster to be provisioned. You can check its status:

    `ibmcloud ks cluster ls
  1. Set up your local environment to use IKS
   ibmcloud ks cluster config --cluster my-iks-cluster
   kubectl get nodes

๐Ÿ› ๏ธ Step 2: Deploy Kong API Gateway

  1. Install Helm (if not installed)
  curl https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash
  1. Add the Kong Helm repository
   helm repo add kong https://charts.konghq.com
   helm repo update
  1. Install Kong Gateway on IKS
  helm install kong kong/kong --set ingressController.installCRDs=false --set proxy.type=LoadBalancer
  1. Verify Kong is running
kubectl get pods -n default
  1. Get the public IP of Kong Gateway
  kubectl get svc kong-kong-proxy

๐Ÿ”น Note the EXTERNAL-IP, this will be used as the API Gateway URL.


๐Ÿ› ๏ธ Step 3: Configure Routes in Kong API Gateway

Now, we will configure Kong to route traffic to User Service and Order Service.

  1. Add User Service route
   curl -i -X POST http://<KONG_EXTERNAL_IP>:8001/services \
     --data "name=user-service" \
     --data "url=http://<USER_SERVICE_URL>"
   curl -i -X POST http://<KONG_EXTERNAL_IP>:8001/services/user-service/routes \
     --data "paths[]=/users"
  1. Add Order Service route
   curl -i -X POST http://<KONG_EXTERNAL_IP>:8001/services \
     --data "name=order-service" \
     --data "url=http://<ORDER_SERVICE_URL>"
   curl -i -X POST http://<KONG_EXTERNAL_IP>:8001/services/order-service/routes \
     --data "paths[]=/orders"

๐Ÿ› ๏ธ Step 4: Test the API Gateway

  1. Check if the User Service is accessible via Kong
   curl http://<KONG_EXTERNAL_IP>/users
  1. Check if the Order Service is accessible via Kong
  curl http://<KONG_EXTERNAL_IP>/orders

Great! Now, let's secure our APIs by adding rate limiting and authentication to Kong API Gateway. ๐Ÿ”’


Step 5: Securing APIs with Rate Limiting & Authentication

** Goal:**

  • Enable Rate Limiting to prevent abuse
  • Implement API Key Authentication for security

** Step 1: Enable Rate Limiting on API Endpoints**

To prevent excessive requests, weโ€™ll limit each client to 5 requests per minute.

  • Access Admin API via kubectl port-forward:
    kubectl port-forward svc/kong-kong-admin 8001:8444
  • Use https:// and -k (skip SSL verification):
    curl -k https://localhost:8001/services
  1. Enable Rate Limiting Plugin on User Service
   `curl -i -X POST https://<KONG_EXTERNAL_IP>:8001/services/user-service/plugins \
     --data "name=rate-limiting" \
     --data "config.minute=5" \
     --data "config.policy=local"
  1. Enable Rate Limiting Plugin on Order Service
   curl -i -X POST https://<KONG_EXTERNAL_IP>:8001/services/order-service/plugins \
     --data "name=rate-limiting" \
     --data "config.minute=5" \
     --data "config.policy=local"
  1. Test the Rate Limiting

    • Make 5 requests in a row:
       for i in {1..5}; do curl -i https://<KONG_EXTERNAL_IP>/users; done
  • Try one more request (should be blocked):
       curl -i https://<KONG_EXTERNAL_IP>/users
  • The response should be "429 Too Many Requests".

๐Ÿ› ๏ธ Step 2: Secure APIs with API Key Authentication

Weโ€™ll now require API keys to access services.

  1. Enable Key Authentication Plugin for User Service
    curl -k -i -X POST https://<KONG_EXTERNAL_IP>:8001/services/user-service/plugins \
      --data "name=key-auth"
  1. Enable Key Authentication Plugin for Order Service
    `curl -k -i -X POST https://<KONG_EXTERNAL_IP>:8001/services/order-service/plugins \
      --data "name=key-auth"
  1. Create an API Key for a Client
  curl -k -i -X POST https://<KONG_EXTERNAL_IP>:8001/consumers/ \
      --data "username=myclient"
    curl -i -X POST http://<KONG_EXTERNAL_IP>:8001/consumers/myclient/key-auth

Save the returned API key. ๐Ÿ”‘

  1. Test API without API Key (Should be blocked)
 curl -k -i https://<KONG_EXTERNAL_IP>/users

You should get "401 Unauthorized".

  1. Test API with API Key (Should work)
  curl -k -i -H "apikey: YOUR_GENERATED_API_KEY" http://<KONG_EXTERNAL_IP>/users