Get started with decK

Uses: Kong Gateway decK
Minimum Version
Kong Gateway - 3.4
TL;DR

This page teaches you how to use decK to create a Gateway Service, Route, Plugins, and Consumers using a declarative configuration file (kong.yaml). It uses the deck gateway apply command to build the configuration up incrementally.

At any point, you can run deck gateway dump to see the entire configuration of Kong Gateway at once.

Prerequisites

This is a Konnect tutorial. If you don’t have a Konnect account, you can get started quickly with our onboarding wizard.

  1. The following Konnect items are required to complete this tutorial:

    • Personal access token (PAT): Create a new personal access token by opening the Konnect PAT page and selecting Generate Token.
    • Control Plane Name: You can use an existing Control Plane or create a new one to use for this tutorial.
    • Konnect Proxy URL: By default, a self-hosted Data Plane uses http://localhost:8000. You can set up Data Plane nodes for your Control Plane from the Gateway Manager in Konnect.
  2. Set the personal access token, the Control Plane name, the Control Plane URL, and the Konnect proxy URL as environment variables:

     export DECK_KONNECT_TOKEN='YOUR KONNECT TOKEN'
     export DECK_KONNECT_CONTROL_PLANE_NAME='YOUR CONTROL PLANE NAME'
     export KONNECT_CONTROL_PLANE_URL=https://us.api.konghq.com
     export KONNECT_PROXY_URL='KONNECT PROXY URL'
    

This tutorial requires Kong Gateway Enterprise. If you don’t have Kong Gateway set up yet, you can use the quickstart script with an enterprise license to get an instance of Kong Gateway running almost instantly.

  1. Export your license to an environment variable:

     export KONG_LICENSE_DATA='LICENSE-CONTENTS-GO-HERE'
    
  2. Run the quickstart script:

     curl -Ls https://get.konghq.com/quickstart | bash -s -- -e KONG_LICENSE_DATA 
    

    Once Kong Gateway is ready, you will see the following message:

     Kong Gateway Ready
    

decK is a CLI tool for managing Kong Gateway declaratively with state files. To complete this tutorial you will first need to install decK.

Create a Service

You can use decK to configure a Service by providing a name and a url. Any requests made to this Service will be proxied to http://httpbin.konghq.com:

echo '
_format_version: "3.0"
services:
  - name: example-service
    url: http://httpbin.konghq.com
' | deck gateway apply -

Create a Route

To access this Service, you need to configure a Route. Create a Route that matches incoming requests that start with /, and attach it to the service that was previously created by specifying service.name:

echo '
_format_version: "3.0"
routes:
  - name: example-route
    service:
      name: example-service
    paths:
    - "/"
' | deck gateway apply -

You can now make an HTTP request to your running Kong Gateway instance and see it proxied to httpbin:

curl "$KONNECT_PROXY_URL/anything"
curl "http://localhost:8000/anything"

Add rate limiting

At this point, Kong Gateway is a transparent layer that proxies requests to the upstream httpbin instance. Let’s add the Rate Limiting plugin to make sure that people only make five requests per minute:

echo '
_format_version: "3.0"
plugins:
  - name: rate-limiting
    service: example-service
    config:
      minute: 5
      policy: local
' | deck gateway apply -

To see this in action, make six requests in rapid succession by pasting the following in to your terminal:

for _ in {1..6}; do
  curl  -i $KONNECT_PROXY_URL/anything  
  echo
done
for _ in {1..6}; do
  curl  -i http://localhost:8000/anything  
  echo
done

On the last request, you should get a 429 response with the message API rate limit exceeded.

Add authentication

You may have noticed that the rate limiting plugin used the limit_by: consumer configuration option. This means that each uniquely identified consumer is allowed 5 requests per minute.

To identify a consumer, let’s add the Key Auth plugin and create a test user named alice:

echo '
_format_version: "3.0"
plugins:
  - name: key-auth
    service: example-service
consumers:
  - username: alice
    keyauth_credentials:
    - key: hello_world
' | deck gateway apply -

After applying the key-auth plugin, you need to provide the apikey header to authenticate your request:

curl "$KONNECT_PROXY_URL/anything" \
     -H "apikey:hello_world"
curl "http://localhost:8000/anything" \
     -H "apikey:hello_world"

If you make a request without the authentication header, you will see a No API key found in request message.

See your decK file

This page provided each configuration snippet separately to focus on what each snippet provides. For production usage, you should apply the whole configuration each time.

To export the complete configuration, run:

deck gateway dump -o kong.yaml

Open the newly created kong.yaml in your favorite editor.

Try changing Alice’s authentication key to test and then sync the entire configuration back up:

deck gateway sync kong.yaml

You’ll see the following output:

creating key-auth test for consumer alice
deleting key-auth world for consumer alice
Summary:
  Created: 1
  Updated: 0
  Deleted: 1

Congratulations! You just went from zero to a configured Kong Gateway using decK in no time at all.

Cleanup

If you created a new control plane and want to conserve your free trial credits or avoid unnecessary charges, delete the new control plane used in this tutorial.

curl -Ls https://get.konghq.com/quickstart | bash -s -- -d
Something wrong?

Help us make these docs great!

Kong Developer docs are open source. If you find these useful and want to make them better, contribute today!