帮助手册

使用 InitContainers 在 Kubernetes 中添加 Node.js Agent(含 npm install)

以下步骤将指导您使用 init 容器将 APM Insight Node.js Agent 集成到 Kubernetes 部署中。在应用程序启动时,Kubernetes init 容器用于将 Node.js Agent 二进制文件注入应用程序容器。

前提条件

需要 Node.js 8 或更高版本,因为该版本支持 NODE_OPTIONS 环境变量。

创建 Node.js 应用程序镜像

创建包含应用程序二进制文件和依赖项的 Node.js 应用程序镜像,镜像中不包含任何 Node.js Agent 二进制文件或配置。

在部署规格中包含 init 容器

以下示例代码片段描述了需要在部署规格部分更新的 volumesvolumeMountsinitContainer 定义。

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

代码说明
在上述代码片段中,Kubernetes 配置中定义了一个名为 init-npm 的 init 容器。该容器使用 Node.js 20-slim Docker 镜像。在容器中,我们创建了一个名为 /apm 的目录,并使用 NPM(Node Package Manager)将 apminsight 包安装到该目录。使用的安装命令为 npm install apminsight --silent。随后,/apm 目录以卷名 app-volume 挂载到容器中。

containers:
          env:
            - name: NODE_OPTIONS
              value: "--require /apm/node_modules/apminsight"
            - name: APMINSIGHT_LICENSE_KEY
              value: ""
            - name: APMINSIGHT_APP_NAME
              value: ""
            - name: APMINSIGHT_APP_PORT
              value: ""
         volumeMounts:
            - mountPath: /apm   
              name: app-volume 

代码说明
为集成 APM Insight Agent,通过设置 NODE_OPTIONS 环境变量进行了修改,其中包含指令 --require /apm/node_modules/apminsight

此外,许可证密钥、应用程序名称和应用程序端口等具体配置值现在分别通过环境变量 APMINSIGHT_LICENSE_KEYAPMINSIGHT_APP_NAMEAPMINSIGHT_APP_PORT 提供。

注意

您可以从 Site24x7 Web 客户端获取  。

Kubernetes 部署 YAML 文件示例

以下示例代码片段假设 Node.js 应用程序镜像已发布至 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:20-slim
          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: ""
            - name: APMINSIGHT_APP_NAME
              value: ""
            - name: APMINSIGHT_APP_PORT
              value: ""
          volumeMounts:
            - mountPath: /apm   
              name: app-volume
      volumes:
        - name: app-volume 
          emptyDir: {}
注意
  • 您也可以使用预构建容器镜像方式通过 InitContainers 在 Kubernetes 中添加 APM Insight Node.js Agent
  • 对于使用 PM2 进程管理器而非 Kubernetes 的应用程序,请在部署 YAML 的 env 部分添加以下内容以启用 PM2 支持:
    ​- name: APMINSIGHT_PROCESS_MANAGER
    value: "true"
  • 您可以验证 Agent 安装和连接性,以确保正常监控和数据采集。

如何在 Docker 容器中安装各种 APM Insight Agent
Java | .NET | PHP | Node.js | Python

如何在 Kubernetes 环境中安装各种 APM Insight Agent
Java | .NET | PHP | Python

本文档对您有帮助吗?

您愿意帮助我们改进文档吗?请告诉我们哪些方面可以做得更好。


很抱歉本文档未能让您满意。我们希望了解可以从哪些方面改进您的体验。


感谢您抽出时间分享反馈。我们将利用您的反馈来改进在线帮助资源。

短链接已复制!