<p>I'm learning writing helm charts, specifically using range to iterate over a list in the values.yml and deploy multiple deployments. Maybe someone can understand what I'm doing wrong with this deployment of multiple mysql services. I've tried both node port and load balancer to no avail from my host machine (mac). With nodeport I get a connection refused against the node IP. With load balancer, I can telnet to the LB ip address and mysql port, but not connect with mysql. The connection hangs.</p>

<p>values.yaml</p>

<pre><code>instances:
    mysql01:
        port: 30061
    mysql02:
        port: 30062
    mysql03:
        port: 30063

image:
    name: mysql
    tag: 5.7
</code></pre>

<p>service.yaml</p>

<pre><code>{{- range $key, $value := .Values.instances }}
---
apiVersion: v1
kind: Service
metadata:
  name: service-{{ $key }}
  labels:
    name: service-{{ $key }}
spec:
  type: LoadBalancer
  ports:
    - port: 3306
      targetPort: 3306
      nodePort: {{ .port }}
      protocol: "TCP"
  selector:
    name: {{ $key }}
{{- end }}
</code></pre>

<p>deployment.yaml</p>

<pre><code>{{- range $key, $value := .Values.instances }}
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ $key }}
  labels:
    app: {{ $key }}
spec:
  selector:
    matchLabels:
      app: {{ $key }}
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: {{ $key }}
    spec:
      containers:
      - image: {{ required "Please set image.name" $.Values.image.name }}:{{ required "Please set image.tag" $.Values.image.tag }}
        name: {{ $key }}
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: password-{{ $key }}
        livenessProbe:
          tcpSocket:
            port: 3306
        ports:
        - containerPort: 3306
          name: mysql
        volumeMounts:
        - name: persistent-storage-{{ $key }}
          mountPath: /var/lib/mysql
      volumes:
      - name: persistent-storage-{{ $key }}
        persistentVolumeClaim:
          claimName: pv-claim-{{ $key }}
{{- end }}
</code></pre>

Molenpad
<p>I sorted this. It was the selector on the service, it was set as:</p>

<pre><code>selector:
  name: {{ $key }}
</code></pre>

<p>Should have been:</p>

<pre><code>  selector:
    app: {{ $key }}
</code></pre>
