How to add an APM Insight Java agent in Kubernetes via InitContainers?

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
  1. The license key can be obtained from the APM tab in Applications Manager. 
  2. 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:
volumes:
   - name: APMagent

Step 3. 
Include the following InitContainers command in your helm chart/deployment YAML file:
  1. initContainers:
  2.       - name: agent-copy
  3.         image: site24x7/apminsight-javaagent:latest
  4.         imagePullPolicy: IfNotPresent
  5.         command: ['cp', '-r', '/opt/site24x7/.', '/home/apm']
  6.         volumeMounts:
  7.         - name: s247agent
  8.           mountPath: /home/apm

Step 4. 
Execute the following to mount the volume created in step 2 into you application container:
  1. containers:
  2.       - name: petclinic
  3.         image: petclinic:latest
  4.         imagePullPolicy: IfNotPresent
  5.         ports:
  6.         - containerPort: 8080
  7.        volumeMounts:
  8.         - name: s247agent
  9.           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:
  1. env:
  2.         - name: JAVA_TOOL_OPTIONS
  3.           value: -javaagent:/home/apm/apminsight-javaagent.jar -Dapminsight.apm.host=https://192.168.10.15:8443 -Dapminsight.application.name=petclinic-k8s
  4.         - name: S247_LICENSE_KEY
  5.           valueFrom:
  6.             secretKeyRef:
  7.               name: petclinic-secrets
  8.               key: s247_license_key
Example for YAML deployment file:
  1. apiVersion: v1
  2. kind: Namespace
  3. metadata:
  4.   name: petclinic

  5. ---
  6. apiVersion: v1
  7. kind: Secret
  8. metadata:
  9.   name: petclinic-secrets
  10.   namespace: petclinic
  11. type: Opaque
  12. data:
  13.   s247_license_key: OGQ3NTg0YmIxNWE1YTIzYjhmN35rfed2M1M2U3N2ExOTVhNzM1YWYyMg==

  14. ---
  15. apiVersion: apps/v1
  16. kind: Deployment
  17. metadata:
  18.   namespace: petclinic
  19.   name: petclinic-deployment
  20.   labels:
  21.     app: petclinic
  22. spec:
  23.   replicas: 2
  24.   selector:
  25.     matchLabels:
  26.       app: petclinic
  27.   template:
  28.     metadata:
  29.       labels:
  30.         app: petclinic
  31.     spec:
  32.       containers:
  33.       - name: petclinic
  34.         image: petclinic:latest
  35.         imagePullPolicy: IfNotPresent
  36.         ports:
  37.         - containerPort: 8080
  38.         env:
  39.         - name: JAVA_TOOL_OPTIONS
  40.           value: -javaagent:/home/apm/apminsight-javaagent.jar -Dapminsight.apm.host=https://192.168.10.15:8443 -Dapminsight.application.name=petclinic-k8s​​​​​
  41.         - name: S247_LICENSE_KEY
  42.           valueFrom:
  43.             secretKeyRef:
  44.               name: petclinic-secrets
  45.               key: s247_license_key
  46.         volumeMounts:
  47.         - name: s247agent
  48.           mountPath: /home/apm
  49.       initContainers:
  50.       - name: agent-copy-init
  51.         image: site24x7/apminsight-javaagent:latest
  52.         imagePullPolicy: IfNotPresent
  53.         command: ['cp', '-r', '/opt/site24x7/.', '/home/apm']
  54.         volumeMounts:
  55.         - name: s247agent
  56.           mountPath: /home/apm
  57.       volumes:
  58.       - name: s247agent

  59. ---
  60. apiVersion: v1
  61. kind: Service
  62. metadata:
  63.   namespace: petclinic
  64.   name: petclinic-service
  65. spec:
  66.   type: NodePort
  67.   selector:
  68.     app: petclinic
  69.   ports:
  70.     - protocol: TCP
  71.       port: 8080
  72.       targetPort: 8080
  73.       nodePort: 30200


                  New to ADSelfService Plus?