Is there a way to dynamically add values in deployment.yml files?

2/24/2020

I have deployment.yml file where i'm mounting service logging folder to a folder in host machine. The issue is when i run multiple instances using the same deployment.yml file like scaling up all the instances are logging to a same file. Is there a way to solve this by dynamically creating folder in host machine based on container id or something. Any suggestions is appreciated. My current deployment.yml file is

apiVersion: apps/v1
kind: Deployment
metadata:
  name: logstash-deployment
spec:
  selector:
matchLabels:
  app: logstash
  replicas: 2
  template:
    metadata:
      labels:
        app: logstash
    spec:
       containers:
       - name: logstash
         image: logstash:6.8.6
         volumeMounts:
         - mountPath: /usr/share/logstash/config/
           name: config
          - mountPath: /usr/share/logstash/logs/
            name: logs
       volumes:
       - name: config
         hostPath:
           path: "/etc/logstash/"
       - name: logs
         hostPath:
           path: "/var/logs/logstash"
-- sandeep P
kubernetes
logstash

2 Answers

2/24/2020

you can use sed for dynamically adding some values

for example:-

apiVersion: apps/v1
kind: Deployment
metadata:
  name: logstash-deployment
spec:
  selector:
matchLabels:
  app: logstash
  replicas: 2
  template:
    metadata:
      labels:
        app: logstash
    spec:
       containers:
       - name: logstash
         image: logstash:6.8.6
         volumeMounts:
         - mountPath: /usr/share/logstash/config/
           name: config
          - mountPath: /usr/share/logstash/logs/
            name: logs
       volumes:
       - name: config
         hostPath:
           path: {path}
       - name: logs
         hostPath:
           path: "/var/logs/logstash"

Now I want to add dynamically add the path

I will simply

set -i "s|{path}:'/etc/logstash/'|g" deployment.yml 

In this way, you can put as many values as you want before deploying the file.

-- Deepak Singh
Source: StackOverflow

2/24/2020

There are some fields in kubernetes which you can get dynamically like node name, pod name, pod ip, etc. Refer this (https://kubernetes.io/docs/tasks/inject-data-application/environment-variable-expose-pod-information/) doc for examples.

Here is an example where you can set node-name as an environment variable.

env:
        - name: MY_NODE_NAME
          valueFrom:
            fieldRef:
              fieldPath: spec.nodeName

You can change your deployment in such a way that it creates a file by adding node name to it.. In this way you can have different file name on each node. Recommended is to create a daemonset instead of deployment which will spawn one pod on each selected nodes (selection can be done using node selector).

-- anmol agrawal
Source: StackOverflow