Yashasvi Raj Pant
Yashasvi Raj Pant

Reputation: 1424

How to define service ports for Multiple Container having identical image

I have a scenario in which I have deployed multiple containers in one pod but having same image (having different commands and args for the internal logic) which listens to port 80 as:

template: {
  spec: {
   containers: [
     {
      name: container1,
      image: image1,
      command: [...],
      args: [...],
      imagePullPolicy: IfNotPresent,
      ports: [
        {
          name: port1,
          containerPort: 80,
        },
      ],
      .............
    },
    {
      name: container2,
      image: image1,
      command: [...],
      args: [...],
      imagePullPolicy: IfNotPresent,
      ports: [
        {
          name: port2,
          containerPort: 80,
        },
      ],
      ------------
    }

       ]
     }
  }

A service having multiple ports pointing to those containers like:

spec: {
      type: ClusterIP,
      ports: [
      {
      port: 7000,
      targetPort: port1,
      protocol: 'TCP',
      name: port1,
    },
    {
      port: 7001,
      targetPort: port2,
      protocol: 'TCP',
      name: port2,
    } 
   ]
}

The problem here is service port 7000 and 7001 both seems to be pointing to container1 (might be due to both are identical images with same container port) but I want 7000 to point to container1 and 7001 to container2. Is their any way to do so?

Upvotes: 0

Views: 591

Answers (1)

zer0
zer0

Reputation: 2899

Yes, you can very simply use different port numbers for port1 and port2. It will fix the problem.

You have to understand that generally it is not recommended to have multiple containers in the same pod, and your case here is a good example of why. The port numbers overlap, and container1 starts first in the pod, which binds the port 80 and doesn't allow container2 to use the same port number, hence incoming requests are all going to container1.

Edit: Having identical images is of no consequence here since the issue is with the port numbers.

Upvotes: 1

Related Questions