Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support systemd socket-activation #10399

Merged
merged 4 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions docs/content/routing/entrypoints.md
Original file line number Diff line number Diff line change
Expand Up @@ -1175,3 +1175,25 @@ entryPoints:
```

{!traefik-for-business-applications.md!}

## Systemd Socket Activation

Traefik supports [systemd socket activation](https://www.freedesktop.org/software/systemd/man/latest/systemd-socket-activate.html).

When a socket activation file descriptor name matches an EntryPoint name, the corresponding file descriptor will be used as the TCP listener for the matching EntryPoint.

```bash
systemd-socket-activate -l 80 -l 443 --fdname web:websecure ./traefik --entrypoints.web --entrypoints.websecure
```

!!! warning "EntryPoint Address"

When a socket activation file descriptor name matches an EntryPoint name its address configuration is ignored.

!!! warning "TCP Only"

Socket activation is not yet supported with UDP entryPoints.

!!! warning "Docker Support"

Socket activation is not supported by Docker but works with Podman containers.
48 changes: 34 additions & 14 deletions pkg/server/server_entrypoint_tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,15 @@ const (
var (
clientConnectionStates = map[string]*connState{}
clientConnectionStatesMu = sync.RWMutex{}

socketActivationListeners map[string]net.Listener
)

func init() {
// Populates pre-defined socketActivationListeners by socket activation.
populateSocketActivationListeners()
}

type connState struct {
State string
KeepAliveState string
Expand Down Expand Up @@ -96,6 +103,7 @@ func NewTCPEntryPoints(entryPointsConfig static.EntryPoints, hostResolverConfig
return clientConnectionStates
}))
}

serverEntryPointsTCP := make(TCPEntryPoints)
for entryPointName, config := range entryPointsConfig {
protocol, err := config.GetProtocol()
Expand All @@ -113,7 +121,7 @@ func NewTCPEntryPoints(entryPointsConfig static.EntryPoints, hostResolverConfig
OpenConnectionsGauge().
With("entrypoint", entryPointName, "protocol", "TCP")

serverEntryPointsTCP[entryPointName], err = NewTCPEntryPoint(ctx, config, hostResolverConfig, openConnectionsGauge)
serverEntryPointsTCP[entryPointName], err = NewTCPEntryPoint(ctx, entryPointName, config, hostResolverConfig, openConnectionsGauge)
if err != nil {
return nil, fmt.Errorf("error while building entryPoint %s: %w", entryPointName, err)
}
Expand Down Expand Up @@ -169,10 +177,10 @@ type TCPEntryPoint struct {
}

// NewTCPEntryPoint creates a new TCPEntryPoint.
func NewTCPEntryPoint(ctx context.Context, configuration *static.EntryPoint, hostResolverConfig *types.HostResolverConfig, openConnectionsGauge gokitmetrics.Gauge) (*TCPEntryPoint, error) {
func NewTCPEntryPoint(ctx context.Context, name string, config *static.EntryPoint, hostResolverConfig *types.HostResolverConfig, openConnectionsGauge gokitmetrics.Gauge) (*TCPEntryPoint, error) {
tracker := newConnectionTracker(openConnectionsGauge)

listener, err := buildListener(ctx, configuration)
listener, err := buildListener(ctx, name, config)
if err != nil {
return nil, fmt.Errorf("error preparing server: %w", err)
}
Expand All @@ -181,19 +189,19 @@ func NewTCPEntryPoint(ctx context.Context, configuration *static.EntryPoint, hos

reqDecorator := requestdecorator.New(hostResolverConfig)

httpServer, err := createHTTPServer(ctx, listener, configuration, true, reqDecorator)
httpServer, err := createHTTPServer(ctx, listener, config, true, reqDecorator)
if err != nil {
return nil, fmt.Errorf("error preparing http server: %w", err)
}

rt.SetHTTPForwarder(httpServer.Forwarder)

httpsServer, err := createHTTPServer(ctx, listener, configuration, false, reqDecorator)
httpsServer, err := createHTTPServer(ctx, listener, config, false, reqDecorator)
if err != nil {
return nil, fmt.Errorf("error preparing https server: %w", err)
}

h3Server, err := newHTTP3Server(ctx, configuration, httpsServer)
h3Server, err := newHTTP3Server(ctx, config, httpsServer)
if err != nil {
return nil, fmt.Errorf("error preparing http3 server: %w", err)
}
Expand All @@ -206,7 +214,7 @@ func NewTCPEntryPoint(ctx context.Context, configuration *static.EntryPoint, hos
return &TCPEntryPoint{
listener: listener,
switcher: tcpSwitcher,
transportConfiguration: configuration.Transport,
transportConfiguration: config.Transport,
tracker: tracker,
httpServer: httpServer,
httpsServer: httpsServer,
Expand Down Expand Up @@ -460,17 +468,29 @@ func buildProxyProtocolListener(ctx context.Context, entryPoint *static.EntryPoi
return proxyListener, nil
}

func buildListener(ctx context.Context, entryPoint *static.EntryPoint) (net.Listener, error) {
listenConfig := newListenConfig(entryPoint)
listener, err := listenConfig.Listen(ctx, "tcp", entryPoint.GetAddress())
if err != nil {
return nil, fmt.Errorf("error opening listener: %w", err)
func buildListener(ctx context.Context, name string, config *static.EntryPoint) (net.Listener, error) {
var listener net.Listener
var err error

// if we have predefined listener from socket activation
if ln, ok := socketActivationListeners[name]; ok {
listener = ln
} else {
if len(socketActivationListeners) > 0 {
log.Warn().Str("name", name).Msg("Unable to find socket activation listener for entryPoint")
}

listenConfig := newListenConfig(config)
listener, err = listenConfig.Listen(ctx, "tcp", config.GetAddress())
if err != nil {
return nil, fmt.Errorf("error opening listener: %w", err)
}
}

listener = tcpKeepAliveListener{listener.(*net.TCPListener)}

if entryPoint.ProxyProtocol != nil {
listener, err = buildProxyProtocolListener(ctx, entryPoint, listener)
if config.ProxyProtocol != nil {
listener, err = buildProxyProtocolListener(ctx, config, listener)
if err != nil {
return nil, fmt.Errorf("error creating proxy protocol listener: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/server/server_entrypoint_tcp_http3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func TestHTTP3AdvertisedPort(t *testing.T) {
epConfig := &static.EntryPointsTransport{}
epConfig.SetDefaults()

entryPoint, err := NewTCPEntryPoint(context.Background(), &static.EntryPoint{
entryPoint, err := NewTCPEntryPoint(context.Background(), "", &static.EntryPoint{
Address: "127.0.0.1:8090",
Transport: epConfig,
ForwardedHeaders: &static.ForwardedHeaders{},
Expand Down
10 changes: 5 additions & 5 deletions pkg/server/server_entrypoint_tcp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func testShutdown(t *testing.T, router *tcprouter.Router) {
epConfig.RespondingTimeouts.ReadTimeout = ptypes.Duration(5 * time.Second)
epConfig.RespondingTimeouts.WriteTimeout = ptypes.Duration(5 * time.Second)

entryPoint, err := NewTCPEntryPoint(context.Background(), &static.EntryPoint{
entryPoint, err := NewTCPEntryPoint(context.Background(), "", &static.EntryPoint{
// We explicitly use an IPV4 address because on Alpine, with an IPV6 address
// there seems to be shenanigans related to properly cleaning up file descriptors
Address: "127.0.0.1:0",
Expand Down Expand Up @@ -159,7 +159,7 @@ func TestReadTimeoutWithoutFirstByte(t *testing.T) {
epConfig.SetDefaults()
epConfig.RespondingTimeouts.ReadTimeout = ptypes.Duration(2 * time.Second)

entryPoint, err := NewTCPEntryPoint(context.Background(), &static.EntryPoint{
entryPoint, err := NewTCPEntryPoint(context.Background(), "", &static.EntryPoint{
Address: ":0",
Transport: epConfig,
ForwardedHeaders: &static.ForwardedHeaders{},
Expand Down Expand Up @@ -196,7 +196,7 @@ func TestReadTimeoutWithFirstByte(t *testing.T) {
epConfig.SetDefaults()
epConfig.RespondingTimeouts.ReadTimeout = ptypes.Duration(2 * time.Second)

entryPoint, err := NewTCPEntryPoint(context.Background(), &static.EntryPoint{
entryPoint, err := NewTCPEntryPoint(context.Background(), "", &static.EntryPoint{
Address: ":0",
Transport: epConfig,
ForwardedHeaders: &static.ForwardedHeaders{},
Expand Down Expand Up @@ -236,7 +236,7 @@ func TestKeepAliveMaxRequests(t *testing.T) {
epConfig.SetDefaults()
epConfig.KeepAliveMaxRequests = 3

entryPoint, err := NewTCPEntryPoint(context.Background(), &static.EntryPoint{
entryPoint, err := NewTCPEntryPoint(context.Background(), "", &static.EntryPoint{
Address: ":0",
Transport: epConfig,
ForwardedHeaders: &static.ForwardedHeaders{},
Expand Down Expand Up @@ -282,7 +282,7 @@ func TestKeepAliveMaxTime(t *testing.T) {
epConfig.SetDefaults()
epConfig.KeepAliveMaxTime = ptypes.Duration(time.Millisecond)

entryPoint, err := NewTCPEntryPoint(context.Background(), &static.EntryPoint{
entryPoint, err := NewTCPEntryPoint(context.Background(), "", &static.EntryPoint{
Address: ":0",
Transport: epConfig,
ForwardedHeaders: &static.ForwardedHeaders{},
Expand Down
24 changes: 24 additions & 0 deletions pkg/server/socket_activation_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//go:build !windows

package server

import (
"net"

"github.com/coreos/go-systemd/activation"
"github.com/rs/zerolog/log"
)

func populateSocketActivationListeners() {
listenersWithName, _ := activation.ListenersWithNames()

socketActivationListeners = make(map[string]net.Listener)
for name, lns := range listenersWithName {
if len(lns) != 1 {
log.Error().Str("listenersName", name).Msg("Socket activation listeners must have one and only one listener per name")
continue
}

socketActivationListeners[name] = lns[0]
}
}
5 changes: 5 additions & 0 deletions pkg/server/socket_activation_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//go:build windows

package server

func populateSocketActivationListeners() {}
Loading