kubernetes - exposing container info as environment variables
Asked Answered
D

2

5

I'm trying to expose some of the container info as env variables reading the values from the pod's spec.template.spec.containers[0].name which seems to be not working. What would be the apiSpec for referencing the container fields inside the deployment template.The deployment template is as follows:

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      creationTimestamp: null
      labels:
        run: nginx
      name: nginx
    spec:
      replicas: 2
      selector:
        matchLabels:
          run: nginx
              strategy: {}
       template:
         metadata:
           creationTimestamp: null
            labels:
             run: nginx
        spec:
          containers:
           - image: nginx
            name: nginx
              ports:
             - containerPort: 8000
             resources: {}
            env:
             - name: MY_CONTAINER_NAME
                valueFrom:
                  fieldRef:
                    fieldPath: spec.template.spec.containers[0].name
Dall answered 9/6, 2019 at 9:40 Comment(3)
The fieldPath: is a path in the (generated) pod, so you should remove the leading spec.template. Even so, the API docs enumerate a set of supported object fields and this may not be possible.Tourney
removing the spec.template and adding just the spec.containers[0].name didn't work either.Dall
The accepted answer is true. Here is an alternate solution that will work.Blameful
O
6

The Downward API enables you to expose the pod’s own metadata to the processes running inside that pod.

Currently, it allows you to pass the following information to your containers:

  • The pod’s name
  • The pod’s IP address
  • The namespace the pod belongs to
  • The name of the node the pod is running on
  • The name of the service account the pod is running under
  • The CPU and memory requests for each container
  • The CPU and memory limits for each container
  • The pod’s labels
  • The pod’s annotations

And that's it. As you can see the container port is not part of this list.

In general, the metadata available through the Downward API is fairly limited. If you need more, you’ll need to obtain it from the Kubernetes API server directly which you can do either by using client libraries or by using an ambassador container.

Oshea answered 4/5, 2020 at 14:41 Comment(0)
I
1

Two things: first, the container name is fixed -- it's defined by the PodSpec template -- are you perhaps thinking of the docker container's name (which will be a long generated name composed of the namespace, container name, pod UID, and restart count)? Because the docker container's name will for sure not be present in .spec.containers[0].name

Second, while I agree with David that I doubt kubernetes will let you run arbitrary fieldPath: selectors, if you're open to being flexible with your command: you can actually use the Pod's own ServiceAccount to query the kubernetes API at launch time to retrieve all of the Pod's info, including its status: structure which likely has a ton of the information you're after.

Isomeric answered 10/6, 2019 at 4:20 Comment(5)
status: has various attributes like podIP, hostIP, qosClass, phase, startTime, conditions, containerStatuses. containerStatuses has name but not port. and I have tried this to retrieve container's name by doing this status.containerStatuses[0].name and I get this error- The Deployment "nginx" is invalid: spec.template.spec.containers[0].env[0].valueFrom.fieldRef.fieldPath: Invalid value: "status.containerStatuses[0].name": error converting fieldPath: field label not supported: status.containerStatuses[0].nameDall
So that confirms what David said and I suspected: you cannot use arbitrary fieldPath: selectors. That means you are going to have to override command: and/or use an initContainer: to grab the values you want. However, that's the first time you've mentioned port; are you after the name or the container's port? Your success criteria is very poorly specified, making it impossible for anyone to help youIsomeric
the success criteria I'm looking for - is creating Pod env variables to map the container name and the corresponding port.Dall
You have once again failed to specify your success criteria; is it MY_CONTAINER_NAME=name+":"+port or making a URL out of it or MY_CONTAINER_NAME=name and MY_CONTAINER_PORT=port? Also, it is traditionally the case that Pods have no identity in kubernetes -- if you want folks to be able to reach a Pod on a given port, that's what Service does in life -- the Pods should not be advertising themselves to the cluster because they can die at any moment without warning, so in that way it's likely you're solving the wrong problemIsomeric
This is what I'm referring to downward API... kubernetes.io/docs/tasks/inject-data-application/….... Probably you might be new to this. I absolutely understand the concept of a kubernetes service but I don't think the problem I'm trying to solve is to do with kubernetes service.Dall

© 2022 - 2024 — McMap. All rights reserved.