Table of Contents
Template engine
Template definition
Template parser
Template converter
模板执行器
Home Backend Development PHP Tutorial PHP design pattern container deployment framework based on template engine

PHP design pattern container deployment framework based on template engine

Aug 16, 2017 am 09:33 AM
php container Design Patterns


Abstract: Container creation or application deployment configuration is complicated and variable. In order to ensure system flexibility and reusability, this article focuses on how to use the template engine as the core , build a unified container deployment framework.

Everyone has an experience when using containers. There are about forty or fifty container configuration items, and it requires a certain technical background to understand them. During the deployment process, users often encounter various problems when starting containers, deploying applications, or upgrading due to a lack of understanding of configuration parameters. How users can speed up their understanding of different parameters and expand accordingly according to different application types and scenarios. This article will focus on exploring and solving these problems.

Container creation or application deployment configuration is complicated and variable. In order to ensure system flexibility and reusability, we decided to build a unified container deployment framework with the template engine as the core. This article focuses on how to build a template engine and the operating principle of building a container deployment framework with the template engine as the core. In the template engine, files that comply with certain format specifications are the basis. For locations that may change or need to be changed according to the deployment process, parameters are used to identify the site. The definition of the parameter identifier is appended to the end of the template file to perform semantic transformation of the parameter identifier. The specific content of the template or parameter identification can be read or received from the client request parameters through a specific configuration file.

Template engine

The template engine consists of four modules: template definition, template parsing, template conversion, and template execution. The template definition depends on the management framework of the container cluster and is a non-executable file. The template parser is responsible for dividing the template into two parts: one part forms the non-executable deployment template; the other part forms the definition description of the parameters in the deployment template. The parameter definition description is unified with the site identifier in the deployment template through a unique site identifier. One correspondence. The template converter accepts parameter values ​​and combines them with the deployment template generated in the parser. The parameter value identifier is associated with the placeholder identifier in the template. The parameter value is replaced by the placeholder identifier to generate an executable file. The template executor is responsible for creating objects based on the template, and is generally responsible for the scheduling framework or container engine.

The execution principle of the template engine is shown in Figure 1:
PHP design pattern container deployment framework based on template engine

Figure 1 The execution principle of the template engine


Template definition

The template definition includes two types of information: deployment template; parameter identification.

Take the deployment template of kubernetes as an example. The deployment template involves four different types of definitions, namely: resources, versions, information descriptions, and data configurations.

  • Resource: Indicates the object type defined in kubernetes.

  • Version: Indicates the version of the object

  • Information description: Including object name, label, comments, etc., providing index for object search or scheduling.

  • Data configuration: Responsible for defining the standards that the container follows when it is running, including ports, environment variables, resources, scheduling, health checks, etc.

The parameter identifier consists of 6 attributes, namely parameters, name, description, displayname, value, and type.

  • parameters: parameter definition start flag

  • description: parameter prompt information

  • ##displayname : Specific semantic information

  • name: Corresponds to the reference parameter name, indicating that the description information is the corresponding reference parameter

  • value: Parameter default value

  • type: represents different styles. The client presents specific styles based on type

Taking the namespace object in kubernetes as an example, the complete definition of the template is as shown in the following code:

1

2

3

4

5

6

7

8

apiVersion: v1kind: Namespacemetadata:

  name: ${name }

---

{"parameters":

  [

    {      "description": "命名空间",      "displayName": "命名空间",      "name": "name",      "value": "",      "type": "String"

    }

]}

Copy after login

The above code contains two parts: deployment template and parameter description.

The deployment template is shown in the following code block:

1

2

apiVersion: v1kind: Namespacemetadata:

  name: ${name }

Copy after login

The deployment template defines all content created by the object. The meaning of the fields in the template is described as follows:

  • apiVersion : Common options, defining version information

  • Kind: Defining object types, distinguishing different objects

  • Metadata: Defining parameter keys specified during deployment Value pair

  • ${}: represents the reference value of the parameter, which can replace the parameter

parameter identifier, which defines the client’s dynamic acquisition of parameters. For the final display form, the following code example parameter identification definition:

1

2

3

4

5

6

7

8

9

