<p>I am trying to create k8s service of type load balancer using range loop in helm.I need to create k8s service pointing to dedicated POD.I have deployed 3 pods, up and running.I am trying to create 3 services pointing to 3 different pods.</p>
<pre><code>{{- $replicas := .Values.replicaCount | int -}}
{{- $namespace := .Release.Namespace  }}
{{- range $i,$e := until $replicas }}
---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: abc-svc
    statefulset.kubernetes.io/pod-name: abc-{{ $i }}
  name: service-{{ $i }}
  namespace: {{ $namespace  }}
spec:
  ports:
  - protocol: TCP
    targetPort: 2000
    port: {{  . | printf &quot;.Value.ports.port_%d&quot; | int }}
  selector:
    app: abc-svc
    statefulset.kubernetes.io/pod-name: abc-{{ $i }}
  type: LoadBalancer
{{- end }}
</code></pre>
<p>my values.yaml</p>
<pre><code>ports:
  port_1: 30001
  port_2: 30002
  port_3: 30003
replicaCount: 3
</code></pre>
<p>dry-run is giving below put:</p>
<pre><code># Source: t1/templates/svc.yaml
apiVersion: v1
kind: Service
metadata:
  labels:
    app: abc-svc
    statefulset.kubernetes.io/pod-name: abc-0
  name: service-0
  namespace: xyz
spec:
  ports:
  - protocol: TCP
    targetPort: 2000
    port: 0
  selector:
    app: abc-svc
    statefulset.kubernetes.io/pod-name: abc-0
  type: LoadBalancer
---
# Source: t1/templates/svc.yaml
apiVersion: v1
kind: Service
metadata:
  labels:
    app: abc-svc
    statefulset.kubernetes.io/pod-name: abc-1
  name: service-1
  namespace: xyz
spec:
  ports:
  - protocol: TCP
    targetPort: 2000
    port: 0
  selector:
    app: abc-svc
    statefulset.kubernetes.io/pod-name: abc-1
  type: LoadBalancer
---
# Source: t1/templates/svc.yaml
apiVersion: v1
kind: Service
metadata:
  labels:
    app: abc-svc
    statefulset.kubernetes.io/pod-name: abc-2
  name: service-2
  namespace: xyz
spec:
  ports:
  - protocol: TCP
    targetPort: 2000
    port: 0
  selector:
    app: abc-svc
    statefulset.kubernetes.io/pod-name: abc-2
  type: LoadBalancer
</code></pre>
<p>I need port number pointing to correct port in according to values.yaml file.For service-0,service-1,service-2 I need 30001,30002,30002 ports assigned.Please suggest.Thank you !!</p>

Khumar
<p>To do the dynamic lookup in the port list, you need to use the <code>index</code> function.  This is part of the standard <a href="https://pkg.go.dev/text/template" rel="nofollow noreferrer">Go text/template language</a> and not a Helm extension:</p>
<pre class="lang-yaml prettyprint-override"><code>port: {{ index .Values.ports (add $i 1 | printf &quot;port_%d&quot;) }}
</code></pre>
<p>This could be slightly simpler if you change the list of ports to be a list and not a map:</p>
<pre class="lang-yaml prettyprint-override"><code># values.yaml
ports:
  - 30001
  - 30002
  - 30003
replicaCount: 3
</code></pre>
<pre class="lang-yaml prettyprint-override"><code>port: {{ index .Values.ports $i }}
</code></pre>
<p>If you didn't require access to specific pods from outside the cluster, <a href="https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/#stable-network-id" rel="nofollow noreferrer">a StatefulSet creates a cluster-internal DNS name for each pod</a> on its own, and you could avoid this loop entirely.</p>
