Skip to content

Convert PetSet to StatefulSet for k8s 1.5+ #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions bootstrap/pvc.yml → 10pvc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ spec:
- ReadWriteOnce
resources:
requests:
storage: 100Mi
storage: 200Gi
selector:
matchLabels:
app: kafka
petindex: "0"
podindex: "0"
---
kind: PersistentVolumeClaim
apiVersion: v1
Expand All @@ -25,11 +25,11 @@ spec:
- ReadWriteOnce
resources:
requests:
storage: 100Mi
storage: 200Gi
selector:
matchLabels:
app: kafka
petindex: "1"
podindex: "1"
---
kind: PersistentVolumeClaim
apiVersion: v1
Expand All @@ -41,8 +41,8 @@ spec:
- ReadWriteOnce
resources:
requests:
storage: 100Mi
storage: 200Gi
selector:
matchLabels:
app: kafka
petindex: "2"
podindex: "2"
2 changes: 0 additions & 2 deletions 20dns.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
apiVersion: v1
kind: Service
metadata:
annotations:
service.alpha.kubernetes.io/tolerate-unready-endpoints: "true"
name: broker
namespace: kafka
spec:
Expand Down
16 changes: 5 additions & 11 deletions 50kafka.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
apiVersion: apps/v1alpha1
kind: PetSet
apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
name: kafka
namespace: kafka
Expand All @@ -10,14 +10,11 @@ spec:
metadata:
labels:
app: kafka
annotations:
pod.alpha.kubernetes.io/initialized: "true"
pod.alpha.kubernetes.io/init-containers: '[
]'
spec:
terminationGracePeriodSeconds: 10
containers:
- name: broker
image: solsson/kafka-persistent:0.10.1.0
image: solsson/kafka-persistent:0.10.1@sha256:110f9e866acd4fb9e059b45884c34a210b2f40d6e2f8afe98ded616f43b599f9
ports:
- containerPort: 9092
command:
Expand All @@ -30,11 +27,8 @@ spec:
volumeClaimTemplates:
- metadata:
name: datadir
namespace: kafka
annotations:
volume.alpha.kubernetes.io/storage-class: anything
spec:
accessModes: [ "ReadWriteOnce" ]
resources:
requests:
storage: 100Mi
storage: 200Gi
32 changes: 17 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@

# Kafka as Kubernetes PetSet

Example of three Kafka brokers depending on three Zookeeper instances.
Example of three Kafka brokers depending on five Zookeeper instances.

