I had to create a cronjob that calls an internal service url to trigger a process. This was necessary to make sure the process runs only once and not multiple times if the pod exists multiple times. For that purpose I created a kubernetes cronjob. I didn’t want to create a new image, so I used the existing curlimage/curl from the docker repository.
Services expose their host and port as environment variables. Unfortunately it was not possible for me to just use those variables as input commands for the
curl command and I needed to find a workaround. It took me a couple hours of me freetime, but I finally found a decent solution.
My service was called nginx_service therefore the host was
NGINX_SERVICE_SERVICE_HOST and the port
NGINX_SERVICE_SERVICE_PORT. I came up with the following yaml file.
apiVersion: batch/v1beta1 kind: CronJob metadata: name: nginx-cronjob spec: concurrencyPolicy: Forbid schedule: "*/1 * * * *" jobTemplate: spec: template: spec: containers: - name: curlimage image: curlimages/curl imagePullPolicy: IfNotPresent command: - sh - -c args: - curl $SERVICE_URL env: - name: SERVICE_URL value: "$(NGINX_SERVICE_SERVICE_HOST):$(NGINX_SERVICE_SERVICE_PORT)/startjob" restartPolicy: OnFailure