Rapidly prototype your APIs on K8s w/Kusk-gateway

June 20, 2022
Est. reading time:
7 min
We will explore how to use Kusk Gateway and OpenAPI to generate automatic mocks of your APIs that will allow for rapid prototyping between your teams
Abdallah Abedraba
Product Leader
Kusk
Table of Contents
Rapidly prototype your APIs on K8s w/Kusk-gateway

[OpenAPI](https://www.openapis.org/) (f.k.a Swagger) has introduced a set of standardized specifications for REST APIs that, among many things, allows producers and consumers of APIs to work together in designing an API before even writing a single line of code!

This design-first approach has improved the experience of API developers by giving them the opportunity to use tools like [OpenAPI generator](https://openapi-generator.tech/) which takes an OpenAPI definition and generates scaffolding code for backenders, making the development of APIs much faster. 

But consumers of APIs still didn’t have a way of really taking advantage of this design-first approach as they still depended on the backend teams to implement and deploy APIs.

###How could consumers of APIs then take advantage of OpenAPI and the design-first approach?

By making the API gateway aware of the OpenAPI definition. Let me explain how!

An API gateway sits in front of your microservices routing incoming traffic that your Kubernetes cluster receives to the corresponding microservice. By having the API gateway be aware of your OpenAPI definition it can then see what a response would look like and generate a mock (or fake) response for the consumers instead.

This approach would allow API consumers to start using the API with mocked results and meanwhile (in parallel!) the backend team can start implementing the actual API; it essentially loses the dependency between consumer and producer teams and allows for quicker prototyping of the API. With no code whatsoever, just your run-of-the-mill OpenAPI definition. 

In this tutorial, you will learn how to use [Kusk Gateway](https://github.com/kubeshop/kusk-gateway), an open-source OpenAPI-driven API gateway, to enable this rapid prototyping development! Let’s make a simple example of how to achieve this:

#Walkthrough

##1. Install Kusk Gateway CLI

-- CODE language-bash --
brew install kubeshop/kusk/kusk

For other installation methods, check the [installation docs](https://docs.kusk.io/getting-started).

##2. Install Kusk Gateway in your Kubernetes cluster

-- CODE language-bash --
kusk cluster install

##3. Create an OpenAPI definition

Create the file `openapi.yaml` with the content:

This is a simple common OpenAPI definition for a single `/hello` route that accepts a GET method. There are a couple of important details here: 

####1. The `x-kusk` extension 

This section is an extension to the OpenAPI specification. We use it to configure Kusk Gateway to enable in this case CORS access and mocking of the API. This approach makes OpenAPI the definition of your API *and* your gateway configuration(!) essentially having a single source of truth. 

####2. The `example` field

To allow Kusk Gateway to generate mock responses, you'll need to define examples of your responses under OpenAPI's `example` field. In this case we have an example message "Hello from a mocked response!". 

##4. Apply the API configuration to the cluster

-- CODE language-bash --
 kusk deploy -i openapi.yaml

This applies the configuration to the gateway and deploys the API. 

##5. Test your mocked API

<p>Now you are ready to test the API and build against mocked responses!</p>

First, you need to get the External IP of the gateway, i.e. the entry point of your APIs. For that run: 

-- CODE language-bash --
kubectl get -n kusk-system svc/kusk-gateway-envoy-fleet

Note: if you're running a local cluster with Minikube, the ExternalIP might be in pending state; in case it is, you can port-forward to the gateway by running `kubectl port-forward -n kusk-system svc/kusk-gateway-envoy-fleet 8080:80`, so your external IP becomes `localhost:8080`. 

And now test the `/hello` path: 

-- CODE language-bash --
curl $EXTERNAL_IP/hello
{"message": "Hello from a mocked response!"}

And that's it! You can see that this is the same response that was defined in our OpenAPI definition. Now your API consumer teams can already start working with the mocked API!

Once the API team has implemented the service, you will stop mocking your endpoint and connect it to a microservice in your cluster. That's what we'll cover in the next sections.

##6. Deploy your application to the cluster

<p>Deploy this example application that we will connect with the API gateway: </p>

-- CODE language-bash --
kubectl apply -f https://gist.githubusercontent.com/aabedraba/e9e7a48c7bc48386f4dadd0d9fe3b7df/raw/9048bcae0675dade44a2cd4b091e752acafaf960/hello-kubernetes.yaml

##7. Connect your application to Kusk Gateway

Now, you will need to disable the mocking for your API and connect the upstream. Update your `openapi.yaml` file with the following changes:

And run the following command to configure Kusk Gateway:

-- CODE language-bash --
 kusk deploy -i openapi.yaml

##8. Test your implemented application

With the same External IP from step 5, run 

-- CODE language-bash --
curl $EXTERNAL_IP/hello
{"message": "Hello from an implemented service!"}

Voilá! You've just implemented and connected a service to your gateway!

You can find the code for the article [on Github](https://github.com/aabedraba/kusk-examples/tree/main/rapid-prototype).

#Conclusions

Kusk Gateway enabled you to take advantage of OpenAPI and a design-first approach to allow your teams to work in parallel once they've agreed on a specific API design. Nothing should have be stopping them anyway!

Having an OpenAPI-aware gateway allows you to reap more advantages of the [OpenAPI lifecycle](https://kubeshop.io/blog/the-apiops-lifecycle-managing-api-workflows-through-the-open-api-definition), like for example [automatic validation of your APIs](https://kubeshop.github.io/kusk-gateway/guides/validation/) and more!

Head over to the [Kusk Gateway documentation](https://kubeshop.github.io/kusk-gateway/getting-started) to try it out and explore all the benefits of an OpenAPI-driven gateway! We would love to hear your thoughts and ideas in our [Discord server](https://discord.gg/uNuhy6GDyn) or just drop by if you to say hi :) 

Thank you!

Tags:
Open source

Related Content

November 30, 2022
|
3 min
read

Kusk-gateway 1.4: OpenAPI code-first support

We're proud to announce the v1.4 release of Kusk Gateway, which adds support for code-first OpenAPI generation, install Kusk CLI using `apt` and `choco` and scalable authorization with Cloudentity.

Open API
Kusk
Design-first
October 14, 2022
|
5 min
read

Kusk + Cloudentity: Fine-Grained API Authorization

Kusk has partnered with Cloudentity, an Authorization as a Service platform, to enable users to create powerful authorization rules while still using OpenAPI at the core of their API workflows!

Open API
Kusk
Design-first
October 3, 2022
|
4 min
read

Kusk v1.3: Updated CLI & dashboard experience

We're excited to release Kusk v1.3 with many improvements to the CLI experience, and a totally new design of the dashboard. Read more to learn about the new features!

Open API
Kusk
Design-first
September 12, 2022
|
6 min
read

Setup Ingress Controller with CORS configuration

Adding CORS should not be complicated. Read more to learn how Kusk help CORS configuration using OpenAPI.

Open API
Kusk
Design-first
September 7, 2022
|
6 min
read

Can Agile Teams Have a Design-First Approach to APIs?

Read more to learn how you can combine the power of OpenAPI with a Design-First approach with a successful agile environment!

Open API
Kusk
Design-first
September 6, 2022
|
3 min
read

Kusk-gateway 1.2.0: OAuth, Local Mocking + more

The Kusk team is proud to announce the v1.2.0 release of Kusk Gateway, which adds OAuth support, mocking of the API directly from the CLI and improved developer experience of the CLI!

Open API
Kusk
Design-first
September 6, 2022
|
2 min
read

Kusk on the Road - Our September ‘22 Schedule

The Kusk team is hitting the road to meet users and industry friends face-to-face across the US in September. Read more to see how we can meet!

Open API
Kusk
Design-first
August 23, 2022
|
Newstack
read

What it means to be OpenAPI-first in Kubernetes

Dive into how OpenAPI can help API developers become productive and what its support looks like in the current state of the Kubernetes realm

Open API
Kusk
Design-first
July 21, 2022
|
6 min
read

Automatic Request Validation with OpenAPI

Let's explore how you can use OpenAPI with Kusk Gateway to ensure that all the request bodies sent to your cluster a properly validated!

Open API
Kusk
Design-first
June 30, 2022
|
5 min
read

Kusk-gateway 1.1.0: Authentication support + more

The Kusk team is proud to announce the v1.1.0 release of Kusk Gateway, which adds Header Based Authentication, Rate limiting, Caching and Post-processed OpenAPI definitions

Open API
Kusk
Design-first
May 19, 2022
|
5 min
read

APIOps Lifecycle: API workflows via OpenAPI Definition

What is APIOps and how can we use the OpenAPI standard as a way to implement an API-First & Design-first approach to development of modern REST APIs deployed to Kubernetes.

Open API
Kusk
Design-first
May 5, 2022
|
2 min
read

Kusk-gateway 1.0: OpenAPI-driven K8s Ingress Controller

The Kusk team is proud to announce the v1.0 of Kusk-gateway, which adds Kubernetes API mocking capabilities, a CLI tool, auto-deployments, an interactive dashboard, and HTTP-related improvements.

Open API
Kusk
Design-first
April 21, 2022
|
8 min
read

Design-First to Automated Deployment w/OpenAPI

Use OpenAPI to drive the functional and operational aspects of your APIs with a Design-First approach and automated deployment.

Open API
Kusk
Design-first
March 28, 2022
|
3 min
read

Kusk-gateway  OpenAPI-driven Kubernetes Ingress Controller Beta

The Kusk team is proud to announce the beta release of Kusk Gateway, which adds API mocking capabilities, a CLI tool, and HTTP/SSL-related improvements.

Open API
Kusk
Design-first
January 31, 2022
|
Newstack
read

Can You GitOps Your APIs?

Kusk automates generation of resources for various ingress controllers by treating your OpenAPI Specification as the source of truth for configuration.

Open API
Kusk
Design-first
January 28, 2022
|
5 min
read

Kusk-gateway OpenAPI-Driven K8s Gateway Alpha 2

Kusk-gateway v0.0.0.alpha.2 was released today with lots of enhancements, new features and bug fixes.

Open API
Kusk
Design-first
November 29, 2021
|
1 min
read

Kusk-gateway OpenAPI-first K8s Ingress Controller

An Argument for using OpenAPI (Swagger) to configure Ingress in Kubernetes.

Open API
Kusk
Design-first
September 16, 2021
|
2 min
read

Kusk 0.2.0 released with Traefik support and more

Today the Kubeshop team released Kusk version 0.2.0 featuring Traefik V2 support, further improvements on x-kusk OpenAPI extension and…

Open API
Kusk
Design-first
September 15, 2021
|
4 min
read

Automate Configuration w/Kusk—a DevOps Design-First Approach to APIs

APIs are everywhere. APIs are a standardized way for applications to expose their functionality. API functionality often supersedes that…

Open API
Kusk
Design-first
September 14, 2021
|
3 min
read

Kusk: Configuring K8s Ingress Controller for CORS w/OpenAPI

Kusk: Configuring your Ingress Controller for CORS with OpenAPI

Open API
Kusk
Design-first
August 16, 2021
|
1 min
read

Hello Kusk—OpenAPI for Kubernetes

Hello Kusk: OpenAPI for Kubernetes

Open API
Kusk
Design-first

Stay in the Loop

Subscribe to stay up to date by getting the blog direct to your inbox

Or join the Kubeshop Community in one of these channels