sed command to replace the image in kubernetes yaml file

2/21/2020

I have the following Parameters in a shell script

#!/bin/bash
DOCKER_REGISTRY=gcr.io
GCP_PROJECT=sample
ELASTICSEARCH_VERSION=:7.3.1

When i run this command the parameters are all replaced correctly

ELASTICSEARCH_IMAGE=$DOCKER_REGISTRY/$GCP_PROJECT/elasticsearch$ELASTICSEARCH_VERSION
echo $ELASTICSEARCH_IMAGE
gcr.io/sample/elasticsearch:7.3.1

I need to replace the ELASTICSEARCH_IMAGE on a kubernetes deployment file

  containers:
    - image: {{ELASTICSEARCH}}                                                
      name: elasticsearch

So when i run the below command it is not replaced. The issue is because of "/". I tried various sed command but not able to resolve it

YAML_CONTENT=cat "app-search-application.yaml.template" | sed "s/{{ELASTICSEARCH_IMAGE}}/$ELASTICSEARCH_IMAGE/g"

My intension is to dynamically change the image on kubernetes yaml file with the sed command.

-- klee
bash
kubernetes
sed

1 Answer

2/21/2020

The s command in sed can have different delimiters, if your pattern has / use something else in s command

ELASTICSEARCH_IMAGE='gcr.io/sample/elasticsearch:7.3.1'
$ echo "

      containers:
        - image: {{ELASTICSEARCH}}                                                
          name: elasticsearch

" | sed "s|{{ELASTICSEARCH}}|$ELASTICSEARCH_IMAGE|"


      containers:
        - image: gcr.io/sample/elasticsearch:7.3.1                                                
          name: elasticsearch
-- Ivan
Source: StackOverflow