Deploying Microgateway as Kubernetes Sidecar Connected to API Gateway
Tags: kubernetes, tutorial, microgateway
Published: 2021-08-09 13:10:35

---
title: Deploying Microgateway as Kubernetes Sidecar Connected to API Gateway
published: true
date: 2021-07-30 06:44:44 UTC
tags: #kubernetes, #tutorial, #microgateway
canonical_url: https://tech.forums.softwareag.com/t/deploying-microgateway-as-kubernetes-sidecar-connected-to-api-gateway/243264
---

## Introduction

A Microgateway Kubernetes sidecar deployment can be established by creating a pod containing two containers; one container runs the native service, and the other container runs the Microgateway protecting the native service.

The native services are accessed by consumers through the Microgateway endpoint.

To access the native service from the Microgateway container, Microgateway has to use localhost as URL together with the port exposed by the native service as both the containers are treated as being within the same host.

There are two types of sidecar deployment models:

1. A stand-alone Kubernetes sidecar deployment ( For more details please refer the article [Microgateway Stand-alone Kubernetes Sidecar Deployment - Knowledge base - webMethods - Software AG Tech Community & Forums](https://tech.forums.softwareag.com/t/microgateway-stand-alone-kubernetes-sidecar-deployment/242974) )
2. A Kubernetes sidecar connected to API Gateway
In this article we will see how we can deploy Microgateway as Kubernetes sidecar connected to API Gateway
The diagram depicts Microgateway Kubernetes sidecar deployment connected to API Gateway. Each pod consists of a Microgateway container and the native service container. The Microgateway can be connected to an API Gateway to pull API definitions and to push runtime metrics.
![kubernetes_arch_mesh](https://aws1.discourse-cdn.com/techcommunity/original/3X/3/9/39754e6829c8b02a94dc4a86ad86e6d769994888.png)

## Audience

This document is intended for users who wants to deploy microgateway as a Kubernetes sidecar connected to API Gateway.

## Pre-Requisites

- Basic Knowledge on Docker, Kubernetes
- The native service Docker images are pushed to a Docker registry to make them available for the Kubernetes environment
- Create API in API Gateway
- Understanding on Microgateway
- Docker and Kubernetes environment setup

## Use Case

In this article we will be using **PostalCode** native Service docker image which is a simple Java based application which will return the postalCode when queried with the specific latitude and longitude.

## Deploying Microgateway as Kubernetes Sidecar connected to API Gateway

Before you start with the deployment ensure you have done the following:

- Ensure you have a running Kubernetes environment.
- There is an API Gateway instance running in a dedicated Kubernetes service.
- There is a native service Docker image in the Docker registry

Below are the steps about how we can achieve microgateway Kubernetes sidecar deployment, where the Microgateway are connected to an API Gateway for pulling API definitions and runtime metrics data aggregation.
1. Create a Microgateway Docker image
Navigate to the Microgateway installation directory `<InstallationDir>\Microgateway` 
Create the configuration file **pull-postalcode-service.yml** that holds the configuration for pulling the definition of the PostalCode API from API Gateway.
![configFile](https://aws1.discourse-cdn.com/techcommunity/original/3X/3/a/3a853ce907e6703cf8e10d28d0e4a78f038a1c2d.png)
Run the below docker command to create docker file
`./microgateway.bat createDockerFile -c pull-postalcode-service.yml --docker_file DockerFilePostalCode -dod`
Add the below environment variables to your docker file
```
ENV mcgw_api_gateway_url http://<<host>>:<<port>/rest/apigateway
ENV mcgw_api_gateway_user <<API Gateway Username>>
ENV mcgw_api_gateway_password <<API Gateway Password>>
```
_Where_,  
**<< host>>** is the IP of the system where the API gateway server is hosted. 
**<< port>>** is the Integration server port where the API gateway is installed.
Run the below command to create a docker image `docker build -t mg-postalcodeimg -f DockerFilePostalCode .`
2. Push the image to the docker repository
Tag the image created in the Step 1 and push it to docker registry.
`docker tag mg-postalcodeimg <<dockerRepo>>/mg-postalcodeimg`
`docker push <<dockerRepo>>/mg-postalcodeimg`
3. Create new namespace in Kubernetes
Below command creates new namespace in Kubernetes.
`kubectl create namespace postalcode`
4. Set Kubernetes context
Below command sets the Kubernetes context to the namespace created in step 3.
`kubectl config set-context --current --namespace= postalcode`
5. Create a template for Kubernetes sidecar deployment
Microgateway offers a function to generate a Kubernetes YAML file, which can be used for a convenient deployment.
We can specify the sidecar parameters together with the Microgateway image parameters to have the two containers created within one pod.
The below command creates the Kubernetes YAML file.
`./microgateway.bat createKubernetesFile --docker_image <<dockerRepo>>/mg-postalcodeimg --pod_name postalcode-sidecar --sidecar_docker_image <<dockerRepo>>/apiserver:latest --sidecar_pod_name postalcode-service --output postalcode-sidecar-deployment.yml`
_Here,_
**`<<dockerRepo>>/mg-postalcodeimg`** is the microgateway image created in Step 1
**`<<dockerRepo>>/apiserver:latest`** _is the native service image_
6. Change the Microgateway configuration
In the resulting Kubernetes file change the microgateway configurations by modifying the Microgateway environment variables.
For example, the API Gateway URL can be reconfigured through the environment variable **mcgw\_api\_gateway\_url** as shown by the following YAML fragment:
![yamlFile](https://aws1.discourse-cdn.com/techcommunity/original/3X/3/b/3bc9b22419a45d79a5630c247179b1303991bc46.png)
7. Create and check the Kubernetes deployment.
Create the Kubernetes deployment with the below command
`kubectl create –f postalcode-sidecar-deployment.yml`
If the deployment is successful you should see 2 out of 2 containers running
![GetPods](https://aws1.discourse-cdn.com/techcommunity/original/3X/1/f/1f46f6fe6da659cbacf03ed6be7be354caf6420d.png)
8. Expose Microgateway sidecar deployment as a Kubernetes service.
Run the below command to expose the deployment created in Step 7
`kubectl expose deployment postalcode-sidecar --type=LoadBalancer --port=9090`
9. Verify the Microgateway Kubernetes service definition including the exposed IP and port
Run the below command to see if the Kubernetes service as created along with the port details.
`kubectl get svc`
![getSvc](https://aws1.discourse-cdn.com/techcommunity/original/3X/c/4/c4db98d377f59d8d1608aa6258e24973da928577.png)
10. Check Microgateway service and Invoke the API
Once the deployment is exposed as service, we can check the Microgateway server status with the below
GET [http://localhost:9090/rest/microgateway/status](http://localhost:9090/rest/microgateway/status)
![Microgateway Status](https://aws1.discourse-cdn.com/techcommunity/optimized/3X/7/a/7a4fa956f489677805892ff3ca901d0356845385_2_690x287.png)
We can also invoke the native API with the below endpoint.
GET [http://localhost:9090/gateway/PostalCode/1.0/postalCodes?latitude=333&longitude=444444](http://localhost:9090/gateway/PostalCode/1.0/postalCodes?latitude=333&longitude=444444)
![postman](https://aws1.discourse-cdn.com/techcommunity/optimized/3X/b/1/b1aaab41cf28d357545f605381b79f75a748ad95_2_690x267.png)
Note here since the native services are not exposed by the Kubernetes configuration the Microgateway can’t be by-passed. Consumer requests are routed by the Microgateway to the native services.
From API Gateway, we can check the details of postalcode microgateway under Microgateways tab.
![APIgateway1](https://aws1.discourse-cdn.com/techcommunity/optimized/3X/3/0/30e0bb72353b0a16e2fcb96843d6e28273821f87_2_690x217.png)
![APIgateway2](https://aws1.discourse-cdn.com/techcommunity/optimized/3X/a/a/aa13432133442c6e93cc32be04dfb52c2b3e7045_2_690x254.png)
![APIgateway3](https://aws1.discourse-cdn.com/techcommunity/optimized/3X/a/c/ac4fb3e0b35cef958b2cdb479c31c0d2517d469c_2_690x307.png)

[Read full topic](https://tech.forums.softwareag.com/t/deploying-microgateway-as-kubernetes-sidecar-connected-to-api-gateway/243264)