From f277bc09e9e113c34830c66397dab1728def198e Mon Sep 17 00:00:00 2001 From: Staffan Olsson Date: Sat, 7 Apr 2018 00:04:50 +0200 Subject: [PATCH 1/9] Transition from init container to sidecar, for future dynamic config support. Can be evaluated manually now (with Kafka 1.1.0) by exec to config pod and edit of server.properties there. If the init container is slow, which is unlikely, kafka will crash loop until server.properties exists and upon existence start normally. --- kafka/10broker-config.yml | 3 +++ kafka/50kafka.yml | 5 ++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/kafka/10broker-config.yml b/kafka/10broker-config.yml index 661eb520..254b22d2 100644 --- a/kafka/10broker-config.yml +++ b/kafka/10broker-config.yml @@ -45,6 +45,9 @@ data: fi } + echo "Completed initial config. Staying alive to support Kafka 1.1.0 dynamic config updates" + tail -f /dev/null + server.properties: |- ############################# Log Basics ############################# diff --git a/kafka/50kafka.yml b/kafka/50kafka.yml index c79e6521..7ea94b54 100644 --- a/kafka/50kafka.yml +++ b/kafka/50kafka.yml @@ -18,8 +18,8 @@ spec: annotations: spec: terminationGracePeriodSeconds: 30 - initContainers: - - name: init-config + containers: + - name: config image: solsson/kafka-initutils@sha256:18bf01c2c756b550103a99b3c14f741acccea106072cd37155c6d24be4edd6e2 env: - name: NODE_NAME @@ -40,7 +40,6 @@ spec: mountPath: /etc/kafka-configmap - name: config mountPath: /etc/kafka - containers: - name: broker image: solsson/kafka:1.0.1@sha256:1a4689d49d6274ac59b9b740f51b0408e1c90a9b66d16ad114ee9f7193bab111 env: From 5823c40ee7849b7130d49c616e6301889ea68208 Mon Sep 17 00:00:00 2001 From: Staffan Olsson Date: Sat, 7 Apr 2018 00:15:23 +0200 Subject: [PATCH 2/9] Splits init into static files copy and var replacement --- kafka/10broker-config.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/kafka/10broker-config.yml b/kafka/10broker-config.yml index 254b22d2..f27c9f3e 100644 --- a/kafka/10broker-config.yml +++ b/kafka/10broker-config.yml @@ -7,9 +7,10 @@ data: init.sh: |- #!/bin/bash set -x + cp /etc/kafka-configmap/log4j.properties /etc/kafka/ KAFKA_BROKER_ID=${HOSTNAME##*-} - cp -Lur /etc/kafka-configmap/* /etc/kafka/ + cp /etc/kafka-configmap/server.properties /etc/kafka/ sed -i "s/#init#broker.id=#init#/broker.id=$KAFKA_BROKER_ID/" /etc/kafka/server.properties LABELS="kafka-broker-id=$KAFKA_BROKER_ID" From 3665a63e0464718f17ee3137e8654d2db6736483 Mon Sep 17 00:00:00 2001 From: Staffan Olsson Date: Sat, 7 Apr 2018 00:59:39 +0200 Subject: [PATCH 3/9] Quoted command args in a variable is apparently tricky --- kafka/10broker-config.yml | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/kafka/10broker-config.yml b/kafka/10broker-config.yml index f27c9f3e..88f731c0 100644 --- a/kafka/10broker-config.yml +++ b/kafka/10broker-config.yml @@ -10,22 +10,20 @@ data: cp /etc/kafka-configmap/log4j.properties /etc/kafka/ KAFKA_BROKER_ID=${HOSTNAME##*-} - cp /etc/kafka-configmap/server.properties /etc/kafka/ - sed -i "s/#init#broker.id=#init#/broker.id=$KAFKA_BROKER_ID/" /etc/kafka/server.properties - + SEDS=" -e 's/#init#broker.id=#init#/broker.id=$KAFKA_BROKER_ID/'" LABELS="kafka-broker-id=$KAFKA_BROKER_ID" ANNOTATIONS="" hash kubectl 2>/dev/null || { - sed -i "s/#init#broker.rack=#init#/#init#broker.rack=# kubectl not found in path/" /etc/kafka/server.properties + SEDS="$SEDS -e 's/#init#broker.rack=#init#/#init#broker.rack=# kubectl not found in path/'" } && { ZONE=$(kubectl get node "$NODE_NAME" -o=go-template='{{index .metadata.labels "failure-domain.beta.kubernetes.io/zone"}}') if [ $? -ne 0 ]; then - sed -i "s/#init#broker.rack=#init#/#init#broker.rack=# zone lookup failed, see -c init-config logs/" /etc/kafka/server.properties + SEDS="$SEDS -e 's/#init#broker.rack=#init#/#init#broker.rack=# zone lookup failed, see -c init-config logs/'" elif [ "x$ZONE" == "x" ]; then - sed -i "s/#init#broker.rack=#init#/#init#broker.rack=# zone label not found for node $NODE_NAME/" /etc/kafka/server.properties + SEDS="$SEDS -e 's/#init#broker.rack=#init#/#init#broker.rack=# zone label not found for node $NODE_NAME/'" else - sed -i "s/#init#broker.rack=#init#/broker.rack=$ZONE/" /etc/kafka/server.properties + SEDS="$SEDS -e 's/#init#broker.rack=#init#/broker.rack=$ZONE/'" LABELS="$LABELS kafka-broker-rack=$ZONE" fi @@ -34,7 +32,7 @@ data: echo "Outside (i.e. cluster-external access) host lookup command failed" else OUTSIDE_PORT=3240${KAFKA_BROKER_ID} - sed -i "s|#init#advertised.listeners=OUTSIDE://#init#|advertised.listeners=OUTSIDE://${OUTSIDE_HOST}:${OUTSIDE_PORT}|" /etc/kafka/server.properties + SEDS="$SEDS -e 's|#init#advertised.listeners=OUTSIDE://#init#|advertised.listeners=OUTSIDE://${OUTSIDE_HOST}:${OUTSIDE_PORT}|'" ANNOTATIONS="$ANNOTATIONS kafka-listener-outside-host=$OUTSIDE_HOST kafka-listener-outside-port=$OUTSIDE_PORT" fi @@ -45,6 +43,7 @@ data: kubectl -n $POD_NAMESPACE annotate pod $POD_NAME $ANNOTATIONS || echo "Failed to annotate $POD_NAMESPACE.$POD_NAME - RBAC issue?" fi } + sed "$SEDS" /etc/kafka-configmap/server.properties > /etc/kafka/server.properties echo "Completed initial config. Staying alive to support Kafka 1.1.0 dynamic config updates" tail -f /dev/null From e4c5b1969c350f0c4a9ed5f7fa798e2eb8517404 Mon Sep 17 00:00:00 2001 From: Staffan Olsson Date: Sat, 7 Apr 2018 01:37:49 +0200 Subject: [PATCH 4/9] A more useful data structure, but still not whitespace compatible --- kafka/10broker-config.yml | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/kafka/10broker-config.yml b/kafka/10broker-config.yml index 88f731c0..eb503ed7 100644 --- a/kafka/10broker-config.yml +++ b/kafka/10broker-config.yml @@ -10,20 +10,20 @@ data: cp /etc/kafka-configmap/log4j.properties /etc/kafka/ KAFKA_BROKER_ID=${HOSTNAME##*-} - SEDS=" -e 's/#init#broker.id=#init#/broker.id=$KAFKA_BROKER_ID/'" + SEDS=("s/#init#broker.id=#init#/broker.id=$KAFKA_BROKER_ID/") LABELS="kafka-broker-id=$KAFKA_BROKER_ID" ANNOTATIONS="" hash kubectl 2>/dev/null || { - SEDS="$SEDS -e 's/#init#broker.rack=#init#/#init#broker.rack=# kubectl not found in path/'" + SEDS+=("s/#init#broker.rack=#init#/#init#broker.rack=# kubectl not found in path/") } && { ZONE=$(kubectl get node "$NODE_NAME" -o=go-template='{{index .metadata.labels "failure-domain.beta.kubernetes.io/zone"}}') if [ $? -ne 0 ]; then - SEDS="$SEDS -e 's/#init#broker.rack=#init#/#init#broker.rack=# zone lookup failed, see -c init-config logs/'" + SEDS+=("s/#init#broker.rack=#init#/#init#broker.rack=# zone lookup failed, see -c init-config logs/") elif [ "x$ZONE" == "x" ]; then - SEDS="$SEDS -e 's/#init#broker.rack=#init#/#init#broker.rack=# zone label not found for node $NODE_NAME/'" + SEDS+=("s/#init#broker.rack=#init#/#init#broker.rack=# zone label not found for node $NODE_NAME/") else - SEDS="$SEDS -e 's/#init#broker.rack=#init#/broker.rack=$ZONE/'" + SEDS+=("s/#init#broker.rack=#init#/broker.rack=$ZONE/") LABELS="$LABELS kafka-broker-rack=$ZONE" fi @@ -32,7 +32,7 @@ data: echo "Outside (i.e. cluster-external access) host lookup command failed" else OUTSIDE_PORT=3240${KAFKA_BROKER_ID} - SEDS="$SEDS -e 's|#init#advertised.listeners=OUTSIDE://#init#|advertised.listeners=OUTSIDE://${OUTSIDE_HOST}:${OUTSIDE_PORT}|'" + SEDS+=("s|#init#advertised.listeners=OUTSIDE://#init#|advertised.listeners=OUTSIDE://${OUTSIDE_HOST}:${OUTSIDE_PORT}|") ANNOTATIONS="$ANNOTATIONS kafka-listener-outside-host=$OUTSIDE_HOST kafka-listener-outside-port=$OUTSIDE_PORT" fi @@ -43,7 +43,7 @@ data: kubectl -n $POD_NAMESPACE annotate pod $POD_NAME $ANNOTATIONS || echo "Failed to annotate $POD_NAMESPACE.$POD_NAME - RBAC issue?" fi } - sed "$SEDS" /etc/kafka-configmap/server.properties > /etc/kafka/server.properties + sed ${SEDS[@]} /etc/kafka-configmap/server.properties > /etc/kafka/server.properties echo "Completed initial config. Staying alive to support Kafka 1.1.0 dynamic config updates" tail -f /dev/null From beddacec22281515790036569a58920cfb1c980b Mon Sep 17 00:00:00 2001 From: Staffan Olsson Date: Sat, 7 Apr 2018 01:50:06 +0200 Subject: [PATCH 5/9] Works, though my yaml highlighting gets confused --- kafka/10broker-config.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/kafka/10broker-config.yml b/kafka/10broker-config.yml index eb503ed7..ecd84c85 100644 --- a/kafka/10broker-config.yml +++ b/kafka/10broker-config.yml @@ -43,7 +43,8 @@ data: kubectl -n $POD_NAMESPACE annotate pod $POD_NAME $ANNOTATIONS || echo "Failed to annotate $POD_NAMESPACE.$POD_NAME - RBAC issue?" fi } - sed ${SEDS[@]} /etc/kafka-configmap/server.properties > /etc/kafka/server.properties + printf '%s\n' "${SEDS[@]}" > /tmp/kafka-init-config.sed + sed -f /tmp/kafka-init-config.sed /etc/kafka-configmap/server.properties > /etc/kafka/server.properties echo "Completed initial config. Staying alive to support Kafka 1.1.0 dynamic config updates" tail -f /dev/null From 0900b91baf0388178f40d10dc3b65726b22e8729 Mon Sep 17 00:00:00 2001 From: Staffan Olsson Date: Sat, 7 Apr 2018 01:54:53 +0200 Subject: [PATCH 6/9] Kafka 1.1.0 --- kafka/10broker-config.yml | 2 +- kafka/50kafka.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/kafka/10broker-config.yml b/kafka/10broker-config.yml index ecd84c85..dabc0e27 100644 --- a/kafka/10broker-config.yml +++ b/kafka/10broker-config.yml @@ -46,7 +46,7 @@ data: printf '%s\n' "${SEDS[@]}" > /tmp/kafka-init-config.sed sed -f /tmp/kafka-init-config.sed /etc/kafka-configmap/server.properties > /etc/kafka/server.properties - echo "Completed initial config. Staying alive to support Kafka 1.1.0 dynamic config updates" + echo "Completed initial config. Staying alive to support Kafka 1.1+ dynamic config updates" tail -f /dev/null server.properties: |- diff --git a/kafka/50kafka.yml b/kafka/50kafka.yml index 7ea94b54..e49d6be5 100644 --- a/kafka/50kafka.yml +++ b/kafka/50kafka.yml @@ -41,7 +41,7 @@ spec: - name: config mountPath: /etc/kafka - name: broker - image: solsson/kafka:1.0.1@sha256:1a4689d49d6274ac59b9b740f51b0408e1c90a9b66d16ad114ee9f7193bab111 + image: solsson/kafka:1.1@sha256:ba863ca7dc28563930584e37f93d57c2cbf3f46b1c1fa104fe8af7bcc0c31df4 env: - name: KAFKA_LOG4J_OPTS value: -Dlog4j.configuration=file:/etc/kafka/log4j.properties From baa2011b4452e826f3e2c950c7a746cccb505427 Mon Sep 17 00:00:00 2001 From: Staffan Olsson Date: Sat, 7 Apr 2018 02:06:13 +0200 Subject: [PATCH 7/9] Avoids potential timing issues with broker start --- kafka/10broker-config.yml | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/kafka/10broker-config.yml b/kafka/10broker-config.yml index dabc0e27..8c259973 100644 --- a/kafka/10broker-config.yml +++ b/kafka/10broker-config.yml @@ -44,10 +44,9 @@ data: fi } printf '%s\n' "${SEDS[@]}" > /tmp/kafka-init-config.sed - sed -f /tmp/kafka-init-config.sed /etc/kafka-configmap/server.properties > /etc/kafka/server.properties - - echo "Completed initial config. Staying alive to support Kafka 1.1+ dynamic config updates" - tail -f /dev/null + sed -f /tmp/kafka-init-config.sed /etc/kafka-configmap/server.properties > /etc/kafka/server.properties.tmp + [ $? -eq 0 ] && mv /etc/kafka/server.properties.tmp /etc/kafka/server.properties + [ -f /etc/kafka/server.properties ] && echo "Ready for Kafka start" && tail -f /dev/null server.properties: |- ############################# Log Basics ############################# From 924c2aa474d88f697306a33b94239e4dd687a127 Mon Sep 17 00:00:00 2001 From: Staffan Olsson Date: Sun, 8 Apr 2018 07:11:02 +0200 Subject: [PATCH 8/9] Avoids a temp file --- kafka/10broker-config.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/kafka/10broker-config.yml b/kafka/10broker-config.yml index 8c259973..dbd69bc0 100644 --- a/kafka/10broker-config.yml +++ b/kafka/10broker-config.yml @@ -43,8 +43,7 @@ data: kubectl -n $POD_NAMESPACE annotate pod $POD_NAME $ANNOTATIONS || echo "Failed to annotate $POD_NAMESPACE.$POD_NAME - RBAC issue?" fi } - printf '%s\n' "${SEDS[@]}" > /tmp/kafka-init-config.sed - sed -f /tmp/kafka-init-config.sed /etc/kafka-configmap/server.properties > /etc/kafka/server.properties.tmp + printf '%s\n' "${SEDS[@]}" | sed -f - /etc/kafka-configmap/server.properties > /etc/kafka/server.properties.tmp [ $? -eq 0 ] && mv /etc/kafka/server.properties.tmp /etc/kafka/server.properties [ -f /etc/kafka/server.properties ] && echo "Ready for Kafka start" && tail -f /dev/null From a131a623fe3ff4803b17e002b46ef325a0b06b00 Mon Sep 17 00:00:00 2001 From: Staffan Olsson Date: Sun, 8 Apr 2018 07:21:06 +0200 Subject: [PATCH 9/9] An invite to experiment --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 009dcb33..606387d2 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,7 @@ If you begin to rely on this kafka setup we recommend you fork, for example to e | tag | k8s ≥ | highlights | | ----- | ------ | ---------- | +| 4.x | 1.9+ | Kafka 1.1 dynamic config | | v3.2 | 1.9.4, 1.8.9, 1.7.14 | Required for read-only ConfigMaps [#162](https://github.com/Yolean/kubernetes-kafka/issues/162) [#163](https://github.com/Yolean/kubernetes-kafka/pull/163) [k8s #58720](https://github.com/kubernetes/kubernetes/pull/58720) | | v3.1 | 1.8 | The painstaking path to `min.insync.replicas`=2 | | v3.0 | 1.8 | [Outside access](#78), [modern manifests](#84), [bootstrap.kafka](#52) |