📜 ⬆️ ⬇️

The concept of simple Load-balancer'a / Failover'a IP services

In the process of solving one technical problem, the idea was born of a universal way of distributing the load on many machines with theoretically any services that can be accessed via IP.

Perhaps another bike, but did not come across before. If the idea turns out to be viable, whoever implements it, then it will not be wasted, unfortunately, there is no time for myself, although I really want to try. If this has already been implemented somewhere, I ask you not to judge strictly: I haven’t found it myself, but now I don’t have time for long searches.

So, in brief:
1. In nsswitch.conf for hosts, we prescribe our library to files and dns. So that in / etc / hosts and in DNS the system only looked if our library returned NSS_STATUS_UNAVAIL.
2. We make our library with just one function int gethostbyname_r (...) (see how to implement details here: www.gnu.org/s/hello/manual/libc/Name-Service-Switch.html#Name-Service-Switch ). The function looks for whether the requested name is in shared memory, if there is - returns the same IP address from there and increments the shared memory counter to use this name by 1. If there is no such name in shared memory, it returns NSS_STATUS_UNAVAIL.
3. If the library returned NSS_STATUS_UNAVAIL - the system continues to resolve the name in the IP further in a regular manner (/ etc / hosts, DNS).
4. In any language capable of opening a multicast socket and working with shared memory and named socket, a small service is written that:
4.1 reads its config (initial configuration) with information about services, sends it to a multicast group with the mark “initial” and puts it in shared memory.
4.2 All that comes from other members of a multicast group is loading into shared memory (or deletes, there is a deletion). If it is marked “initial”, send in response known changes regarding the initial configuration.
4.3 Listens to certain commands through a named socket, as a result, adding / deleting / rearranging something from shared memory and duplicating it into a multicast group.
4.4 Optional: when the counters in shared memory reach certain values, the host is deleted from there, it duplicates the command into the multicast group.
5. Everyone who needs to use a certain service connects to it by name - the library will give it away from shared memory instantly.
')
Since we essentially intercept the standard function of the system gethostbyname (), and in theory it uses everything that works on the IP protocol and agrees to use the host name (and not just IP directly) - without any additional changes with this scheme Most client applications will work. Some problems may arise with clients who cache proresolvleny IP (for example, web-browser-s), however, sometimes it is even a plus - the session will not break.

Thus, we get a certain skeleton that can be adjusted to anything and easily manage the load and availability of services (and / or scatter the load between them) by simply throwing commands at the named socket. This can be done with smart scripts / monitoring system / web-interface, stupid 'cat' there is no one file with commands in named socket by cron, and so on.

In addition, since the nss-library itself will be very primitive, small and stupid - it will work very quickly and reliably (it is quite difficult to make serious mistakes in such a primitive piece of code). True, nothing prevents you from messing with the managing service or with what is being addressed to it ...

What do you think?

Source: https://habr.com/ru/post/122162/


All Articles