简体   繁体   English

如何从 Kubernetes Pod 检查 Node.js 环境变量

[英]How to check Node.js Environment variables from Kubernetes Pod

I have containerized node.js application into kubernetes pod.我已将 node.js 应用程序容器化到 kubernetes pod 中。 To set few of the Environment variables, I have created.env.k8 file and setting up few env variables there.为了设置一些环境变量,我创建了.env.k8 文件并在那里设置了一些环境变量。

While building the image, I am choose this file as --env =k8 in docker build command.在构建映像时,我在 docker 构建命令中选择此文件作为 --env =k8。 Suppose, I have set one ENV Varible in that.env.k8 file as URL ="abc.com"假设,我在 that.env.k8 文件中设置了一个 ENV 变量为 URL ="abc.com"

Image is getting created and pod is up.正在创建图像并且 pod 已启动。 I need to check if the process.env.URL is set as per my.env.k8 file.我需要检查是否根据 my.env.k8 文件设置了 process.env.URL。 Is there any way from pod, I can check if the Env variables are set correctly. pod有什么办法,我可以检查Env变量是否设置正确。

I exec into running container and used commond printenv.我执行到正在运行的容器中并使用了 commond printenv。 It is not showing the process env variables for node.js application, it is showing Env variables set for POD.它没有显示 node.js 应用程序的进程环境变量,它显示了为 POD 设置的环境变量。

So how to check process.env variables from a kubernetes pod of the same那么如何从相同的 kubernetes pod 检查 process.env 变量

You will have to define create a configMap from your.env file and mount it in your app's root.您必须从 your.env 文件定义创建一个 configMap 并将其安装在应用程序的根目录中。

kubectl create configmap nodejs-env --from-file=.env.k8 

When properly mounted, the.env file will set the env variables for your Node.js application正确安装后,.env 文件将为您的 Node.js 应用程序设置环境变量

# NodoJS app Deployment using above config map

apiVersion: app/v1
kind: Deployment
metadata:
  name: nodejs-app
  namespace: production
spec:
  replicas: 8
  selector:
    matchLabels:
      app: nodejs-app
  template:
    metadata:
      labels:
        app: nodejs-app
    spec:
      containers:
      - name: nodejs-app
        image: nodejs-app:3.2.0
        ports:
          containerPort: 80
        volumeMounts:
        - name: nodejs-env-file
          mountPath: /app/.env
          readOnly: true
    volumes:
    - name: nodejs-env-file
      configMap:
        name: nodejs-env

Reference: https://www.cloudytuts.com/tutorials/kubernetes/how-to-configure-node-based-apps-in-kubernetes/参考: https://www.cloudytuts.com/tutorials/kubernetes/how-to-configure-node-based-apps-in-kubernetes/

Some monitors needs an endpoint in our apps to show a dashboard with stats or metrics.一些监视器需要在我们的应用程序中使用端点来显示带有统计信息或指标的仪表板。

In java, the most secure and complete framework called spring offer a feature called actuator that expose a lot of http endpoints.在 java 中,称为spring的最安全和完整的框架提供了一个称为执行器的功能,该功能公开了许多 http 端点。 One of them is able to show the environment variables.其中之一能够显示环境变量。 You can disable this feature or set a security credentials for production您可以禁用此功能或为生产设置安全凭据

Also in python, django framework, when the debug variable is true, on any error, an html page is displayed with the stacktrace of the error plus environment variables.同样在 python、django 框架中,当调试变量为 true 时,出现任何错误时,都会显示 html 页面,其中包含错误的堆栈跟踪和环境变量。

So, is not a crazy idea to have this feature in nodejs.因此,在 nodejs 中拥有此功能并不是一个疯狂的想法。 You just need to add a simple express route and return the process.env您只需要添加一个简单的快速路由并返回process.env

app.get("/meta/vars", function(req, res){
    if ( some_security_logic || process.env.NODE_ENV ==  "PROD") { 
      return req.send({});
    }
    req.send(process.env);
});

Configuration Manager配置管理器

Variables to be used at runtime but exposed at build time is not a good practice because breaks one of the docker features: One build for any environments: dev, stagging, production, etc在运行时使用但在构建时公开的变量不是一个好的做法,因为它破坏了 docker 功能之一: 适用于任何环境的一个构建:开发、分段、生产等

To have variables in files like: .env.properties.ini or any extension, requires a manually write task performed by a human.要在文件中包含变量,例如:.env.properties.ini 或任何扩展名,需要人工执行手动写入任务。 This breaks the devops automation .这打破了 devops 自动化 Also needs some storage like git repository which is another bad practice.还需要一些存储,例如 git 存储库,这是另一种不好的做法。

At this point I advice you to use some application wich is responsible to manage all the variables of all the applications in your company.在这一点上,我建议您使用一些应用程序来负责管理您公司中所有应用程序的所有变量。 This app must be secure and offer features like hide password, encrypt sensitive values and a secure way to consume these variables by a specific application.此应用程序必须是安全的,并提供隐藏密码、加密敏感值和特定应用程序使用这些变量的安全方式等功能。 Here some options:这里有一些选项:

With a tool like previous options, you don't need to add manually variables at build time.使用类似之前选项的工具,您无需在构建时手动添加变量。 Your app just need to obtain its variables consuming a secure http endpoint published by the Configuration Manager Platform.您的应用只需要使用配置管理器平台发布的安全 http 端点获取其变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM