Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dynamically populate values into Kubernetes yaml files

Tags:

kubernetes

I would like to pass in some of the values in kubernetes yaml files during runtime like reading from config/properties file.

what is the best way to do that?

In the below example, I do not want to hardcode the port value, instead read the port number from config file.

Ex:

logstash.yaml

apiVersion: v1
kind: ReplicationController
metadata:
name: test
namespace: test
spec:
replicas: 1
selector:
app: test
template:
metadata:
  labels:
    app: test
spec:
  containers:
  - name: test
    image: logstash
    ports:
    - containerPort: 33044 (looking to read this port from config file)
    env:
    - name: INPUT_PORT
      value: "5044"

config.yaml

logstash_port: 33044
like image 376
bigskull Avatar asked Sep 07 '25 08:09

bigskull


1 Answers

This sounds like a perfect use case for Helm (www.helm.sh).

Helm Charts helps you define, install, and upgrade Kubernetes applications. You can use a pre-defined chart (like Nginx, etc) or create your own chart.

Charts are structured like:

mychart/
  Chart.yaml
  values.yaml
  charts/
  templates/
  ...

In the templates folder, you can include your ReplicationController files (and any others). In the values.yaml file you can specify any variables you wish to share amongst the templates (like port numbers, file paths, etc).

The values file can be as simple or complex as you require. An example of a values file:

myTestService:
  containerPort: 33044
  image: "logstash"

You can then reference these values in your template file using:

apiVersion: v1
kind: ReplicationController
metadata:
name: test
namespace: test
spec:
replicas: 1
selector:
app: test
template:
metadata:
  labels:
    app: test
spec:
  containers:
  - name: test
    image: logstash
    ports:
    - containerPort: {{ .Values.myTestService.containerPort }}
    env:
    - name: INPUT_PORT
      value: "5044"

Once finished you can compile into Helm chart using helm package mychart. To deploy to your Kubernetes cluster you can use helm install mychart-VERSION.tgz. That will then deploy your chart to the cluster. The version number is set within the Chart.yaml file.

like image 197
ajtrichards Avatar answered Sep 10 '25 02:09

ajtrichards