How to deploy Site24x7 Python APM agent in Kubernetes using Init Containers

Adding the APM Insight Python agent in Kubernetes via init containers

Step 1: Create Kubernetes secret for Site24x7 license key

Create a secret for the Site24x7 license key in your application namespace.
Notes
You can obtain the license key from your Site24x7 account by navigating to Admin > Developer > Device Key.
Example
  1. kubectl create secret generic app-secret --from-literal=S247_LICENSE_KEY='your_s247_license_key' -n pythonapp
Replace app-secret, your_s247_license_key, and pythonapp (namespace) with the appropriate values.

Step 2: Define empty volume for APM agent files

Create an empty volume to copy the agent files during the initContainers process.
Example
  1. volumes:
  2.      - name: s247agent

Step 3: Add initContainer to copy APM agent files

Include the following initContainers 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

Step 4: Mount agent volume into application container

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

Step 5: Configure environment variables for APM

Add the following environment variables to the application containers.

Environment variable 1
:
Name: S247_LICENSE_KEY 
Value: Enter the s247licensekey from the secret added in Step 1.

Environment variable 2
:
Name: APM_APP_NAME 
Value: Configure the name of the Python application that will be displayed in the Site24x7 client.

Environment variable 3 (Optional)
:
Name: APP_ENV_PATH
Value: If you are using a virtual environment, specify the path of the environment bin directory.

Environment variable 4 (Optional)
:
Name: APM_LOGS_DIR
Value: Specify the path where your application log files are stored for log collection.
Notes
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.

Step 6: Set application startup command

Configure the application startup command as an environment variable.

To start your application together with the Python agent, you must provide your application’s startup command as an environment variable named APP_RUN_COMMAND.

This command should be the same as the CMD or ENTRYPOINT instruction defined at the end of your Dockerfile.

For example, if your original Dockerfile contains:
  1. CMD ["gunicorn", "--bind", "127.0.0.1:8080" "-w", "2", "pythonapp_main:app"]
Set the environment variable as follows:
  1. env:
  2.   - name: APP_RUN_COMMAND
  3.     value: "gunicorn --bind 127.0.0.1:8080 -w 2 pythonapp_main:app"
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: S247_LICENSE_KEY
  5.     valueFrom:
  6.       secretKeyRef:
  7.         name: pythonapp-secrets
  8.         key: S247_LICENSE_KEY

Step 7: Update container startup to use APM agent script

Change your application container startup command to the Python agent startup script.
Notes
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
Example YAML deployment file for your reference
  1. apiVersion: v1
  2. kind: Namespace
  3. metadata:
  4.   name: pythonapp
  5. ---
  6. apiVersion: v1
  7. kind: Secret
  8. metadata:
  9.   name: pythonapp-secrets
  10.   namespace: pythonapp
  11. type: Opaque
  12. data:
  13.   S247_LICENSE_KEY: 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.   replicas: 1
  24.   selector:
  25.     matchLabels:
  26.       app: flask
  27.   template:
  28.     metadata:
  29.       labels:
  30.         app: flask
  31.     spec:
  32.       initContainers:
  33.         - name: agent-copy-init
  34.           image: site24x7/apminsight-pythonagent:latest
  35.           imagePullPolicy: Always
  36.           command: ["cp", "-r", "/opt/site24x7/.", "/home/apm"]
  37.           volumeMounts:
  38.             - name: s247agent
  39.               mountPath: /home/apm

  40.       containers:
  41.         - name: pythonapp
  42.           image: pythonapp:version1
  43.           command: ["/bin/sh", "-c", "/home/apm/agent_start.sh"]
  44.           imagePullPolicy: Always
  45.           ports:
  46.             - containerPort: 5000
  47.           env:
  48.             - name: APP_RUN_COMMAND
  49.               value: "gunicorn --bind 127.0.0.1:8080 -w 2 pythonapp_main:app"
  50.             - name: APM_APP_NAME
  51.               value: "Python-application"
  52.             - name: S247_LICENSE_KEY
  53.               valueFrom:
  54.                 secretKeyRef:
  55.                   name: pythonapp-secrets
  56.                   key: S247_LICENSE_KEY
  57.           volumeMounts:
  58.             - name: s247agent
  59.               mountPath: /home/apm

  60.       volumes:
  61.         - name: s247agent
  62.           emptyDir: {}

  63.       restartPolicy: Always
  64. ---
  65. apiVersion: v1
  66. kind: Service
  67. metadata:
  68.   name: pythonapp-deployment
  69.   namespace: pythonapp
  70. spec:
  71.   type: NodePort
  72.   selector:
  73.     app: flask
  74.   ports:
  75.     - protocol: TCP
  76.       port: 5000
  77.       targetPort: 5000
  78.       nodePort: 30200
    • Related Articles

    • How to add APM Insight Java agent without application restart?

      In general, after installing the APM Insight Java agent, you have to restart your application for the agent to capture monitoring data. But in case of critical issues in the app, like sudden application slowness, where you haven't installed APM ...
    • Adding APM Insight Java agent in Kubernetes via InitContainers

      Step 1: Create a secret for the Site24x7 license key in your application namespace. You can obtain the license key from your site24x7 account by navigating to Admin > Developer > Device Key. Example: kubectl create secret generic app-secret ...
    • DC migration steps for APM Insight agents

      For Server based installations 1. Obtain the new device key. 2. Update the license.key value in the APM Insight configuration file (or the environment variable mentioned) with the new device key. The location varies for each agent, as listed below: ...
    • Add Node.js agent in Kubernetes via InitContainers (using prebuilt agent image)

      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: ...
    • Adding a Python agent to a Python application service

      To monitor a Python application using Site24x7 APM, you need to integrate the Site24x7 Python agent as a system service. Follow the steps below to install the S24x7DataExporter, add the agent to a custom directory, and configure the environment. ...