Support load balancing in caddy

This commit is contained in:
Roberto Alsina 2024-07-04 16:39:43 -03:00
parent 83b6615503
commit d0e2a1a494
3 changed files with 34 additions and 24 deletions

View File

@ -1,13 +1,15 @@
{
https_port 8888
http_port 8887
http_port 8888
https_port 8887
local_certs
admin off
}
localhost:8888 {
http://localhost:8888 {
handle_path /admin/terminal/* {
reverse_proxy /* http://127.0.0.1:7681
}
handle_path /admin/* {
reverse_proxy /* http://127.0.0.1:3000
}
}
}

View File

@ -23,6 +23,5 @@ COPY Caddyfile ./
COPY --from=build /home/app/bin/faaso-daemon /home/app/bin/faaso /usr/bin/
RUN mkdir /secrets
RUN echo "sarasa" > /secrets/sarlanga
CMD ["/usr/bin/multirun", "-v", "faaso-daemon", "caddy run --config Caddyfile"]

View File

@ -20,37 +20,46 @@ module Proxy
def self.update_proxy_config
docker_api = Docr::API.new(Docr::Client.new)
containers = docker_api.containers.list(all: true)
funkos = [] of String
containers.each { |container|
names = container.names.select &.starts_with? "/faaso-"
next if names.empty?
funkos << names[0][7..]
}
funkos.sort!
config = %(
config = <<-CONFIG
{
https_port 8888
http_port 8887
http_port 8888
https_port 8887
local_certs
admin off
}
localhost:8888 {
http://localhost:8888 {
handle_path /admin/terminal/* {
reverse_proxy /* http://127.0.0.1:7681
}
handle_path /admin/* {
reverse_proxy /* http://127.0.0.1:3000
}
) + funkos.map { |funko| %(
handle_path /faaso/#{funko.split("-")[0]}/* {
reverse_proxy /* http://#{funko}:3000
}
) }.join("\n") + "}"
# FIXME that 👆🏼 is not a functional load balancing config if scale > 1
CONFIG
funkos = Funko::Funko.from_docker
funkos.each do |funko|
next if funko.name == "proxy"
containers = funko.containers
next if containers.empty?
funko_urls = containers.map { |container|
"http://#{container.names[0].lstrip("/")}:3000"
}
config += %(
handle_path /faaso/#{funko.name}/* {
reverse_proxy /* #{funko_urls.join(" ")} {
health_uri /ping
fail_duration 30s
}
}
)
end
config += "\n}"
if @@current_config != config
Log.info { "Updating proxy config" }
File.open("Caddyfile", "w") do |file|
file << config
end