Routing with Gloo Function Gateway

Scott Cranton
Mar 13, 2019 · Unlisted
Image for post
Image for post
Photo by Pietro Jeng.

Prerequisites

Setup

Setup example service

kubectl apply -f petstore-v1.yaml
---
# petstore-v1
apiVersion: v1
kind: Service
metadata:
name: petstore-v1
namespace: default
labels:
app: petstore-v1
spec:
type: ClusterIP
ports:
- name: http
port: 8080
targetPort: 8080
protocol: TCP
selector:
app: petstore-v1
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: petstore-v1
namespace: default
labels:
app: petstore-v1
spec:
replicas: 1
selector:
matchLabels:
app: petstore-v1
template:
metadata:
labels:
app: petstore-v1
spec:
containers:
- name: petstore-v1
image: scottcranton/petstore:v1
ports:
- containerPort: 8080
kubectl get all --namespace default
NAME READY STATUS RESTARTS AGE
pod/petstore-v1-986747fc8-6hn9p 1/1 Running 0 16s

NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 22h
service/petstore-v1 ClusterIP 10.110.99.86 <none> 8080/TCP 17s

NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/petstore-v1 1/1 1 1 16s

NAME DESIRED CURRENT READY AGE
replicaset.apps/petstore-v1-986747fc8 1 1 1 16s
kubectl get service --namespace defaultNAME          TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)    AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 22h
petstore-v1 ClusterIP 10.110.99.86 <none> 8080/TCP 42s
kubectl port-forward service/petstore-v1 8080:8080
curl localhost:8080/api/pets[{"id":1,"name":"Dog","status":"available"},{"id":2,"name":"Cat","status":"pending"}]
curl localhost:8080/swagger.json

Setup Gloo

brew install glooctl
glooctl install gateway
kubectl get all --namespace gloo-system
NAME                                 READY   STATUS    RESTARTS   AGE
pod/discovery-66c865f9bc-h6v8f 1/1 Running 0 22h
pod/gateway-777cf4486c-8mzj5 1/1 Running 0 22h
pod/gateway-proxy-5f58774ccc-rcmdv 1/1 Running 0 22h
pod/gloo-5c6c4466f-ptc8v 1/1 Running 0 22h

NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/gateway-proxy LoadBalancer 10.97.13.246 <pending> 80:31333/TCP,443:32470/TCP 22h
service/gloo ClusterIP 10.104.80.219 <none> 9977/TCP 22h

NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/discovery 1/1 1 1 22h
deployment.apps/gateway 1/1 1 1 22h
deployment.apps/gateway-proxy 1/1 1 1 22h
deployment.apps/gloo 1/1 1 1 22h

NAME DESIRED CURRENT READY AGE
replicaset.apps/discovery-66c865f9bc 1 1 1 22h
replicaset.apps/gateway-777cf4486c 1 1 1 22h
replicaset.apps/gateway-proxy-5f58774ccc 1 1 1 22h
replicaset.apps/gloo-5c6c4466f 1 1 1 22h

Routing

Upstreams

