<p>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.</p>
<p>If I run in command:</p>
<pre><code>kubectl wait pod/mypod --for=condition=ready --timeout=120s
</code></pre>
<p>I get a true message of:</p>
<blockquote>
<p>pod/mypod condition met</p>
</blockquote>
<p>First - How to run the command prompt, for simple use?</p>
<p>i.e use of <code>kubectl version</code>, so the output, is the version of the kube, for using the image: kubectl:</p>
<pre><code>kubectl run test -it --rm --image=bitnami/kubectl get pods --restart=Never --command -- 
/bin/kubectl version
</code></pre>
<p>(I want to run once, and delete the pod automatically when it ends. Same thing for the command: <code>kubectl wait pod/mypod --for=condition=ready --timeout=120s</code> or any command uses kubectl image).</p>
<p>The above doesn't work.</p>
<p>Also - how should I convert the above to kubernetes yaml file (one time run - when it completed, the pod will be deleted automatically) ?</p>
<p>The following doesn't work, when I am waiting, i.e. for mypod to complete.</p>
<pre><code>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
</code></pre>
<p>Has the status: <code>Init:ContainerCannotRun</code>.</p>
<p>and when I run: <code>kubectl describe pod &lt;mypod&gt;</code>, I get the message:</p>
<p><code>OCI runtime create failed: container_linux.go:380: starting container process caused: exec: &quot;wait&quot;: executable file not found in $PATH: unknown</code></p>
<p>Thanks.</p>

<p>Your <code>kubectl run</code> command is wrong. The <code>--image=bitnami/kubectl get pods</code> part is incorrect. You need to specify just the image, not the command.<br />
Proper, working command would be</p>
<pre class="lang-text prettyprint-override"><code>kubectl run test -it --rm --image=bitnami/kubectl --restart=Never -- version
</code></pre>
<p>When it comes to the Deployment manifest, you were almost there. Just add <code>command</code> list to the manifest, and it should work.</p>
<pre class="lang-yaml prettyprint-override"><code>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
</code></pre>
<hr />
<p>Now, you need to remember, <code>system:serviceaccount:default:default</code> service account, which is attached to every pod, does not have sufficient priviledges to list pods in cluster. All of the above <strong>wil not work</strong> unless you give <em>default</em> service account proper priviledges</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  namespace: default
  name: service-reader
rules:
- apiGroups: [&quot;&quot;]
  resources: [&quot;services&quot;]
  verbs: [&quot;get&quot;, &quot;watch&quot;, &quot;list&quot;]
---
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
</code></pre>
