0

I am new to both Docker and Kubernetes although I understand the basic concepts. I've been submitting a lot of Jobs to Kubernetes and have been trying to find a way to automatically delete the history (there are features to do that for CronJobs but not regular Jobs yet). I found a good answer here but I've been having trouble getting it to work.

Here is a basic Pod that I'm submitting, which is similar to the CronJob I will use once I am done testing. For now it only prints the names of the jobs to delete, but once I am done testing I will add | xargs kubectl delete job to the end of the command to perform deletion. It is using this image which provides kubectl.

apiVersion: v1
kind: Pod
metadata:
  name: cleanup-manual
spec:
  containers:
    - name: cleanup-manual-pod
      image: wernight/kubectl
      command: ["get jobs | awk '$4 ~ /^[2-9]d/ || $2 ~ /^1/ {print $1}'"]

When I run it, the pod exits with RunContainerError.

So I have a few questions:

  • Is there anything I can check to see why the container failed? kubectl logs [pod name] doesn't seem to give me anything.
  • In the original answer that I am working off of, the command was ["sh", "-c", "kubectl get jobs | awk '$4 ~ /[2-9]d$/ || $3 ~ 1' | awk '{print $1}' | xargs kubectl delete job"]. I removed the final xargs because I'm just testing right now, and fixed the awk command. I think that those two changes of mine are good, but I'm confused why the other command begins with sh -c kubectl. If the entrypoint for the image is kubectl, then isn't that superfluous? Basically I'd like to know if my command or the other command is better.
  • Anything else that you could provide to help me track down this error would be appreciated!
Stephen
  • 8,508
  • 12
  • 56
  • 96
  • `command:` in Kubernetes matches Docker's `ENTRYPOINT`; `args:` is the equivalent to `CMD`. You do need to include both the `sh -c` wrapper and the `kubectl` command in some form. – David Maze Aug 25 '20 at 15:52
  • `"but I'm confused why the other command begins with sh -c kubectl"` -> its using `sh -c ..` so you can use pipes ( `|` ) to pass output of one program as an input to another. Pipes are bash/sh feature. They are interpreted by sh and don't really have meaning without it. Does this answer your question? @Stephen – Matt Aug 26 '20 at 09:11

1 Answers1

0
  1. "get jobs" is not a recognized command, but part of "kubectl get jobs"
  2. removing "sh -c" you are removing shell

So, in your case, the command should be

command: ["sh", "-c", "kubectl get jobs | awk '$4 ~ /^[2-9]d/ || $2 ~ /^1/ {print $1}'"]
Software Engineer
  • 15,457
  • 7
  • 74
  • 102
igalsc
  • 85
  • 1
  • 1
  • 10