Let’s chat about Kubernetes. Here at , we’ve seen a huge shift recently, with businesses moving towards automation, redundancy and continuous delivery. Modern times call for modern deployments, and when it comes to scalable, portable and extensible deployments, little can compete with Kubernetes (K8s).
and Kubernetes make a beautiful pairing, with Operator bringing GitOps practices to API management processes. With , you can describe the entire API management system declaratively. You can also customise the Gateway and augment its functionality with a range of custom plugins
Today, we’ll look at one of those plugins – a custom Python plugin – that you can use with and Kubernetes as you pivot from imperative to declarative configurations. We’ll show you how to deploy the plugin into a installation running on a Kubernetes cluster.
, Python and K8s
Whether an object-oriented or functional programmer, we support your methods and functions through ’s extensible plugin options. Out of the box, provides support for GoLang, Lua, JavaScript, Python development and all gRPC – supported languages. We’re here to make your life easier, not force you to do things a certain way!
In Figure 1, you can see the request chain lifecycle outlining the stages where you can inject custom logic or plugins. The Python plugin will execute at the Custom pre-middleware stage and inject a sample header into the request.
Figure 1: Middleware chain depicting where custom logic can be injected throughout the request lifecycle.
Methodology and Code
Estimated time:
~10-20 minutes based on hardware
System prerequisites:
Minikube, Helm CLI, Python, Postman (or curl)
——————————————————————————————————————–
Suppose you were interested in injecting a header into a request. You need to create a directory housing two files. The first is a manifest.json indicating what powers the plugin, at what stage the plugin executes, and the path to which the plugin should be applied.
In our code snippet Reference 1 below, we are defining a Python plugin to run on the pre-stage, on the /get path, invoking the function SetHeader.
Reference 1: A manifest.json file illustrating what plugin is being executed, where it’s executed and when it’s executed.
The second file you need is the Python file itself. Reference 2 illustrates a Python script which injects a header testheader into a request with the value testvalue.
from .decorators import *
from gateway import Gateway as
@Hook
def SetHeader(request, session, spec):
.log(“PreHook is called”, “info”)
request.add_header(“testheader”, “testvalue”)
return request, session
Reference 2: A middleware.py file containing Python logic.
Now for some example code:
mkdir example_python_plugin
cd example_python_plugin
touch middleware.py manifest.json
Populate the two created files with the examples from Reference 1 and Reference 2. In order to compile the files and load them onto the Gateway, you first need to bundle them together.
We are setting the shell variable IMAGETAG to be the version of the gateway we intend to load the Python bundle onto. In this case, we are loading the plugin onto a Gateway v3.2.1. When completed, you should see a bundle.zip in your example_python_plugin directory.
Now, you need to serve the bundle. The easiest way to do so is through a local server using the following command:
python3 -m http.server
Your bundle should now be accessible locally through the URL https://localhost:8000/bundle.zip, or through https://host.minikube.internal:8000/bundle.zip from within minikube.
Python development is now complete.
Loading the Python plugin onto the Gateway
Now, it’s time to spin up a Kubernetes cluster locally using minikube and deploy the Gateway onto it. Supposing minikube and helm are installed on your system, you can simply start the cluster using:
minikube start
NOTE: If minikube and helm are not installed, please use these links for minikube and helm to install them.
Now, you can follow up with some post-install commands. These commands are taken from our documentation on installing with helm.
# Add helm charts
helm repo add -helm https://helm..io/public/helm/charts/
helm repo update
# Export values of helm to a local values.yaml
helm show values -helm/-pro > values.yaml
# Under the section dash: license:, ensure your license key is filled out
# Create the Namespace
kubectl create namespace
# Add dependencies
helm install redis -helm/simple-redis -n
helm install mongo -helm/simple-mongodb -n
At this point, you should have a values.yaml file on your local filesystem. Under the section dash: license:, ensure your license key is filled out. Please feel free to specify a password for the admin user in the .yaml file as well.
In the values.yaml, underneath the section gateway: extraEnvs:, please add the following environment variables:
Depending on where you served the bundle, your value for _GW_BUNDLEBASEURL may change. In this tutorial, the bundle is being served locally on the host machine, so we are accessing the host network through minikube’s hostname entry.
Once the install is complete, you can install Operator. Operator allows you to manage resources on your Kubernetes cluster through custom resource definitions (CRDs). These are extensions of the Kubernetes API. They allow you to manage extra, non-native Kubernetes resources. To install Operator, follow these instructions:
At this point, both and Operator should be loaded onto your minikube cluster. Now it’s time to create an API and load the Python plugin onto that API. You can download a sample CRD named httpbin.yaml with the content depicted in Reference 3.
Reference 3: A httpbin.yaml CRD containing the information to load our custom plugin.
In order to create this resource using Operator, simply run the following:
kubectl apply -f httpbin.yaml -n
Congratulations! You have now loaded a custom Python plugin onto your Gateway running within minikube. In order to test it out, you can enable a tunnel into the Dashboard and gateway services:
minikube service dashboard-svc--pro -n –url
minikube service gateway-svc--pro -n –url
Figure 3 illustrates where the tunnel URL for our internal gateway service can be found.
You can access the gateway running within minikube using this URL.
Figure 3: Obtaining the tunnel URL into the gateway service.
Using these URLs provided into the local minikube cluster, you may now use Postman to hit the /get endpoint of our deployed httpbin service.
As depicted in Figure 4, the header has been injected into the request, and echoed back to us.
Figure 4: Response from the managed httpbin microservice. The headers are injected into the request and echoed back in the response.
And that’s it! Everything you need to start deploying Python plugins in running K8s. Time to enjoy the benefits of your new, declarative configurations!
A version of Operator is available within the open-source repository, but it has been archived and will be unmaintained. The latest release of Operator will be available exclusively to paying customers.
Create, secure & test APIs Faster API deployment Monitor, troubleshoot & update APIs