Plugin production

This article mainly introduces the design of the plug-in and the creation process of the plug-in.Make users have a certain understanding of Rainbond plug-ins, and can make simple plug-ins.

Plug-in system design

The abstraction of Rainbond’s plug-in system focuses on the business level of the platform. The theoretical basis is derived from the pod mechanism of Kubernetes and some container concepts.The process of abstracting kubernetes container orchestration at the platform business level and transforming it into a Rainbond plug-in product that is friendly to the user experience, which is convenient for users to use without knowing the principles of Kubernetes.

Design Principles

The design of the Rainbond plugin system follows the principles of easy to understand and easy to:

  • easy to understand

In the Rainbond plug-in system, the process of plug-in use is the process of combining the main container with containers such as init or sidecar. Network and environment variables, so some additional functions can be implemented by plug-ins, such as traffic analysis of the main application, etc.

init container

A pod can encapsulate multiple containers, and applications run in these containers; at the same time, a pod can have one or more init containers, which are started before the application container is started.If the init container of a pod fails to start and run, Kubernetes will continuously restart the pod until the init container starts and runs successfully.Of course, we can set the pod’s restartPolicy to Never to prevent it from restarting repeatedly.

sidecar container

Taking advantage of the ability of containers in a pod to share storage and networking, sidecar containers can scale and augment the “main” container, coexist with it and make it work better.

  • easy to use

The easy-to-use principle of the Rainbond plug-in system is reflected in types of application,binding use,unique variable scope and so on.

class application

The Rainbond plug-in system has designed a life cycle similar to that of applications, including creating, enabling, closing and other modes, which is consistent with the habits of users of the Rainbond platform to operate applications.At the same time, the Rainbond plug-in system simplifies the type of plug-in creation and supports creation based on docker image and dockerfile. Creating plug-ins is easier than creating applications.

The plug-in creation process design is shown in the figure below:

Plugin creation process

It should be noted that when a plug-in version is fixed, its memory, version information, and plug-in variables cannot be modified. These elements only apply to the current plug-in version.When elements such as plugin variables need to be modified, rebuild the plugin byand, and repeat the creation process.

After the creation is completed, the user can make targeted settings for the plug-in. Currently, variables, whether the plug-in is valid or not, and memory settings can be set.The memory limit will be limited when the pod is created, and the plugin variables will take effect and real-time modification will continue to be introduced below.

Unique variable scope

The variables injected into the container are designed to have two types of:shared variables and plugin variables.

The shared variable is the variable of the main container. In order to allow the plug-in to participate in or even extend the functions of the main application, the environment variables of the main application are injected into the plug-in container during the pod creation process; variable repetition and mixing.

Plugin configuration

The plugin information is divided into two parts,version basic information and configuration group management.Specify the plugin’s build sources and the configuration of plugin variables separately.

Version basic information

The version information specifies the plugin’s installation source, plugin type, and minimum memory limit.

  • installation source

    Rainbond provides two installation sources: image and Dockerfile.When using a mirror, provide the mirror address.When using the Dockerfile, provide the project source code address.

  • Plugin type

    Plug-in types have entry network,exit network,exit entry co-governance network,performance analysis,initialization typeandgeneral type.

    ingress network,egress network,egress ingress co-governance network and performance analysis four types of plug-ins have the default support of Rainbond and are used as the default plug-in type.When users create a new plug-in, they can use to initialize type and to general type , two completely open types.

Plugin variable configuration

Plugin variables contain dependency metadata types, injection types and configuration item settings

Plug-in variables are set in the way of configuration management group , and the settings of plug-in variables are different between different configuration management groups.The settings of the plugin variables include dependent metadata types,injection types and configuration items.

  • Depends on metadata type

    Depending on the metadata type, you can choose independent of, component ports and downstream component ports three options.

    When making network management and plug-ins, you can use component ports and downstream component ports to monitor them to meet the needs of network management.Generic types and initialization types plugins use no dependencies by default.

  • injection type

    When the dependent metadata type chooses not to depend, the injection type has environment variables and actively discovered.Users can use environment variables to set configuration items, or use active discovery to dynamically set configuration items.

  • configuration item

    The configuration item supports setting protocol and parameter type.

    The purpose of the protocol setting is to support different port protocols differently.After the configuration in the configuration item sets the protocol, if it is inconsistent with the protocol of the listening port, the configuration cannot be performed, and naturally it cannot take effect during the use process.

    The parameter type supports string,single choice and multiple choice three types, providing rich parameter configuration for selection when using the plug-in.If the parameter type is selected as radio, when using this plug-in to configure the parameters, you can select the required value from the set value.

Plugin production

Here, the default plugin Alibaba Cloud Log Service Collection Plugin is used as an example to introduce how to create a plugin.

