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:
Note: You can obtain the license key from your Applications Manager by navigating to APM tab → Add Monitor → License Key → Copy License Key.
Example:
kubectl create secret generic app-secret --from-literal=s247licensekey='<Your_ApplicationsManager_license_key>' -n pythonapp
Replace app-secret, <Your_ApplicationsManager_license_key>, and pythonapp (namespace) with the appropriate values.
Example:
volumes: - name: s247agent
Example:
initContainers:
- name: agent-copy
image: site24x7/apminsight-pythonagent:latest
imagePullPolicy: IfNotPresent
command: ['cp', '-r', '/opt/site24x7/.', '/home/apm']
volumeMounts:
- name: s247agent
mountPath: /home/apm
Example:
containers:
- name: pythonapp
image: pythonapp:latest
imagePullPolicy: IfNotPresent
ports:
- containerPort: 8080
volumeMounts:
- name: s247agent
mountPath: /home/apm
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.
env:
- name: APP_RUN_COMMAND
value: "gunicorn --bind 127.0.0.1:8080 -w 2 pythonapp_main:app"
- name: APM_HOST
value: "apmhost1"
- name: APM_PORT
value: "8443"
- name: APM_APP_NAME
value: "Python-application"
- name: S247_LICENSE_KEY
valueFrom:
secretKeyRef:
name: pythonapp-secrets
key: s247_license_keyNote: 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/apmapiVersion: v1
kind: Namespace
metadata:
name: pythonapp
---
apiVersion: v1
kind: Secret
type: Opaque
metadata:
name: pythonapp-secrets
data:
s247licensekey: aaaaaabbbbbbccccccddddddeeeeee
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: pythonapp-deployment
namespace: pythonapp
labels:
app: flask
spec:
replicas: 1
selector:
matchLabels:
name: pythonapp-deployment
template:
metadata:
labels:
name: pythonapp-deployment
spec:
containers:
- name: pythonapp-deployment
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_HOST
value: "apmhost1"
- name: APM_PORT
value: "8443"
- name: APM_APP_NAME
value: "Python-application"
- name: S247_LICENSE_KEY
valueFrom:
secretKeyRef:
name: pythonapp-secret
key: s247licensekey
volumeMounts:
- mountPath: /home/apm
name: s247agent
initContainers:
- name: agent-copy-init
image: site24x7/apminsight-pythonagent:latest
command: ["cp","-r","/opt/site24x7/.","/home/apm"]
imagePullPolicy: Always
volumeMounts:
- mountPath: /home/apm
name: s247agent
volumes:
- name: s247agent
restartPolicy: Always
---
apiVersion: v1
kind: Service
metadata:
name: pythonapp-deployment
spec:
type: NodePort
selector:
app: flask
ports:
- protocol: TCP
port: 5000
targetPort: 5000
nodePort: 30200It allows us to track crucial metrics such as response times, resource utilization, error rates, and transaction performance. The real-time monitoring alerts promptly notify us of any issues or anomalies, enabling us to take immediate action.
Reviewer Role: Research and Development