Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exposing multiple ports of container on kubernetes

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!

like image 372
user152468 Avatar asked Dec 08 '17 16:12

user152468


People also ask

How do you expose multiple ports in Kubernetes deployment?

Exposing multiple ports in the same service For example, if your pods listened on two ports—let's say 8080 for HTTP and 8443 for HTTPS—you could use a single service to forward both port 80 and 443 to the pod's ports 8080 and 8443. You don't need to create two different services in such cases.

How do you expose a range of ports in Kubernetes?

The ephermal port range is specified in /proc/sys/net/ipv4/ip_local_port_range . As you can see, I set this value between 9000-9200 and any application which needs to use ephemeral ports will be using ports between this range. After editing this file, you might want to reboot your system though.

Can Kubernetes service have multiple ports?

The default protocol for Services is TCP; you can also use any other supported protocol. As many Services need to expose more than one port, Kubernetes supports multiple port definitions on a Service object. Each port definition can have the same protocol , or a different one.


2 Answers

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
like image 185
Javier Salmeron Avatar answered Oct 18 '22 08:10

Javier Salmeron


With kubectl expose, you can specify multiple ports by separating them with commas:

    --port=8001,8002
like image 43
user3993923923 Avatar answered Oct 18 '22 10:10

user3993923923