CronJob for running a ConfigMap

1/28/2019

I am trying to write a CronJob for executing a shell script within a ConfigMap for Kafka.

My intention is to reassign partitions at specific intervals of time.

However, I am facing issues with it. I am very new to it. Any help would be appreciated.

cron-job.yaml

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: partition-cron
spec:
  schedule: "*/10 * * * *"
  startingDeadlineSeconds: 20
  successfulJobsHistoryLimit: 5
  jobTemplate:
    spec:
      completions: 2
      template:
        spec:
          containers:
          - name: partition-reassignment
            image: busybox
            command: ["/configmap/runtimeConfig.sh"]
            volumeMounts:
            - name: configmap
              mountPath: /configmap
          restartPolicy: Never
          volumes:
            - name: configmap
              configMap:
                name: configmap-config

configmap-config.yaml

{{- if .Values.topics -}}
{{- $zk := include "zookeeper.url" . -}}
apiVersion: v1
kind: ConfigMap
metadata:
  labels:
    app: {{ template "kafka.fullname" . }}
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    heritage: "{{ .Release.Service }}"
    release: "{{ .Release.Name }}"
  name: {{ template "kafka.fullname" . }}-config
data:
  runtimeConfig.sh: |
    #!/bin/bash
    set -e
    cd /usr/bin
    until kafka-configs --zookeeper {{ $zk }} --entity-type topics --describe || (( count++ >= 6 ))
    do
      echo "Waiting for Zookeeper..."
      sleep 20
    done
    until nc -z {{ template "kafka.fullname" . }} 9092 || (( retries++ >= 6 ))
    do
      echo "Waiting for Kafka..."
      sleep 20
    done
    echo "Applying runtime configuration using {{ .Values.image }}:{{ .Values.imageTag }}"
  {{- range $n, $topic := .Values.topics }}
    {{- if and $topic.partitions $topic.replicationFactor $topic.reassignPartitions }}
    cat << EOF > {{ $topic.name }}-increase-replication-factor.json
  {"version":1, "partitions":[
    {{- $partitions := (int $topic.partitions) }}
    {{- $replicas := (int $topic.replicationFactor) }}
    {{- range $i := until $partitions }}
      {"topic":"{{ $topic.name }}","partition":{{ $i }},"replicas":[{{- range $j := until $replicas }}{{ $j }}{{- if ne $j (sub $replicas 1) }},{{- end }}{{- end }}]}{{- if ne $i (sub $partitions 1) }},{{- end }}
    {{- end }}
  ]}
EOF
kafka-reassign-partitions --zookeeper {{ $zk }} --reassignment-json-file {{ $topic.name }}-increase-replication-factor.json --execute
kafka-reassign-partitions --zookeeper {{ $zk }} --reassignment-json-file {{ $topic.name }}-increase-replication-factor.json --verify
  {{- end }}
{{- end -}}

My intention is to run the runtimeConfig.sh script as a cron job at regular intervals for partition reassignment in Kafka.

I am not sure if my approach is correct.

Also, I have randomly put image: busybox in the cron-job.yaml file. I am not sure about what should I be putting in there.

Information Part

$ kubectl get cronjobs
NAME             SCHEDULE       SUSPEND   ACTIVE    LAST SCHEDULE   AGE
partition-cron   */10 * * * *   False     1         5m              12m
$ kubectl get pods
NAME                                               READY     STATUS              RESTARTS   AGE
elegant-hedgehog-metrics-server-58995fcf8b-2vzg6   1/1       Running             0          5d
my-kafka-0                                         1/1       Running             1          12m
my-kafka-1                                         1/1       Running             0          10m
my-kafka-2                                         1/1       Running             0          9m
my-kafka-config-644f815a-pbpl8                     0/1       Completed           0          12m
my-kafka-zookeeper-0                               1/1       Running             0          12m
partition-cron-1548672000-w728w                    0/1       ContainerCreating   0          5m
$ kubectl logs partition-cron-1548672000-w728w
Error from server (BadRequest): container "partition-reassignment" in pod "partition-cron-1548672000-w728w" is waiting to start: ContainerCreating

Modified Cron Job YAML

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: partition-cron
spec:
  schedule: "*/5 * * * *"
  startingDeadlineSeconds: 20
  successfulJobsHistoryLimit: 5
  jobTemplate:
    spec:
      completions: 1
      template:
        spec:
          containers:
          - name: partition-reassignment
            image: busybox
            command: ["/configmap/runtimeConfig.sh"]
            volumeMounts:
              - name: configmap
                mountPath: /configmap
          restartPolicy: Never
          volumes:
            - name: configmap
              configMap:
                name: {{ template "kafka.fullname" . }}-config

Now, I am getting Status of Cron Job pods as ContainerCannotRun.

-- amankedia
apache-kafka
kubernetes

1 Answer

1/28/2019

You've set the ConfigMap to name: {{ template "kafka.fullname" . }}-config but in the job you are mounting configmap-config. Unless you installed the Helm chart using configmap as the name of the release, that Job will never start.

One way to fix it would be to define the volume as:

          volumes:
            - name: configmap
              configMap:
                name: {{ template "kafka.fullname" . }}-config
-- Stefan P.
Source: StackOverflow