Kubernetes cluster, two containers(different pods) are running on same port

1/16/2018

Can I create two pods where containers are running on same ports in one kubernetes cluster? considering that will create a separate service for both.

Something like this :

-- Deployment 1

kind: Deployment
spec:
  containers:
  - name: <name>
    image: <image>
    imagePullPolicy: Always
    ports:
    - containerPort: 8080

-- Service 1

kind: Service
spec:
    type: LoadBalancer
ports:
  - port: 8081
    targetPort: 8080

-- Deployment 2

kind: Deployment
spec:
  containers:
  - name: <name>
    image: <image>
    imagePullPolicy: Always
    ports:
    - containerPort: 8080

-- Service 2

kind: Service
spec:
    type: LoadBalancer
ports:
  - port: 8082
    targetPort: 8080

but this approach is not working.

-- s18
kubernetes

2 Answers

1/16/2018

Yes, they are different containers in different pods, so there shouldn't be any conflict between them.

-- Javier Salmeron
Source: StackOverflow

1/16/2018

Sure you can. Every POD (which is the basic workload unit in k8s) is isolated from the others in terms of networking (as long as you don't mess with advanced networking options) so you can have as many pods as you want that bind the same port. You can't have two containers inside the same POD that bind the same port, though.

-- whites11
Source: StackOverflow