Cronjob with Service

Hello I’m using Kubernetes with the sakuli Framework for checking my home automation. It would be cool if a cronjob can connect to a service. Have anyone experience with that and can explain how he do this? I tested it with the normal definition but I can’t connect to the service on the public port.

Hi @Theroth!

Do you mean a Cron Job Pod connecting to a different service within your cluster?

Or do you mean having a Service target a pod spawned from a CronJob?

Thanks!

1 Like

Hi @jeefy,

I have cronjobs which starts every 5 minutes a container (pod) with a sakuli framework inside. This framework will start a vnc view on port 6901 where I can see what this framework does. When I want to see this I want to create a service with a cronjob pod as a target. So when there is no pod it should not avaiable and if the pod comes up the service should show the port 6901 from this pod. I tried it now many times and it failed every time.

So long as the podspec has a label like “app=cronjob-vnc” and the service targets “app=cronjob-vnc” that should do the trick.

The service will persist, but will only resolve when there is a pod that matches the target.

Does that make sense? I can knock out a quick example if needed :slight_smile:

1 Like

@jeefy I tried it like you said a few times, but never had success. Idon’t know if an example will help me, because I have other services where this work very well, just for the cronjobs this don’t work.
But if you have time you can write an example for me.

Thank you jeefy

[EDIT] I think an example would be great, because I think that I set the labels to a wrong place and I don’t know where to place these lines.

1 Like

Here ya go! Hopefully this helps.

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: vnc-cronjob
spec:
  schedule: "15 * * * *"
  jobTemplate:
    spec:
      metadata:
        labels:
          app: vnc-cron
      template:
        spec:
          containers:
          - name: vnc-example
            image: busybox
            args:
            - /bin/sh
            - -c
            - date; echo This would be running VNC
            ports:
            - containerPort: 5900
          restartPolicy: OnFailure
---
kind: Service
apiVersion: v1
metadata:
  name: vnc-service
spec:
  type: NodePort
  selector:
    app: vnc-cron
  ports:
  - protocol: TCP
    port: 5900
2 Likes

Thanks for the example @jeefy, but I have a problem. This doesn’t work. I tried it with my cronjobs and then with yours all fails with this text.

error: error validating "test.yaml": error validating data: ValidationError(CronJob.spec.jobTemplate.spec): unknown field "metadata" in io.k8s.api.batch.v1.JobSpec; if you choose to ignore these errors, turn validation off with --validate=false

Sorry, wrote that up in haste. This should be better.

Try this.

spec:
  schedule: "15 * * * *"
  jobTemplate:
    spec:
      template:
        metadata:
          labels:
            app: vnc-cron
        spec:
1 Like

This works :blush:

Thank you very much @jeefy

1 Like