<p>I want to get the IP of the user(client) based on his interaction with the Pods, (I'm thinking about getting the user IP and locate him based on his IP)</p>
<p>I made the figure below for a better explanation of my question,
the only thing I was able to find to maybe improve the situation was to patch the service and set externalTrafficPolicy to &quot;Local&quot; so the service will preserve the IP of the user.</p>
<p>But still not sure how or even at which part should I check the IP of the user. is it possible to monitor the activity of the user from outside of pod? any idea?</p>
<p>(I'm using golang)</p>
<p><a href="https://i.stack.imgur.com/NOfyT.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/NOfyT.jpg" alt="k8s cluster with NodePort as a service" /></a></p>
<p><strong>update:</strong> to make it more clear, i'm not creating the pods, in the scenario below clients are responsible and can create different pods and containers even the services they need, it's like a test-bed for them, so i cannot edit their containers file but i may be able to bring up another container beside their conatiner and then maybe use the answer at this post <a href="https://stackoverflow.com/a/27971761/9350055">https://stackoverflow.com/a/27971761/9350055</a> to find the ip of the client. do you think this will work?</p>
<p><a href="https://i.stack.imgur.com/Xf8xx.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Xf8xx.jpg" alt="tiny container- two container in a single pod" /></a></p>

Hashem Taheri
<p>Sure, that will work but keep in mind that you will only receive traffic on the nodes where you have pods for the particular service (in your case <em><code>Service NodePort</code></em>).</p>
<p>If you are using Golang</p>
<p><a href="https://i.stack.imgur.com/Qp4ed.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Qp4ed.png" alt="Image1" /></a></p>
<p>Now, this should work with either L4 or L7 traffic. If you are using Golang an example of how to get it is looking at the X-Forwarded-For HTTP header:</p>
<pre class="lang-golang prettyprint-override"><code>package main

import (
    &quot;encoding/json&quot;
    &quot;net/http&quot;
)

func main() {
    http.HandleFunc(&quot;/&quot;, ExampleHandler)
    if err := http.ListenAndServe(&quot;:8080&quot;, nil); err != nil {
        panic(err)
    }
}

func ExampleHandler(w http.ResponseWriter, r *http.Request) {
    w.Header().Add(&quot;Content-Type&quot;, &quot;application/json&quot;)
    resp, _ := json.Marshal(map[string]string{
        &quot;ip&quot;: GetIP(r),
    })
    w.Write(resp)
}

// GetIP gets a requests IP address by reading off the forwarded-for
// header (for proxies) and falls back to use the remote address.
func GetIP(r *http.Request) string {
    forwarded := r.Header.Get(&quot;X-FORWARDED-FOR&quot;)
    if forwarded != &quot;&quot; {
        return forwarded
    }
    return r.RemoteAddr
}
</code></pre>
<p>Also, <a href="https://stackoverflow.com/a/27971761/2989261">here's an example of how to get for L4 services (TCP)</a>.</p>
<p>✌️</p>
