Home > front end >  Install & use rate-limiting plugin in Kong using the declarative format
Install & use rate-limiting plugin in Kong using the declarative format

Time:07-11

I am trying to enable rate-limiting plugin in Kong. However, I keep getting the error - 'name': plugin 'rate-limiting' not enabled; add it to the 'plugins' configuration property

I am deploying kong as a containerised service orchestrated by docker compose. The docker-compose.yml file looks like -

version: "3"

services:
  # API Service
  api-service:
    container_name: api-service
    build:
      context: .
      dockerfile: Dockerfile
    environment:

      ...

      # Kong config
      - KONG_HTTP_PORT=${KONG_HTTP_PORT}
      - KONG_HTTPS_PORT=${KONG_HTTPS_PORT}

    depends_on:
      - postgres

  kong:
    container_name: kong
    image: kong
    restart: unless-stopped
    ports:
      - ${KONG_HTTP_PORT}:8000/tcp
      - ${KONG_HTTPS_PORT}:8443/tcp
    environment:
      KONG_DATABASE: "off"
      KONG_DECLARATIVE_CONFIG: /var/lib/kong/kong.yml
      KONG_DNS_ORDER: LAST,A,CNAME
      KONG_PLUGINS: request-transformer,cors,key-auth,acl
    volumes:
      - ./deploy/volumes:/var/lib/kong

  postgres:
    container_name: postgres
    image: postgres
    restart: unless-stopped
    ports:
      - ${DB_PORT}:5432

volumes:
  kong:
  postgres:

Kong configuration (kong.yml) looks like below -

services:
  - name: api-v1
    url: http://api-service:{PORT}
    routes:
      - name: api-v1
        # strip_path: true
        paths:
          - /api/
    plugins:
      - name: cors
      - name: key-auth
        config:
          hide_credentials: false
      - name: acl
        config:
          hide_groups_header: true
          allow:
            - admin
            - anon
      - name: rate-limiting
      config:
        # Number of HTTP requests that can be made per second
        second: 5
        # Number of HTTP requests that can be made per hour
        hour: 10000
        policy: local
        fault_tolerant: true
        hide_client_headers: false

A detailed stacktrace looks like below -

2022/07/08 08:29:56 [warn] 1#0: the "user" directive makes sense only if the master process runs with super-user privileges, ignored in /usr/local/kong/nginx.conf:6
nginx: [warn] the "user" directive makes sense only if the master process runs with super-user privileges, ignored in /usr/local/kong/nginx.conf:6
2022/07/08 08:29:56 [error] 1#0: init_by_lua error: /usr/local/share/lua/5.1/kong/init.lua:553: error parsing declarative config file /var/lib/kong/kong.yml:
in 'services':
  - in entry 1 of 'services':
    in 'plugins':
      - in entry 4 of 'plugins':
        in 'name': plugin 'rate-limiting' not enabled; add it to the 'plugins' configuration property
stack traceback:
    [C]: in function 'error'
    /usr/local/share/lua/5.1/kong/init.lua:553: in function 'init'
    init_by_lua:3: in main chunk
nginx: [error] init_by_lua error: /usr/local/share/lua/5.1/kong/init.lua:553: error parsing declarative config file /var/lib/kong/kong.yml:
in 'services':
  - in entry 1 of 'services':
    in 'plugins':
      - in entry 4 of 'plugins':
        in 'name': plugin 'rate-limiting' not enabled; add it to the 'plugins' configuration property
stack traceback:
    [C]: in function 'error'
    /usr/local/share/lua/5.1/kong/init.lua:553: in function 'init'
    init_by_lua:3: in main chunk

CodePudding user response:

In your docker-compose.yml, did you forgot to add rate-limiting to KONG_PLUGINS variable?

KONG_PLUGINS: request-transformer,cors,key-auth,acl

Since you specified the value of KONG_PLUGINS, kong will only load plugins from that list, that's why rate-limiting plugin is not available for you.

KONG_PLUGINS configuration reference

  • Related