Exposing multiple ports of container on kubernetes
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
With kubectl expose
, you can specify multiple ports by separating them with commas:
--port=8001,8002