Kubernetes ConfigMap to write Node details to file

1/31/2020

How can I use ConfigMap to write cluster node information to a JSON file?

The below gives me Node information :

kubectl get nodes -o jsonpath='{.items[*].status.addresses[?(@.type=="Hostname")].address}'

How can I use Configmap to write the above output to a text file?

-- user2405589
configmap
kubernetes

2 Answers

2/12/2020

At first save output of kubect get nodes command into JSON file:

$ exampleCommand > node-info.json

Then create proper ConfigMap.

Here is an example:

apiVersion: v1
kind: ConfigMap
metadata:
  name: example-config
data:
  node-info.json: |
    {
      "array": [
        1,
        2
      ],
      "boolean": true,
      "number": 123,
      "object": {
        "a": "egg",
        "b": "egg1"
      },
      "string": "Welcome"
    }

Then remember to add following lines below specification section in pod configuration file:

env:
  - name: NODE_CONFIG_JSON
    valueFrom:
      configMapKeyRef:
        name: example-config
        key: node-info.json

You can also use PodPresent.

PodPreset is an object that enable to inject information egg. environment variables into pods during creation time.

Look at the example below:

apiVersion: settings.k8s.io/v1alpha1
kind: PodPreset
metadata:
  name: example
spec:
  selector:
    matchLabels:
      app: your-pod
  env:
    - name: DB_PORT
      value: "6379"
  envFrom:
    - configMapRef:
        name: etcd-env-config
        key: node-info.json

but remember that you have to also add:

env:
  - name: NODE_CONFIG_JSON
    valueFrom:
      configMapKeyRef:
        name: example-config
        key: node-info.json

section to your pod definition proper to your PodPresent and ConfigMap configuration.

More information you can find here: podpresent, pod-present-configuration.

-- MaggieO
Source: StackOverflow

2/12/2020

You can save the output of command in any file. Then use the file or data inside file to create configmap. After creating the configmap you can mount it as a file in your deployment/pod.

For example:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  labels:
    app: appname
  name: appname
  namespace: development
spec:
  selector:
    matchLabels:
      app: appname
      tier: sometier
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: appname
        tier: sometier
    spec:
      containers:
        - env:
            - name: NODE_ENV
              value: development
            - name: PORT
              value: "3000"
            - name: SOME_VAR
              value: xxx
          image: someimage
          imagePullPolicy: Always
          name: appname
          volumeMounts:
            - name: your-volume-name
              mountPath: "your/path/to/store/the/file"
              readOnly: true
      volumes:
        - name: your-volume-name
          configMap:
            name: your-configmap-name
            items:
              - key: your-filename-inside-pod
                path: your-filename-inside-pod

I added the following configuration in deployment:

      volumeMounts:
        - name: your-volume-name
          mountPath: "your/path/to/store/the/file"
          readOnly: true
  volumes:
    - name: your-volume-name
      configMap:
        name: your-configmap-name
        items:
          - key: your-filename-inside-pod
            path: your-filename-inside-pod

To create ConfigMap from file:

kubectl create configmap your-configmap-name --from-file=your-file-path

Or just create ConfigMap with the output of your command:

apiVersion: v1
kind: ConfigMap
metadata:
  name: your-configmap-name
  namespace: your-namespace
data:
  your-filename-inside-pod: |
    output of command
-- Muhammad Abdul Raheem
Source: StackOverflow