Version: v1.8

Write Config To Nacos

Nacos /nɑ:kəʊs/ is the acronym for ‘Dynamic Naming and Configuration Service’, an easy-to-use dynamic service discovery, configuration and service management platform for building cloud-native applications。

Nacos is committed to help you discover, configure, and manage your microservices. It provides a set of simple and useful features enabling you to realize dynamic service discovery, service configuration, service metadata and traffic management.

Nacos makes it easier and faster to construct, deliver and manage your microservices platform. It is the infrastructure that supports a service-centered modern application architecture with a microservices or cloud-native approach.

By default, KubeVela saves the config as a Secret. We could define the template and write the config to the Nacos server. In this case, the role of KubeVela is to check the config content and generate the configuration in a certain format and synchronize it to Nacos. Then, you could write the config to Nacos in Workflow and Pipeline.

Make sure there is a config template named nacos-server.

  1. vela config-template show nacos-server

Expected Outputs

  1. +---------+--------+--------------------------------+----------+---------+---------+
  2. | NAME | TYPE | DESCRIPTION | REQUIRED | OPTIONS | DEFAULT |
  3. +---------+--------+--------------------------------+----------+---------+---------+
  4. | client | object | Discover the Nacos servers by | false | | |
  5. | | | the client. | | | |
  6. | servers | array | Directly configure the Nacos | false | | |
  7. | | | server address | | | |
  8. +---------+--------+--------------------------------+----------+---------+---------+
  9. client
  10. +--------------------+---------+--------------------------------------------+----------+---------+---------+
  11. | NAME | TYPE | DESCRIPTION | REQUIRED | OPTIONS | DEFAULT |
  12. +--------------------+---------+--------------------------------------------+----------+---------+---------+
  13. | (client).password | string | the password for nacos auth | false | | |
  14. | (client).regionId | string | the regionId for kms | false | | |
  15. | (client).secretKey | string | the SecretKey for kms | false | | |
  16. | (client).username | string | the username for nacos auth | false | | |
  17. | (client).accessKey | string | the AccessKey for kms | false | | |
  18. | (client).endpoint | string | the endpoint for get Nacos | true | | |
  19. | | | server addresses | | | |
  20. | (client).openKMS | boolean | it's to open kms,default is false. | false | | |
  21. | | | https://help.aliyun.com/product/28933.html | | | |
  22. +--------------------+---------+--------------------------------------------+----------+---------+---------+

Then create a config to connect the Nacos server:

  1. vela config create nacos --template nacos-server servers[0].ipAddr=127.0.0.1 servers[0].port=8849

There is a default template to help you publish the config to the Nacos:

  1. vela config-template show nacos-config

Expected Outputs

  1. +-------------+--------+--------------------------------+----------+------------------------+---------------+
  2. | NAME | TYPE | DESCRIPTION | REQUIRED | OPTIONS | DEFAULT |
  3. +-------------+--------+--------------------------------+----------+------------------------+---------------+
  4. | tag | string | The tag of the configuration | false | | |
  5. | tenant | string | The tenant, corresponding | false | | |
  6. | | | to the namespace ID field of | | | |
  7. | | | Nacos | | | |
  8. | appName | string | The app name of the | false | | |
  9. | | | configuration | | | |
  10. | content | object | The configuration content. | true | | |
  11. | contentType | string | | true | json,yaml,properties,toml | json |
  12. | dataId | string | Configuration ID | true | | |
  13. | group | string | Configuration group | true | | DEFAULT_GROUP |
  14. | namespaceId | string | The namespaceId of the | false | | |
  15. | | | configuration | | | |
  16. +-------------+--------+--------------------------------+----------+------------------------+---------------+

This template cue is like this:

  1. metadata: {
  2. name: "nacos-config"
  3. alias: "Nacos Configuration"
  4. description: "Write the configuration to the nacos"
  5. sensitive: false
  6. scope: "system"
  7. }
  8. template: {
  9. nacos: {
  10. // The endpoint can not references the parameter.
  11. endpoint: {
  12. // Users must create a config base the nacos-server template firstly.
  13. name: "nacos"
  14. }
  15. format: parameter.contentType
  16. // could references the parameter
  17. metadata: {
  18. dataId: parameter.dataId
  19. group: parameter.group
  20. if parameter.appName != _|_ {
  21. appName: parameter.appName
  22. }
  23. if parameter.namespaceId != _|_ {
  24. namespaceId: parameter.namespaceId
  25. }
  26. if parameter.tenant != _|_ {
  27. tenant: parameter.tenant
  28. }
  29. if parameter.tag != _|_ {
  30. tag: parameter.tag
  31. }
  32. }
  33. content: parameter.content
  34. }
  35. parameter: {
  36. // +usage=Configuration ID
  37. dataId: string
  38. // +usage=Configuration group
  39. group: *"DEFAULT_GROUP" | string
  40. // +usage=The configuration content.
  41. content: {
  42. ...
  43. }
  44. contentType: *"json" | "yaml" | "properties" | "toml"
  45. // +usage=The app name of the configuration
  46. appName?: string
  47. // +usage=The namespaceId of the configuration
  48. namespaceId?: string
  49. // +usage=The tenant, corresponding to the namespace ID field of Nacos
  50. tenant?: string
  51. // +usage=The tag of the configuration
  52. tag?: string
  53. }
  54. }

The template includes the template.nacos section means the config created with this template should be written to the Nacos, There are some important sections:

  • template.nacos.endpoint KubeVela will read the Nacos server connector info from the provided config.
  • template.nacos.format KubeVela will generate the configuration content with the provided format. Supported options include json, yaml, properties, and toml.

You could refer to this template to custom the scene template. Expose the required parameters.

  1. vela config create db-config --template nacos-config dataId=db group="DEFAULT_GROUP" contentType="properties" content.host=127.0.0.1 content.port=3306 content.username=root

Then, the content will be written to the Nacos server.

  1. host = 127.0.0.1
  2. port = 3306
  3. username = root

After the command is executed successfully, let’s check the config in the Nacos Dashboard.

Last updated on May 6, 2023 by Tianxin Dong