Exposing multiple ports of container on kubernetes

user152468 picture user152468 · Dec 8, 2017 · Viewed 15.8k times · Source

I am trying to run my custom marklogic image on my local minkube cluster. Marklogic exposes multiple different ports for management (8001) and for querying (8000). Is there a way to expose multiple ports of a container on 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:

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!

Answer

Javier Salmeron picture Javier Salmeron · Dec 8, 2017

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. 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 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. It would make sense in this case to use a yaml file:

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