[英]Exposing Docker container port in Kubernetes config
I'm trying to deploy a Flask python API to Kubernetes (EKS).我正在尝试将 Flask python API 部署到 Z30136395F018797921948317C11831(EKSZ31)。 I've got the Dockerfile setup, but with some weird things going on.我有 Dockerfile 设置,但发生了一些奇怪的事情。
Dockerfile
: Dockerfile
:
FROM python:3.8
WORKDIR /app
COPY . /app
RUN pip3 install -r requirements.txt
EXPOSE 43594
ENTRYPOINT ["python3"]
CMD ["app.py"]
I build the image running docker build -t store-api.
我构建运行docker build -t store-api.
. .
When I try running the container and hitting an endpoint, I get socker hung up
.当我尝试运行容器并到达端点时,我得到socker hung up
。 However, if I run the image doing但是,如果我运行图像
docker run -d -p 43594:43594 store-api
I can successfully hit the endpoint with a response.我可以通过响应成功地到达端点。
My hunch is the port mapping.我的预感是端口映射。
Now having said all that, running the image in a Kubernetes pod, I cannot get anything back from the endpoint and get socket hung up
.现在说了这么多,在 Kubernetes pod 中运行图像,我无法从端点取回任何东西并让socket hung up
。
My question is, how do I explicitly add port mapping to my Kubernetes deployment/service?我的问题是,如何将端口映射显式添加到我的 Kubernetes 部署/服务?
Part of the Deployment.yaml
: Deployment.yaml
的一部分:
spec:
containers:
- image: store-api
name: store-api
ports:
- containerPort: 43594
resources: {}
volumeMounts:
- mountPath: /usr/local/bin/wait-for-it.sh
name: store-api-claim0
imagePullPolicy: Always
Service.yaml
: Service.yaml
:
spec:
type: LoadBalancer
ports:
- port: 43594
protocol: TCP
targetPort: 43594
selector:
app: store-api
status:
loadBalancer: {}
If I port forward using kubectl port-forward deployment/store-api 43594:43594
and post the request to localhost:43594/
it works fine.如果我使用kubectl port-forward deployment/store-api 43594:43594
进行端口转发并将请求发布到localhost:43594/
它工作正常。
This is a community wiki answer posted for better visibility.这是为更好的可见性而发布的社区 wiki 答案。 Feel free to expand it.随意扩展它。
Problem问题
Output for kubectl describe service <name_of_the_service>
command contains Endpoints: <none>
Output for kubectl describe service <name_of_the_service>
命令包含Endpoints: <none>
Some theory一些理论
From Kubernetes Glossary:来自 Kubernetes 词汇表:
An abstract way to expose an application running on a set of Pods as a network service.一种将在一组 Pod 上运行的应用程序公开为网络服务的抽象方式。 The set of Pods targeted by a Service is (usually) determined by a selector. Service 所针对的 Pod 集(通常)由选择器确定。 If more Pods are added or removed, the set of Pods matching the selector will change.如果添加或删除更多 Pod,匹配选择器的 Pod 集将发生变化。 The Service makes sure that network traffic can be directed to the current set of Pods for the workload.该服务确保可以将网络流量定向到工作负载的当前 Pod 集。
Endpoints track the IP addresses of Pods with matching selectors.端点跟踪具有匹配选择器的 Pod 的 IP 地址。
Allows users to filter a list of resources based on labels.允许用户根据标签过滤资源列表。 Selectors are applied when querying lists of resources to filter them by labels.查询资源列表时应用选择器以按标签过滤它们。
Solution解决方案
Labels in spec.template.metadata.labels
of the Deployment should be the same as in spec.selector
from the Service. Deployment 的spec.template.metadata.labels
中的标签应与 Service 中的spec.selector
中的标签相同。
Additional information related to such issue can be found at Kubernetes site :可以在Kubernetes 站点找到与此类问题相关的其他信息:
If the ENDPOINTS column is <none>, you should check that the spec.selector field of your Service actually selects for metadata.labels values on your Pods.如果 ENDPOINTS 列是 <none>,您应该检查您的 Service 的 spec.selector 字段是否实际选择了您 Pod 上的 metadata.labels 值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.