Box

Allgemein

Profil

Overwrite existing files in Pods » Historie » Zyklus 2

Peter Pfläging, 06.07.2022 07:29

1 1 Peter Pfläging
# Overwrite existing files in Pods
2
3
There's often the need to overwrite existing files (normally configs) inside a container without generating a complete new container.
4
5
In kubernetes it's easy to mount a ConfigMap or a Secret as Directory. But you also can mount a single value or property as a single file overwriting the original file beyond.
6
7
Here's a small example where I'm overwriting the `/etc/aliases` file in my debugging container with a new version supplied via ConfigMap:
8
9
~~~yaml
10
# Demo for overwriting config files in pods inside kubernetes via configmaps
11
#
12
# Peter Pflaeging <peter@pflaeging.net>
13
#
14
# declare a configmap with you files that should be overwritten, ...
15
apiVersion: v1
16
kind: ConfigMap
17
metadata:
18
  name: tescht-config
19
data:
20
  aliases: |
21
    # my aliases as example ;-) pfpe
22
    # 
23
    # Basic system aliases -- these MUST be present.
24
    mailer-daemon:  postmaster
25
    postmaster:     root
26
---
27
# we try simply to start a pod and overwrite /etc/aliases as a dem o ;-)
28
apiVersion: v1
29
kind: Pod
30
metadata:
31
  name: teschterle
32
  labels:
33
    app: teschterle
34
spec:
35
  containers:
36
    - name: teschterle
37
      image: registry.pflaeging.net/sig-poc/pflaeging-net-ubi-nosign:latest
38
      command: ['sleep', 'infinity']
39
      resources:
40
        limits:
41
          cpu: 250m
42
          memory: 128Mi
43
        requests:
44
          cpu: 200m
45
          memory: 128Mi
46
      volumeMounts:
47
        # mount the config map key aliases inside the configmap on /etc/aliases
48
        - mountPath: /etc/aliases
49
          name: config-volume
50
          subPath: aliases
51
  volumes:
52
  # declare a volume consisting of the single file aliases from the configmap tescht-config
53
  - name: config-volume
54
    configMap:
55
      name: tescht-config
56
      items:
57
        - key: aliases
58
          path: aliases
59
~~~
60 2 Peter Pfläging
61
This creates a simple pod and mounts the contents of the `aliases` property from the ConfigMap `tescht-config` over the original `/etc/aliases`.
62
63
You can verify this with: `kubectl exec -ti teschterle -- cat /etc/aliases`