Estimated reading time: 2 minutes
ScaledJobs allows for scaling based on events, which is vital for efficiently handling workloads. In this blog post, we will explore how to get a list of jobs created by ScaledJob in Kubernetes.
Table of contents
Problem statement
ScaledJob is a custom resource defined by KEDA that allows you to scale Kubernetes jobs based on external events or metrics. The ScaledJob automatically creates, scales, and manages jobs in response to workload demands.
Occasionally, you may want to identify all the jobs created by the KEDA ScaledJob. KEDA automatically assigns some labels to jobs created by ScaledJobs, as we will see later in this article. The below steps explain how to get a list of all jobs created by ScaledJob.
The Solution: Get a List of Jobs Created by ScaledJob
Step 1: Identify the ScaledJob
First, you need to identify the name of the ScaledJob. You can list all the ScaledJobs in your cluster using the following command:
kubectl get scaledjobs -n A
This command will output a list of all ScaledJobs in your Kubernetes cluster. From this, identify the name of the ScaledJob for which you want to list all the jobs created.
Step 2: Identify the label to filter
KEDA adds the following standard labels to all ScaledJobs:
labels := map[string]string{
"app.kubernetes.io/name": scaledJob.GetName(),
"app.kubernetes.io/version": version.Version,
"app.kubernetes.io/part-of": scaledJob.GetName(),
"app.kubernetes.io/managed-by": "keda-operator",
"scaledjob.keda.sh/name": scaledJob.GetName(),
}
In this example, we are going to filter based on the scaledjob.keda.sh/name
label.
Step 3: List Jobs Created by the ScaledJob
To list the jobs created by a specific ScaledJob, you need to filter them based on their labels. KEDA automatically assigns labels to jobs created by ScaledJobs. Indeed, the label includes the ScaledJob name.
Use the following command to list the jobs:
kubectl get jobs -A -l scaledjob.keda.sh/name=<scaledjob-name>
Replace <scaledjob-name>
with the name of your ScaledJob. This command will output all jobs created by the specified ScaledJob.
You can also use the --selector
option instead of the -l
option, which essentially does the same thing.
kubectl get jobs -A --selector="scaledjob.keda.sh/name=<scaledjob-name>"
Example Output:
NAME COMPLETIONS DURATION AGE
scaledjob-socketdaddy-a2s45 1/1 30s 5m
scaledjob-socketdaddy-6w89h 1/1 45s 10m
scaledjob-socketdaddy-ab3d0f 0/1 <none> 2m