The easiest way to run a simple one-shot job on Kubernetes.
run-job πββοΈ does the following with a simple YAML file definition:
- Creates a Kubernetes Job
- Watches until it passes or fails
- Collects its logs (if available)
- Deletes the job
It's primary usecase is for checking OpenFaaS installations for customers where it requires a service account to access various resources in a controlled way.
The first example is a real-world job for OpenFaaS customers, you probably won't run this example yourself, but read over it to learn the syntax and options. Then feel free to try Example 2 and 3, which anyone should be able to run.
The image:
field in the Job YAML is for a container image that can be pulled by the cluster.
Note: the examples in this repo are built with the
faas-cli publish
command because it can create multi-arch container images that work on PCs and ARM devices. You can build your images however you like, or by manually typing in various buildx commands for multi-arch.
Create a job.yaml
file:
name: checker
image: ghcr.io/openfaas/config-checker:latest
namespace: openfaas
sa: openfaas-checker
Download run-job from the releases page, or use arkade:
$ arkade get run-job
Then start the job defined in job.yaml
and export the logs to a report.txt
file:
$ run-job \
-f job.yaml \
-out report.txt
In order to access the K8s API, an RBAC file is required along with a serviceAccount
field in the job YAML.
The command kubectl get nodes -o wide
is hard-coded in the Dockerfile, however, in a future version, you'll be able to set this in the job's YAML file instead.
$ kubectl apply ./examples/kubectl/rbac.yaml
$ run-job -f ./examples/kubectl/kubectl_get_nodes_job.yaml
Created job get-nodes.default (4097ed06-9422-41c2-86ac-6d4a447d10ab)
....
Job get-nodes.default (4097ed06-9422-41c2-86ac-6d4a447d10ab) succeeded
Deleted job get-nodes
Recorded: 2022-09-05 21:43:57.875629 +0000 UTC
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
k3s-server-1 Ready control-plane,etcd,master 25h v1.24.4+k3s1 192.168.2.1 <none> Raspbian GNU/Linux 10 (buster) 5.10.103-v7l+ containerd://1.6.6-k3s1
k3s-server-2 Ready control-plane,etcd,master 25h v1.24.4+k3s1 192.168.2.2 <none> Raspbian GNU/Linux 10 (buster) 5.10.103-v7l+ containerd://1.6.6-k3s1
k3s-server-3 Ready control-plane,etcd,master 25h v1.24.4+k3s1 192.168.2.3 <none> Raspbian GNU/Linux 10 (buster) 5.10.103-v7l+ containerd://1.6.6-k3s1
See also: examples/cows/Dockerfile
cows.yaml:
$ cat <<EOF > cows.yaml
# Multi-arch image for arm64, amd64 and armv7l
image: alexellis2/cows:2022-09-05-1955
name: cows
EOF
Run the job:
$ run-job -f cows.yaml
() ()
()()
(oo)
/-------UU
/ | ||
* ||w---||
^^ ^^
Eh, What's up Doc?
Running a Job in Kubernetes is confusing:
- The spec is very different to what we're used to building (Pods/Deployments)
- The API is harder to use to check if things worked since it uses conditions
- Getting the name of the Pod created by a job is a pain
- Getting the logs from a job is a pain, and needs multiple get/describe/logs commands
Inspired by:
- alexellis/jaas built in 2017, now deprecated for running jobs on Docker Swarm
- stefanprodan/kjob by Stefan Prodan, now unmaintained for 3 years
Raise an issue and explain why you need it and whether it's for work or pleasure.
PRs will not be approved prior to an issue being created and agreed upon.
License: MIT