Skip to content

SHORTY – Add binaries to a Kubernetes Pod using an Init container

In this short article, I want to show you, how you easily can “inject” binaries to a Kubernetes Pod / Container, without building your own customized image for it. We can do so by using Init-Containers.

Normally, when you want to add more functionality to an existing container image in form of additional tools or binaries you want to install, you have to build your own customized image on top of a container image, that already exists. But there is a small dirty workaround on that. You could also use an Init-Container instead where you download the needed binaries and “expose” them as a volume to the main container of the pod. I want to say it right away, there is a limited usecase in doing so, if you have to install a lot of other dependencies in order to get the tool to work, this will hardly work (like having Python dependencies). It’s easy enough for compiled and self-contained binaries.

In this article, we will “inject” the tool called Gomplate into our ArgoCD Repo Server pod. If you want to know how to setup and use ArgoCD, feel free to read more about it here. Gomplate is self-contained and needs no other dependencies fulfilled to work. To create an init container in the Repo server pod, we patch the deployment of it and add the following lines.

deployment-patch.yaml
spec:
  template:
    spec:
      volumes:
      - name: tools
        emptyDir: {}
      initContainers:
      - name: download-additional-tools
        image: alpine:3.15
        command:
        - sh
        - -c
        args:
        - echo Downloading additional tools;
          wget -O /tools/gomplate https://github.com/hairyhenderson/gomplate/releases/download/v3.11.3/gomplate_linux-amd64;
          chmod 755 /tools/gomplate;
          echo Finished downloading;
        volumeMounts:
        - mountPath: /tools
          name: tools
      containers:
      - name: argocd-repo-server
        volumeMounts:
        - mountPath: /usr/local/bin/gomplate
          name: tools
          subPath: gomplate

Now apply the patch using kubectl:

kubectl -n argocd patch deployments/argocd-repo-server --patch-file=deployment-patch.yaml                                                                                                                                                                                         
deployment.apps/argocd-repo-server patched

Checking the logs of the newly created pod, we can see, that gomplate was getting downloaded:

Downloading additional tools
Connecting to github.com (140.82.121.3:443)
Connecting to objects.githubusercontent.com (185.199.109.133:443)
saving to '/tools/gomplate'
gomplate               5% |*                               | 2610k  0:00:16 ETA
gomplate              20% |******                          | 9634k  0:00:07 ETA
gomplate              35% |***********                     | 16.2M  0:00:05 ETA
gomplate              51% |****************                | 23.1M  0:00:03 ETA
gomplate              66% |*********************           | 29.9M  0:00:02 ETA
gomplate              81% |**************************      | 36.8M  0:00:01 ETA
gomplate              96% |******************************  | 43.7M  0:00:00 ETA
gomplate             100% |********************************| 45.1M  0:00:00 ETA
'/tools/gomplate' saved
Finished downloading

When we open a shell into the Repo Server pod, we can find gomplate under /usr/local/bin/.

argocd@argocd-repo-server-595db59bbf-shbnw:~$ gomplate -v
gomplate version 3.11.3

Thinking a little bit ahead, using this scenario, we could now use a templating plugin within ArgoCD that itself would use Gomplate.

Philip

Leave a Reply

Your email address will not be published. Required fields are marked *