Openshift cron job curl - cron

I have Java Rest application on Openshift. I need to create cron for link in my application. Is it possible?
I tried this example
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: pi
spec:
schedule: "*/1 * * * *"
jobTemplate:
spec:
template:
metadata:
labels:
parent: "cronjobpi"
spec:
containers:
- name: pi
image: perl
command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"]
restartPolicy: OnFailure
this job works well, but I dont know how I need to change it from perl to curl correctly.
Thank you for advice.

I think you can run curl command if you change the image to rhel7 as follows.
...
spec:
containers:
- name: pi
image: rhel7
command: ["curl", "-kvs", "https://www.google.com/"]
...
I hope it help you. :^)

Related

How to execute a function in Node.js using cronjob in Kubernetes

I want to execute a function written in Node.js, lets assume on an image called helloworld every minute on Kubernetes using cronjob.
function helloWorld() {
console.log('hello world!')'
}
I don't understand how I can call it in yaml file.
Config.yaml
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: hello
spec:
schedule: "*/1 * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: hello
image: helloworld
restartPolicy: OnFailure
I think you should use fn. One of the most powerful features of Fn is the ability to use custom defined Docker container images as functions. This feature makes it possible to customize your function’s runtime environment including letting you install any Linux libraries or utilities that your function might need. And thanks to the Fn CLI’s support for Dockerfiles it’s the same user experience as when developing any function.
Deploying your function is how you publish your function and make it accessible to other users and systems. To see the details of what is happening during a function deploy, use the --verbose switch. The first time you build a function of a particular language it takes longer as Fn downloads the necessary Docker images. The --verbose option allows you to see this process.
New image will be created - example node-app-hello.
Then you can configure CronJob.
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: hello-fn-example
spec:
schedule: "*/1 * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: hello
image: example node-app-hello
args:
- ...
restartPolicy: OnFailure
You can also add extra command to run hello container.
Then simply exacute command:
$ kubectl create -f you-cronjob-file.yaml
Take a look: cron-jobs.

Best practice for running a cron job inside an app on Openshift?

I want to run a simple backup of my postgres db deployed in Openshift. What are the best practices for running a cron job? Since systemd is not available on the containers and can only be enabled through a hack, I'd rather use a 'cleaner' approach. Besides cronie or systemd timer units, what options are there? There seems one could enable cron in earlier Openshift versions, however Openshift v4.x doesn't support this feature anymore and the docs only mention the Kubernetes Cron Jobs objects.
Here is what I use:
Dedicated Pod with same image (ensure db dump client is available) and PVC for backup mounted
ConfigMap with backup script
Cronjob running that pod frequently
Here's some example manifests:
PVC:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: database-bkp
namespace: database
annotations:
volume.beta.kubernetes.io/storage-class: "storage-class-name"
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 1Gi
CM:
apiVersion: v1
kind: ConfigMap
metadata:
name: psqldump
namespace: database
labels:
job-name: db-backup
data:
psqldump.sh: |
#!/bin/bash
DBS=$(psql -xl |awk /^Name/'{print $NF}')
for DB in ${DBS}; do
SCHEMAS=$(psql -d ${DB} -xc "\dn" |awk /^Name/'{print $NF}')
for SCHEMA in ${SCHEMAS}; do
echo "Dumping database '${DB}' from Schema '${SCHEMA}' into ${BACKUPDIR}/${PGHOST}_${SCHEMA}_${DB}_${ENVMNT}_$(date -I).sql"
pg_dump -n "${SCHEMA}" ${DB} > ${BACKUPDIR}/${PGHOST}_${SCHEMA}_${DB}_${ENVMNT}_$(date -I).sql
done
done
echo "Deleting dumps older than ${RETENTION} days"
find ${BACKUPDIR} -name "*.sql" -mtime +${RETENTION} -exec rm -rf {} \;
CronJob:
apiVersion: v1
kind: Template
metadata:
name: postgres-backup
namespace: database
objects:
- kind: CronJob
apiVersion: batch/v1beta1
metadata:
name: postgres-backup
namespace: database
spec:
schedule: "0 3 * * *"
successfulJobsHistoryLimit: 1
jobTemplate:
spec:
template:
metadata:
namespace: database
spec:
containers:
- name: postgres-dbbackup
image: "postgres:11"
env:
- name: PGHOST
value: "${_PGHOST}"
- name: PGUSER
value: "${_PGUSER}"
- name: RETENTION
value: "${_RETENTION}"
- name: BACKUPDIR
value: "${_BACKUPDIR}"
command: ["/bin/bash", "-c", "/usr/local/bin/psqldump.sh"]
volumeMounts:
- mountPath: /usr/local/bin
name: psqldump-volume
- mountPath: /backup
name: backup-volume
volumes:
- name: psqldump-volume
configMap:
name: psqldump
defaultMode: 0755
- name: backup-volume
persistentVolumeClaim:
claimName: database-bkp
restartPolicy: Never
parameters:
- name: _PGHOST
value: postgres
- name: _PGUSER
value: postgres
- name: _RETENTION
value: "30"
- name: _BACKUPDIR
value: "/backup"
PGHOST is the pod name of your data base. If you have a dedicated user and password for your backup, export the env vars PGUSER and PGPASS accordingly
Running the cronjob inside the same pod as your db is not a good idea (the pod where the db runs can be killed/respawned etc)
IMHO the best solution is to define a Cronjob in the same project as the db, the Job will use an official OpenShift base image with the OC CLI, and from there execute a script that will connect to the pod where the db runs (oc rsh..) and perform the backup
Or execute a script from outside OCP that will connect to the cluster (with a system account), then executeoc rsh <db pod name> <backup command>

