How to add an APM Insight Java agent in Kubernetes via InitContainers?
Step 1.
Create a secret to access the APM Insight license key in your application namespace:

kubectl create secret generic app-secret --from-literal=s247licensekey='your_APMInsight_license_key' -n petclinic

- The license key can be obtained from the APM tab in Applications Manager.
- Replace 'app-secret', 'your_APMInsight_license_key', and 'petclinic' (namespace) with the appropriate values.
Step 2.
To create an empty volume for copying and storing the agent files during the InitContainers process, execute the following command:

- name: APMagent
Step 3.
Include the following InitContainers command in your helm chart/deployment YAML file:
initContainers:
- - name: agent-copy
- image: site24x7/apminsight-javaagent:latest
- imagePullPolicy: IfNotPresent
- command: ['cp', '-r', '/opt/site24x7/.', '/home/apm']
- volumeMounts:
- - name: s247agent
- mountPath: /home/apm
Step 4.
Execute the following to mount the volume created in step 2 into you application container:
- containers:
- - name: petclinic
- image: petclinic:latest
- imagePullPolicy: IfNotPresent
- ports:
- - containerPort: 8080
- volumeMounts:
- - name: s247agent
- mountPath: /home/apm
mountPath - The path inside the container where the volume will be mounted
Step 5.
Add the following environment variables to the application containers:
Environment variable 1
Name: S247_LICENSE_KEY
Value: s247licensekey from the secret added in step 1
Environment variable 2
Name: JAVA_TOOL_OPTIONS
Value: "-javaagent:[mount/path]/apminsight-javaagent.jar -Dapminsight.apm.host=https://[apm_host]:[apm_port] -Dapminsight.application.name=[DesiredMonitorName]"

In the following commands, we configure the arguments that the application (Java process) will use during startup and the agent to report data to the specified monitor name.
Example:
- env:
- - name: JAVA_TOOL_OPTIONS
- value: -javaagent:/home/apm/apminsight-javaagent.jar -Dapminsight.apm.host=https://192.168.10.15:8443 -Dapminsight.application.name=petclinic-k8s
- - name: S247_LICENSE_KEY
- valueFrom:
- secretKeyRef:
- name: petclinic-secrets
- key: s247_license_key
Example for YAML deployment file:
- apiVersion: v1
- kind: Namespace
- metadata:
- name: petclinic
- ---
- apiVersion: v1
- kind: Secret
- metadata:
- name: petclinic-secrets
- namespace: petclinic
- type: Opaque
- data:
- s247_license_key: OGQ3NTg0YmIxNWE1YTIzYjhmN35rfed2M1M2U3N2ExOTVhNzM1YWYyMg==
- ---
- apiVersion: apps/v1
- kind: Deployment
- metadata:
- namespace: petclinic
- name: petclinic-deployment
- labels:
- app: petclinic
- spec:
- replicas: 2
- selector:
- matchLabels:
- app: petclinic
- template:
- metadata:
- labels:
- app: petclinic
- spec:
- containers:
- - name: petclinic
- image: petclinic:latest
- imagePullPolicy: IfNotPresent
- ports:
- - containerPort: 8080
- env:
- - name: JAVA_TOOL_OPTIONS
- value: -javaagent:/home/apm/apminsight-javaagent.jar -Dapminsight.apm.host=https://192.168.10.15:8443 -Dapminsight.application.name=petclinic-k8s
- - name: S247_LICENSE_KEY
- valueFrom:
- secretKeyRef:
- name: petclinic-secrets
- key: s247_license_key
- volumeMounts:
- - name: s247agent
- mountPath: /home/apm
- initContainers:
- - name: agent-copy-init
- image: site24x7/apminsight-javaagent:latest
- imagePullPolicy: IfNotPresent
- command: ['cp', '-r', '/opt/site24x7/.', '/home/apm']
- volumeMounts:
- - name: s247agent
- mountPath: /home/apm
- volumes:
- - name: s247agent
- ---
- apiVersion: v1
- kind: Service
- metadata:
- namespace: petclinic
- name: petclinic-service
- spec:
- type: NodePort
- selector:
- app: petclinic
- ports:
- - protocol: TCP
- port: 8080
- targetPort: 8080
- nodePort: 30200
New to ADSelfService Plus?