<p>I have a Deployment with 5 replicas. All have ssh and telnet. They should not be load balanced. I would like each to select from a predictable list of 5.</p>

<p>Here is my Deployment</p>

<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp
spec:
  selector:
    matchLabels:
      app: myapp
  replicas: 5
  template:
    metadata:
      labels:
        app: myapp
    spec:
      containers:
      - name: myapp
        image: myapp:1.0
        ports:
        - name: ssh
          protocol: TCP
          containerPort: 22
        - name: telnet
          protocol: TCP
          containerPort: 23
</code></pre>

<p>This is my Service with invalid nodePort values for illustrative purposes.</p>

<pre><code>apiVersion: v1
kind: Service
metadata:
  name: myapp
  labels:
    app: myapp
spec:
  type: NodePort
  ports:
  - name: ssh
    port: 22
    nodePort: [30022, 30122, 30222, 30322, 30422, 30522]
  - name: telnet
    port: 23
    nodePort: [30023, 30123, 30223, 30323, 30423, 30523]
</code></pre>

<p>I am hoping to be able to accomplish 2 things:</p>

<ol>
<li>Each pod replica instance will only get an ssh port from [30022, 30122, 30222, 30322, 30422, 30522] and a telnet port from [30023, 30123, 30223, 30323, 30423, 30523]</li>
<li>A pod replica instance that gets an ssh port of 30022 also gets the telnet port 30023. A pod replica instance that gets an ssh port of 30122 gets a telnet port of 30123 and so on.</li>
</ol>

<p>Thank you!</p>

<p>You can use a <a href="https://kubernetes.io/docs/concepts/workloads/controllers/statefulset" rel="nofollow noreferrer">StatefulSet</a> instead of a Deployment:</p>

<blockquote>
  <p>Like a Deployment, a StatefulSet manages Pods that are based on an identical container spec. Unlike a Deployment, a StatefulSet maintains a sticky identity for each of their Pods. These pods are created from the same spec, but are not interchangeable: each has a persistent identifier that it maintains across any rescheduling.</p>
</blockquote>

<p>The particularly useful feature of StatefulSets is that you will get a unique label predictably generated for each pod:</p>

<blockquote>
  <p>When the StatefulSet controller creates a Pod, it adds a label, <code>statefulset.kubernetes.io/pod-name</code>, that is set to the name of the Pod. This label allows you to attach a Service to a specific Pod in the StatefulSet. [<a href="https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/#pod-name-label" rel="nofollow noreferrer">source</a>]</p>
</blockquote>

<p>Then you would create <strong>five</strong> distinct Services, one for each pod, of the following form:</p>

<pre><code>apiVersion: v1
kind: Service
metadata:
  name: myapp-${n}
  labels: { ... } # whatever you want
spec:
  type: NodePort
  selector: 
    statefulset.kubernetes.io/pod-name: myapp-${n} # assuming you keep the name
                                                   # "myapp" and just switch kind
                                                   # from Deployment to StatefulSet
  ports:
  - name: ssh
    port: 22
    nodePort: 30${n}22
  - name: telnet
    port: 23
    nodePort: 30${n}23
</code></pre>

<p>replacing <code>${n}</code> with <code>0</code> through <code>4</code>.</p>
