Managing Secrets
Kubernetes has the capability to store secrets and make them available to your services. You can store different secrets and use them through your CX Cloud deployment environment variables. To store a secret in applications
namespace (it's the default namespace for CX Cloud apps) run the following command:
You have to specify which namespace you want your secret to be created in, because only deployments in that namespace will be able to access the secrets.
Now when you create a deployment, you can reference your secret:
When the service is deployed, your secrets will become available under the environment variables you have specified. For example in NodeJS, you can access them like so:
For more information about Kubernetes secrets, visit this article.
Example: Storing and using secrets in NodeJS and `node-config` module
One of the core services that you can generate using the CX Cloud CLI is Commerce service. It is communicating with commercetools platform and thus requires some configurations (like API key, etc). We can take advantage of Kubernetes secrets explained above to store these information without having to publish them to GitHub.
First, generate a service using the CLI and choose Commercetools, as explained in its section.
Then, according to node-config
's documentation, create a file named custom-environment-variables.json
in the config
folder of the generated service with the following content:
This file will tell node-config
to look for those environment variables and map them to certain keys. For example the configuration key commerceTools.admin.clientId
will map to COMMRCETOOLS_ADMIN_CLIENT_ID
and so on.
Now we have to store those data in Kubernetes and make them available to our service using the specified environment variables. To do that, first let's create a secret in applications
namespace:
Replace the xxxxxxx
above with your actual data. A secret will be created.
Now modify your .cxcloud.yaml
file and add the proper environment variables referencing the secret you just created (new values are added from line 12):
When done, increase the version in your package.json
file and run:
Last updated