Appendix B: Final Exam
Goal: Show without instructions that you can operate Kong: publish, secure, limit, distribute, find drift, back up and route in Kubernetes. · Duration: approx. 120 min · Prerequisite: Day 15
Rules of Play
- Each exercise describes only the goal. You find the way yourself; the days in parentheses help you look things up.
- Each exercise ends with a check block: individual commands with what they must show. Only when all of them match does the exercise count.
- Work in
~/kong-lab/anhband change Kong only viakong.yamlandksync(exception: Exercise 5).
source ~/kong-lab/setup/env.sh
mkdir -p ~/kong-lab/anhb && cd ~/kong-lab/anhbExercise 1: A Status Endpoint
Publish notes-api’s /healthz as
GET /api/v1/status, via the upstream notes-up.
Other methods must not reach notes-api. (Day 2, Day 9)
Check block:
curl -s $PROXY/api/v1/status; echo
curl -s -X POST $PROXY/api/v1/status | jq -r .urlExpected:
ok
http://echo:8080/anything/v1/status
Solution
cat > status.yaml <<'EOF'
services:
- name: status
url: http://notes-up/healthz
routes:
- name: status
paths: [/api/v1/status]
methods: [GET]
EOF
deck file merge ~/kong-lab/kong/kong.yaml status.yaml -o ~/kong-lab/kong/kong.yaml
ksync 2>/dev/null | tail -1Expected:
Deleted: 0
The POST lands on the catch-all route /api
from Day 2, not at notes-api.
Save your progress? Sign up for free
Exercise 2: A New Colleague
Create the consumer carla with the key
lab-key-carla-123 in the group leser. (Day
5)
Check block:
curl -s -H 'apikey: lab-key-carla-123' $PROXY/whoami \
| jq -r '.headers."X-Consumer-Username"[0]'
curl -s -o /dev/null -w '%{http_code}\n' -X POST -H 'apikey: lab-key-carla-123' \
-H 'Content-Type: application/json' -d '{"text":"carla"}' \
--cacert ~/kong-lab/tag10/tls/ca.crt https://localhost:8443/api/v1/notesExpected: carla is recognized, but may not write.
carla
403
Solution
cat > carla.yaml <<'EOF'
consumers:
- username: carla
keyauth_credentials:
- key: lab-key-carla-123
acls:
- group: leser
EOF
deck file merge ~/kong-lab/kong/kong.yaml carla.yaml -o ~/kong-lab/kong/kong.yaml
ksync 2>/dev/null | tail -1Expected:
Deleted: 0
Save your progress? Sign up for free
Exercise 3: A Limit Just for carla
carla may call /whoami at most twice per minute;
everyone else gets no new limit. (Day 4, Day 8)
Check block:
for k in carla-123 anna-456; do
for i in 1 2 3; do
curl -s -o /dev/null -w '%{http_code} ' -H "apikey: lab-key-$k" $PROXY/whoami
done; echo
doneExpected:
200 200 429
200 200 200
Solution
A plugin on the consumer applies only to that consumer’s requests:
deck file add-plugins -s ~/kong-lab/kong/kong.yaml \
--selector '$.consumers[?(@.username=="carla")]' \
--config '{"name":"rate-limiting","config":{"minute":2,"policy":"local"}}' \
-o ~/kong-lab/kong/kong.yaml
ksync 2>/dev/null | tail -1Expected:
Deleted: 0
Save your progress? Sign up for free
Exercise 4: Drain an Instance
From now on, notes-api-2 should get no more requests, without you stopping the container. (Day 9)
Check block:
for i in $(seq 1 10); do curl -s $PROXY/api/v1/version; done | sort | uniq -cExpected:
10 0.2.0
Solution
U='$.upstreams[?(@.name=="notes-up")].targets'
deck file patch -s ~/kong-lab/kong/kong.yaml \
--selector "$U[?(@.target==\"notes-api-2:8080\")]" \
--value 'weight:0' -o ~/kong-lab/kong/kong.yaml
ksync 2>/dev/null | tail -1Expected:
Deleted: 0
Weight 0 takes a target out of the distribution, but Kong keeps checking it. This is how you drain an instance before maintenance without any client noticing.
Save your progress? Sign up for free
Exercise 5: The Back Door
Someone created a service through the Admin API (this is the
exception to the rule: the examiner runs this block). Find it and remove
it so that kong.yaml remains the truth. (Day 7)
curl -s -o /dev/null -X POST $ADMIN/services -d name=hintertuer \
-d url=http://echo:8080/anything
curl -s -o /dev/null -X POST $ADMIN/services/hintertuer/routes -d 'paths[]=/hintertuer'
sleep 10Check block:
curl -s -o /dev/null -w '%{http_code}\n' $PROXY/hintertuer
deck gateway diff ~/kong-lab/kong/kong.yaml 2>/dev/null | tail -3Expected:
404
Created: 0
Updated: 0
Deleted: 0
Solution
deck gateway diff ~/kong-lab/kong/kong.yaml 2>/dev/null | grep -E '^(creating|deleting)'
ksync 2>/dev/null | tail -1Expected (shortened):
deleting route 762f5687-…
deleting service hintertuer
Deleted: 2
Save your progress? Sign up for free
Exercise 6: Version in Kubernetes
In k3s, publish GET /api/v1/version (exactly this path)
to notes-api’s /version, with the Gateway API. (Day 14)
Check block (in the VM k3s):
curl -s http://localhost/api/v1/version
kubectl -n notes get httproute version \
-o jsonpath='{.status.parents[0].conditions[?(@.type=="Accepted")].status}'; echoExpected:
0.2.0
True
Solution
cat <<'EOF' | kubectl apply -f -
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: version
namespace: notes
spec:
parentRefs:
- {name: kong, namespace: kong}
rules:
- matches:
- path: {type: Exact, value: /api/v1/version}
method: GET
filters:
- type: URLRewrite
urlRewrite:
path: {type: ReplaceFullPath, replaceFullPath: /version}
backendRefs:
- {name: notes-api, port: 8080}
EOFExpected:
httproute.gateway.networking.k8s.io/version created
Exact instead of PathPrefix,
ReplaceFullPath instead of ReplacePrefixMatch:
the whole path is replaced.
Save your progress? Sign up for free
Exercise 7: Back Up and Save the State
Back up the control plane’s database to
~/kong-lab/anhb/pruefung.sql.gz and commit your state. (Day
3, Day 7)
Check block (on the VM kong):
zcat ~/kong-lab/anhb/pruefung.sql.gz | grep -c '^COPY public\.'
zcat ~/kong-lab/anhb/pruefung.sql.gz | grep -c 'carla'
git -C ~/kong-lab/kong status --short | wc -lExpected: many tables, carla is in the backup, nothing uncommitted.
35
2
0
Solution
docker compose -f ~/kong-lab/kong/compose.yaml exec -T kong-db \
pg_dump -U kong -d kong | gzip > ~/kong-lab/anhb/pruefung.sql.gz
cd ~/kong-lab/kong && git add kong.yaml && git commit -qm "Abschlussprüfung"Save your progress? Sign up for free
Knowledge Test
- Why is the Admin API never bound to
0.0.0.0in this book, and how do you still reach it from your laptop? - What distinguishes
deck file validatefromdeck gateway validate? - Why does the route with
/api/v1/noteswin over/apiforGET /api/v1/notes, even though/apicomes first in the file? - Two auth plugins on one route: what applies without and what with
anonymous? - Why does the
jwtplugin need a consumer, and what limitation follows from that? - What happens to
rate-limitingwithpolicy: clusterin a data plane? - How do you tell whether a 404 comes from Kong or from the backend?
- Why does
/upstreams/notes-up/healthshow nothing on the CP in hybrid mode? - What does the KIC do with a
URLRewritefilter, and what is the consequence? - Which three steps belong to every change to
kong.yaml?
Answers
- Anyone who reaches the Admin API can reconfigure everything and read
keys; OSS has no login there. Access via SSH tunnel (Day 1) or through
Kong itself with
basic-auth,acl,ip-restrictionand HTTPS (Days 5, 8, 10). file validatechecks only the shape of the file offline;gateway validateasks Kong’s schema about allowed values (Day 3).- Kong picks the most specific route: more conditions and the longer prefix win; the order in the file does not matter (Day 2).
- Without
anonymous, both must be satisfied (and); withanonymous, one is enough (or), and the ACL decides via the anonymous consumer (Day 6). jwtlooks up the key viaisson a consumer. All tokens of a realm end up at the same consumer; onlyjwt-identitypasses on the name from the token (Day 6, 11).- The DP has no database and discards the entire new configuration; it keeps working with the old one (Day 13, Day 15).
- By
X-Kong-Response-Latencyand the messageno Route matchedfor Kong, and byX-Kong-Upstream-Latencyand the backend’s text otherwise (Day 1, Day 15). - Health checks run in the DP; the CP processes no traffic. The DP’s metrics show the health state (Day 15).
- It implements it as a
request-transformer; an additionalrequest-transformeron the same route has no effect (Day 14). - Validate (
deck gateway validate), compare (deck gateway diff), apply and commit (Day 3, Day 7).
Where to Go from Here?
- Keycloak in depth: realms, clients, LDAP and operations in the book “Keycloak hands-on”.
- Gateway API with Envoy: the same concepts with a different gateway in the book “Gateway API hands-on”.
- Kubernetes: the database as a StatefulSet instead of in-memory operation, Helm charts, RKE2 in the book “Kubernetes hands-on”.
- More Kong: Konnect as a managed control plane, Kong Operator, testing custom plugins with Pongo.