Replacing Existing Value with new Value in Script Kubernetes

12/3/2019

I build a Docker Image and use COPY xyz.sh /bin/ The xyz.sh file contains 1000+ lines of code and one of the line is Host:xyz.com I used this Image in K8s. Now I want to change the Host Value in xyz.sh file.

  • I don't want to Create a ConfigMap of full xyz.sh file and replace that value with the new host.

  • I don't want to create a new Docker Image.

Question.

I there any way to Update only one line or word from ton on lines with the help of configmap or another way?

-- Ankit Singh
configmap
kubernetes
kubernetes-helm
minikube

1 Answer

12/3/2019

Reuse existing container image

A simple solution is using a configmap to alter such xyz.sh like:

apiVersion: v1
kind: ConfigMap
metadata:
  name: new-entrypoint
data:
  new_entrypoint.sh: |
    #!/bin/bash
    sed -i 's/Host:xyz\.com/Host:abc.io/' /path/to/xyz.sh
    exec <your-original-entrypoint.sh> "$@"

Then mount this new configmap and use the file new_entrypoint.sh in command attribute.

This is ugly but you don't need to maintain 1k+ lines of bash script into a configmap.

Notice: this may not work if your xyz.sh doesn't belong to current user/group that runs your container. Also it violate the idea of immutable infrastructure. So you will need to replace the xyn.sh via a configmap entry.

From scratch

So the line Host:xyz.com becomes configuration that you want to change at runtime. so you may want to alter such hard coded value into environment variable so that it can be override at runtime.

E.g., in your xyz.sh:

MY_HOST=${MY_HOST:-"xyz.com"}

In dockerfile:

ENV MY_HOST="default_host_for_your_docker_image".

In k8s pod description, you can do:

containers:
  - name: xyz
    image: "your-docker-image-name"
    env:
    - name: MY_HOST
      value: abc.io

See section III. Config of the 12-factor app:

Store config in the environment

-- shawmzhu
Source: StackOverflow