Skip to content

Commit 61545c0

Browse files
committed
set up kafka replication
1 parent 9bfe207 commit 61545c0

File tree

5 files changed

+98
-29
lines changed

5 files changed

+98
-29
lines changed

charts/kafka/Chart.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ apiVersion: v2
22
name: kafka
33
description: Kafka chart
44
type: application
5-
version: 0.1.2
5+
version: 0.1.3
66
appVersion: "1.0.0"
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
apiVersion: v1
2+
kind: ConfigMap
3+
metadata:
4+
name: kafka-config
5+
data:
6+
start-kafka.sh: |
7+
#!/bin/bash
8+
set -e
9+
10+
# Derive node ID from pod hostname
11+
NODE_ID="${HOSTNAME##*-}"
12+
13+
# Build quorum voters list for all replicas
14+
VOTERS=""
15+
for i in $(seq 0 $(( {{ .Values.replicas }} - 1 ))); do
16+
[ -n "$VOTERS" ] && VOTERS="${VOTERS},"
17+
VOTERS="${VOTERS}${i}@kafka-${i}.kafka:9093"
18+
done
19+
20+
ADVERTISED="PLAINTEXT://${HOSTNAME}.kafka.{{ .Release.Namespace }}.svc.cluster.local:9092"
21+
22+
echo "Starting Kafka with:"
23+
echo " NODE_ID=${NODE_ID}"
24+
echo " ADVERTISED_LISTENERS=${ADVERTISED}"
25+
echo " CONTROLLER_QUORUM_VOTERS=${VOTERS}"
26+
27+
# Generate server.properties
28+
cat > /tmp/server.properties <<EOF
29+
node.id=${NODE_ID}
30+
process.roles=controller,broker
31+
listeners=PLAINTEXT://:9092,CONTROLLER://:9093
32+
advertised.listeners=${ADVERTISED}
33+
controller.quorum.voters=${VOTERS}
34+
controller.listener.names=CONTROLLER
35+
listener.security.protocol.map=CONTROLLER:PLAINTEXT,PLAINTEXT:PLAINTEXT
36+
inter.broker.listener.name=PLAINTEXT
37+
log.dirs=/var/lib/kafka/data
38+
auto.create.topics.enable=true
39+
offsets.topic.replication.factor={{ .Values.replicationFactor }}
40+
transaction.state.log.replication.factor={{ .Values.replicationFactor }}
41+
transaction.state.log.min.isr={{ .Values.minIsr }}
42+
EOF
43+
44+
# Format storage if not already formatted
45+
if [ ! -f /var/lib/kafka/data/meta.properties ]; then
46+
echo "Formatting storage with cluster ID: ${CLUSTER_ID}"
47+
/opt/kafka/bin/kafka-storage.sh format \
48+
--cluster-id "${CLUSTER_ID}" \
49+
--config /tmp/server.properties
50+
fi
51+
52+
exec /opt/kafka/bin/kafka-server-start.sh /tmp/server.properties

charts/kafka/templates/pdb.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
apiVersion: policy/v1
2+
kind: PodDisruptionBudget
3+
metadata:
4+
name: kafka
5+
spec:
6+
maxUnavailable: 1
7+
selector:
8+
matchLabels:
9+
app: kafka

charts/kafka/templates/statefulset.yaml

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ metadata:
44
name: kafka
55
spec:
66
serviceName: "kafka"
7-
replicas: 1
7+
replicas: {{ .Values.replicas }}
8+
podManagementPolicy: Parallel
89
selector:
910
matchLabels:
1011
app: kafka
@@ -13,41 +14,19 @@ spec:
1314
labels:
1415
app: kafka
1516
spec:
17+
terminationGracePeriodSeconds: 120
1618
containers:
1719
- name: kafka
1820
image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
21+
command: ["/bin/bash", "/opt/kafka-config/start-kafka.sh"]
1922
env:
20-
- name: KAFKA_NODE_ID
21-
value: "0"
22-
- name: KAFKA_PROCESS_ROLES
23-
value: "controller,broker"
24-
- name: KAFKA_LISTENERS
25-
value: "PLAINTEXT://:9092,CONTROLLER://:9093"
26-
- name: KAFKA_ADVERTISED_LISTENERS
27-
value: "PLAINTEXT://kafka-0.kafka.{{ .Release.Namespace }}.svc.cluster.local:9092"
28-
- name: KAFKA_CONTROLLER_QUORUM_VOTERS
29-
value: "0@kafka-0.kafka:9093"
30-
- name: KAFKA_CONTROLLER_LISTENER_NAMES
31-
value: "CONTROLLER"
32-
- name: KAFKA_LISTENER_SECURITY_PROTOCOL_MAP
33-
value: "CONTROLLER:PLAINTEXT,PLAINTEXT:PLAINTEXT"
34-
- name: KAFKA_INTER_BROKER_LISTENER_NAME
35-
value: "PLAINTEXT"
36-
- name: KAFKA_AUTO_CREATE_TOPICS_ENABLE
37-
value: "true"
38-
- name: KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR
39-
value: "1"
40-
- name: KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR
41-
value: "1"
42-
- name: KAFKA_TRANSACTION_STATE_LOG_MIN_ISR
43-
value: "1"
44-
- name: KAFKA_HEAP_OPTS
45-
value: {{ .Values.heapOpts | quote }}
4623
- name: CLUSTER_ID
4724
valueFrom:
4825
secretKeyRef:
4926
name: {{ .Values.secrets }}
5027
key: CLUSTER_ID
28+
- name: KAFKA_HEAP_OPTS
29+
value: {{ .Values.heapOpts | quote }}
5130
resources:
5231
requests:
5332
cpu: {{ .Values.resources.requests.cpu }}
@@ -57,10 +36,30 @@ spec:
5736
memory: {{ .Values.resources.limits.memory }}
5837
ports:
5938
- containerPort: 9092
39+
name: client
6040
- containerPort: 9093
41+
name: controller
42+
readinessProbe:
43+
tcpSocket:
44+
port: 9092
45+
initialDelaySeconds: 30
46+
periodSeconds: 10
47+
livenessProbe:
48+
tcpSocket:
49+
port: 9092
50+
initialDelaySeconds: 60
51+
periodSeconds: 15
52+
failureThreshold: 5
6153
volumeMounts:
6254
- name: data
6355
mountPath: /var/lib/kafka/data
56+
- name: config
57+
mountPath: /opt/kafka-config
58+
volumes:
59+
- name: config
60+
configMap:
61+
name: kafka-config
62+
defaultMode: 0755
6463
volumeClaimTemplates:
6564
- metadata:
6665
name: data

charts/kafka/values.yaml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
1+
# Image
12
image:
23
repository: apache/kafka
3-
tag: 3.7.0
4+
tag: 3.9.0
5+
6+
# Cluster sizing
7+
replicas: 3
8+
replicationFactor: 3
9+
minIsr: 2
10+
11+
# Resources per broker
412
storage: 20Gi
513
resources:
614
requests:
@@ -10,4 +18,5 @@ resources:
1018
cpu: "1000m"
1119
memory: "1Gi"
1220
heapOpts: "-Xmx512M -Xms512M"
21+
1322
secrets: "kafka-secrets"

0 commit comments

Comments
 (0)