Apache BookKeeper can be easily deployed in Kubernetes clusters. The managed clusters on Google Container Engine is the most convenient way.

The deployment method shown in this guide relies on YAML definitions for Kubernetes resources. The kubernetes subdirectory holds resource definitions for:

  • A three-node ZooKeeper cluster
  • A BookKeeper cluster with a bookie runs on each node.

Setup on Google Container Engine

To get started, get source code of kubernetes from github by git clone.

If you’d like to change the number of bookies, or ZooKeeper nodes in your BookKeeper cluster, modify the replicas parameter in the spec section of the appropriate Deployment or StatefulSet resource.

Google Container Engine (GKE) automates the creation and management of Kubernetes clusters in Google Compute Engine (GCE).

Prerequisites

To get started, you’ll need:

Create a new Kubernetes cluster

You can create a new GKE cluster using the container clusters create command for gcloud. This command enables you to specify the number of nodes in the cluster, the machine types of those nodes, and more.

As an example, we’ll create a new GKE cluster for Kubernetes version 1.6.4 in the us-central1-a zone. The cluster will be named bookkeeper-gke-cluster and will consist of three VMs, each using two locally attached SSDs and running on n1-standard-8 machines. These SSDs will be used by Bookie instances, one for the BookKeeper journal and the other for storing the actual data.

  1. $ gcloud config set compute/zone us-central1-a
  2. $ gcloud config set project your-project-name
  3. $ gcloud container clusters create bookkeeper-gke-cluster \
  4. --machine-type=n1-standard-8 \
  5. --num-nodes=3 \
  6. --local-ssd-count=2 \
  7. --enable-kubernetes-alpha

By default, bookies will run on all the machines that have locally attached SSD disks. In this example, all of those machines will have two SSDs, but you can add different types of machines to the cluster later. You can control which machines host bookie servers using labels.

Dashboard

You can observe your cluster in the Kubernetes Dashboard by downloading the credentials for your Kubernetes cluster and opening up a proxy to the cluster:

  1. $ gcloud container clusters get-credentials bookkeeper-gke-cluster \
  2. --zone=us-central1-a \
  3. --project=your-project-name
  4. $ kubectl proxy

By default, the proxy will be opened on port 8001. Now you can navigate to localhost:8001/ui in your browser to access the dashboard. At first your GKE cluster will be empty, but that will change as you begin deploying.

When you create a cluster, your kubectl config in ~/.kube/config (on MacOS and Linux) will be updated for you, so you probably won’t need to change your configuration. Nonetheless, you can ensure that kubectl can interact with your cluster by listing the nodes in the cluster:

  1. $ kubectl get nodes

If kubectl is working with your cluster, you can proceed to deploy ZooKeeper and Bookies.

ZooKeeper

You must deploy ZooKeeper as the first component, as it is a dependency for the others.

  1. $ kubectl apply -f zookeeper.yaml

Wait until all three ZooKeeper server pods are up and have the status Running. You can check on the status of the ZooKeeper pods at any time:

  1. $ kubectl get pods -l component=zookeeper
  2. NAME READY STATUS RESTARTS AGE
  3. zk-0 1/1 Running 0 18m
  4. zk-1 1/1 Running 0 17m
  5. zk-2 0/1 Running 6 15m

This step may take several minutes, as Kubernetes needs to download the Docker image on the VMs.

If you want to connect to one of the remote zookeeper server, you can usezk-shell, you need to forward a local port to the remote zookeeper server:

  1. $ kubectl port-forward zk-0 2181:2181
  2. $ zk-shell localhost 2181

Deploy Bookies

Once ZooKeeper cluster is Running, you can then deploy the bookies. You can deploy the bookies either using a DaemonSet or a StatefulSet.

NOTE: DaemonSet vs StatefulSet

A DaemonSet ensures that all (or some) nodes run a pod of bookie instance. As nodes are added to the cluster, bookie pods are added automatically to them. As nodes are removed from the cluster, those bookie pods are garbage collected. The bookies deployed in a DaemonSet stores data on the local disks on those nodes. So it doesn’t require any external storage for Persistent Volumes.

A StatefulSet maintains a sticky identity for the pods that it runs and manages. It provides stable and unique network identifiers, and stable and persistent storage for each pod. The pods are not interchangeable, the idenifiers for each pod are maintained across any rescheduling.

Which one to use? A DaemonSet is the easiest way to deploy a bookkeeper cluster, because it doesn’t require additional persistent volume provisioner and use local disks. BookKeeper manages the data replication. It maintains the best latency property. However, it uses hostIP and hostPort for communications between pods. In some k8s platform (such as DC/OS), hostIP and hostPort are not well supported. A StatefulSet is only practical when deploying in a cloud environment or any K8S installation that has persistent volumes available. Also be aware, latency can be potentially higher when using persistent volumes, because there is usually built-in replication in the persistent volumes.

  1. # deploy bookies in a daemon set
  2. $ kubectl apply -f bookkeeper.yaml
  3. # deploy bookies in a stateful set
  4. $ kubectl apply -f bookkeeper.stateful.yaml

You can check on the status of the Bookie pods for these components either in the Kubernetes Dashboard or using kubectl:

  1. $ kubectl get pods

While all BookKeeper pods is Running, by zk-shell you could find all available bookies under /ledgers/

You could also run a bookkeeper tutorial instance, which named as ‘dice’ here, in this bookkeeper cluster.

  1. $kubectl run -i --tty --attach dice --image=caiok/bookkeeper-tutorial --env ZOOKEEPER_SERVERS="zk-0.zookeeper"

An example output of Dice instance is like this:

  1. $ kubectl run -i --tty --attach dice --image=caiok/bookkeeper-tutorial --env ZOOKEEPER_SERVERS="zk-0.zookeeper"
  2. If you don't see a command prompt, try pressing enter.
  3. Value = 1, epoch = 5, leading
  4. Value = 2, epoch = 5, leading
  5. Value = 1, epoch = 5, leading
  6. Value = 4, epoch = 5, leading
  7. Value = 5, epoch = 5, leading
  8. Value = 4, epoch = 5, leading
  9. Value = 3, epoch = 5, leading
  10. Value = 5, epoch = 5, leading
  11. Value = 3, epoch = 5, leading
  12. Value = 2, epoch = 5, leading
  13. Value = 1, epoch = 5, leading
  14. Value = 4, epoch = 5, leading
  15. Value = 2, epoch = 5, leading

Un-Deploy

Delete Demo dice instance

  1. $kubectl delete deployment dice

Delete BookKeeper

  1. $ kubectl delete -f bookkeeper.yaml

Delete ZooKeeper

  1. $ kubectl delete -f zookeeper.yaml

Delete cluster

  1. $ gcloud container clusters delete bookkeeper-gke-cluster