Search code examples
cronopenshiftschedulekubernetes-pod

Pod scheduling in Openshift/Kubernetes


I would like to apply a daily schedule to a pod so that it is created at 9 AM and destroyed at 9 PM.

I believe this is possible using two almost-identical CronJobs set with the appropriate schedules.

The Jobs create a pod that runs the following commands in the morning and evening respectively:

...
command: ["oc scale dc my-dc",  "--replicas=1"] # The command to execute in the pod
...
...
command: ["oc scale dc my-dc",  "--replicas=0"] # The command to execute in the pod
...

To execute these commands I'm planning to use this image since it comes with the Openshift CLI installed: https://hub.docker.com/r/ebits/openshift-client

Can anyone advise any recommendations to carry out this task? Are there any aspects I didn't consider?

Thanks


Solution

  • I'll share the method that worked for me.

    The CronJob I wrote creates a container based on this image (remember to specify the Openshift version using the right tag) and runs the typical oc commands to login and scale the DC mentioned above.

    The tricky part for me was to understand the correct syntax execute the commands in the container after creation.

    Anyway, below I included the container definition I specified in the CronJob yaml file:

    ...
    spec:
      containers:
        - name: oc-cli
          image: 'registry.access.redhat.com/openshift3/ose-cli:v3.11.153' # <===== SPECIFY YOUR OPENSHIFT VERSION
          command:
            - /bin/sh
            - '-c'
            - >-
              oc login https://<LOGIN URL>/
              --insecure-skip-tls-verify -u <LOGIN USERNAME> -p <LOGIN PASSWORD> ; oc scale
              dc/example --replicas=1 -n cronjob-test ;
    ...
    

    Writing the password explicitly might be a problem so I added a Secret and made a reference to it in the same CronJob so that the secret data were part of the environment variables:

    ...
    spec:
      containers:
        - name: oc-cli
          image: 'registry.access.redhat.com/openshift3/ose-cli:v3.11.153' # <===== SPECIFY YOUR OPENSHIFT VERSION
          command:
            - /bin/sh
            - '-c'
            - >-
              oc login $OC_URL --insecure-skip-tls-verify -u $OC_USER -p
              $OC_PASSWORD ; oc scale dc/example --replicas=5 -n
              cronjob-test ;
          env:
            - name: OC_USER
              valueFrom:
                secretKeyRef:
                  name: oc-login-credentials
                  key: username
            - name: OC_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: oc-login-credentials
                  key: password
            - name: OC_URL
              valueFrom:
                secretKeyRef:
                  name: oc-login-credentials
                  key: url
    ...
    

    Unfortunately, Red Hat's documentation on the matter is not very helpful as it only provides one example.

    Kubernetes' docs were slightly more useful instead.