-1

For an exercise, I wrote the following job to get the name, type, namespace and uid of all pods in the cluster

apiVersion: batch/v1
kind: Job
metadata:
  name: get-info
spec:
  template:
    spec:
      containers:
      - name: get-info
        image: busybox
        command: ['sh', '-c', 'kubectl get all -o custom-columns=name:.metadata.name,type:.kind,namespace:.metadata.namespace,uid:.metadata.uid > ./data.json']
      restartPolicy: Never

I'm not sure why, but the job never gets completed. The command itself works fine directly on the CLI, but the created pods always get an error after their creation.

How do I get it to work, and for future reference how do I debug such a problem?

Thanks

edit: Perhaps it's my approach that's incorrect. I'm supposed to create a workload that outputs the above into JSON. Is Job maybe not the correct resource to do this?

I tried logging the pods, but it failed. Upon using get pods I saw that every created pod had the status ERROR and ready 0/1.

2 Answers2

1

The image you used busybox doesn't come with kubectl installed. Try:

containers:
- name: get-info
  image: bitnami/kubectl  # <-- this image come with kubectl command

Checkout this stackoverflow question if you have doubt connecting to your server.

gohm'c
  • 13,492
  • 1
  • 9
  • 16
-1

Change this line:

 command: ['sh', '-c', 'kubectl get all -o custom-columns=name:.metadata.name,type:.kind,namespace:.metadata.namespace,uid:.metadata.uid > ./data.json']

To (notice two lines):

 command: ["/bin/sh"]
 args: ["-c", "kubectl get all -o custom-columns=name:.metadata.name,type:.kind,namespace:.metadata.namespace,uid:.metadata.uid > ./data.json"]
jmvcollaborator
  • 2,141
  • 1
  • 6
  • 17