mpen
mpen

Reputation: 283325

How to run one-off job?

I've found 2 different ways to run a one-off command in my kubernetes cluster:

Method 1

kubectl apply -f kubernetes/migrate.job.yaml
kubectl wait --for=condition=complete --timeout=600s job/migrate-job
kubectl delete job/migrate-job

The problem with this is (a) it doesn't show me the output which I like to see, and (b) it's 3 commands

Method 2

kubectl run migrate --stdin --tty --rm --restart=Never --image=example.org/app/php:v-$(VERSION) --command -- ./artisan -vvv migrate

This almost works except I also need a volume mount to run this command, which AFAIK would require a rather lengthy --overrides arg. If I could pull the override in from a file instead it'd probably work well. Can I do that?

I also need to to return the exit code if the command fails.

Upvotes: 0

Views: 1164

Answers (1)

mpen
mpen

Reputation: 283325

There's an open ticket for this: https://github.com/kubernetes/kubernetes/issues/63214

A short term solution is to run your job like this:

kubectl run migrate --stdin --tty --rm --restart=Never --image=example.org/app/php:v-$(VERSION) --overrides="$(cat kubernetes/migrate.pod.yaml | y2j)"

Using y2j to convert YAML to JSON so that I can use a standard pod manifest.

migrate.pod.yaml looks like:

apiVersion: v1
kind: Pod
metadata:
    name: migrate-pod
spec:
    volumes:
        -   name: migrate-secrets-volume
            secret:
                secretName: google-service-account
    containers:
        -   name: migrate-container
            image: example.org/app/php
            command: ["./artisan",  "-vvv", "migrate"]
            stdin: true
            stdinOnce: true
            tty: true
            envFrom:
                -   secretRef:
                        name: dev-env
            volumeMounts:
                -   name: migrate-secrets-volume
                    mountPath: /app/secrets
                    readOnly: true
    restartPolicy: Never
    imagePullSecrets:
        -   name: regcred

Upvotes: 1

Related Questions