Welcome to my GitHub

Github.com/zq2599/blog…

Content: all original article classification summary and supporting source code, involving Java, Docker, Kubernetes, DevOPS, etc.;

Links to OpenFaaS Field series articles

  1. The deployment of
  2. Introduction to the function
  3. Java functions
  4. Template operation (Template)
  5. Big watchdog
  6. Of -watchdog(For performance)
  7. Java11 template parsing
  8. Maven +jdk8
  9. Maven +maven+jdk8

This paper gives an overview of

  • This is the fourth chapter of The OpenFaaS Combat series. After the fast-paced operation of the first three chapters, we have a basic understanding of OpenFaaS. At least, we are familiar with the deployment and development of OpenFaaS.
  • The purpose of this article is to learn about templates.
  1. Basic commands
  2. Using a Third-party Template
  3. Make your own template warehouse
  4. Matters needing attention
  • From the overview above, we can see that this article is not much, but it is very basic and important.

About simplifying commands

As shown below, FAAS is actually a faas-CLI link, so it’s easier to type commands using FAAS:

[root@node1 template]# ls -l /usr/local/bin/faas
lrwxrwxrwx. 1 root root 23 11月 19 11:06 /usr/local/bin/faas -> /usr/local/bin/faas-cli
Copy the code

Basic commands

  1. Get all official templates:
faas template pull
Copy the code

After the execution is complete, a folder named template appears in the current directory containing all official templates

[root@node1 21]# ls template [root@node1 21]# cd template/ [root@node1 template]# ls csharp dockerfile go java11 java11-vert-x node node12 php7 python python3 python3-debian ruby [root@node1 template]# cd .. [21] root @ node1 # tree template/template / ├ ─ ─ csharp │ ├ ─ ─ Dockerfile │ ├ ─ ─ function │ │ ├ ─ ─ function. The csproj │ │ └ ─ ─ FunctionHandler. Cs │ ├ ─ ─ the Program. The cs │ ├ ─ ─ root. The csproj │ └ ─ ─ the template. The yml ├ ─ ─ dockerfile │ ├ ─ ─ function │ │ └ ─ ─ ├ ─ ├ ─ ├ ─ ├ ─ ├ ─ ├ ─ ├ ─ ├...Copy the code
  1. View the official template list:
faas template store list
Copy the code

The following message is returned (too many, part omitted) :

NAME SOURCE DESCRIPTION csharp openfaas Classic C# template dockerfile openfaas Classic Dockerfile template go openfaas Classic Golang template java8 openfaas Java 8 template java11 openfaas Java 11 template rust-http openfaas-incubator Rust HTTP template bash-streaming openfaas-incubator Bash Streaming template ... 3. Check the templates available in the current directory: shell faas new --listCopy the code

Terminal display:

[root@node1 21]# faas new --list
Languages available as templates:
- csharp
- dockerfile
- go
- java11
- java11-vert-x
- node
- node12
- php7
- python
- python3
- python3-debian
- ruby
Copy the code
  1. Once you have a template, you can create a function called java-function by using the following command:
faas-cli new --lang java11 java-function
Copy the code

After success, modify this file to add business code:./ SRC /main/ handler.java;

  1. Note the SOURCE field for each template if it was OpenFaas-incubator, such as rust-http, To download this, use the following command (with the openfaas-incubator prefix) :
faas-cli template store pull openfaas-incubator/rust-http
Copy the code

The above is the basic operation of the template, at this moment you may have a question: those are official templates, how to obtain the template of the third party? In addition, if I want to make templates for others to use, and how to operate? These questions, I’m going to answer one by one;

Using a Third-party Template

  1. I created a third-party template repository on GitHub to see how to use it.
  2. The template repository is still a normal GitHub repository, but it has to meet OpenFaaS requirements.
  3. My template warehouse address is: github.com/zq2599/open… , as shown below:

4. Download the above template repository command (note, find a clean folder to execute the command) :

faas template pull https://github.com/zq2599/openfaas-templates
Copy the code

You can see that the warehouse address is placed at the end of the command as an argument

  1. Dockerfile: java11extend: dockerFile: java11extend: dockerfile: java11extend:
