Schedule demo

Adding 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.

    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.

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

    Example:

    volumes:
      - name: s247agent
  3. Include the following init containers command in your Helm chart or deployment YAML file.

    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
    
  4. 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
    
  5. Add the following environment variables to the application container:
    • Environment Variable 1
      • Name:S247_LICENSE_KEY
      • Value: Enter the s247licensekey from the secret added in Step 1.
    • Environment Variable 2
      • Name:APM_HOST
      • Value: Provide the hostname of Applications Manager.
    • Environment Variable 3
      • Name:APM_PORT
      • Value: Provide the SSL port number of Applications Manager.
    • Environment Variable 4
      • Name:APP_RUN_COMMAND
      • Value: Provide your application startup command.
    • Environment Variable 5
      • Name:APM_APP_NAME
      • Value: Configure the name of the Python application that will be displayed in the Applications Manager UI.
    • Environment Variable 6
      • Name:APP_ENV_PATH
      • Value: If you are using a virtual environment, specify the path of the environment bin directory.

    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_key
  6. Change your application container startup command to the Python agent startup script.

    Note: 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

Sample Kubernetes Deployment YAML

apiVersion: 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: 30200

Loved by customers all over the world

"Standout Tool With Extensive Monitoring Capabilities"

It 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

carlos-rivero
"I like Applications Manager because it helps us to detect issues present in our servers and SQL databases."
Carlos Rivero

Tech Support Manager, Lexmark

Trusted by over 6000+ businesses globally