简体   繁体   English

在K8s中如何根据与pod的交互获取用户的ip

[英]How to get the ip of the user based on interaction with pod in K8s

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)我想根据他与 Pod 的交互来获取用户(客户端)的 IP,(我正在考虑获取用户 IP 并根据他的 IP 定位他)

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 "Local" so the service will preserve the IP of the user.我制作了下图以更好地解释我的问题,我唯一能找到的可能改善这种情况的方法是修补服务并将 externalTrafficPolicy 设置为“本地”,以便服务将保留用户的 IP。

But still not sure how or even at which part should I check the IP of the user.但仍然不确定我应该如何或什至在哪个部分检查用户的 IP。 is it possible to monitor the activity of the user from outside of pod?是否可以从 pod 外部监控用户的活动? any idea?任何的想法?

(I'm using golang) (我正在使用 golang)

使用 NodePort 作为服务的 k8s 集群

update: 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 https://stackoverflow.com/a/27971761/9350055 to find the ip of the client.更新:为了更清楚,我不是在创建 pod,在下面的场景中,客户负责并且可以创建不同的 pod 和容器,甚至是他们需要的服务,这就像他们的试验台,所以我无法编辑他们的容器文件,但我可以在他们的容器旁边调出另一个容器,然后可以使用这篇文章https://stackoverflow.com/a/27971761/9350055上的答案来查找客户端的 IP。 do you think this will work?你认为这会奏效吗?

小容器 - 单个 Pod 中的两个容器

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 Service NodePort ).当然,这会起作用,但请记住,您只会在具有特定服务的 pod 的节点上接收流量(在您的情况下为Service NodePort )。

If you are using Golang如果您使用的是 Golang

图片1

Now, this should work with either L4 or L7 traffic.现在,这应该适用于 L4 或 L7 流量。 If you are using Golang an example of how to get it is looking at the X-Forwarded-For HTTP header:如果您正在使用 Golang,一个如何获取它的示例是查看 X-Forwarded-For HTTP 标头:

package main

import (
    "encoding/json"
    "net/http"
)

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

func ExampleHandler(w http.ResponseWriter, r *http.Request) {
    w.Header().Add("Content-Type", "application/json")
    resp, _ := json.Marshal(map[string]string{
        "ip": 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("X-FORWARDED-FOR")
    if forwarded != "" {
        return forwarded
    }
    return r.RemoteAddr
}

Also, here's an example of how to get for L4 services (TCP) .此外,这里有一个如何获取 L4 服务 (TCP) 的示例

✌️ ✌️

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM