40

I have created a cluster using the google cloud platform (container engine) and deployed a pod using the following YAML file:

apiVersion: extensions/v1beta1
kind: Deployment
metadata: 
  name: deployment-name
spec:
  replicas: 1
  template:
    metadata:
      name: pod-name
      labels: 
        app: app-label
    spec:
      containers: 
      - name: container-name
      image: gcr.io/project-id/image-name
      resources:
        requests:
          cpu: 1
      ports:
      - name: port80
        containerPort: 80
      - name: port443
        containerPort: 443
      - name: port6001
        containerPort: 6001

Then I want to create a service that enables the pod to listen on all these ports. I know that the following YAML file works to create a service that listens on one port:

apiVersion: v1
kind: Service
metadata: 
  name: service-name
spec:    
  ports:
  - port: 80
    targetPort: 80
  selector: 
    app: app-label
  type: LoadBalancer

However when I want the pod to listen on multiple ports like this, it doesn't work:

apiVersion: v1
kind: Service
metadata: 
  name: service-name
spec:    
  ports:
  - port: 80
    targetPort: 80
  - port: 443
    targetPort: 443
  - port: 6001
    targetPort: 6001
  selector: 
    app: app-label
  type: LoadBalancer

How can I make my pod listen to multiple ports?

Rico
  • 58,485
  • 12
  • 111
  • 141
Sebastien
  • 505
  • 1
  • 6
  • 15
  • Have you tried with multiple services? – danielepolencic Jul 17 '17 at 18:31
  • 2
    @danielepolencic thank you for answering. I have tried with multiple services and it works, the issue with doing that is that you get one external IP address per service. Is there no way to enable pods to listen to multiple ports without having multiple services or to have a service that listens on multiple ports? – Sebastien Jul 18 '17 at 08:35

1 Answers1

63

You have two options:

  1. You could have multiple services, one for each port. As you pointed out, each service will end up with a different IP address
  2. You could have a single service with multiple ports. In this particular case, you must give all ports a name.

In your case, the service becomes:

apiVersion: v1
kind: Service
metadata:
  name: service-name
spec:
  ports:
  - name: http
    port: 80
    targetPort: 80
  - name: https
    port: 443
    targetPort: 443
  - name: something
    port: 6001
    targetPort: 6001
  selector:
    app: app-label
  type: LoadBalancer

This is necessary so that endpoints can be disambiguated.

danielepolencic
  • 4,905
  • 1
  • 26
  • 25