Passing in Arguments to Kubernetes Deployment Pod

I'm trying to create a kubernetes deployment that creates a pod.
I want this pod to run the command "cron start" on creation so that cron is automatically initialized.
This is currently how I am trying to run the command though it clearly isn't working (kubernetes_deployment.yaml)
- containerPort: 8080
command: [ "/bin/sh" ]
args: ["cron start"]
Thank you in advance :)
Maybe you could use Kubernetes CronJobs.
You can set a cron expression.
https://kubernetes.io/es/docs/concepts/workloads/controllers/cron-jobs/
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: hello
spec:
schedule: "*/1 * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: hello
image: busybox
args:
- /bin/sh
- -c
- date; echo Hello from the Kubernetes cluster
restartPolicy: OnFailure

Azure Kubernetes Container Env Variables

so in Docker, I can do a Docker run -e to pass in environment variables.
But how does one do that for Azure Kubernetes Pods? They aren't username/password kinds of variables but more so URLs segments we would want to use during startup.
http://webapi/august where august is what we would want to pass in, then in September, we would want to pass in september.
This aren't the best examples, but it shows what I'm looking for.
Thanks.
There is a clear example on kubernetes documentation for this - https://kubernetes.io/docs/tasks/inject-data-application/define-environment-variable-container/
Short example from there:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.7.9
ports:
- containerPort: 80
env:
- name: DEMO_GREETING
value: "Hello from the environment"
- name: DEMO_FAREWELL
value: "Such a sweet sorrow"
Take a note of env
later if you want to change the variable on the fly - you can use kubectl set env -h command

Creating Cron job in OpenShift online results in: admission webhook "validate.cron.create" denied the request: Prohibited resource for this cluster

I'm learning Openshift Online, I tried to create a cron job from either UI or CLI, both resulted in the below error:
Error from server: admission webhook "validate.cron.create" denied the request: Prohibited resource for this cluster.
I checked permissions for the account, and it had admin rights with CRUD on cronjobs..
I use the same example as documentation:
https://docs.openshift.com/container-platform/3.5/dev_guide/cron_jobs.html
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: pi
spec:
schedule: "*/1 * * * *"
jobTemplate:
spec:
template:
metadata:
labels:
parent: "cronjobpi"
spec:
containers:
- name: pi
image: perl
command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"]
restartPolicy: OnFailure
So as #Daein Park mentioned :
Unfortunately, CronJob can be available on OpenShift Online Pro. So if you use the cluster as Starter plan, you can not create CronJob.
https://docs.openshift.com/online/dev_guide/cron_jobs.html#overview

Resources