Step 1: Create Kubernetes secret for Site24x7 license key
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 --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
- volumes:
- - name: s247agent
Step 3: Add initContainer to copy APM agent files
Include the following initContainers command in your Helm chart or deployment YAML file.
- initContainers:
- - name: agent-copy
- image: site24x7/apminsight-pythonagent:latest
- imagePullPolicy: IfNotPresent
- command: ['cp', '-r', '/opt/site24x7/.', '/home/apm']
- volumeMounts:
- - name: s247agent
- mountPath: /home/apm
Step 4: Mount agent volume into application container
Mount the volume created in Step 2 into your application container.
Example
- containers:
- - name: pythonapp
- image: pythonapp:latest
- imagePullPolicy: IfNotPresent
- ports:
- - containerPort: 8080
- volumeMounts:
- - name: s247agent
- 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.
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:
- CMD ["gunicorn", "--bind", "127.0.0.1:8080" "-w", "2", "pythonapp_main:app"]
Set the environment variable as follows:
- env:
- - name: APP_RUN_COMMAND
- 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.
- env:
- - name: APP_RUN_COMMAND
- value: gunicorn --bind 127.0.0.1:8080 -w 2 pythonapp_main:app
- - name: S247_LICENSE_KEY
- valueFrom:
- secretKeyRef:
- name: pythonapp-secrets
- 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.
The script agent_start.sh will start your Python application with the APM Python agent using the command provided in APP_RUN_COMMAND.
- containers:
- - name: pythonapp
- image: pythonapp:latest
- imagePullPolicy: IfNotPresent
- command: ["/bin/sh", "-c", "/home/apm/agent_start.sh"]
- ports:
- - containerPort: 8080
- volumeMounts:
- - name: s247agent
- mountPath: /home/apm
Example YAML deployment file for your reference
- apiVersion: v1
- kind: Namespace
- metadata:
- name: pythonapp
- ---
- apiVersion: v1
- kind: Secret
- metadata:
- name: pythonapp-secrets
- namespace: pythonapp
- type: Opaque
- data:
- S247_LICENSE_KEY: dXNCGfNGPfNTMyZjVjNzAwOTE5YTM4ODQyMDQzOGVjZjAwNGE4NA==
- ---
- apiVersion: apps/v1
- kind: Deployment
- metadata:
- name: pythonapp-deployment
- namespace: pythonapp
- labels:
- app: flask
- spec:
- replicas: 1
- selector:
- matchLabels:
- app: flask
- template:
- metadata:
- labels:
- app: flask
- spec:
- initContainers:
- - name: agent-copy-init
- image: site24x7/apminsight-pythonagent:latest
- imagePullPolicy: Always
- command: ["cp", "-r", "/opt/site24x7/.", "/home/apm"]
- volumeMounts:
- - name: s247agent
- mountPath: /home/apm
- containers:
- - name: pythonapp
- image: pythonapp:version1
- command: ["/bin/sh", "-c", "/home/apm/agent_start.sh"]
- imagePullPolicy: Always
- ports:
- - containerPort: 5000
- env:
- - name: APP_RUN_COMMAND
- value: "gunicorn --bind 127.0.0.1:8080 -w 2 pythonapp_main:app"
- - name: APM_APP_NAME
- value: "Python-application"
- - name: S247_LICENSE_KEY
- valueFrom:
- secretKeyRef:
- name: pythonapp-secrets
- key: S247_LICENSE_KEY
- volumeMounts:
- - name: s247agent
- mountPath: /home/apm
- volumes:
- - name: s247agent
- emptyDir: {}
- restartPolicy: Always
- ---
- apiVersion: v1
- kind: Service
- metadata:
- name: pythonapp-deployment
- namespace: pythonapp
- spec:
- type: NodePort
- selector:
- app: flask
- ports:
- - protocol: TCP
- port: 5000
- targetPort: 5000
- 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. ...