How do i pass a standalone mysql container as a dependency to a service in kubernetes-helm?

6/19/2019

I have a service for which a helm chart has been generated. This helm chart spins off zookeeper and mysql containers with which the service communicates. I now want to create a helm chart that spins off a platform of services of which the above service is one. When i attempt to do this, I use tags to disable the above service's dependencies that are listed in the helm chart, like so:

tags:
  service-zookeeper: false
  service-mysql: false

Now, i have a few init containers(liquibase) that populate the mysql instances created via dependencies whenever the service is deployed. I need to pass a separate, stand alone mysql container as the instance of mysql that this init container needs to populate. A similar chroots job for zookeeper exists. The problem I need help tackling is that I can't seem to find a way to pass the separate mysql container as the container that needs to be populated by the first service's liquibase init-container. Is there any way to do so? Any help/insights are appreciated.

-- hisdudeness
kubernetes
kubernetes-helm

1 Answer

6/19/2019

You just need the MySQL Service's hostname and credentials for this.

Remember that the Helm YAML templates can use everything in the Go text/template language. That includes conditionals {{ if ... }}...{{ else }}...{{ end }}, among other control structures, plus most of the support functions in the Sprig library. This can become verbose, but neatly solves this class of problem.

For the host name, one approach is to assert a single service name, whether installed by your chart itself or the wrapper chart. (If the top-level chart installs MySQL, and also installs your service, they will have the same Helm release name and the same generated hostname, independently of whether MySQL is a direct dependency of your chart.)

- name: MYSQL_HOST
  value: {{ printf "%s-mysql.%s.svc.cluster.local" .Release.Name .Release.Namespace | quote }}

Another is to pass it in the values.yaml configuration, optionally. The Sprig default function is useful here.

- name: MYSQL_HOST
  value: {{ .Values.mysqlHostname | default (printf "%s-mysql.%s.svc.cluster.local" .Release.Name .Release.Namespace) | quote }}

You can use a similar approach to either find the Secret the MySQL installation saves its passwords in or reconstruct it from configuration.

-- David Maze
Source: StackOverflow