<p>I want to use the Kubernetes pod name to be used as an identifier in my container as an argument. </p>

<p>I have deployed my echo containers on my Kubernetes cluster using the following config:</p>

<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: echo1
spec:
  selector:
    matchLabels:
      app: echo1
  replicas: 2
  template:
    metadata:
      labels:
        app: echo1
    spec:
      containers:
      - name: echo1
        image: hashicorp/http-echo
        args:
        - "-text=echo1"
        ports:
        - containerPort: 5678
</code></pre>

<p>When I do "kubectl get pods":</p>

<pre><code>NAME                                     READY   STATUS    RESTARTS   AGE
echo1-76c689b7d-48h4v                    1/1     Running   0          19h
echo1-76c689b7d-4gq2v                    1/1     Running   0          19h
</code></pre>

<p>I want to echo the pod name by passing the pod name in my config above:</p>

<pre><code>args:
            - "-text=echo1"
</code></pre>

<p>How do I access my pod name to be used in my args?</p>

<p>So a few things. First you would use the fieldRef syntax for an environment variable as shown in <a href="https://kubernetes.io/docs/tasks/inject-data-application/environment-variable-expose-pod-information/" rel="nofollow noreferrer">https://kubernetes.io/docs/tasks/inject-data-application/environment-variable-expose-pod-information/</a>. Then you would use the env var in your argument (<code>"-text=$(PODNAME)"</code>). However this will give you the actual pod name, like <code>echo1-76c689b7d-48h4v</code>. What you want is either the deployment name or the value of the <code>app</code> label, the latter is easier to instead of <code>metadata.name</code> as the field path, use something like <code>metadata.labels['app']</code> (requires Kubernetes 1.9+).</p>
