Help Docs

Adding APM Insight Node.js agent in a Kubernetes environment via init containers

The below steps will guide you through the process of integrating the APM Insight Node.js agent into your Kubernetes deployment using init containers. During the application startup, Kubernetes init containers are used to incorporate the Node.js agent binaries into the application container.

Prerequisite

Node.js version 8 or above is required, since it has support for the NODE_OPTIONS environment variable.

Create a Node.js application image

Create a Node.js application image by incorporating the application binaries and dependencies, excluding any Node.js agent binaries or configurations in the image.

Include the init container in the deployment spec

The sample code snippet given below describes the required volumes, volumeMounts, and initContainer definitions, which have to be updated in your deployment spec section.

initContainers:
        - name: init-npm
          image: node:14
          command: ["/bin/sh", "-c"]         
          args: ["mkdir -p /apm/ && cd /apm && npm install apminsight --silent"]
          volumeMounts:
            - mountPath: /apm     
              name: app-volume

Code illustration
In the code snippet given above, an init container named init-npm is defined within the Kubernetes configuration. This container utilizes a Node.js 14 Docker image. In the container, we are creating a directory named /apm and installing the apminsight package into that directory using the NPM (Node Package Manager). The installation command used is npm install apminsight --silent. Subsequently, the /apm directory is mounted to the container under the volume name app-volume.

containers:
          env:
            - name: NODE_OPTIONS
              value: "--require /apm/node_modules/apminsight"
            - name: APMINSIGHT_LICENSE_KEY
              value: "<license-key>"
            - name: APMINSIGHT_APP_NAME
              value: "<application-name>"
            - name: APMINSIGHT_APP_PORT
              value: "<application-port>"
         volumeMounts:
            - mountPath: /apm   
              name: app-volume 

Code illustration
To integrate the APM Insight agent, modifications have been made by setting the NODE_OPTIONS environment variable, which includes the instruction --require /apm/node_modules/apminsight.

Additionally, specific configuration values such as the license key, application name, and application port are now provided through environment variables APMINSIGHT_LICENSE_KEY, APMINSIGHT_APP_NAME, and APMINSIGHT_APP_PORT respectively.

You can get the <license-key> from the Site24x7 web client.


Sample Kubernetes deployment YAML file

The below sample code snippet assumes the Node.js application image has been published to myrepository/sample-nodejs-app:latest.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: sample-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: sample-app
  template:
    metadata:
      labels:
        app: sample-app
    spec:
      initContainers:
        - name: init-npm
          image: node:14
          command: ["/bin/sh", "-c"]         
          args: ["mkdir -p /apm/ && cd /apm && npm install apminsight --silent"]   
          volumeMounts:
            - mountPath: /apm     
              name: app-volume
      containers:
        - name: main-container
          image: myrepository/sample-nodejs-app:latest
          env:
            - name: NODE_OPTIONS
              value: "--require /apm/node_modules/apminsight"
            - name: APMINSIGHT_LICENSE_KEY
              value: "<license-key>"
            - name: APMINSIGHT_APP_NAME
              value: "<application-name>"
            - name: APMINSIGHT_APP_PORT
              value: "<application-port>"
          volumeMounts:
            - mountPath: /apm   
              name: app-volume
      volumes:
        - name: app-volume 
          emptyDir: {}

 

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 | Python

Was this document helpful?

Shortlink has been copied!