How to deploy Kong locally on K8S with Ingress
My goal is to deploy kong as an api-gateway and ingress-controller locally and test out its ingress and gateway (e.g. rate-limiting) capabilities. I am facing issues in deploying kong-ingress locally on k3s. Anyone here who can help out?
Thanks.
Hello! How are you?
here are the general steps to deploy Kong locally on a Kubernetes cluster with Ingress:
-
Install Kubernetes locally on your machine. You can use a lightweight distribution like k3s if you want to minimize resource usage.
-
Install and configure Helm, which is a package manager for Kubernetes. You’ll use Helm to deploy Kong and its dependencies.
-
Add the official Kong Helm repository to your local Helm installation using the following command:
helm repo add kong [https://charts.konghq.com](https://charts.konghq.com)
- Update your local Helm repository cache using the following command:
helm repo update
- Create a new namespace in your Kubernetes cluster where you’ll deploy Kong:
kubectl create namespace kong
- Install Postgres using the Bitnami Helm chart. Kong requires a database to store configuration and other data:
helm install postgresql bitnami/postgresql --namespace kong
- Install Kong using the official Helm chart. This will create a deployment for Kong and a service to expose it:
helm install kong kong/kong --namespace kong
- Verify that Kong is running by checking the logs for the Kong pod:
kubectl logs -f KONG_POD_NAME -n kong
Replace KONG_POD_NAME
with the actual name of the Kong pod in your cluster.
- Deploy an example application that uses Kong as an ingress controller. You can use any application you want. Here’s an example deployment file that deploys the default nginx welcome page:
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: nginx
name: nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- image: nginx:latest
name: nginx
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
labels:
app: nginx
name: nginx-service
spec:
selector:
app: nginx
ports:
- name: http
port: 80
targetPort: 80
- Create an ingress resource that uses Kong as the ingress controller. Here’s an example ingress file that routes traffic to the nginx welcome page:
apiVersion: [networking.k8s.io/v1beta1](http://networking.k8s.io/v1beta1)
kind: Ingress
metadata:
name: kong-ingress
annotations:
[kubernetes.io/ingress.class](http://kubernetes.io/ingress.class): kong
spec:
rules:
- host: [example.com](http://example.com) # change this to your own hostname
http:
paths:
- path: /nginx
backend:
serviceName: nginx-service
servicePort: http
- Apply the deployment and ingress files to your Kubernetes cluster using the following commands:
kubectl apply -f nginx.yaml
kubectl apply -f ingress.yaml
- Verify that the nginx welcome page is accessible through Kong by navigating to
http://<KONG_IP_ADDRESS>/nginx
in your web browser, where KONG_IP_ADDRESS
is the IP address of the node running the Kong pod.
That’s it! You should now have Kong set up as an API gateway and ingress controller on your local Kubernetes cluster, and you should be able to test out its features like rate-limiting by modifying the application and ingress resources as needed.
I get an error
ensure CRDs are installed first```
If I try to change the `apiVersion` to `[networking.k8s.io/v1](http://networking.k8s.io/v1)` I get a different error
```Error from server (BadRequest): error when creating "kong-ingress.yaml": Ingress in version "v1" cannot be handled as a Ingress: strict decoding error: unknown field "spec.rules[0].http.paths[0].backend.serviceName", unknown field "spec.rules[0].http.paths[0].backend.servicePort"```