Refer to the log collection configuration template:officially provided by Alibaba Cloud

  1. apiVersion: batch/v1
  2. kind: Job
  3. metadata:
  4. name: nginx-log-sidecar-demo
  5. namespace: default
  6. spec:
  7. template:
  8. metadata:
  9. name: nginx-log-sidecar-demo
  10. spec:
  11. restartPolicy: Never
  12. containers:
  13. - name: nginx-log-demo
  14. image: registry.cn-hangzhou.aliyuncs.com/log-service/docker-log-test:latest
  15. command: ["/bin/mock_log"]
  16. args: ["--log-type=nginx", "--stdout=false", "--stderr=true", "--path=/var/log/nginx/access.log", "--total-count=1000000000", "--logs-per-sec=100"]
  17. volumeMounts:
  18. - name: nginx-log
  19. mountPath: /var/log/nginx
  20. ##### logtail sidecar container
  21. - name: logtail
  22. # more info: https://cr.console.aliyun.com/repository/cn-hangzhou/log-service/logtail/detail
  23. # this images is released for every region
  24. image: registry.cn-hangzhou.aliyuncs.com/log-service/logtail:latest
  25. # when recevie sigterm, logtail will delay 10 seconds and then stop
  26. command:
  27. - sh
  28. - -c
  29. - /usr/local/ilogtail/run_logtail.sh 10
  30. livenessProbe:
  31. exec:
  32. command:
  33. - /etc/init.d/ilogtaild
  34. - status
  35. initialDelaySeconds: 30
  36. periodSeconds: 30
  37. resources:
  38. limits:
  39. memory: 512Mi
  40. requests:
  41. cpu: 10m
  42. memory: 30Mi
  43. env:
  44. ##### base config
  45. # user id
  46. - name: "ALIYUN_LOGTAIL_USER_ID"
  47. value: "${your_aliyun_user_id}"
  48. # user defined id
  49. - name: "ALIYUN_LOGTAIL_USER_DEFINED_ID"
  50. value: "${your_machine_group_user_defined_id}"
  51. # config file path in logtail's container
  52. - name: "ALIYUN_LOGTAIL_CONFIG"
  53. value: "/etc/ilogtail/conf/${your_region_config}/ilogtail_config.json"
  54. ##### env tags config
  55. - name: "ALIYUN_LOG_ENV_TAGS"
  56. value: "_pod_name_|_pod_ip_|_namespace_|_node_name_|_node_ip_"
  57. - name: "_pod_name_"
  58. valueFrom:
  59. fieldRef:
  60. fieldPath: metadata.name
  61. - name: "_pod_ip_"
  62. valueFrom:
  63. fieldRef:
  64. fieldPath: status.podIP
  65. - name: "_namespace_"
  66. valueFrom:
  67. fieldRef:
  68. fieldPath: metadata.namespace
  69. - name: "_node_name_"
  70. valueFrom:
  71. fieldRef:
  72. fieldPath: spec.nodeName
  73. - name: "_node_ip_"
  74. valueFrom:
  75. fieldRef:
  76. fieldPath: status.hostIP
  77. volumeMounts:
  78. - name: nginx-log
  79. mountPath: /var/log/nginx
  80. ##### share this volume
  81. volumes:
  82. - name: nginx-log
  83. emptyDir: {}

The template is divided into two containers, one is the log generation service and the other is the log collection service.The two realize shared storage by mounting nginx-log storage at the same time, so that the log collection service can collect logs.

In addition to the mirror provided by the log collection service, you need to specify environment variables such as ALIYUN_LOGTAIL_USER_ID, ALIYUN_LOGTAIL_USER_DEFINED_ID, ALIYUN_LOGTAIL_CONFIG, ALIYUN_LOG_ENV_TAGS to configure the log collection service.

With the above template, the idea of building a plug-in will be very clear. First, you need to create a new plug-in by mirroring, and then set the required environment variables in advance.

  • Plugin build

On the plug-in page of the team management page, select New plug-in, choose for the installation source, for the mirror, fill in the mirror address in the template, and use general types and plug-in types to build the plug-in.

  • Plugin variable settings

In the plugin’s admin page, add plugin to configure management group.Add the environment variables in the template to the configuration management group of the plugin in the injection method of environment variable , using strings and parameters, and after installing and enabling the plugin, you can configure these environment variables to make them Takes effect in the plugin container.

Such a log collection plug-in is completed, and it can be installed in a component that generates log files to collect logs and test them on the Alibaba Cloud platform.

For the use of plugins, please refer to chapter Best Practices.

common problem

  • Plugin build failed

Pull the image, build the source code

When the plug-in is built, the build log will be printed on the page, and the log can be analyzed whether it is the problem of pulling the mirror, or the problem of source code compilation during the source code construction process.

  • Plugin does not work after installation

Check the image or Dockerfile of the plug-in to make sure that it can run independently, or you can use the log page in the component management page to check the reason why the plug-in cannot run through the real-time log.