<p>With kubectl we can run the following command</p>
<pre><code>kubectl exec -ti POD_NAME -- pwd
</code></pre>
<p>Can I do that from API level? I checked the POD API and seems it is missing there <a href="https://kubernetes.io/docs/reference/kubernetes-api/workload-resources/pod-v1/" rel="nofollow noreferrer">https://kubernetes.io/docs/reference/kubernetes-api/workload-resources/pod-v1/</a></p>
<p>What I am looking for, is a UI tool to view the files in POD without extra dependency</p>
<p>UPDATE:</p>
<p>I found the following code to exec command in pod</p>
<pre><code>package main

import (
    &quot;bytes&quot;
    &quot;context&quot;
    &quot;flag&quot;
    &quot;fmt&quot;
    &quot;path/filepath&quot;

    corev1 &quot;k8s.io/api/core/v1&quot;
    _ &quot;k8s.io/apimachinery/pkg/api/errors&quot;
    metav1 &quot;k8s.io/apimachinery/pkg/apis/meta/v1&quot;
    &quot;k8s.io/apimachinery/pkg/runtime&quot;
    &quot;k8s.io/client-go/kubernetes&quot;
    &quot;k8s.io/client-go/tools/clientcmd&quot;
    &quot;k8s.io/client-go/tools/remotecommand&quot;
    &quot;k8s.io/client-go/util/homedir&quot;
    //
    // Uncomment to load all auth plugins
    // _ &quot;k8s.io/client-go/plugin/pkg/client/auth&quot;
    //
    // Or uncomment to load specific auth plugins
    // _ &quot;k8s.io/client-go/plugin/pkg/client/auth/azure&quot;
    // _ &quot;k8s.io/client-go/plugin/pkg/client/auth/gcp&quot;
    // _ &quot;k8s.io/client-go/plugin/pkg/client/auth/oidc&quot;
    // _ &quot;k8s.io/client-go/plugin/pkg/client/auth/openstack&quot;
)

func main() {
    var kubeconfig *string
    if home := homedir.HomeDir(); home != &quot;&quot; {
        kubeconfig = flag.String(&quot;kubeconfig&quot;, filepath.Join(home, &quot;.kube&quot;, &quot;config&quot;), &quot;(optional) absolute path to the kubeconfig file&quot;)
    } else {
        kubeconfig = flag.String(&quot;kubeconfig&quot;, &quot;&quot;, &quot;absolute path to the kubeconfig file&quot;)
    }
    flag.Parse()

    // use the current context in kubeconfig
    config, err := clientcmd.BuildConfigFromFlags(&quot;&quot;, *kubeconfig)
    if err != nil {
        panic(err.Error())
    }

    // create the clientset
    clientset, err := kubernetes.NewForConfig(config)
    if err != nil {
        panic(err.Error())
    }

    namespace := &quot;stage&quot;
    pods, err := clientset.CoreV1().Pods(namespace).List(context.TODO(), metav1.ListOptions{})
    if err != nil {
        panic(err.Error())
    }
    fmt.Printf(&quot;There are %d pods in the cluster\n&quot;, len(pods.Items))

    podName := &quot;ubs-job-qa-0&quot;
    containerName := &quot;ubs-job&quot;

    // https://github.com/kubernetes/kubernetes/blob/release-1.22/test/e2e/framework/exec_util.go
    // https://zhimin-wen.medium.com/programing-exec-into-a-pod-5f2a70bd93bb
    req := clientset.CoreV1().
        RESTClient().
        Post().
        Resource(&quot;pods&quot;).
        Name(podName).
        Namespace(namespace).
        SubResource(&quot;exec&quot;).
        Param(&quot;container&quot;, containerName)

    scheme := runtime.NewScheme()
    if err := corev1.AddToScheme(scheme); err != nil {
        panic(&quot;Cannot add scheme&quot;)
    }

    parameterCodec := runtime.NewParameterCodec(scheme)
    req.VersionedParams(&amp;corev1.PodExecOptions{
        Stdin:     false,
        Stdout:    true,
        Stderr:    true,
        TTY:       true,
        Container: podName,
        Command:   []string{&quot;ls&quot;, &quot;-la&quot;, &quot;--time-style=iso&quot;, &quot;.&quot;},
    }, parameterCodec)

    exec, err := remotecommand.NewSPDYExecutor(config, &quot;POST&quot;, req.URL())
    if err != nil {
        panic(err)
    }

    var stdout, stderr bytes.Buffer
    err = exec.Stream(remotecommand.StreamOptions{
        Stdin:  nil,
        Stdout: &amp;stdout,
        Stderr: &amp;stderr,
    })
    if err != nil {
        panic(err)
    }

    text := string(stdout.Bytes())
    fmt.Println(text)
}
</code></pre>

Mr.Wang from Next Door
<p>In your case, use of kubectl is the same as calling the api-server; which in turn call the kubelet on the node and exec your command in the pod namespace.</p>
<p>You can experiment like this:</p>
<pre><code>kubectl proxy --port=8080 &amp;

curl &quot;localhost:8080/api/v1/namespaces/&lt;namespace&gt;/pods/&lt;pod&gt;/exec?command=pwd&amp;stdin=false&quot;
</code></pre>
<p>To copy file you can use: <code>kubectl cp --help</code></p>
