kafkactl Documentation

repository·main·Indexed 21 days ago

https://github.com/deviceinsight/kafkactl

A command-line interface for interacting with Apache Kafka clusters. kafkactl supports advanced features including Avro/JSON schema registry integration, Protobuf encoding, plugin-based OAuth authentication, and Kubernetes-native execution. It provides tools to alter topics, partitions, brokers, and users; clone topics and consumer groups; and manage configuration contexts.

Tokens
19.2K
Snippets
89
Records
95
Agent score
75%

What's inside kafkactl

  1. Handle passwords and credentials

    main

    kafkactl supports several strategies for supplying sensitive credentials (SASL, TLS passphrases, Schema Registry).

    1. Interactive Prompting

    If a credential is missing from the config/environment, kafkactl will prompt you in the terminal.

    By default, kafkactl stores successfully entered passwords in your OS keyring (GNOME Keyring, macOS Keychain, or Windows Credential Manager). Subsequent commands will retrieve them automatically.

    • Disable Keyring: Set keyring.enabled: false in config.yml.
    • Clear Credentials: Use the --clear-keyring flag to delete a stored entry and force a re-prompt.
      kafkactl get topics --clear-keyring

    3. Configuration File

    You can store credentials in plaintext in config.yml. Not recommended for production.

    4. Kubernetes Secrets

    When running in Kubernetes mode, use the saslSecret configuration to avoid passing credentials as plaintext environment variables in the pod spec.

    keyring:
      enabled: true
  2. Use Plugins for OAuth Token Providers

    main

    kafkactl supports plugins to handle specific authentication requirements, such as cloud provider OAuth tokens (AWS, Azure).

    Generic Token Provider

    If you don't want to use a specific cloud plugin, you can use the built-in generic provider to execute a local script that returns an OAuth token.

    Configuration

    In your context configuration:

    sasl:
      enabled: true
      mechanism: oauth
      tokenprovider:
        plugin: generic
        options:
          script: /path/to/get-token.sh
          args:
            - intended-scope

    Script Requirements

    The script must output a JSON object in the following format:

    {
      "token": "your-oauth-access-token",
      "extensions": {
        "key": "value"
      }
    }

    The script is executed every time a token is needed, allowing for automatic refresh.

    {
      "token": "your-oauth-access-token",
      "extensions": {
        "key": "value"
      }
    }
  3. Use Avro and JSON Schema with kafkactl

    main

    If a schemaRegistry is configured in your context, kafkactl automatically detects and handles Avro and JSON schema encoded topics.

    Avro Support

    • Production: kafkactl looks up the topic in the registry. If you need to produce using a specific version, use --key-schema-version or --value-schema-version.
    • Consumption: Automatically decodes messages. Use --print-schema to see the schema used.

    JSON Schema Support

    • Production: Input JSON is validated against the registered schema before being produced using the Confluent wire format.
    • Consumption: Automatically decodes messages. Use --print-schema to see the schema used.

    Configuration Example

    contexts:
      local:
        schemaRegistry:
          url: localhost:8081
    kafkactl produce avro_topic --value {"next":{"next":{}}}
    kafkactl consume avro_topic --from-beginning --print-schema -o yaml
  4. Run kafkactl in Kubernetes

    main

    If your Kafka cluster is only accessible via a Kubernetes cluster, you can configure kubernetes support in your context.

    When enabled, kafkactl deploys a pod into the specified namespace and kubeContext to act as a proxy between your shell and the Kafka brokers.

    Configuration Options

    • enabled: Set to true to use Kubernetes mode.
    • binary: Path to kubectl (optional).
    • kubeConfig: Path to kubeconfig (optional).
    • kubeContext: The Kubernetes context to use.
    • namespace: The Kubernetes namespace to deploy the pod in.
    • image: The Docker image to use (defaults to private.registry.com/deviceinsight/kafkactl).
    • saslSecret: Use to avoid plaintext credentials in the pod.
      • name: Reference an existing K8s secret containing username and password keys.
      • create: true: Tells kafkactl to create a short-lived secret from your local config and delete it after the pod exits.

    Usage Modes

    1. kafkactl attach: Creates a pod and gives you a bash shell inside the container. This provides full auto-completion but takes longer to start.
    2. Direct Commands: Running any other command (e.g., kafkactl get topics) deploys a short-lived pod using a scratch image, which is faster but lacks the interactive bash environment.
    contexts:
      kafka-cluster:
        brokers:
          - broker1:9092
        kubernetes:
          enabled: true
          kubeContext: k8s-cluster
          namespace: k8s-namespace
          saslSecret:
            create: true
        sasl:
          enabled: true
          mechanism: scram-sha256
          username: my-user
          password: my-password
  5. Enable shell auto-completion

    main

    kafkactl supports dynamic auto-completion for bash, zsh, and fish, including dynamic completion for topics and consumer groups.

    Bash

    To load completions for the current session:

    source <(kafkactl completion bash)

    To install permanently:

    • Linux: kafkactl completion bash > /etc/bash_completion.d/kafkactl
    • macOS: kafkactl completion bash > /usr/local/etc/bash_completion.d/kafkactl

    Zsh

    Ensure shell completion is enabled in your ~/.zshrc via autoload -U compinit; compinit. Then install:

    kafkactl completion zsh > "${fpath[1]}/_kafkactl"

    Note: Restart your shell after setup.

    Fish

    To load for the current session:

    kafkactl completion fish | source

    To install permanently:

    kafkactl completion fish > ~/.config/fish/completions/kafkactl.fish
    source <(kafkactl completion bash)
  6. Install kafkactl

    main

    You can install kafkactl using various package managers or by compiling from source.

    Pre-compiled Binaries

    • Homebrew (macOS/Linux):
      brew install kafkactl
      # To upgrade:
      brew upgrade kafkactl
    • Winget (Windows):
      winget install kafkactl
    • yay (Arch Linux/AUR):
      yay -S kafkactl
    • deb/rpm: Download from the releases page and install using dpkg -i or rpm -i.
    • Manual: Download binaries from the releases page and move them to your PATH.

    Compiling from Source

    Use go install to compile the latest version:

    go install github.com/deviceinsight/kafkactl/v5@latest

    Note: Ensure kafkactl is in your PATH for shell auto-completion to function.

    go install github.com/deviceinsight/kafkactl/v5@latest
  7. Configure kafkactl contexts and settings

    main

    Configuration is managed via a YAML file. If no config is found, a default is generated at $HOME/.config/kafkactl/config.yml.

    Configuration File Resolution Order

    1. Command line argument: --config-file=$PATH_TO_CONFIG
    2. Environment variable: KAFKA_CTL_CONFIG
    3. Project config file in the current working directory (kafkactl.yml or .kafkactl.yml).
    4. Default locations:
      • $HOME/.config/kafkactl/config.yml
      • $HOME/.kafkactl/config.yml
      • $APPDATA/kafkactl/config.yml
      • /etc/kafkactl/config.yml

    Contexts

    Contexts allow you to define multiple Kafka clusters. Each context can have its own brokers, tls, sasl, kubernetes, avro, schemaRegistry, protobuf, producer, and consumer settings.

    Environment Variable Overrides

    Every key in config.yml can be overridden using environment variables. Convert the key to uppercase, replacing . and - with _. Example: contexts.default.tls.certKey becomes CONTEXTS_DEFAULT_TLS_CERTKEY.

    To set the default context specifically, you can omit the CONTEXTS_DEFAULT_ prefix (e.g., TLS_CERTKEY).

    contexts:
      default:
        brokers:
          - localhost:9092
      remote-cluster:
        brokers:
          - remote-cluster001:9092
        tls:
          enabled: true
          ca: my-ca
          cert: my-cert
          certKey: my-key
          insecure: false
  8. Decode Protobuf messages

    main

    To decode Protobuf messages, provide the necessary schema files and message types. You can use .proto files, compiled .protoset files, and specify import paths for dependencies.

    Key flags for Protobuf:

    • --key-proto-type and --value-proto-type: Specify the message type.
    • --proto-file: Provide additional .proto files.
    • --proto-import-path: Provide paths for imports within your .proto files.
    • --proto-marshal-option: Pass JSON marshalling options in key=value format.
  9. Configure Zsh auto-completion

    main

    To enable Zsh auto-completion for the current session, use source with a command substitution:

    $ source <(kafkactl completion zsh)

    To make completions permanent, save the output to a file in your $fpath:

    $ kafkactl completion zsh > "${fpath[1]}/_kafkactl"
    # Current session
    $ source <(kafkactl completion zsh)
    
    # Permanent
    $ kafkactl completion zsh > "${fpath[1]}/_kafkactl"
  10. Configure Bash auto-completion

    main

    To enable Bash auto-completion for the current session, use source with a command substitution:

    $ source <(kafkactl completion bash)

    To make completions permanent for all sessions, write the output to your system's bash completion directory:

    Linux:

    $ kafkactl completion bash > /etc/bash_completion.d/kafkactl

    MacOS:

    $ kafkactl completion bash > /usr/local/etc/bash_completion.d/kafkactl
    # Current session
    $ source <(kafkactl completion bash)
    
    # Permanent (Linux)
    $ kafkactl completion bash > /etc/bash_completion.d/kafkactl
    
    # Permanent (MacOS)
    $ kafkactl completion bash > /usr/local/etc/bash_completion.d/kafkactl
  11. Configure Fish auto-completion

    main

    To enable Fish auto-completion for the current session, pipe the output directly to source:

    $ kafkactl completion fish | source

    To make completions permanent, save the output to your Fish completions directory:

    $ kafkactl completion fish > ~/.config/fish/completions/kafkactl.fish
    # Current session
    $ kafkactl completion fish | source
    
    # Permanent
    $ kafkactl completion fish > ~/.config/fish/completions/kafkactl.fish
  12. Produce messages to a topic

    main

    Use the produce command to send messages to a Kafka topic.

    Input Formats

    1. Standard (Separator-based): Use a separator (default is newline) to split key and value.
      echo "my-key#my-value" | kafkactl produce my-topic --separator=#
    2. JSON Format: Each line must be a JSON object with key, value, and optional headers.
      echo '{"key": "k", "value": "v"}' | kafkactl produce my-topic --input-format=json
    3. File Input: Use the --file flag to read messages from a file.

    Common Flags

    • --key=<val> / --value=<val>: Specify key and value directly.
    • --separator=<char>: Custom delimiter between key and value.
    • --lineSeparator=<string>: Custom delimiter between messages.
    • --partition=<n>: Force a specific partition.
    • --partitioner=<type>: Change partitioning scheme (e.g., random).
    • --header <key>:<value>: Add a message header.
    • --rate=<n>: Limit production to n messages per second.
    • --null-value: Produce a tombstone record.
    • --required-acks=<level>: Set acknowledgement level (e.g., WaitForAll).

    Protobuf Production

    Convert JSON to Protobuf messages on the fly:

    kafkactl produce my-topic --value='{"field": 1}' --value-proto-type MyMsg --proto-file msg.proto
    kafkactl produce my-topic --key=my-key --value=my-value --header key1:value1