{"parameters":  [

    {

      "description": "命名空间",

      "displayName": "命名空间",

      "name": "name",

      "value": "",

      "type": "String"

    }

]}

Copy after login

Parameter identification defines a unified format. Through semantic transformation, complex configurations are transformed into a way that is easy for users to understand. The client reads the Parameters identifier, abstracts the input parameters through the template parser, displays the required Form form, and provides the user input function.

Template definitions are written by professionals who are familiar with Kubernetes or Docker. Real-time and dynamic adjustments can be made based on specific business scenarios to ensure deployment flexibility and scalability. At the same time, the system provides basic templates based on different objects. Users can also create and maintain templates based on a certain knowledge background.

Template parser

Obtains the parameter identifiers in the template through the input and output streams, performs semantic conversion, and obtains easy-to-understand configuration parameters. The working principle of the template parser is shown in Figure 2 below:

PHP design pattern container deployment framework based on template engine

Figure 2 The working principle of the template parser


The client initiates a request to create an object. After the server receives the request, it will automatically associate the basic template according to the requested object type. The basic template is read through the file stream. During the reading process, the Parameters flag is used as the starting point to obtain parameter description information. After the parsing is completed, the parameters are returned to the client in the form of a Json string. The client dynamically generates a form that needs to be filled in by the user based on the Json string. The user completes the parameter input operation based on the content of the form.

The template parser focuses on parsing the parameter identifiers in the template definition. Through semantic transformation and information prompts, easily identifiable input items are formed. For users, complex technical indicators can be shielded after the analysis is completed, and the user's focus shifts from technology to business configuration. Minimize usage costs and increase ease of use.

Template converter

The template converter is the core of the template engine and focuses on solving three problems: obtaining deployment templates, converting parameters and values, and building executable files. The client assigns real values ​​to the parameters in the template parser and passes them to the server. The server reads the template content and ends when it encounters the flag bit of the parameter. It writes the read content to a new file through the file stream, generates a deployment file, and then Replace parameters in the deployment file with parameter values ​​to generate the final executable file. The working principle of the template converter is shown in Figure 3:

PHP design pattern container deployment framework based on template engine

Figure 3 The working principle of the template converter


Get the deployment template: It can be seen from the template definition that the template contains two parts: deployment template and parameter identification. The template converter first needs to deploy the template and read the deployment template in the template definition through file stream. During the reading process, it is divided by parameters identifier to obtain the deployment template.

参数值转化:核心是解决参数与占位符关联和赋值问题。模板转换器通过模板参数定义的name属性key关联,模板转化器拿到参数值以后,获取参数值对应的key(key在部署模板唯一),并且根据key,替换部署模板中占位标识,完成参数替换。

构建可执行文件:通过文件流的方式,把前两部转化的字符流输出到文件,构建出可执行文件。

模板转换器执行以后,生成的可执行文件如下所示:

1

2

apiVersion: v1kind: Namespacemetadata:

  name: ruffy

Copy after login

模板执行器

模板执行器接收可执行的部署文件,对于文件中定义的部署类型进行解析,拆分成若干个可执行任务。容器引擎根据收到的任务执行操作,最终协同完成部署工作。模板执行器往往依赖于容器调度和执行引擎。以Kubernetes容器编排框架为例,模板转化器生成的可执行文件,以字符流的方式传输到Kubernetes的Server端,Kubernetes根据传入文件,自动解析文件内容,并且做出相关操作。对于模板引擎而言,无论是Kubernetes还是Swarmkit都能够得到友好的支持。模板执行器的工作原理如图4所示:

PHP design pattern container deployment framework based on template engine

Figure 4 The working principle of the template executor


The result after the template executor is executed is shown in Figure 5:
PHP design pattern container deployment framework based on template engine

Figure 5


Through the template engine, the configuration of the container can be used flexibly, whether it is container deployment or other resource theme object creation, there are corresponding Template support. The template processing engine does not need to constantly modify the code according to template changes. At the same time, users can focus on configuration information from the semantics they understand, without paying attention to specific technical details and implementation methods, simplifying operating behaviors and reducing usage costs.

The above is the detailed content of PHP design pattern container deployment framework based on template engine. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1660
14
PHP Tutorial
1260
29
C# Tutorial
1233
24
How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

See all articles