Run Kubernetes/Openshift cronjob with container user id

9/14/2020

I am using Openshift to deploy a django application which uses pyodbc for connecting to external database. Currently I wanted to schedule a cronjob in openshift using yaml file. The cronjob gets created with no problem but throws this error when run: ('IM004', "IM004Driver ManagerDriver's SQLAllocHandle on SQL_HANDLE_HENV failed (0) (SQLDriverConnect)")

This error occcured before as Openshift overrides uid when running a container. I overcame this error by following this workaround: https://github.com/VeerMuchandi/mssql-openshift-tools/blob/master/mssql-client/uid_entrypoint.sh

This error pops up again when the cronjob is run and this maybe due to same uid issue. Following is my yaml file for scheduling cronjob:

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: samplecron
spec:
  securityContext:
    runAsUser: 1001
    runAsGroup: 0
  schedule: "*/5 * * * *"  
  concurrencyPolicy: "Forbid" 
  startingDeadlineSeconds: 60 
  suspend:            
  successfulJobsHistoryLimit: 3 
  failedJobsHistoryLimit: 3     
  jobTemplate:             
    spec:
      template:
        metadata:
          labels:          
            parent: "cronjobpi"
        spec:
          containers:
          - name: samplecron
            image: docker-registry.default.svc:5000/image-name
            volumeMounts:
            - mountPath: /path-to-mount
              name: "volume-name"
            command: [ "python3", "/script.py" ]
            

          volumes:
          - name: "vol-name"
          restartPolicy: Never 

Can someone suggest how I can provide same userid's information in yaml file of cronjob or any other way of solving this issue?

-- Abhilash Challa
containers
docker
kubernetes
openshift
pyodbc

1 Answer

10/10/2020

Was able to solve the issue using the entrypoint script I mentioned above and I included the the command to run the python script inside the .sh entrypoint script and instead of command: "python3", "/script.py" was used..The python script is used to connect to a DB server using pyodbc. pyodbc.connect() causes an issue if UID of container of is not written in etc/passwd which is done by entrypoint script mentioned above.

-- Abhilash Challa
Source: StackOverflow