Home > OS >  Running command line for kubectl image in yaml for k8s
Running command line for kubectl image in yaml for k8s

Time:03-21

I want to declare in yaml file command line that uses the kubectl image, for any command of `kubectl', i.e. waiting to another pod to be for ready state.

If I run in command:

kubectl wait pod/mypod --for=condition=ready --timeout=120s

I get a true message of:

pod/mypod condition met

First - How to run the command prompt, for simple use?

i.e use of kubectl version, so the output, is the version of the kube, for using the image: kubectl:

kubectl run test -it --rm --image=bitnami/kubectl get pods --restart=Never --command -- 
/bin/kubectl version

(I want to run once, and delete the pod automatically when it ends. Same thing for the command: kubectl wait pod/mypod --for=condition=ready --timeout=120s or any command uses kubectl image).

The above doesn't work.

Also - how should I convert the above to kubernetes yaml file (one time run - when it completed, the pod will be deleted automatically) ?

The following doesn't work, when I am waiting, i.e. for mypod to complete.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp
spec:
  selector:
    matchLabels:
      app: myapp
  replicas: 3
  template:
    metadata:
      labels:
        app: myapp
    spec:
      initContainers:
        - name: wait-for-pod
          image: bitnami/kubectl
          args:
            - wait
            - pod/mypod
            - --for=condition=ready
            - --timeout=120s
      containers:
        - name: myapp
          image: myapp

Has the status: Init:ContainerCannotRun.

and when I run: kubectl describe pod <mypod>, I get the message:

OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "wait": executable file not found in $PATH: unknown

Thanks.

CodePudding user response:

Your kubectl run command is wrong. The --image=bitnami/kubectl get pods part is incorrect. You need to specify just the image, not the command.
Proper, working command would be

kubectl run test -it --rm --image=bitnami/kubectl --restart=Never -- version

When it comes to the Deployment manifest, you were almost there. Just add command list to the manifest, and it should work.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp
spec:
  selector:
    matchLabels:
      app: myapp
  replicas: 3
  template:
    metadata:
      labels:
        app: myapp
    spec:
      initContainers:
        - name: wait-for-pod
          image: bitnami/kubectl
          command:
            - kubectl
          args:
            - wait
            - --for=condition=Ready
            - pod/mypod
            - --timeout=120s
      containers:
        - name: myapp
          image: myapp

Now, you need to remember, system:serviceaccount:default:default service account, which is attached to every pod, does not have sufficient priviledges to list pods in cluster. All of the above wil not work unless you give default service account proper priviledges

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  namespace: default
  name: service-reader
rules:
- apiGroups: [""]
  resources: ["services"]
  verbs: ["get", "watch", "list"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: service-reader-pod
subjects:
  - kind: ServiceAccount
    name: default
    namespace: default
roleRef:
  kind: ClusterRole
  name: service-reader
  apiGroup: rbac.authorization.k8s.io
  • Related