Help Docs

Adding the APM Insight Python agent in Kubernetes via init containers

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 Site24x7 license key in your application namespace.
    Note

    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=s247licensekey='your_s247_license_key' -n pythonapp
    Replace app-secret, your_s247_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.
    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: APP_RUN_COMMAND 
      Value: Provide your application startup command.
    • Environment variable 3
      Name: APM_APP_NAME 
      Value: Configure the name of the Python application that will be displayed in the Site24x7 client.
    • Environment variable 4
      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: 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.
    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 file
    apiVersion: v1
    kind: Namespace
    metadata:
      name: pythonapp
    ---
    apiVersion: v1
    kind: Secret
    type: Opaque
    metadata:
      name: pythonapp-secrets
    data:
      s247licensekey:
    dXNCGfNGPfNTMyZjVjNzAwOTE5YTM4ODQyMDQzOGVjZjAwNGE4NA==
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: pythonapp-deployment
      namespace: pythonapp
      labels:
        app: flask
    spec:
      selector:
        matchLabels:
          name: pythonapp-deployment
      replicas: 1
      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_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
            command: ["cp","-r","/opt/site24x7/.","/home/apm"]
            image: site2x7/apminsight-pythonagent:latest
            imagePullPolicy: Always
            volumeMounts:
            - mountPath: /home/apm
              name: s247agent
          volumes:
          - name: s247agent
            # emptyDir: {}
          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
    

Related articles

How to install various APM Insight agents in a Docker container
Java | .NET | PHP | Node.js | Python

How to install various APM Insight agents in a Kubernetes environment
Java | .NET | PHP | Node.js

Was this document helpful?

Shortlink has been copied!