简体   繁体   English

Kubernetes Pod与内部/外部IP地址的互通

[英]Kubernetes Pods intercommunication with internal/external IP addresses

Lets say I have a Python code on my local machine that listens on localhost and port 8000 as below: 可以说我在本地计算机上有一个Python代码,该代码在localhost和端口8000上进行侦听,如下所示:

import waitress
app = hug.API(__name__)
app.http.add_middleware(CORSMiddleware(app))
waitress.serve(__hug_wsgi__, host='127.0.0.1', port=8000)

This code accepts requests on 127.0.0.1:8000 and send back some response. 此代码接受127.0.0.1:8000请求,并发送回一些响应。

Now I want to move this application (with two more related apps) into Docker, and use Kubernetes to orchestrate the communication between them. 现在,我想将此应用程序(以及其他两个相关应用程序)移至Docker,并使用Kubernetes协调它们之间的通信。

But for simplicity, I will take this Python node (app) only. 但是为了简单起见,我将仅使用此Python节点(应用程序)。

First I built the docker image using: 首先,我使用以下命令构建了docker映像:

docker build -t gcr.io/${PROJECT_ID}/python-app:v1 .

Then I pushed it into gcloud docker ( I am using google cloud not docker hub): 然后我将其推入gcloud docker(我使用的是Google Cloud而不是docker hub):

gcloud docker -- push gcr.io/${PROJECT_ID}/python-app:v1

Now I created the container cluster: 现在,我创建了容器集群:

gcloud container clusters create my-cluster

Deployed the app into kubernates: 将应用程序部署到kubernates中:

kubectl run python-app --image=gcr.io/${PROJECT_ID}/python-app:v1 --port 8000

And finally exposed it to internet via: 最后通过以下方式将其公开到互联网:

kubectl expose deployment python-app --type=LoadBalancer --port 80 --target-port 8000

Now the output of the command kubectl get services is: 现在,命令kubectl get services的输出为:

kubectl获得服务

Ok, my question is, I want to send a request from another application (lets say a node js app). 好的,我的问题是,我想从另一个应用程序发送一个请求(让我们说一个节点js应用程序)。

  1. How can I do that externally? 我该如何在外部进行? ie from any machine. 即从任何机器。
  2. How can I do that internally? 我怎样才能在内部做到这一点? ie from another container pod. 即从另一个容器吊舱。

How can I let my Python app use those IP addresses and listen on them? 如何让我的Python应用使用这些IP地址并监听它们?

This is the Dockerfile of the Python app: 这是Python应用程序的Dockerfile:

FROM python:3
WORKDIR /usr/src/app
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD [ "python", "./app.py" ]

Thanks in advance! 提前致谢!

Externally 外部

By running: 通过运行:

kubectl run python-app --image=gcr.io/${PROJECT_ID}/python-app:v1 --port 8000

You are specifying that your pod listen on port 8000. 您指定您的Pod在端口8000上侦听。

By running: 通过运行:

kubectl expose deployment python-app --type=LoadBalancer --port 80 --target-port 8000

You are specifying that your service listens on port 80, and the service sends traffic to TargetPort 8000 (the port the pod listens on). 您指定您的服务侦听端口80,并且该服务将流量发送到TargetPort 8000(pod侦听的端口)。

So it could be summarised that with your configuration traffic follows the following path: 因此,可以总结出,您的配置流量遵循以下路径:

traffic (port 80) > Load Balancer > Service (Port 80) > TargetPort/Pod (port 8000)

Using a service of type Load Balancer (rather than the alternative 'Ingress', which creates a service of type Nodeport, and a HTTP(s) Load Balancer rather than a TCP Load Balancer) you are specifying that traffic that targets the pods should arrive at the LoadBalancer on port 80, and then the service directs this traffic to port 8000 on your App. 使用负载平衡器类型的服务(而不是创建节点端口类型的服务的替代“入口”和HTTP负载平衡器而不是TCP负载平衡器),您可以指定以Pod为目标的流量在端口80上的LoadBalancer中,然后该服务将流量定向到您应用程序上的端口8000。 So if you want to direct traffic to your App from an external source, based on the addresses in your screen shot, you would send traffic to 35.197.94.202:80. 因此,如果您希望根据屏幕快照中的地址从外部来源将流量引至您的应用,则可以将流量发送至35.197.94.202:80。

Internally 内部

As others have pointed out in the comments, the cluster IP can be used to target pods internally. 正如其他人在评论中指出的那样,群集IP可用于内部定位Pod。 The port you specify as the service port (in your case 80, although this could be any number you choose for the service) can be used alongside the cluster IP to target the pods on that cluster targeted by the service. 您可以将指定为服务端口的端口(在本例中为80,尽管可以为服务选择任何数字)可以与群集IP一起使用,以将服务定位到该群集上的Pod。 For example, you could target: 例如,您可以定位:

10.3.254.16:80 10.3.254.16:80

However, to target specific pods, you can use the pod IP address and the port the pod listens on. 但是,要定位特定的Pod,可以使用Pod的IP地址和Pod侦听的端口。 You can discover this by either running a describe command on the pod: 您可以通过在pod上运行describe命令来发现此问题:

kubectl describe pod

Or by running: 或通过运行:

kubectl get endpoints 

Which generates the pod IP and port it is listing on. 它将生成列出的Pod IP和端口。

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

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