glooctl get upstreams
+-------------------------------+------------+----------+------------------------------+
| UPSTREAM | TYPE | STATUS | DETAILS |
+-------------------------------+------------+----------+------------------------------+
| default-kubernetes-443 | Kubernetes | Accepted | svc name: kubernetes |
| | | | svc namespace: default |
| | | | port: 443 |
| | | | |
| default-petstore-v1-8080 | Kubernetes | Accepted | svc name: petstore-v1 |
| | | | svc namespace: default |
| | | | port: 8080 |
| | | | REST service: |
| | | | functions: |
| | | | - addPet |
| | | | - deletePet |
| | | | - findPetById |
| | | | - findPets |
| | | | |
| gloo-system-gateway-proxy-443 | Kubernetes | Accepted | svc name: gateway-proxy |
| | | | svc namespace: gloo-system |
| | | | port: 443 |
| | | | |
| gloo-system-gateway-proxy-80 | Kubernetes | Accepted | svc name: gateway-proxy |
| | | | svc namespace: gloo-system |
| | | | port: 80 |
| | | | |
| gloo-system-gloo-9977 | Kubernetes | Accepted | svc name: gloo |
| | | | svc namespace: gloo-system |
| | | | port: 9977 |
| | | | |
| kube-system-kube-dns-53 | Kubernetes | Accepted | svc name: kube-dns |
| | | | svc namespace: kube-system |
| | | | port: 53 |
| | | | |
+-------------------------------+------------+----------+------------------------------+
glooctl get upstream default-petstore-v1-8080 --output yamldiscoveryMetadata: {}
metadata:
annotations:
kubectl.kubernetes.io/last-applied-configuration: |
{"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"labels":{"app":"petstore-v1"},"name":"petstore-v1","namespace":"default"},"spec":{"ports":[{"name":"http","port":8080,"protocol":"TCP","targetPort":8080}],"selector":{"app":"petstore-v1"},"type":"ClusterIP"}}
labels:
app: petstore-v1
discovered_by: kubernetesplugin
name: default-petstore-v1-8080
namespace: gloo-system
resourceVersion: "20387"
status:
reportedBy: gloo
state: Accepted
upstreamSpec:
kube:
selector:
app: petstore-v1
serviceName: petstore-v1
serviceNamespace: default
servicePort: 8080
serviceSpec:
rest:
swaggerInfo:
url: http://petstore-v1.default.svc.cluster.local:8080/swagger.json
transformations:
addPet:
body:
text: '{"id": {{ default(id, "") }},"name": "{{ default(name, "")}}","tag":
"{{ default(tag, "")}}"}'
headers:
:method:
text: POST
:path:
text: /api/pets
content-type:
text: application/json
deletePet:
headers:
:method:
text: DELETE
:path:
text: /api/pets/{{ default(id, "") }}
content-type:
text: application/json
findPetById:
body: {}
headers:
:method:
text: GET
:path:
text: /api/pets/{{ default(id, "") }}
content-length:
text: "0"
content-type: {}
transfer-encoding: {}
findPets:
body: {}
headers:
:method:
text: GET
:path:
text: /api/pets?tags={{default(tags, "")}}&limit={{default(limit,
"")}}
content-length:
text: "0"
content-type: {}
transfer-encoding: {}

Basic Routing

glooctl create virtualservice --name coalmine
apiVersion: gateway.solo.io/v1
kind: VirtualService
metadata:
name: coalmine
namespace: gloo-system
spec:
displayName: coalmine
virtualHost:
domains:
- '*'
name: gloo-system.coalmine
glooctl add route \
--name coalmine \
--path-prefix /petstore \
--dest-name default-petstore-v1-8080 \
--prefix-rewrite /api/pets
export PROXY_URL=$(glooctl proxy url)
curl ${PROXY_URL}/petstore
[{"id":1,"name":"Dog","status":"available"},{"id":2,"name":"Cat","status":"pending"}]
apiVersion: gateway.solo.io/v1
kind: VirtualService
metadata:
name: coalmine
namespace: gloo-system
spec:
displayName: coalmine
virtualHost:
domains:
- '*'
name: gloo-system.coalmine
routes:
- matcher:
prefix: /petstore
routeAction:
single:
upstream:
name: default-petstore-v1-8080
namespace: gloo-system
routePlugins:
prefixRewrite:
prefixRewrite: /api/pets

Function Routing

glooctl add route \
--name coalmine \
--path-prefix /findPets \
--dest-name default-petstore-v1-8080 \
--rest-function-name findPets
curl ${PROXY_URL}/findPets
glooctl add route \
--name coalmine \
--path-prefix /findPetWithId \
--dest-name default-petstore-v1-8080 \
--rest-function-name findPetById \
--rest-parameters ':path=/findPetWithId/{id}'
curl ${PROXY_URL}/findPetWithId/1{"id":1,"name":"Dog","status":"available"}
curl ${PROXY_URL}/findPetWithId/2{"id":2,"name":"Cat","status":"pending"}
apiVersion: gateway.solo.io/v1
kind: VirtualService
metadata:
name: coalmine
namespace: gloo-system
spec:
displayName: coalmine
virtualHost:
domains:
- '*'
name: gloo-system.coalmine
routes:
- matcher:
prefix: /findPetWithId
routeAction:
single:
destinationSpec:
rest:
functionName: findPetById
parameters:
headers:
:path: /findPetWithId/{id}
upstream:
name: default-petstore-v1-8080
namespace: gloo-system
- matcher:
prefix: /findPets
routeAction:
single:
destinationSpec:
rest:
functionName: findPets
parameters: {}
upstream:
name: default-petstore-v1-8080
namespace: gloo-system
- matcher:
prefix: /petstore
routeAction:
single:
upstream:
name: default-petstore-v1-8080
namespace: gloo-system
routePlugins:
prefixRewrite:
prefixRewrite: /api/pets

Summary


solo.io

Blogs and articles from solo.io

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store