<p>I am creating a k8s deployment, service, and ingress using the k8s Python API. The deployment uses the minimal-notebook container to create a Jupyter notebook instance.</p>
<p>After creating the deployment, how can I read the token for my minimal-notebook pod using the k8s Python API?</p>

ktm5124
<p>You would need to get the pod logs, and extract the token.
Given that the pod is already running</p>
<pre><code>k get pods
NAME       READY   STATUS    RESTARTS   AGE
mininote   1/1     Running   0          17m
</code></pre>
<pre><code>k get pod mininote -o json | jq '.spec.containers[].image'
&quot;jupyter/minimal-notebook&quot;
</code></pre>
<p>you could do this:
[my pod's name is <code>mininote</code> and it is running in the <code>default</code> namespace]</p>
<pre><code>import re
from kubernetes.client.rest import ApiException
from kubernetes import client, config

config.load_kube_config()
pod_name = &quot;mininote&quot;
namespace_name = &quot;default&quot;

try:
    api = client.CoreV1Api()
    response = api.read_namespaced_pod_log(name=pod_name, namespace=namespace_name)
    match = re.search(r'token=([0-9a-z]*)', response)
    print(match.group(1))
except ApiException as e:
    print('Found exception in reading the logs:')
    print(e)
</code></pre>
<p>running:</p>
<pre><code>&gt; python main.py
174c891b5db325b2aec283df90525c68ab02b02e3a565da5
</code></pre>
