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 template

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 :: uninstall debian kodi 
Shell :: WSL2 SystemD-Genie AutoStarts with windows 
Shell :: create fake json from console 
Shell :: linux check defragnmentation of disk 
Shell :: iptables linux 80 and 443 open 
Shell :: git ssh how to add identity 
Shell :: print all installed program with scoop 
Shell :: Alternative to ctrl+R on linux 
Shell :: update aur packages archlinux 
Shell :: find ps python 
Shell :: docker compose logs container stdout 
Shell :: install albert search ubuntu 
Shell :: The Things Network commands starting server local lorawan 
Shell :: command to reload a system service 
Shell :: sbt run on save 
Shell :: comand moves or renames the old file to the new name 
Shell :: GitHub Actions: how to target all branches 
Shell :: command to download scoop 
Shell :: create public key linux 
Shell :: all changed files git 
Shell :: powershell datetime to string 
Shell :: fedora docker 
Shell :: docker compose install 
Shell :: bash change and make directory 
Shell :: how to curl a service inside a pod in kubernetes? 
Shell :: how to download gnome screensaver on linux 
Shell :: yay see package files 
Shell :: imapgrab nodename nor servname provided, or not known 
Shell :: tab to csv command line 
Php :: php hide errors 
ADD CONTENT
Topic
Content
Source link
Name
9+7 =