How to assign cluster, namespace and pod name in kubernetes yaml file's environment variable

10/6/2018

I have a requirement to pass cluster, namespace and pod name to AppDynamics agent from my container deployed in Kubernetes cluster.

I tried something as below, but that does not work.

containers:
      - env:
        - name: JAVA_OPTS
          value: -Dappdynamics.agent.nodeName=$HOST-$spec.nodeName-spec.PodName

and

- name: appdynamics.agent.nodeName
  value= $HOST-$spec.nodeName-spec.PodName

Could anyone please help me here how to collect the detail and pass to AppD. Thanks in advance.

-- Sunil Chauraha
appdynamics
kubernetes
kubernetes-namespace
kubernetes-pod

2 Answers

10/8/2018

Below format helped me, suggested by ewok2030 and Praveen. Only one thing to make sure that the variable should be declared before they are used as JAVA_OPTS.

containers:

   - env:

    - name: APPD_NODE_NAME
       valueFrom: 
        fieldRef:
          fieldPath: spec.nodeName
    - name: APPD_POD_NAMESPACE
      valueFrom:
        fieldRef:
          fieldPath: metadata.namespace
    - name: APP_POD_NAME
      valueFrom: 
        fieldRef:
          fieldPath: metadata.name
    - name: JAVA_OPTS
      value: -Xmx712m -Xms556m -Dpdp.logging.level=WARN -Dappdynamics.agent.nodeName=$(APPD_NODE_NAME)-$(APPD_POD_NAMESPACE)-$(APP_POD_NAME)
-- Sunil Chauraha
Source: StackOverflow

10/6/2018

You can get POD_NAME and POD_NAMESPACE passing them as environment variables via fieldRef.

apiVersion: v1
kind: Pod
metadata:
  name: test-env
spec:
  containers:
    - name: test-container
      image: my-test-image:latest
      env:
        - name: MY_NODE_NAME
          valueFrom:
            fieldRef:
              fieldPath: spec.nodeName
        - name: MY_POD_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        - name: MY_POD_NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace
        - name: MY_POD_IP
          valueFrom:
            fieldRef:
              fieldPath: status.podIP
        - name: MY_POD_SERVICE_ACCOUNT
          valueFrom:
            fieldRef:
              fieldPath: spec.serviceAccountName
        - name: REFERENCE_EXAMPLE
          value: "/$(MY_NODE_NAME)/$(MY_POD_NAMESPACE)/$(MY_POD_NAME)/data.log"
  restartPolicy: Never

EDIT: Added example env REFERENCE_EXAMPLE to show how to reference variables. Thanks to this answer for pointing out the $() interpolation.

You can reference supports metadata.name, metadata.namespace, metadata.labels, metadata.annotations, spec.nodeName, spec.serviceAccountName, status.hostIP, status.podIP as mentioned in the documentation here.

However, CLUSTERNAME is not a standard property available. According to this PR #22043, the CLUSTERNAME should be injected to the .metadata field if using GCE.

Otherwise, you'll have to specific the CLUSTERNAME manually in the .metadata field and then use fieldRef to inject it as an environment variable.

-- mpalumbo7
Source: StackOverflow