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

  • As part 5 of the OpenFaaS Series, we need to face the key technology of OpenFaaS: Watchdog. We can’t continue without understanding it.
  • This article is based on theory, which is also the author’s weakness, but I will try to put the key points to be concise and clear, if you find a problem, please be sure to point out in time, thank you!
  • The whole article consists of the following paragraphs:
  1. From the faas – netes talked about
  2. The resources of the OpenFaaS
  3. The watchdog analysis
  4. summary
  5. Worries for Java programmers

Let’s start this journey together, the most important stop on the OpenFaaS development road!

From the faas – netes talked about

  1. The overall architecture of OpenFaaS under Kubernetes is shown in the following figure. The external request is forwarded to the FaAS-Netes component by Gateway:

In the K8S environment, faas-Netes is the service provider. The service it provides supports REST API, client, WEB and other interconnection methods. In addition, it can also be managed by kubectl command. Implement K8S operator mode (for K8S operator, you can learn Controller first, and then imagine highly customizing CDR and Controller) :

  1. Faas-netes is very powerful, but in this article we only need to focus on one point: Faas-netes provides a function service, that is, python and Java Hello world functions are related to Faas-Netes.
  2. A lot of talk, leading role watchdog still not come out? Wait, because everyone is asking the same question: How did I write a Python script that only has a Hello world method in it and become a function provided by Faas-Netes?
  3. The request from the API Gateway will arrive at port 8080 of the FAAS-Provider. If it is to call published functions, it will be processed in the red box in the upper left corner. If it is to add, delete, change and check resources, it will be handled in the green box in the lower right corner.

The resources of the OpenFaaS

Xuan’s responsibility is to deal with resources. This is not the focus of this article, but the author is a Kubernetes fan and feels it is necessary to xuan’s knowledge about resources.

  1. In K8S, Pod, Deployment and Service are all resources, and there are corresponding controllers to adjust and control these resources according to the expected state saved in ETCD.
  2. For OpenFaaS in the K8S environment, it also has its own defined resource types (the YAML folder, mentioned in the first installment, contains a crd.yml file that records OpenFaaS resource definitions).
  3. How does OpenFaaS control its resources? Faas-netes provides CRUD interfaces for external calls, and the internal implementation of these interfaces is shown in the green box above. Obviously, the classical K8S Controller mode does not meet the requirements of OpenFaaS for resource control, so the popular Operator mode is adopted. More complex resource definitions, more complex resource control logic
  4. The crd.yml file and the OpenFaaS Operator code are available in more detail, but the figure above gives us some direction: Secret, Deployment, Service, and so on. When we publish business functions to OpenFaaS, we focus on Secret, Deployment configuration, and exposed Services.
  • Is our understanding of Faas-Netes any better now, after A chui and niu by Hsin Chen? The faas-Netes Operator is responsible for publishing functions, controlling resources and adjusting them later, so it is time to return to the main topic: function call
  • Watchdog is already crying…

The watchdog analysis

  • Again in the previous image, let’s focus on the upper right corner and I’ll cut it out as shown below:

  • If we use nodejs template development function, wrote a index. The js file, then the response external request will go to the below red box position, into the Watchdog 8080 port, the Watchdog is a new node process, the process index, js file:

  • Isn’t that clear enough? So let’s go into a little bit more detail. We’ll make a Docker image when we develop the function. What’s in the image? Here’s the official picture: There is a Watchdog in the mirror, listening to port 8080, fork a process after receiving the request, pass the request parameters to this process through stdin, process call our own written function method, and pass the parameters to this method, such as method execution, return value through stdout to the Watchdog

  • Now, after you’ve written a function, you probably have a pretty good idea of how an external request gets called into the code you wrote. However, there’s still a small blind spot: I know what a Watchdog can do, but what is a Watchdog? Docker image is a docker image.
  • Dockerfile is a script file used to create a docker image. It can be found in the Dockerfile template.
  • Openfaas/classics-watchdog :0.18.18: openfaas/ classics-watchdog :0.18.18:
FROM --platform=${TARGETPLATFORM:-linux/amd64} openfaas/classic-watchdog:0.18.18 as watchdog FROM --platform=${TARGETPLATFORM:-linux/amd64} openfaas/classic-watchdog:0.18.18 as watchdog FROM --platform=${TARGETPLATFORM:-linux/amd64} node:12.13.0-alpine as ship COPY --from=watchdog /fwatchdog /usr/bin/fwatchdog  RUN chmod +x /usr/bin/fwatchdogCopy the code
  • The Dockerfile ends with the following, which means that the image’s container executes fwatchdog as soon as it starts:
CMD ["fwatchdog"]
Copy the code
  • So far, you have enough understanding of Watchdog, if the previous information is too large, let’s make a summary;

summary

  1. Function development, when the function file written after the completion of the docker image began to make;
  2. The image produced contains the fwatchdog file and the function we wrote. If it is python, NodeJS and other scripting languages, it will copy the script and NodeJS or Python to the image. If it is Java, it will also involve compilation and construction.
  3. Once the function is deployed, the Kubernetes environment will create a POD based on the image, and once the POD is started, it will run the fwatchdog file, that is, the watchdog process is started;
  4. When an external access function is requested, the request goes first to the API Gateway and then to port 8080 of the pod created in the previous step.
  5. This POD, is the watchdog in listening port 8080, after receiving the request, create a node process, the request parameters through stdin to the node process;
  6. The Node process will execute the function we wrote when we developed the function, and receive the parameters as the function input parameter;
  7. After the function is executed, the Node process will write the return value to stdout, at this time the watchdog will receive the return value of the function through stdout;
  8. The watchdog returns the received value to the API Gateway and ultimately to the user;
  • This time finally clear it, if you still want to go further, please move watchdog source code, the address is: github.com/openfaas/cl… This is written by Golang. The author, Hsin Chen, is afraid to say more for fear of being beaten because of his Java background.

Worries for Java programmers

  1. If you’re a Java programmer, do you share the author’s concerns?
  2. Let’s take a look at the architecture of Tomcat, as shown below:

Tomcat: listen on 8080, receive the request, from the thread pool specified thread processing; Watctdog: listen on 8080 and start a process to process the request. 4. If you’re a Java programmer, you should be aware of this concern: Starting a process means creating JVM instances, and then creating threads, which consume more system resources (CPU, memory) than the business logic. If you fork a lot of processes to handle high concurrency, you can expect the cost. 5. So what’s the truth? Is it possible to use watchdog + fork to develop Java functions on OpenFaaS? Let’s go into detail in the next article, this article does not paste code, pure manual typing, really too tired… 6. Reveal the plot: OpenFaaS is great, the above problems have been solved, it’s up to Alex Ellis to do something about it;

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…