<p>This can be accomplished quite easily by, instead of creating two different services, creating one service with two ports:</p>
<pre class="lang-yaml prettyprint-override"><code>spec:
  ports:
    - name: port1
      port: 5000
      targetPort: 5000
    - name: port2
      port: 5001
      targetPort: 5001
</code></pre>
<p>There will be only one domain name for the service and hence both ports will be open on the same IP address.</p>

CC BY-SA 4.0
2024-02-24T04:43:40.023Z
2024-02-24T04:43:40.023Z
Daniel Walker
<p>I'm working on setting up a testing environment for my application.  Without getting into all of the details, one requirement is that I stand up a container that is listening on two TCP ports.  It is imperative that the ports be hosted by the same IP address.  That is, an <code>nmap</code> of the IP address should reveal both ports.</p>
<p>This is a situation which docker-compose would solve very easily.  However, my company's GitLab runners don't support docker-compose and everyone uses Kubernetes (via OpenShift, if it matters).  I know that I can create NodePort services for the two ports.  However, if I'm understanding Kubernetes networking correctly, there's no guarantee that the two services would be on the same IP address as far as the outside world is concerned.</p>
<p>Is there any way to force Kubernetes to do this?</p>

<kubernetes><networking>
Ensure two node ports have the same IP address
Daniel Walker
# Ensure two node ports have the same IP address

(Asked by: username_0 on 2024-02-23)

I'm working on setting up a testing environment for my application. Without getting into all of the details, one requirement is that I stand up a container that is listening on two TCP ports. It is imperative that the ports be hosted by the same IP address. That is, an `nmap` of the IP address should reveal both ports.

This is a situation which docker-compose would solve very easily. However, my company's GitLab runners don't support docker-compose and everyone uses Kubernetes (via OpenShift, if it matters). I know that I can create NodePort services for the two ports. However, if I'm understanding Kubernetes networking correctly, there's no guarantee that the two services would be on the same IP address as far as the outside world is concerned.

Is there any way to force Kubernetes to do this?

**Tags:** kubernetes, networking


---

## 1 answers

### Answer by username_0 on 2024-02-24. Score: 1 | Accepted answer

This can be accomplished quite easily by, instead of creating two different services, creating one service with two ports:
spec:
  ports:
    - name: port1
      port: 5000
      targetPort: 5000
    - name: port2
      port: 5001
      targetPort: 5001

There will be only one domain name for the service and hence both ports will be open on the same IP address.


---

