Deploying Gate with Kubernetes as API Gateway
In this example, we will deploy Gate in K8s as API Gateway that routes requests to a defined service based on configured request URL.
info
This is just an example of deployment. You should adjust it to your infrastructure.
Prerequisites
This tutorial uses Gate's Docker image.
Architecture
Example configuration
gate-api-gateway.yaml
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: gate
spec:
replicas: 3
selector:
matchLabels:
app: gate
template:
metadata:
labels:
app: gate
spec:
containers:
- name: gate
image: slashid/gate
command: [gate, --env]
ports:
- containerPort: 8080
env:
- name: GATE_PORT
value: "8080"
- name: GATE_DEFAULT_TARGET
value: http://products.default.svc.cluster.local:80/
- name: GATE_URLS_0_PATTERN
value: "*/products"
- name: GATE_URLS_0_TARGET
value: http://products.default.svc.cluster.local:80/
- name: GATE_URLS_1_PATTERN
value: "*/orders"
- name: GATE_URLS_1_TARGET
value: http://orders.default.svc.cluster.local:80/
- name: GATE_LOG_LEVEL
value: debug
- name: GATE_LOG_FORMAT
value: text
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: products
spec:
replicas: 3
selector:
matchLabels:
app: products
template:
metadata:
labels:
app: products
spec:
containers:
- name: products
image: slashid/example-service
env:
- name: PORT
value: "80"
- name: SERVICE_NAME
value: products
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: orders
spec:
replicas: 3
selector:
matchLabels:
app: orders
template:
metadata:
labels:
app: orders
spec:
containers:
- name: orders
image: slashid/example-service
env:
- name: PORT
value: "80"
- name: SERVICE_NAME
value: orders
---
apiVersion: v1
kind: Service
metadata:
labels:
app: gate
name: gate
spec:
externalTrafficPolicy: Local
ports:
- port: 8080
targetPort: 8080
selector:
app: gate
sessionAffinity: None
type: LoadBalancer
---
apiVersion: v1
kind: Service
metadata:
labels:
app: products
name: products
spec:
ports:
- port: 80
targetPort: 80
selector:
app: products
sessionAffinity: None
---
apiVersion: v1
kind: Service
metadata:
labels:
app: orders
name: orders
spec:
ports:
- port: 80
targetPort: 80
selector:
app: orders
sessionAffinity: None
Testing locally
You can test this setup locally (for example, with Docker Desktop Kubernetes).
kubectl apply -f gate-api-gateway.yaml
Once the deployment is running, you can check if requests go through Gate.
curl -v http://localhost:8080/
> GET / HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.79.1
> Accept: */*
>
< HTTP/1.1 200 OK
< Content-Length: 81
< Content-Type: application/json
< Servicehostname: products-57c5667b6c-kg6qh
< Servicename: products
< Via: gate
<
{
"service_hostname": "products-57c5667b6c-kg6qh",
"service_name": "products"
}
If the Via: 1.0 gate
header is present, it means that everything works properly and gate is up and running.
curl -v http://localhost:80/products
> GET /products HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.79.1
> Accept: */*
>
< HTTP/1.1 200 OK
< Content-Length: 81
< Content-Type: application/json
< Servicehostname: products-57c5667b6c-kg6qh
< Servicename: products
< Via: 1.0 gate
<
{
"service_hostname": "products-57c5667b6c-kg6qh",
"service_name": "products"
}
curl -v http://localhost:80/orders
> GET /orders HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.79.1
> Accept: */*
>
< HTTP/1.1 200 OK
< Content-Length: 77
< Content-Type: application/json
< Servicehostname: orders-66cfb49594-mw6nf
< Servicename: orders
< Via: 1.0 gate
<
{
"service_hostname": "orders-66cfb49594-mw6nf",
"service_name": "orders"
}