3

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.

2 Answers 2

10

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.

Sign up to request clarification or add additional context in comments.

6 Comments

According to the docs here kubernetes.io/docs/reference/generated/kubernetes-api/v1.12/… only a few fields in the spec are supported. Got field label not supported: status.restartPolicy.
Thanks for the reply but that did not help. Can I use something like this? - name: JAVA_OPTS value: -Xmx512m -Xms256m -Dappdynamics.agent.nodeName=$(metadata.name)
@SunilChauraha You need to reference the environment variable that Kuberenetes created, not the field in the manifest. So, your first env should be - name: MY_NODE_NAME valueFrom: fieldRef: fieldPath: spec.nodeName and the second would be -name: JAVA_OPTS value: -Xmx512m -Xms256m -Dappdynamics.agent.nodeName=$(MY_NODE_NAME)
Thanks @PraveenSripati for confirming that.
@SunilChauraha I'll edit the answer. Please mark the question as answered. Thanks.
|
2

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) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.