[root@node1 333]# faas template pull https://github.com/zq2599/openfaas-templates Fetch templates from repository: https://github.com/zq2599/openfaas-templates at master 2020/11/22 11:19:53 Attempting to expand templates from https://github.com/zq2599/openfaas-templates 2020/11/22 11:19:58 Fetched 2 template(s) : [dockerfile java11extend] from https://github.com/zq2599/openfaas-templates [root@node1 333]# ls template [root@node1 055]# ├─ ├─ ├─ ├─ ├─ ├─ 055]# ├─ ├─ ├─ 055]# ├─ ├─ ├─ 055]# ├─ ├─ ├─ 055 Build. Gradle ├ ─ ─ Dockerfile ├ ─ ─ function │ ├ ─ ─ build. Gradle │ ├ ─ ─ gradle │ │ └ ─ ─ wrapper │ │ ├ ─ ─ gradle - wrapper. Jar │ │ └ ─ ─ gradle - wrapper. The properties │ ├ ─ ─ gradlew │ ├ ─ ─ gradlew. Bat │ ├ ─ ─ Settings. Gradle │ └ ─ ─ the SRC │ ├ ─ ─ the main │ │ └ ─ ─ Java │ │ └ ─ ─ com │ │ └ ─ ─ openfaas │ │ └ ─ ─ function │ │ └ ─ ─ Handler. Java │ └ ─ ─ the test │ └ ─ ─ Java │ └ ─ ─ HandlerTest. Java ├ ─ ─ gradle │ └ ─ ─ wrapper │ ├ ─ ─ gradle - wrapper. Jar │ └ ─ ─ gradle - wrapper. The properties ├ ─ ─ the README. Md ├ ─ ─ Settings. Gradle └ ─ ─ template.yml 16 directories, 17 filesCopy the code
  1. Run faas new –list to see if there are two templates in the GitHub repository:
[root@node1 333]# faas new --list
Languages available as templates:
- dockerfile
- java11extend
Copy the code
  1. Create functions using templates:
faas-cli new java11extend-function --lang java11extend -p bolingcavalry
Copy the code
  1. Making a mirror:
faas-cli build -f ./java11extend-function.yml
Copy the code

The console displays the following information indicating that the image is successfully created:

Step 29/30 : HEALTHCHECK --interval=5s CMD [ -e /tmp/.lock ] || exit 1
 ---> Running in 6b68ca763980
Removing intermediate container 6b68ca763980
 ---> 50cea9002e9c
Step 30/30 : CMD ["fwatchdog"]
 ---> Running in c2f87a87c8f4
Removing intermediate container c2f87a87c8f4
 ---> 8094a5064a20Successfully built 8094a5064a20 Successfully tagged bolingcavalry/java11extend-function:latest Image: Bolingcavalry/java11extend - function: latest built. [0] < Building java11extend - function done in 81.75 s. [0] Worker done. Total build time: 81.76 sCopy the code
  1. Next, please refer to the previous “OpenFaaS: Java Functions” for detailed steps, which will not be expanded in this article. The following pictures prove that the functions made by this template can be deployed and run normally:

Make your own template warehouse

After the actual combat in front of you, for how to make template warehouse, I believe you have the answer in your heart, here is a simple summary of the principles that need to be followed:

  1. There’s a public repository on GitHub;
  2. Everything must be in the Master branch;
  3. Use a folder named Template in the repository;
  4. In the template folder, there is the folder of each template, as shown below, there are two templates:

  1. As for the contents in the folder of each template, this involves template development. There are too many related contents to be expanded in this article, but I will only say the key points: Template. yml is a function description file, which is required, and Dockerfile, which is used to build the image. Other content is related to the characteristics of the various templates, which are used to create the image. All of these are used when building Java projects, and the construction results of Java projects will be put into the docker image. Reading the content of Dockerfile, you can see all of these in detail.
  2. Now in order to quickly make a template, you can copy the content of the official template, change the use of the address: github.com/openfaas/te…
  3. Once the repository is ready, anyone can download the repository and use it with commands like:
faas template pull https://github.com/zq2599/openfaas-templates
Copy the code

Pay attention to the point

Two final words of caution:

  1. For GitHub’s new repository, the default branch is mian instead of Master, but the pass-CLI command selects the master branch, so please ensure that the contents of your repository are in the Master branch, not main
  2. When you run the following command to view the official template, you can see the Java 8 template in the red box below, which is expected to be a good news for Java developers:

, despite in the list can see java8 OpenFaaS announced the official documentation java8 template has been abandoned, is not recommended, as the chart red box, the document address: docs.openfaas.com/cli/templat…

  • The above is the template related operation, this as the foundation is still very important, suggest you more hands-on familiar with the relevant commands, for the subsequent in-depth study to lay a good foundation;

You are not alone, Xinchen original accompany all the way

  1. Java series
  2. Spring series
  3. The Docker series
  4. Kubernetes series
  5. Database + middleware series
  6. The conversation series

Welcome to pay attention to the public number: programmer Xin Chen

Wechat search “programmer Xin Chen”, I am Xin Chen, looking forward to enjoying the Java world with you…

Github.com/zq2599/blog…