In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the relevant knowledge of "how to achieve Kubernetes Resource monitoring". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Analysis of WATCH request
Using Kubernetes API from Python is easy if you use the Request library. API runs very well, always using and returning JSON messages. But issuing watch requests becomes much more complicated. There are theoretically two ways to send a watch request: one is to stream a normal HTTP request of the result, while using chunked encoding, and the other is to use websockets. Unfortunately, when testing Kubernetes1.1 master, the websocket protocol is not used correctly, so using streaming results is the right approach.
When using a chunked coded stream, Kubernetes master begins to transmit chunks by sending the size of the chunk. But it does not send an entire chunk, it only sends one line of text, which is terminated by a new line of text. This line of text is an JSON-encoded object, along with event and a modified resource project. So the protocol is line-based, and block coding is just a way to stream results when they are available. On the face of it, it doesn't seem so difficult to do this with a request:
While the iter_lines method doesn't go the way you want it to, it keeps an external buffer, which means you'll never see the last event because you're still waiting to fill that buffer.
Raising this question means using the original set of socket by implementing your own iter_lines () function, from responding to socket to reading socket. Unfortunately, that simple method made some mistakes. First of all, it does not handle the block encoding correctly, and octets that describe the size of the block appear in the output process. But more importantly, another buffer layer is continuing, one that you can't do emergency operations. The additional buffering is because the request uses the generated file method of the original socket to read data from it. This makes sense for Requests, where both the Python standard library and OS are good at accelerating through buffering. However, this does not mean that after Requests parses the response header, the buffer does not know how many bytes of the response itself are used, and these bytes cannot be retrieved. So it's basically impossible to use Requests to use watch API.
HTTP manually
So how do I use watch API from Python? Make requests and process responses on your own. This is actually very simple to do, socket programming is actually not that scary. First, you need to connect socket to the server, and then send HTTP request. HTTP is very simple, you just need to send some headers on socket:
Note that the Host header is required by Kubernetes master to accept request.
Parsing the HTTP response is a little more complicated. However, when the http-parser library implements something about HTTP parsing, it doesn't involve sockets or anything similar to the web. So we can easily read and parse the response:
Now let's respond to the header that has been parsed. Most likely, some ontology data has been received, which is great, and it is still in a buffered state in the parser until we retrieve it. But first, let's keep reading until there is nothing left (don't do this during production, it's bad for your storage system).
The figure above shows how to use select to read only data when it is available, rather than blocking it and then making it readable again. Of course, once all the data is used, Kubernetes master may send the next version update to PodList, but let's read the received events first:
It is! If the data is received up to the newline character, then the lines.split () call returns an empty string (bounded') as the last item. If the data does not end at a new newline character, an unfinished event will be received so that we need to save it when we get other data.
This is the end of the content of "how to implement Kubernetes Resource Monitoring". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.