How can I run cronjobs in kubernetes PHP pods?

5/22/2018

I have Kubernetes replication controller which is running 2 pods of PHP applicaton. Now, I need to schedule 3 cronjobs which run some PHP scripts in those pods. How can I achieve it? (The base image for the container is centos:7).

-- Arun
centos7
containers
docker
google-kubernetes-engine
kubernetes

1 Answer

5/23/2018

As said in comments your best option is create Kubernetes CronJobs.

If you don't want to expose you PHP functions, you can always use a container that execute commands in your PHP pods.

Here is an example that create a cronjob. This cronjob runs kubectl exec for every pod with label app=my-php, calling ls in each pod.

  apiVersion: batch/v1beta1
  kind: CronJob
  metadata:
    name: hello-cron
  spec:
    schedule: "*/1 * * * *"
    jobTemplate:
      spec:
        template:
          spec:
            containers:
            - name: kubectl
              image: lachlanevenson/k8s-kubectl
              command:
                - /bin/sh
                - -c
              args: 
                - kubectl get pod -l app=my-php -o jsonpath='{range .items[*]}{.metadata.name}{"\n"}{end}' | xargs -i kubectl exec {} ls
            restartPolicy: Never
-- Ignacio Millán
Source: StackOverflow