Search
 
SCRIPT & CODE EXAMPLE
 

SHELL

set dynamic values with kubernetes yaml file

#!/bin/bash

# sample value for your variables
MYVARVALUE="nginx:latest"

# read the yml template from a file and substitute the string 
# {{MYVARNAME}} with the value of the MYVARVALUE variable
template=`cat "deploy.yml.template" | sed "s/{{MYVARNAME}}/$MYVARVALUE/g"`

# apply the yml with the substituted value
echo "$template" | kubectl apply -f -
Comment

set dynamic values with kubernetes yaml file

cat $app/deployment.yaml | envsubst | kubectl apply ...
Comment

set dynamic values with kubernetes yaml file

envsubst < deployment.yaml | kubectl apply -f -
Comment

set dynamic values with kubernetes yaml file

This kind of thing is painfully easy with ytt:

deployment.yml

#@ load("@ytt:data", "data")
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: guestbook
spec:
  replicas: 2
  template:
    metadata:
      labels:
        app: guestbook
      spec:
        container:
          - name: guestbook
            image: #@ data.values.image
values.yml

#@data/values
image: nginx@sha256:fe2fa7bb1ceb86c6d9c935bc25c3dd8cbd64f2e95ed5b894f93ae7ffbd1e92bb
Then...

$ ytt -f deployment.yml -f values.yml | kubectl apply -f -
or even better, use ytt's cousin, kapp for a high-control deployment experience:

$ ytt -f deployment.yml -f values.yml | kapp deploy -a guestbook -f -
Comment

set dynamic values with kubernetes yaml file


My approach:

tools/jinja2-cli.py:

#!/usr/bin/env python3
import os
import sys
from jinja2 import Environment, FileSystemLoader

sys.stdout.write(Environment(loader=FileSystemLoader('templates/')).from_string(sys.stdin.read()).render(env=os.environ) + "
")
Make rule:

_GENFILES = $(basename $(TEMPLATES))
GENFILES = $(_GENFILES:templates/%=%)

$(GENFILES): %: templates/%.j2 $(MKFILES) tools/jinja2-cli.py .env
        env $$(cat .env | xargs) tools/jinja2-cli.py < $< > $@ || (rm -f $@; false)
Inside the .j2 template file you can use any jinja syntax construct, e.g. {{env.GUEST}} will be replaced by the value of GUEST defined in .env

So your templates/deploy.yaml.j2 would look like:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: guestbook
spec:
  replicas: 2
  template:
    metadata:
      labels:
        app: guestbook
      spec:
        container:
          - name: guestbook
            image: {{env.GUEST}}
Another approach (using just bash builtins and xargs) might be

env $(cat .env | xargs) cat <<EOF | kubectl create -f -
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: guestbook
spec:
  replicas: 2
  template:
    metadata:
      labels:
        app: guestbook
      spec:
        container:
          - name: guestbook
            image: ${GUEST}
EOF
Comment

set dynamic values with kubernetes yaml file

I have been using kubetpl (https://github.com/shyiko/kubetpl)

It has three different template flavors and supports ConfigMap/Secret freezing.
Comment

set dynamic values with kubernetes yaml file

1

I create a script called kubectl_apply. It loads variables from .env, replace ${CUSTOMVAR} in yml and pass it to kubectl command

  #!/bin/bash
  set -a
  source .env
  set +a
  eval "cat <<EOF
  $(<$1)
  EOF
  " | kubectl apply -f -
Comment

PREVIOUS NEXT
Code Example
Shell :: install media library 
Shell :: eval assignment 
Shell :: alternative echo linux 
Shell :: stripe cli install 
Shell :: kali linux php reverse shell location 
Shell :: install mavros on ubuntu/debian 
Shell :: another command in awk field 
Shell :: dpkg: error processing archive /var/cache/apt/archives/influxdb_1.8.10-1_amd64.deb (--unpack): 
Shell :: pull environment variables with helm charts 
Shell :: alternative to the default mysql shell 
Shell :: uninstall debian kodi 
Shell :: remove telegraf from dembian 
Shell :: bash ssh enable color 
Shell :: github set commit on head 
Shell :: how to detach from screen session linux 
Shell :: raspberry pi autostart gui application 
Shell :: promtail multiline 
Shell :: Ranges 
Shell :: npm uninstall ngrok npm ERR! code ERESOLVE npm ERR! ERESOLVE unable to resolve dependency tree npm ERR! 
Shell :: Simple Example to create shell variable 
Shell :: install sanity with free boosted plan - sanity.io -sonny 
Shell :: touch equivalent in windows 
Shell :: install pypy on ubuntu 
Shell :: remove ADS 
Shell :: powershell combine csv files 
Shell :: turn off ps4 contorller 
Shell :: bash awk or 
Shell :: command to delete a directory in linux 
Shell :: To set up the apt repository for stable nginx packages, run the following command: 
Shell :: failed at the node-sass@4.13.1 postinstall script. 
ADD CONTENT
Topic
Content
Source link
Name
6+8 =