---
title: Charmhub | Deploy Apache Kafka - K8s using Charmhub - The Open Operator Collection
description: Deploy the latest version of Apache Kafka - K8s as a Kubernetes Operator
  on any cloud.
url: https://charmhub.io/kafka-k8s/libraries/kafka
---

# Apache Kafka - K8s

[Canonical](https://charmhub.io/publisher/data-platform "View all packages from Canonical")

* [Canonical](https://charmhub.io/publisher/data-platform "View all packages from Canonical")
* [Databases](https://charmhub.io/?filter=databases)

Platform:

4/stable 111

```
juju deploy kafka-k8s --channel 4/stable
```

[Learn to deploy on juju >](https://juju.is/docs/juju/manage-applications)

[Toggle side navigation](https://charmhub.io/kafka-k8s/libraries/kafka#drawer)

## charms.kafka\_k8s.v0.kafka

* [*Docstrings*Docstrings](https://charmhub.io/kafka-k8s/libraries/kafka)
  [*Code*Source code](https://charmhub.io/kafka-k8s/libraries/kafka/source-code)
* + Download

    Fetch library

    ```
    charmcraft fetch-lib charms.kafka_k8s.v0.kafka
    ```

    [Download kafka.py](https://charmhub.io/kafka-k8s/libraries/kafka/download)
  + *Last updated* 04 Mar 2022
  + *Revision* Library version 0.4

Kafka library.

This [library](https://juju.is/docs/sdk/libraries) implements both sides of the
`kafka` [interface](https://juju.is/docs/sdk/relations).

The *provider* side of this interface is implemented by the
[kafka-k8s Charmed Operator](https://charmhub.io/kafka-k8s).

Any Charmed Operator that *requires* Kafka for providing its
service should implement the *requirer* side of this interface.

In a nutshell using this library to implement a Charmed Operator *requiring*
Kafka would look like

```
$ charmcraft fetch-lib charms.kafka_k8s.v0.kafka
```

`metadata.yaml`:

```
requires:
  kafka:
    interface: kafka
    limit: 1
```

`src/charm.py`:

```
from charms.kafka_k8s.v0.kafka import KafkaEvents, KafkaRequires
from ops.charm import CharmBase

class MyCharm(CharmBase):

    on = KafkaEvents()

    def __init__(self, *args):
        super().__init__(*args)
        self.kafka = KafkaRequires(self)
        self.framework.observe(
            self.on.kafka_available,
            self._on_kafka_available,
        )
        self.framework.observe(
            self.on["kafka"].relation_broken,
            self._on_kafka_broken,
        )

    def _on_kafka_available(self, event):
        # Get Kafka host and port
        host: str = self.kafka.host
        port: int = self.kafka.port
        # host => "kafka-k8s"
        # port => 9092

    def _on_kafka_broken(self, event):
        # Stop service
        # ...
        self.unit.status = BlockedStatus("need kafka relation")
```

You can file bugs
[here](https://github.com/charmed-osm/kafka-k8s-operator/issues)!

---

Index

* [class KafkaEvents](https://charmhub.io/kafka-k8s/libraries/kafka#kafkaevents)
* [class KafkaRequires](https://charmhub.io/kafka-k8s/libraries/kafka#kafkarequires)
* + [def \_\_init\_\_(
    self,
    charm,
    endpoint\_name)](https://charmhub.io/kafka-k8s/libraries/kafka#kafkarequires-__init__)
* + [def host(
    self)](https://charmhub.io/kafka-k8s/libraries/kafka#kafkarequires-host)
* + [def port(
    self)](https://charmhub.io/kafka-k8s/libraries/kafka#kafkarequires-port)
* [class KafkaProvides](https://charmhub.io/kafka-k8s/libraries/kafka#kafkaprovides)
* + [def \_\_init\_\_(
    self,
    charm,
    endpoint\_name)](https://charmhub.io/kafka-k8s/libraries/kafka#kafkaprovides-__init__)
* + [def set\_host\_info(
    self,
    host,
    port,
    relation)](https://charmhub.io/kafka-k8s/libraries/kafka#kafkaprovides-set_host_info)

#### class KafkaEvents

Kafka events.

Description

This class defines the events that Kafka can emit.

Events:
kafka\_available (\_KafkaAvailableEvent)

#### class KafkaRequires

Description

Requires-side of the Kafka relation. None

Methods

KafkaRequires.
\_\_init\_\_(

*self*

,
charm: CharmBase

,
endpoint\_name: str
)

KafkaRequires.
host(

*self*
)

Description

Get kafka hostname. None

KafkaRequires.
port(

*self*
)

Description

Get kafka port number. None

#### class KafkaProvides

Description

Provides-side of the Kafka relation. None

Methods

KafkaProvides.
\_\_init\_\_(

*self*

,
charm: CharmBase

,
endpoint\_name: str
)

KafkaProvides.
set\_host\_info(

*self*

,
host: str

,
port: int

,
relation
)

Set Kafka host and port.

Arguments

host
(str)

Kafka hostname or IP address.

port
(int)

Kafka port.

relation
(Optional[Relation])

Relation to update.
If not specified, all relations will be updated.

Description

This function writes in the application data of the relation, therefore,
only the unit leader can call it.