To get consistent service DNS names `kafka-N.broker.kafka`(`.svc.cluster.local`), run everything in a namespace:
To get consistent service DNS names `kafka-N.broker.kafka`(`.svc.cluster.local`), run everything in a [namespace](http://kubernetes.io/docs/admin/namespaces/walkthrough/):
```
kubectl create -f 00namespace.yml
```

## Set up volume claims

This step can be skipped in clusters that support automatic volume provisioning, such as GKE.
You may add [storage class](http://kubernetes.io/docs/user-guide/persistent-volumes/#storageclasses)
to the kafka StatefulSet declaration to enable automatic volume provisioning.

You need this step in Minikube.

```
./zookeeper/bootstrap/pv.sh
kubectl create -f ./zookeeper/bootstrap/pvc.yml
```
Alternatively create [PV](http://kubernetes.io/docs/user-guide/persistent-volumes/#persistent-volumes)s and [PVC](http://kubernetes.io/docs/user-guide/persistent-volumes/#persistentvolumeclaims)s manually. For example in Minikube.

```
./bootstrap/pv.sh
Expand All @@ -26,21 +22,27 @@ kubectl create -f ./bootstrap/pvc.yml
kubectl get pvc
```

The volume size in the example is very small. The numbers don't really matter as long as they match. Minimal size on GKE is 1 GB.

## Set up Zookeeper

This module contains a copy of `pets/zookeeper/` from https://github.com/kubernetes/contrib.
There is a Zookeeper+StatefulSet [blog post](http://blog.kubernetes.io/2016/12/statefulset-run-scale-stateful-applications-in-kubernetes.html) and [example](https://github.com/kubernetes/contrib/tree/master/statefulsets/zookeeper),
but it appears tuned for workloads heavier than Kafka topic metadata.

See the `./zookeeper` folder and follow the README there.
The Kafka book (Definitive Guide, O'Reilly 2016) recommends that Kafka has its own Zookeeper cluster,
so we use the [official docker image](https://hub.docker.com/_/zookeeper/)
but with a [startup script change to guess node id from hostname](https://github.com/solsson/zookeeper-docker/commit/df9474f858ad548be8a365cb000a4dd2d2e3a217).

An additional service has been added here, create using:
Zookeeper runs as a [Deployment](http://kubernetes.io/docs/user-guide/deployments/) without persistent storage:
```
kubectl create -f ./zookeeper/service.yml
kubectl create -f ./zookeeper/
```

If you lose your zookeeper cluster, kafka will be unaware that persisted topics exist.
The data is still there, but you need to re-create topics.

## Start Kafka

Assuming you have your PVCs `Bound`, or enabled automatic provisioning (see above), go ahead and:

```
kubectl create -f ./
```
Expand Down
6 changes: 3 additions & 3 deletions bootstrap/pv-template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ metadata:
name: datadir-kafka-0
labels:
app: kafka
petindex: "0"
podindex: "0"
spec:
accessModes:
- ReadWriteOnce
Expand All @@ -20,7 +20,7 @@ metadata:
name: datadir-kafka-1
labels:
app: kafka
petindex: "1"
podindex: "1"
spec:
accessModes:
- ReadWriteOnce
Expand All @@ -35,7 +35,7 @@ metadata:
name: datadir-kafka-2
labels:
app: kafka
petindex: "2"
podindex: "2"
spec:
accessModes:
- ReadWriteOnce
Expand Down
5 changes: 5 additions & 0 deletions zookeeper/00namespace.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
apiVersion: v1
kind: Namespace
metadata:
name: kafka
15 changes: 15 additions & 0 deletions zookeeper/20zoo-service.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
apiVersion: v1
kind: Service
metadata:
name: zoo
labels:
app: zookeeper
spec:
ports:
- port: 2888
name: peer
- port: 3888
name: leader-election
clusterIP: None
selector:
app: zookeeper
2 changes: 1 addition & 1 deletion zookeeper/service.yml → zookeeper/30service.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ spec:
- port: 2181
name: client
selector:
app: zk
app: zookeeper
38 changes: 38 additions & 0 deletions zookeeper/50zoo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
name: zoo
namespace: kafka
spec:
serviceName: "zoo"
replicas: 5
template:
metadata:
labels:
app: zookeeper
spec:
terminationGracePeriodSeconds: 10
containers:
- name: zookeeper
image: solsson/zookeeper-statefulset:3.4.9@sha256:d32b44b32009a69b3450a5216f459e504f1041f587596895219fc04cf22f5546
env:
- name: ZOO_SERVERS
value: server.1=zoo-0.zoo:2888:3888:participant server.2=zoo-1.zoo:2888:3888:participant server.3=zoo-2.zoo:2888:3888:participant server.4=zoo-3.zoo:2888:3888:participant server.5=zoo-4.zoo:2888:3888:participant
ports:
- containerPort: 2181
name: client
- containerPort: 2888
name: peer
- containerPort: 3888
name: leader-election
volumeMounts:
- name: datadir
mountPath: /data
# There's defaults in this folder, such as logging config
#- name: conf
# mountPath: /conf
volumes:
#- name: conf
# emptyDir: {}
- name: datadir
emptyDir: {}
85 changes: 0 additions & 85 deletions zookeeper/README.md

This file was deleted.

48 changes: 0 additions & 48 deletions zookeeper/bootstrap/pv-template.yml

This file was deleted.

11 changes: 0 additions & 11 deletions zookeeper/bootstrap/pv.sh

This file was deleted.

Loading