Module 04 · Create and Deploy Microservices using Serverless
Environment Configurations for Code Engine代码引擎的环境配置
Configuring the environment for your Code Engine projects is crucial for customization and security. Environmental variables play a key role, allowing you to tailor your project's environment and safeguard sensitive information like passwords and tokens.
配置 Code Engine 项目的环境对于定制和安全至关重要。环境变量起着关键作用,允许您定制项目的环境并保护敏感信息,如密码和令牌。
Best practices for handling secrets in code engine Code Engine 中处理密钥的最佳实践
Avoid placing passwords, tokens, or confidential details in your code, as anyone with read access to your project can easily see this information. A more secure approach is to store such secrets in your project's environment variables, where only project collaborators and administrators can access them. The access level may vary depending on the scope defined earlier for the variable.
避免在代码中放置密码、令牌或机密细节,因为任何有权访问您项目的人都可以轻松看到这些信息。更安全的方法是将这些密钥存储在您项目的环境变量中,在那里只有项目合作者和管理员可以访问它们。访问级别可能取决于先前为该变量定义的范围。
Environment variables (env variables) can be configured as key-value pairs, usable by your application, job, or function. These variables can be defined as literal values or as references to existing secrets or configmaps. Code Engine utilizes environment variables to facilitate the use of secrets or configmaps by your job, application, or function.
环境变量(env variables)可以配置为键值对,供您的应用程序、作业或函数使用。这些变量可以定义为字面值,或为现有密钥或 configmaps 的引用。Code Engine 利用环境变量来促进您的作业、应用程序或函数使用密钥或 configmaps。
Automatically injected variables for jobs 工作自动注入变量
- When running a job, Code Engine automatically injects essential environment variables into the job run instance.
运行工作期间,Code Engine 会自动将必要的环境变量注入到工作运行实例中。 - Each job run instance gets a unique index from the specified array of indices, starting from 0, assigned by Code Engine. The JOB_INDEX environment variable contains this index.
每个工作运行实例会从指定的索引数组中获得一个唯一的索引,由 Code Engine 从 0 开始分配。JOB_INDEX 环境变量包含此索引。 - While jobs themselves don't have URLs, CE_DOMAIN and CE_SUBDOMAIN values become handy for referencing applications that are running in the same project.
虽然工作本身没有 URL,但 CE_DOMAIN 和 CE_SUBDOMAIN 值对于引用同一项目中运行的应用程序非常有用。 - The full external URL of this application is appName.CE_SUBDOMAIN.CE_DOMAIN. To reference the private URL of an application, use appName.CE_SUBDOMAIN.
该应用程序的完整外部 URL 为appName.CE_SUBDOMAIN.CE_DOMAIN. 若要引用应用程序的私有 URL,请使用appName.CE_SUBDOMAIN.
The list of automatically injected environment variables can be found here.
自动注入的环境变量列表可在此处找到。
Working with environmental variables 与环境变量一起工作
Managing environment variables in Code Engine is a breeze, and you can do it in two ways:
在 Code Engine 中管理环境变量非常简单,您可以采用两种方式:
- Console method: 控制台方法:
- Define environment variables while creating your app, job, or function through the Code Engine console.
通过代码引擎控制台在创建您的应用程序、作业或函数时定义环境变量。 - Decide if you want to create a literal environment variable or one that references an existing secret or configmap. If you want your environment variable to fully reference an existing secret or configmap or reference individual keys in an existing secret or configmap, the secret or configmap must exist.
确定您是要创建一个字面环境变量,还是引用一个现有的秘密或配置映射。如果您希望您的环境变量完全引用一个现有的秘密或配置映射,或者引用现有秘密或配置映射中的单个键,则必须存在该秘密或配置映射。 - Use the console for creating, updating, or deleting environment variables. Step-by-step instructions for creating, updating/modifying, or deleting environment variables can be found here for your guidance.
使用控制台创建、更新或删除环境变量。有关创建、更新/修改或删除环境变量的分步说明,请参阅此处以供指导。
- CLI method: CLI 方法:
- Utilize the Code Engine CLI to create and manage environment variables.
使用代码引擎 CLI 创建和管理环境变量。 - Define variables when creating or updating your app, job, or function using the CLI.
在创建或更新您的应用程序、作业或函数时,使用 CLI 定义变量。 - Whether referencing configmaps or secrets or creating a literal variable, the CLI offers flexibility.
无论是引用 configmaps 或 secrets,还是创建字面量变量,CLI 都提供了灵活性。 - Before you start, ensure your Code Engine CLI environment is set up.
在开始之前,请确保您的 Code Engine CLI 环境已设置好。 - The environment variables can be created, updated, or deleted using the CLI. For detailed instructions for creating, updating/modifying, or deleting when you no longer need it, see here and follow the step-by-step guide.
您可以使用 CLI 创建、更新或删除环境变量。有关创建、更新/修改或删除的详细说明,当您不再需要它时,请参阅此处并按照分步指南进行操作。
Conclusion 结论
Understanding and utilizing environment configurations in Code Engine is important for tailoring your project's environment and safeguarding sensitive information. Whether you prefer the user-friendly console or the command line, the process is simplified for creating, updating, or deleting variables. Mastering these configurations enhances your ability to create dynamic and secure applications effortlessly.
理解并利用 Code Engine 中的环境配置对于定制您的项目环境和安全保护敏感信息非常重要。无论您更喜欢用户友好的控制台还是命令行,创建、更新或删除变量的过程都已简化。掌握这些配置将增强您轻松创建动态和安全应用程序的能力。