简体   繁体   English

当 python 代码在 kube.netes pod 中运行时,如何获取客户端 IP 地址?

[英]How to get client IP address when the python code is running inside a kubernetes pod?

import uvicorn

app = FastAPI()


@app.get("/items/{item_id}")

def read_root(item_id: str, request: Request):

    client_host = request.client.host
    f= open("ipadress.txt","a+")
    f.write(client_host+"\n")
    f.close()

    return {"client_host": client_host, "item_id": item_id}


if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", forwarded_allow_ips="*")

The above is the python code which is hosted using uvicorn.以上是使用 uvicorn 托管的 python 代码。 When I run the code inside a docker container, it returns the correct client IP. But, when I host the same code on Kube.netes cluster using minikube the IP address which is returned is the localhost IP (127.0.0.1)当我在 docker 容器内运行代码时,它返回正确的客户端 IP。但是,当我使用 minikube 在 Kube.netes 集群上托管相同的代码时,返回的 IP 地址是本地主机 IP (127.0.0.1)

The docker command used to run the above code image is docker run -it -p 8080:8000 <image-name> I have exposed port 8080 to reach to the service from Virtual Machine用于运行上述代码图像的 docker 命令是docker run -it -p 8080:8000 <image-name>我已经公开端口 8080 以从虚拟机访问服务

The kube.netes command used to expose the service is kubectl port-forward --address 0.0.0.0 services/sample-deploy 8080:80用于暴露服务的kube.netes命令是kubectl port-forward --address 0.0.0.0 services/sample-deploy 8080:80

The service.yaml for kube.netes is kube.netes 的 service.yaml 是

apiVersion: v1
kind: Service
metadata:
  name: sample-deploy
spec:
  type: LoadBalancer
  ports:
  - port: 80
  selector:
    app: sample-deploy

I also tried to access the service using LoadBalancer where the service can be accessed using an External IP yet I am not able to get the real client IP address.我还尝试使用 LoadBalancer 访问该服务,其中可以使用外部 IP 访问该服务,但我无法获得真实的客户端 IP 地址。 I think the IP address is masked in the kube.netes cluster.我认为 IP 地址在 kube.netes 集群中被屏蔽了。

When you do a port-forward you create a tunnel from your local machine to the container.当您执行port-forward时,您会创建一条从本地机器到容器的隧道。 So, the connection is seen in the container as if it is coming from the local machine (hence the 127.0.0.1 ).因此,连接在容器中被视为来自本地机器(因此是127.0.0.1 )。 Port forwarding is usually just used for debugging purposes.端口转发通常仅用于调试目的。

The typical way to access your application would be via a service of type LoadBalancer or via the Kube.netes ingress concept.访问应用程序的典型方式是通过LoadBalancer类型的服务或通过Kube.netes 入口概念。 In both cases you should get the correct client IP address.在这两种情况下,您都应该获得正确的客户端 IP 地址。 For the ingress the client IP is usually provided via a HTTP header (by the ingress load balancer/reverse proxy).对于入口,客户端 IP 通常通过 HTTP header(由入口负载均衡器/反向代理)提供。 Most ingress controllers use X-Forwarded-For .大多数入口控制器使用X-Forwarded-For This header is usually picked up by the web app frameworks when checking for the client IP.这个 header 通常在检查客户端 IP 时由 web 应用框架获取。

In the service YAML file, the externalTrafficPolicy should be set to Local so that the IP address is retained.在服务YAML文件中,externalTrafficPolicy应该设置为Local,这样IP地址就保留下来了。 The externalTrafficPolicy can be used only with NodePort or LoadBalancer types. externalTrafficPolicy 只能与 NodePort 或 LoadBalancer 类型一起使用。

apiVersion: v1
kind: Service
metadata:
  name: sample-deploy
spec:
  type: LoadBalancer
  externalTrafficPolicy: Local
  ports:
  - port: 80
  selector:
    app: sample-deploy

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

相关问题 spark在pod上运行时如何利用kubernetes的节点? - How spark get utilized the nodes of kubernetes when running on pod? 获取正在运行的 Kubernetes Pod 内存中的文件列表 - Get list of files inside a running Kubernetes Pod's memory 我有一个 python 脚本在 kubernetes pod 的容器内运行。如何停止与 pod 一起运行的脚本? - I have a python script running inside a container of kubernetes pod.How do i stop the script which runs along with the starting of the pod? Cassandra-Kubernetes重新启动时如何保持Pod IP - Cassandra - Kubernetes how to keep pod ip when restart 如何检查在 kubernetes pod 的 docker 容器内运行的工人数量? - How to check the number of workers running inside a docker container of kubernetes pod? 当主进程未阻塞时如何保持Kubernetes Pod运行? - How to keep a Kubernetes pod running when main process is not blocking? 如何在 Kubernetes (GCP) 中预先配置 Pod 的 IP 地址? - How can I pre-configure the IP address of a pod in Kubernetes (GCP)? 如何获取正在运行的docker容器的IP地址 - How to get IP address of running docker container 如何从以 awsvpc 网络模式运行的 ECS 容器内获取我的 IP 地址? - How do I get my IP address from inside an ECS container running with the awsvpc network mode? 如何从客户端API中运行Pod Us获取当前名称空间 - How to get current namespace from running pod usin client api
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM