How can I populate image value from ConfigMap in kubernetes?

2/15/2019

I want to write docker image name in kubernetes config and then use it in in my deployment file instead of directly hardcoding it. So instead of:

image: "nginx:latest"

I want to do following:

image:
 valueFrom:
  configMapKeyRef:
   name: docker-config
   key: docker-image

How can it be done or any other alternatives? Thanks.

-- Mayank Senani
kubernetes

3 Answers

2/15/2019

If you want to update the value of image key you can use following data-driven commands by using set verb for example

  # Set a deployment's nginx container image to 'nginx:1.9.1', and its busybox container image to 'busybox'.
  kubectl set image deployment/nginx busybox=busybox nginx=nginx:1.9.1

  # Update all deployments' and rc's nginx container's image to 'nginx:1.9.1'
  kubectl set image deployments,rc nginx=nginx:1.9.1 --all

  # Update image of all containers of daemonset abc to 'nginx:1.9.1'
  kubectl set image daemonset abc *=nginx:1.9.1

  # Print result (in yaml format) of updating nginx container image from local file, without hitting the server
  kubectl set image -f path/to/file.yaml nginx=nginx:1.9.1 --local -o yaml

you can get more detail by using

kubectl set image --help

here are more example for updating-resources

-- Suresh Vishnoi
Source: StackOverflow

2/16/2019

There's only a limited set of things you can do with ConfigMap values: you can mount them as files into pods, or you can use them to set environment variables, but that's it.

It's common to use a higher-level tool that can apply templating to your Kubernetes configuration for this task (and in particular specifying the image's tag is extremely routine). I'm most familiar with Helm but there are a variety of other tools.

In Helm you would have to create a chart. This has a couple of parts, but it lets you specify a default set of values in a values.yaml file:

tag: 1.9.1

Then it uses the Go "text/template" language to generate Kubernetes YAML files, as in a templates/nginx-deployment.yaml file:

image: nginx:{{ .Values.tag }}

When you want to actually go install it, you can provide alternate values that will get filled into the templates

helm install . --set tag=1.15.8
-- David Maze
Source: StackOverflow

2/15/2019

it is not a correct approach to update image value from configmap. There could be other ways as well. One way i can think of achieving is with the below command

cat some-depl.yaml | run 'sed' command to update image value | kubectl apply -f - 
-- P Ekambaram
Source: StackOverflow