WSO2 API Microgateway in Kubernetes

Nipuna Prashan
4 min readOct 18, 2018

--

The WSO2 API Microgateway is a lightweight proxy for APIs. I am not here to introduce WSO2 API microgateway. You can simply go and read about WSO2 API Microgateway using the official documentation. Let’s go briefly on how WSO2 API Microgateway works and then get back to the original topic.

How WSO2 Microgateway works?

API Manager comes with a Microgateway that is responsible for Microgateway related management tasks. Yeah! You need to WSO2 API publisher to create APIs. When you setup API microgateway, it pulls created APIs and places in the microgateway.

Q> Is there a way to push API artifacts directly to API Microgateway without creating an API in the wso2 API publisher?
A>
Current WSO2 API Microgateway is written to pull APIs from WSO2 API manager. Wait for a new release, or why not trying to contribute to the open source project. Let’s continue with the current released version Microgateway 2.6.0.

Step 1: Deploy and subscribe to the sample API

Firstly, you have to create an API or some APIs in the API manager. Let’s start with a sample API given in the WSO2 API manager. Following 8 steps are also documented in WSO2 documentation. I just repeat here to keep the flow in this page itself.

  1. Open the API Publisher (https://<hostname>:9443/publisher) and sign in with admin/admin credentials.
  2. Close the interactive tutorial that starts automatically if you are a first-time user.
  3. Click the Deploy Sample API button. It deploys a sample API called PizzaShackAPI into the API Manager. The Deploy Sample API option is available only when there are no APIs in API Publisher. If you have already created a API, this option is not available.
  4. Click PizzaShackAPI to open it.
  5. Go to the Lifecycle tab and note that the State is PUBLISHED. The API is already published to the API Store.
  6. Sign in to the API Store (https://<hostname>:9443/store) with the admin/admin credentials and click the PizzaShackAPI API.
  7. Select the default application and an available tier, and click Subscribe.
  8. When the subscription is successful, click View Subscriptions on the information message that appears.

Step 2: Create docker files to run API Microgateway.

We have to run two commands before starting the API Microgateway. That is SETUP and BUILD.
We can run the setup command and the build command in an init-container and start the microgateway in the main container. You also can run all in one container.

Folder Structure
  1. Create the init container Dockerfile.
# set to latest Ubuntu LTS
FROM ubuntu:18.04
# copy java and microgateway to the containerCOPY ./files/jdk1.8.0_121 /java
COPY ./files/wso2am-micro-gw-2.6.0 /wso2am-micro-gw-2.6.0
COPY ./files/init.sh /
# set environment variables
ENV JAVA_HOME=/java
ENV PATH=$JAVA_HOME/bin:$PATH
# install required packages
RUN apt-get update && apt-get install -y zip
ENTRYPOINT ["/init.sh"]

2. Download JDK and WSO2 API Microgateway, extract them save in a folder “files”

3. Create init.sh file

#!/bin/sh#Navigate to the wso2am-micro-gw-2.6.0/bin directory
cd /wso2am-micro-gw-2.6.0/bin
# $1-API project name
# $2-API name
# $3-API version
# $4-username
# $5-password
# $6-APIM base URL
# $7-Trust store location
# $8-Trust store password
./micro-gw setup $1 -a $2 -v $3 -u $4 -p $5 -s $6 -t $7 -w $8 --insecure
./micro-gw build $1
# unzip target file
unzip /wso2am-micro-gw-2.6.0/bin/$1/target/micro-gw-$1.zip -d /
# renaming the generated folder to a common name to mount
mv /micro-gw-$1/* /target
# make gateway an executable file
chmod +x /target/bin/gateway

4. Create main container Dockerfile to run API microgateway.

FROM ubuntu:18.04# Copy java to the docker container
COPY ./files/jdk1.8.0_121 /java
# set environment variables
ENV JAVA_HOME=/java
ENV PATH=$JAVA_HOME/bin:$PATH
EXPOSE 9096 9095 9090ENTRYPOINT ["/target/bin/gateway"]

Step 3: Build docker files and push to your docker repository.

  1. If you don’t have docker installed in your machine. Go ahead and install docker
  2. If you don’t have a docker hub account. Go ahead and create.
  3. Build init-container docker file and push to your repository.
$ cd init-container
$ docker build . -t <docker-repository-name>/microgateway_init
$ docker push <docker-repository-name>/microgateway_init

4. Build main container docker file and push to your repository.

$ cd microgateway-container
$ docker build . -t <docker-repository-name>/microgateway
$ docker push <docker-repository-name>/microgateway

After pushing these docker images you can go to dockerhub and check your docker images. Here is my docker images
https://hub.docker.com/r/nipunaprashan/microgateway_init
https://hub.docker.com/r/nipunaprashan/microgateway

Step 4: Create Kubernetes file for API Microgateway.

create following microgateway.yaml file

apiVersion: apps/v1
kind: Deployment
metadata:
name: micro-gateway-deployment
labels:
app: micro-gateway
spec:
replicas: 1
selector:
matchLabels:
app: micro-gateway
template:
metadata:
labels:
app: micro-gateway
spec:
containers:
- name: micro-gateway
image: nipunaprashan/microgateway
volumeMounts:
- name: targetdir
mountPath: /target
initContainers:
- name: initialize-micro-gateway
image: nipunaprashan/microgateway_init
args: ["pizzaProject", "PizzaShackAPI", "1.0.0", "admin", "admin", "https://apim-service/", "lib/platform/bre/security/ballerinaTruststore.p12", "ballerina"]
volumeMounts:
- name: targetdir
mountPath: "/target"
volumes:
- name: targetdir
emptyDir: {}
---
apiVersion: v1
kind: Service
metadata:
name: micro-gateway
spec:
selector:
app: micro-gateway
ports:
- protocol: TCP
port: 443
targetPort: 9095

Step 5: Deploy the API Microgateway in Kubernetes.

kubectl create -f microgateway.yaml

--

--