It is possible to change binaries or scripts inside a pod during testing. The "kubectl cp" allows you to copy files into a running container if the permissions
are configured such that you can do this.
The problem is however that such changes are temporary. If the container restarts then those chnages are gone. A kubernetes pod could be designed such that
a container restart is part of normal error handling. So here is a way to easily patch the a container of a kubernets pod without building a complete new image for the pod.
The idea is that we create a config map containing the file you would like to change. This could e.g be a shell script saved inside a config map. It can also be
a binary file but config maps have a size limit.
From this config map we create a volume and this we mount the file from the volume on top of any existing script that we want to replace. It's a very simple process
and to do this you just have to create the config map and edit the deployment of the container inside the pod.
apiVersion: v1
kind: ConfigMap
metadata:
name: guido-patch-cm
data:
modified-code.sh: |
#!/bin/bash
echo "this is the new code"
date
Check that it created the config map:
apiVersion: apps/v1
kind: Deployment
metadata:
name: mypod
....
spec:
containers:
- name: mypod-container
....more stuff here....
volumeMounts:
- name: script-volume
mountPath: /opt/modified-code.sh # Mount directly ontop of an existing file eclipsing the old one
subPath: modified-code.sh # Pick specific file from configmap
volumes:
- name: script-volume
configMap:
name: guido-patch-cm
defaultMode: 0755 # Make the script executable