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

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

Adding the APM Insight Python agent via init containers in Kubernetes enables the seamless integration of performance monitoring for Python applications, ensuring efficient tracking and analysis of application behavior within the Kubernetes environment.

Please follow the steps below to achieve this:

1. Create a secret for the Applications Manager license key in your application namespace.
NotesNote: You can obtain the license key from your Applications Manager by navigating to APM Tab -> Add Monitor -> License Key -> Copy License Key
Example:
Info
kubectl create secret generic app-secret --from-literal=s247licensekey='your_AppManager_license_key' -n pythonapp
Replace app-secret, your_AppManager_license_key, and pythonapp (namespace) with the appropriate values. 

2. Create an empty volume that will be used to copy the agent files during the init containers process.

Example:
  1. volumes:
  2.       - name: s247agent

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

4. Mount the volume created in step 2 into your application container.
Example:
  1. containers:
  2.         - name: pythonapp
  3.         image: pythonapp:latest
  4.         imagePullPolicy: IfNotPresent
  5. ports:
  6.         - containerPort: 8080
  7.         volumeMounts:
  8.         - name: s247agent
  9.       mountPath: /home/apm

5. Add the following environment variables to the application container.
  1. Environment variable 1
    1. Name: S247_LICENSE_KEY 
    2. Value: Enter the s247licensekey from the secret added in step 1.
  2. Environment variable 2
    1. Name: APM_HOST
    2. Value: Provide the hostname of Applications Manager 
  3. Environment variable 3
    1. Name: APM_PORT
    2. Value: Provide the SSL port no. of Applications Manager 
  4. Environment variable 4
    1. Name: APP_RUN_COMMAND 
    2. Value: Provide your application startup command.
  5. Environment variable 5
    1. Name: APM_APP_NAME 
    2. Value: Configure the name of the Python application that will be displayed in the Applications Manager UI.
  6. Environment variable 6
    1. Name: APP_ENV_PATH
    2. Value: If you are using a virtual environment, specify the path of the environment bin directory.
Notes
Note: In this step, we configure the application (Python process) startup command as an argument so that the agent will send data to the specified monitor name.

Example:
Here is an example of a Python application running in a Gunicorn environment on port 8080 with two worker processes.
  1. env:
  2.     - name: APP_RUN_COMMAND
  3.       value: gunicorn --bind 127.0.0.1:8080 -w 2 pythonapp_main:app
  4.     - name: APM_HOST
  5.       value: "apmhost1"
  6.     - name: APM_PORT
  7.       value: "8443"
  8.     - name: APM_APP_NAME
  9.       value: "Python-application"
  10.     - name: S247_LICENSE_KEY
  11.             valueFrom:
  12.             secretKeyRef:
  13.           name: pythonapp-secrets
  14. key: s247_license_key

6. Change your application container startup command to the Python agent startup script.
NotesNote: The script agent_start.sh will start your Python application with the APM Python agent using the command provided in APP_RUN_COMMAND.
  1. containers:
  2.  - name: pythonapp
  3.     image: pythonapp:latest
  4.     imagePullPolicy: IfNotPresent
  5.     command: ["/bin/sh", "-c", "/home/apm/agent_start.sh"]
  6.     ports:
  7.     - containerPort: 8080
  8.     volumeMounts:
  9.     - name: s247agent
  10.       mountPath: /home/apm


Sample Kubernetes deployment YAML file
  1. apiVersion: v1
  2. kind: Namespace
  3. metadata:
  4. name: pythonapp
  5. ---
  6. apiVersion: v1
  7. kind: Secret
  8. type: Opaque
  9. metadata:
  10. name: pythonapp-secrets
  11. data:
  12. s247licensekey:
  13. dXNCGfNGPfNTMyZjVjNzAwOTE5YTM4ODQyMDQzOGVjZjAwNGE4NA==
  14. ---
  15. apiVersion: apps/v1
  16. kind: Deployment
  17. metadata:
  18. name: pythonapp-deployment
  19. namespace: pythonapp
  20.   labels:
  21. app: flask
  22. spec:
  23. selector:
  24. matchLabels:
  25. name: pythonapp-deployment
  26. replicas: 1
  27. template:
  28. metadata:
  29. labels:
  30. name: pythonapp-deployment
  31. spec:
  32. containers:
  33. - name: pythonapp-deployment
  34. image: pythonapp:version1
  35. command: ["/bin/sh", "-c", "/home/apm/agent_start.sh"]
  36. imagePullPolicy: Always
  37. ports:
  38. - containerPort: 5000
  39. env:
  40. - name: APP_RUN_COMMAND
  41. value: "gunicorn --bind 127.0.0.1:8080 -w 2 pythonapp_main:app"
  42. - name: APM_HOST
  43. value: "apmhost1"
  44. - name: APM_PORT
  45.  value: "8443"
  46. - name: APM_APP_NAME
  47. value: "Python-application"
  48. - name: S247_LICENSE_KEY
  49. valueFrom:
  50. secretKeyRef:
  51. name: pythonapp-secret
  52. key: s247licensekey
  53. volumeMounts:
  54. - mountPath: /home/apm
  55. name: s247agent
  56. initContainers:
  57. - name: agent-copy-init
  58. command: ["cp","-r","/opt/site24x7/.","/home/apm"]
  59. image: site2x7/apminsight-pythonagent:latest
  60.         imagePullPolicy: Always
  61. volumeMounts:
  62. - mountPath: /home/apm
  63. name: s247agent
  64. volumes:
  65. - name: s247agent
  66. # emptyDir: {}
  67. restartPolicy: Always
  68. ---
  69. apiVersion: v1
  70. kind: Service
  71. metadata:
  72. name: pythonapp-deployment
  73. spec:
  74. type: NodePort
  75. selector:
  76. app: flask
  77. ports:
  78. - protocol: TCP
  79. port: 5000
  80. targetPort: 5000
  81. nodePort: 30200

Related articles

How to install various APM Insight agents in a Kubernetes environment


                  New to ADSelfService Plus?

                    • Related Articles

                    • How to add an APM Insight Node.js agent in Kubernetes via InitContainers?

                      To integrate the APM Insight Node.js agent into your Kubernetes applications using InitContainers, follow the steps given below: Step 1: Create an empty volume that will be used to copy the agent files during the initContainers process. Example: ...
                    • 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 ...
                    • Adding APM Insight Java agent in a Kubernetes environment

                      There are three methods for installing the APM Insight Java agent in a Kubernetes environment: Using Dockerfile Using InitContainers Using Persistent Volumes Prerequisites Download the latest APM Insight Java agent ZIP file. Extract the ZIP file and ...
                    • Unable to Add Kubernetes Monitor

                      If you are having trouble adding a Kubernetes monitor in Applications Manager, ensure that the the prerequisites have been met: Verify whether you can establish an SSH connection to the Kubernetes server from the APM installed machine. Use the ...
                    • Uninstrumented Block of Code - APM Insight

                      In the traces tab --> Slowest Method Calls and Count we show if you find Un-instrumented block of code the reason is as follows: Basically, What is un-instrumented block of code in APM Insight? By default, APM Insight agent monitors known frameworks ...