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

# Charmed Apache Zookeeper - 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:

3/stable 78

```
juju deploy zookeeper-k8s --channel 3/stable
```

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

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

## charms.zookeeper\_k8s.v0.zookeeper

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

    Fetch library

    ```
    charmcraft fetch-lib charms.zookeeper_k8s.v0.zookeeper
    ```

    [Download zookeeper.py](https://charmhub.io/zookeeper-k8s/libraries/zookeeper/download)
  + *Last updated* 15 Feb 2022
  + *Revision* Library version 0.13

ZooKeeper Library.

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

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

Any Charmed Operator that *requires* a ZooKeeper database for providing its
service should implement the *requirer* side of this interface.
[kafka-k8s](https://charmhub.io/kafka-k8s) is an example.

In a nutshell using this library to implement a Charmed Operator *requiring* a
ZooKeeper database (and talking to it as a ZooKeeper client) would look like

```
$ charmcraft fetch-lib charms.zookeeper_k8s.v0.zookeeper
```

`metadata.yaml`:

```
requires:
  zookeeper:
    interface: zookeeper
```

`src/charm.py`:

```
from charms.zookeeper_k8s.v0.zookeeper import ZooKeeperEvents, ZooKeeperRequires
from ops.charm import CharmBase

class MyCharm(CharmBase):

    on = ZooKeeperEvents()

    def __init__(self, *args):
        super().__init__(*args)
        self.zookeeper = ZooKeeperRequires(self)
        self.framework.observe(
            self.on.zookeeper_clients_changed,
            self._on_zookeeper_clients_changed,
        )
        self.framework.observe(
            self.on.zookeeper_clients_broken,
            self._on_zookeeper_clients_broken,
        )

    def _on_zookeeper_clients_changed(self, event):
        # Get zookeeper client addresses
        client_addresses: str = self.zookeeper.hosts
        # client_addresses => "zk-0:2181,zk-1:2181"

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

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

---

Index

* [class ZooKeeperEvents](https://charmhub.io/zookeeper-k8s/libraries/zookeeper#zookeeperevents)
* [class ZooKeeperRequires](https://charmhub.io/zookeeper-k8s/libraries/zookeeper#zookeeperrequires)
* + [def \_\_init\_\_(
    self,
    charm,
    endpoint\_name)](https://charmhub.io/zookeeper-k8s/libraries/zookeeper#zookeeperrequires-__init__)
* + [def hosts(
    self)](https://charmhub.io/zookeeper-k8s/libraries/zookeeper#zookeeperrequires-hosts)
* [class ZooKeeperProvides](https://charmhub.io/zookeeper-k8s/libraries/zookeeper#zookeeperprovides)
* + [def \_\_init\_\_(
    self,
    charm,
    endpoint\_name)](https://charmhub.io/zookeeper-k8s/libraries/zookeeper#zookeeperprovides-__init__)
* + [def update\_hosts(
    self,
    client\_addresses,
    relation\_id)](https://charmhub.io/zookeeper-k8s/libraries/zookeeper#zookeeperprovides-update_hosts)

#### class ZooKeeperEvents

ZooKeeper events.

Description

This class defines the events that ZooKeeper can emit.

Events:
zookeeper\_clients\_changed (\_ClientsBrokenEvent)

#### class ZooKeeperRequires

Description

ZooKeeper requires relation. None

Methods

ZooKeeperRequires.
\_\_init\_\_(

*self*

,
charm: CharmBase

,
endpoint\_name: str
)

Constructor.

Arguments

charm
(CharmBase)

The charm that implements the relation.

endpoint\_name
(str)

Endpoint name of the relation.

ZooKeeperRequires.
hosts(

*self*
)

Get zookeeper hosts.

Returns

Optional[str]

Comma-listed zookeeper client hosts.

#### class ZooKeeperProvides

ZooKeeper provides relation.

Methods

ZooKeeperProvides.
\_\_init\_\_(

*self*

,
charm: CharmBase

,
endpoint\_name: str
)

Constructor.

Arguments

charm
(CharmBase)

The charm that implements the relation.

endpoint\_name
(str)

Endpoint name of the relation.

ZooKeeperProvides.
update\_hosts(

*self*

,
client\_addresses: str

,
relation\_id
)

Update hosts in the zookeeper relation.

Arguments

client\_addresses
(str)

Comma-listed addresses of zookeeper clients.

relation\_id
(Optional[int])

Id of the relation. If set, it will be used to update
the relation data of the specified relation. If not set,
the data for all the relations will be updated.

Description

This method will cause a relation-changed event in the requirer units
of the relation.
