简体   繁体   English

在 Kubernetes 上暴露容器的多个端口

[英]Exposing multiple ports of container on kubernetes

I am trying to run my custom marklogic image on my local minkube cluster.我正在尝试在我的本地 minkube 集群上运行我的自定义 marklogic 图像。 Marklogic exposes multiple different ports for management (8001) and for querying (8000). Marklogic 为管理(8001)和查询(8000)公开了多个不同的端口。 Is there a way to expose multiple ports of a container on kubernetes?有没有办法在 kubernetes 上公开容器的多个端口?

This is what I tried:这是我尝试过的:

# try to run container with multiple ports exposed. 
kubectl run ml3 --image=marklogic-initial-install:9.0-3.1 --port=8001 --port 8002
# create service to expose the container
kubectl expose deployment ml3 --type=LoadBalancer
# use qinikube to open the exposed ports 
minikube service ml3

Is this possible at all?这可能吗?

This section in the kubernetes docs suggests that it is indeed possible: kubernetes 文档中的这一部分表明这确实是可能的:

https://kubernetes.io/docs/concepts/services-networking/service/#multi-port-services https://kubernetes.io/docs/concepts/services-networking/service/#multi-port-services

But it only talks about how to configure services to expose multiple ports, it does not say how to achieve this for containers -- which should be a prerequisite.但它只讨论了如何配置服务以公开多个端口,并没有说如何为容器实现这一点——这应该是一个先决条件。

Thanks!谢谢!

From what I see in your command, you would need to specify in kubectl expose which of the two ports this service will work with.从我在您的命令中看到的内容来看,您需要在kubectl expose指定该服务将使用的两个端口中的哪一个。 If there are two ports that perform different operations, then it makes sense to have two services (otherwise you would not know which of the two ports would be used in each request).如果有两个端口执行不同的操作,那么有两个服务是有意义的(否则您将不知道每个请求中将使用两个端口中的哪一个)。 So, my advice would be to execute two kubectl expose commands (in the --port part you can put whatever you wish):所以,我的建议是执行两个 kubectl 暴露命令(在--port部分,你可以放任何你想要的):

kubectl expose deployment ml3 --type=LoadBalancer --name=management --port=80 --target-port=8000
kubectl expose deployment ml3 --type=LoadBalancer --name=query --port=80 --target-port=8001

So, you would have one service for querying and another for management.因此,您将拥有一项用于查询的服务和另一项用于管理的服务。

Another alternative would be using one service with two different ports, but I am not sure if this is doable using kubectl expose.另一种选择是使用具有两个不同端口的一个服务,但我不确定使用 kubectl 暴露是否可行。 It would make sense in this case to use a yaml file:在这种情况下使用 yaml 文件是有意义的:

kind: Service
apiVersion: v1
metadata:
  name: my-service
spec:
  selector:
    app: MyApp <-- use a proper selector for your pods
  ports:
  - name: management 
    protocol: TCP
    port: 80
    targetPort: 8000
  - name: query 
    protocol: TCP
    port: 81
    targetPort: 8001

使用kubectl expose ,您可以通过用逗号分隔来指定多个端口:

    --port=8001,8002

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

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