Skip to content

Commit 467ac9d

Browse files
authored
Merge pull request #14 from Yolean/statefulset
Convert PetSet to StatefulSet for k8s 1.5+
2 parents 9323076 + e631d58 commit 467ac9d

19 files changed

+90
-612
lines changed

bootstrap/pvc.yml renamed to 10pvc.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ spec:
99
- ReadWriteOnce
1010
resources:
1111
requests:
12-
storage: 100Mi
12+
storage: 200Gi
1313
selector:
1414
matchLabels:
1515
app: kafka
16-
petindex: "0"
16+
podindex: "0"
1717
---
1818
kind: PersistentVolumeClaim
1919
apiVersion: v1
@@ -25,11 +25,11 @@ spec:
2525
- ReadWriteOnce
2626
resources:
2727
requests:
28-
storage: 100Mi
28+
storage: 200Gi
2929
selector:
3030
matchLabels:
3131
app: kafka
32-
petindex: "1"
32+
podindex: "1"
3333
---
3434
kind: PersistentVolumeClaim
3535
apiVersion: v1
@@ -41,8 +41,8 @@ spec:
4141
- ReadWriteOnce
4242
resources:
4343
requests:
44-
storage: 100Mi
44+
storage: 200Gi
4545
selector:
4646
matchLabels:
4747
app: kafka
48-
petindex: "2"
48+
podindex: "2"

20dns.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
apiVersion: v1
44
kind: Service
55
metadata:
6-
annotations:
7-
service.alpha.kubernetes.io/tolerate-unready-endpoints: "true"
86
name: broker
97
namespace: kafka
108
spec:

50kafka.yml

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
apiVersion: apps/v1alpha1
2-
kind: PetSet
1+
apiVersion: apps/v1beta1
2+
kind: StatefulSet
33
metadata:
44
name: kafka
55
namespace: kafka
@@ -10,14 +10,11 @@ spec:
1010
metadata:
1111
labels:
1212
app: kafka
13-
annotations:
14-
pod.alpha.kubernetes.io/initialized: "true"
15-
pod.alpha.kubernetes.io/init-containers: '[
16-
]'
1713
spec:
14+
terminationGracePeriodSeconds: 10
1815
containers:
1916
- name: broker
20-
image: solsson/kafka-persistent:0.10.1.0
17+
image: solsson/kafka-persistent:0.10.1@sha256:110f9e866acd4fb9e059b45884c34a210b2f40d6e2f8afe98ded616f43b599f9
2118
ports:
2219
- containerPort: 9092
2320
command:
@@ -30,11 +27,8 @@ spec:
3027
volumeClaimTemplates:
3128
- metadata:
3229
name: datadir
33-
namespace: kafka
34-
annotations:
35-
volume.alpha.kubernetes.io/storage-class: anything
3630
spec:
3731
accessModes: [ "ReadWriteOnce" ]
3832
resources:
3933
requests:
40-
storage: 100Mi
34+
storage: 200Gi

README.md

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
11

22
# Kafka as Kubernetes PetSet
33

4-
Example of three Kafka brokers depending on three Zookeeper instances.
4+
Example of three Kafka brokers depending on five Zookeeper instances.
55

6-
To get consistent service DNS names `kafka-N.broker.kafka`(`.svc.cluster.local`), run everything in a namespace:
6+
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/):
77
```
88
kubectl create -f 00namespace.yml
99
```
1010

1111
## Set up volume claims
1212

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

15-
You need this step in Minikube.
16-
17-
```
18-
./zookeeper/bootstrap/pv.sh
19-
kubectl create -f ./zookeeper/bootstrap/pvc.yml
20-
```
16+
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.
2117

2218
```
2319
./bootstrap/pv.sh
@@ -26,21 +22,27 @@ kubectl create -f ./bootstrap/pvc.yml
2622
kubectl get pvc
2723
```
2824

29-
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.
30-
3125
## Set up Zookeeper
3226

33-
This module contains a copy of `pets/zookeeper/` from https://github.com/kubernetes/contrib.
27+
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),
28+
but it appears tuned for workloads heavier than Kafka topic metadata.
3429

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

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

39+
If you lose your zookeeper cluster, kafka will be unaware that persisted topics exist.
40+
The data is still there, but you need to re-create topics.
41+
4242
## Start Kafka
4343

44+
Assuming you have your PVCs `Bound`, or enabled automatic provisioning (see above), go ahead and:
45+
4446
```
4547
kubectl create -f ./
4648
```

bootstrap/pv-template.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ metadata:
55
name: datadir-kafka-0
66
labels:
77
app: kafka
8-
petindex: "0"
8+
podindex: "0"
99
spec:
1010
accessModes:
1111
- ReadWriteOnce
@@ -20,7 +20,7 @@ metadata:
2020
name: datadir-kafka-1
2121
labels:
2222
app: kafka
23-
petindex: "1"
23+
podindex: "1"
2424
spec:
2525
accessModes:
2626
- ReadWriteOnce
@@ -35,7 +35,7 @@ metadata:
3535
name: datadir-kafka-2
3636
labels:
3737
app: kafka
38-
petindex: "2"
38+
podindex: "2"
3939
spec:
4040
accessModes:
4141
- ReadWriteOnce

zookeeper/00namespace.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
apiVersion: v1
3+
kind: Namespace
4+
metadata:
5+
name: kafka

zookeeper/20zoo-service.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
apiVersion: v1
2+
kind: Service
3+
metadata:
4+
name: zoo
5+
labels:
6+
app: zookeeper
7+
spec:
8+
ports:
9+
- port: 2888
10+
name: peer
11+
- port: 3888
12+
name: leader-election
13+
clusterIP: None
14+
selector:
15+
app: zookeeper

zookeeper/service.yml renamed to zookeeper/30service.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ spec:
99
- port: 2181
1010
name: client
1111
selector:
12-
app: zk
12+
app: zookeeper

zookeeper/50zoo.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
apiVersion: apps/v1beta1
2+
kind: StatefulSet
3+
metadata:
4+
name: zoo
5+
namespace: kafka
6+
spec:
7+
serviceName: "zoo"
8+
replicas: 5
9+
template:
10+
metadata:
11+
labels:
12+
app: zookeeper
13+
spec:
14+
terminationGracePeriodSeconds: 10
15+
containers:
16+
- name: zookeeper
17+
image: solsson/zookeeper-statefulset:3.4.9@sha256:d32b44b32009a69b3450a5216f459e504f1041f587596895219fc04cf22f5546
18+
env:
19+
- name: ZOO_SERVERS
20+
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
21+
ports:
22+
- containerPort: 2181
23+
name: client
24+
- containerPort: 2888
25+
name: peer
26+
- containerPort: 3888
27+
name: leader-election
28+
volumeMounts:
29+
- name: datadir
30+
mountPath: /data
31+
# There's defaults in this folder, such as logging config
32+
#- name: conf
33+
# mountPath: /conf
34+
volumes:
35+
#- name: conf
36+
# emptyDir: {}
37+
- name: datadir
38+
emptyDir: {}

zookeeper/README.md

Lines changed: 0 additions & 85 deletions
This file was deleted.

zookeeper/bootstrap/pv-template.yml

Lines changed: 0 additions & 48 deletions
This file was deleted.

zookeeper/bootstrap/pv.sh

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)