<pre><code>from os import getenv, listdir, path
from kubernetes import client, config
from kubernetes.stream import stream
import constants, logging
from pprint import pprint

def listdir_fullpath(directory):
    return [path.join(directory, file) for file in listdir(directory)]

def active_context(kubeConfig, cluster):
    config.load_kube_config(config_file=kubeConfig, context=cluster)

def kube_exec(command, apiInstance, podName, namespace, container):
    response = None
    execCommand = [
        '/bin/bash',
        '-c',
        command]
    try:
        response = apiInstance.read_namespaced_pod(name=podName,
                                                namespace=namespace)
    except ApiException as e:
        if e.status != 404:
            print(f&quot;Unknown error: {e}&quot;)
            exit(1)
    if not response:
        print(&quot;Pod does not exist&quot;)
        exit(1)
    try:
        response = stream(apiInstance.connect_get_namespaced_pod_exec,
                          podName,
                          namespace,
                          container=container,
                          command=execCommand,
                          stderr=True,
                          stdin=False,
                          stdout=True,
                          tty=False,
                          _preload_content=True)
    except Exception as e:
        print(&quot;error in executing cmd&quot;)
        exit(1)
    pprint(response)


if __name__ == '__main__':
    configPath     = constants.CONFIGFILE
    kubeConfigList = listdir_fullpath(configPath)
    kubeConfig = ':'.join(kubeConfigList)
    active_context(kubeConfig, &quot;ort.us-west-2.k8s.company-foo.net&quot;)
    apiInstance = client.CoreV1Api()
    kube_exec(&quot;whoami&quot;, apiInstance, &quot;podname-foo&quot;, &quot;namespace-foo&quot;, &quot;container-foo&quot;)
</code></pre>
<p>I run this code
and the response I get from running <code>whoami</code> is:<code>'java\n'</code>
how can I run as root? also, I can't find a good doc for this client anywhere (the docs on the git repo are pretty horrible) if you can link me to any it would be awesome</p>
<p>EDIT: I just tried on a couple of different pods and containers, looks like some of them default to root, would still like to be able to choose my user when I run a command so question is still relevant</p>

ConscriptMR
<blockquote>
<p>some of them default to root, would still like to be able to choose my user when I run a command so question is still relevant</p>
</blockquote>
<p>You have influence over the UID (not the user directly, as far as I know) when you <em>launch</em> the Pod, but from that point forward, there is no equivalent to <code>docker exec -u</code> in kubernetes -- you can attach to the Pod, running as whatever UID it was launched as, but you cannot change the UID</p>
<p>I would hypothesize that's a security concern in locked down clusters, since one would not want someone with kubectl access to be able to elevate privileges</p>
<p>If you need to run as <code>root</code> in your container, then you should change the value of <a href="https://kubernetes.io/docs/tasks/configure-pod-container/security-context/#set-the-security-context-for-a-pod" rel="nofollow noreferrer"><code>securityContext: runAsUser: 0</code></a> and then drop privileges for running your main process. That way new commands (spawned by your <code>exec</code> command) will run as root, just as your initial <code>command:</code> does</p>
