1
0
Fork 0

Add TCP Healthcheck

This commit is contained in:
Douglas De Toni Machado 2025-10-22 06:42:05 -03:00 committed by GitHub
parent d1ab6ed489
commit 8392503df7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
37 changed files with 2416 additions and 307 deletions

View file

@ -0,0 +1,52 @@
[global]
checkNewVersion = false
sendAnonymousUsage = false
[log]
level = "DEBUG"
noColor = true
[entryPoints]
[entryPoints.tcp]
address = ":8093"
[api]
insecure = true
[providers.file]
filename = "{{ .SelfFilename }}"
## dynamic configuration ##
[tcp.routers]
[tcp.routers.router1]
rule = "HostSNI(`*`)"
service = "weightedservice"
[tcp.services]
[tcp.services.weightedservice.weighted]
[tcp.services.weightedservice.weighted.healthCheck]
[[tcp.services.weightedservice.weighted.services]]
name = "service1"
weight = 1
[[tcp.services.weightedservice.weighted.services]]
name = "service2"
weight = 1
[tcp.services.service1.loadBalancer]
[tcp.services.service1.loadBalancer.healthCheck]
interval = "500ms"
timeout = "500ms"
send = "PING"
expect = "Received: PING"
[[tcp.services.service1.loadBalancer.servers]]
address = "{{.Server1}}:8080"
[tcp.services.service2.loadBalancer]
[tcp.services.service2.loadBalancer.healthCheck]
interval = "500ms"
timeout = "500ms"
send = "PING"
expect = "Received: PING"
[[tcp.services.service2.loadBalancer.servers]]
address = "{{.Server2}}:8080"

View file

@ -199,9 +199,9 @@ func matchesConfig(wantConfig string, buf *bytes.Buffer) try.ResponseCondition {
sanitizedExpected := rxURL.ReplaceAll(bytes.TrimSpace(expected), []byte(`"$1": "XXXX"`))
sanitizedGot := rxURL.ReplaceAll(got, []byte(`"$1": "XXXX"`))
rxServerStatus := regexp.MustCompile(`"http://.*?":\s+(".*")`)
sanitizedExpected = rxServerStatus.ReplaceAll(sanitizedExpected, []byte(`"http://XXXX": $1`))
sanitizedGot = rxServerStatus.ReplaceAll(sanitizedGot, []byte(`"http://XXXX": $1`))
rxServerStatus := regexp.MustCompile(`"(http://)?.*?":\s+(".*")`)
sanitizedExpected = rxServerStatus.ReplaceAll(sanitizedExpected, []byte(`"XXXX": $1`))
sanitizedGot = rxServerStatus.ReplaceAll(sanitizedGot, []byte(`"XXXX": $1`))
if bytes.Equal(sanitizedExpected, sanitizedGot) {
return nil

View file

@ -0,0 +1,12 @@
services:
whoamitcp1:
image: traefik/whoamitcp
command:
- -name
- whoamitcp1
whoamitcp2:
image: traefik/whoamitcp
command:
- -name
- whoamitcp2

View file

@ -0,0 +1,114 @@
package integration
import (
"net"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"github.com/traefik/traefik/v3/integration/try"
)
// TCPHealthCheckSuite test suite for TCP health checks.
type TCPHealthCheckSuite struct {
BaseSuite
whoamitcp1IP string
whoamitcp2IP string
}
func TestTCPHealthCheckSuite(t *testing.T) {
suite.Run(t, new(TCPHealthCheckSuite))
}
func (s *TCPHealthCheckSuite) SetupSuite() {
s.BaseSuite.SetupSuite()
s.createComposeProject("tcp_healthcheck")
s.composeUp()
s.whoamitcp1IP = s.getComposeServiceIP("whoamitcp1")
s.whoamitcp2IP = s.getComposeServiceIP("whoamitcp2")
}
func (s *TCPHealthCheckSuite) TearDownSuite() {
s.BaseSuite.TearDownSuite()
}
func (s *TCPHealthCheckSuite) TestSimpleConfiguration() {
file := s.adaptFile("fixtures/tcp_healthcheck/simple.toml", struct {
Server1 string
Server2 string
}{s.whoamitcp1IP, s.whoamitcp2IP})
s.traefikCmd(withConfigFile(file))
// Wait for Traefik.
err := try.GetRequest("http://127.0.0.1:8080/api/rawdata", 60*time.Second, try.BodyContains("HostSNI(`*`)"))
require.NoError(s.T(), err)
// Test that we can consistently reach servers through load balancing.
var (
successfulConnectionsWhoamitcp1 int
successfulConnectionsWhoamitcp2 int
)
for range 4 {
out := s.whoIs("127.0.0.1:8093")
require.NoError(s.T(), err)
if strings.Contains(out, "whoamitcp1") {
successfulConnectionsWhoamitcp1++
}
if strings.Contains(out, "whoamitcp2") {
successfulConnectionsWhoamitcp2++
}
}
assert.Equal(s.T(), 2, successfulConnectionsWhoamitcp1)
assert.Equal(s.T(), 2, successfulConnectionsWhoamitcp2)
// Stop one whoamitcp2 containers to simulate health check failure.
conn, err := net.DialTimeout("tcp", s.whoamitcp2IP+":8080", time.Second)
require.NoError(s.T(), err)
s.T().Cleanup(func() {
_ = conn.Close()
})
s.composeStop("whoamitcp2")
// Wait for the health check to detect the failure.
time.Sleep(1 * time.Second)
// Verify that the remaining server still responds.
for range 3 {
out := s.whoIs("127.0.0.1:8093")
require.NoError(s.T(), err)
assert.Contains(s.T(), out, "whoamitcp1")
}
}
// connectTCP connects to the given TCP address and returns the response.
func (s *TCPHealthCheckSuite) whoIs(addr string) string {
s.T().Helper()
conn, err := net.DialTimeout("tcp", addr, time.Second)
require.NoError(s.T(), err)
s.T().Cleanup(func() {
_ = conn.Close()
})
_, err = conn.Write([]byte("WHO"))
require.NoError(s.T(), err)
_ = conn.SetReadDeadline(time.Now().Add(2 * time.Second))
buffer := make([]byte, 1024)
n, err := conn.Read(buffer)
require.NoError(s.T(), err)
return string(buffer[:n])
}

View file

@ -362,7 +362,10 @@
}
]
},
"status": "enabled"
"status": "enabled",
"serverStatus": {
"domain.com:9090": "UP"
}
},
"default-test3.route-673acf455cb2dab0b43a-whoamitcp-8080@kubernetescrd": {
"loadBalancer": {
@ -375,7 +378,11 @@
}
]
},
"status": "enabled"
"status": "enabled",
"serverStatus": {
"10.42.0.2:8080": "UP",
"10.42.0.6:8080": "UP"
}
},
"default-test3.route-673acf455cb2dab0b43a@kubernetescrd": {
"weighted": {

View file

@ -233,7 +233,11 @@
}
]
},
"status": "enabled"
"status": "enabled",
"serverStatus": {
"10.42.0.2:8080": "UP",
"10.42.0.6:8080": "UP"
}
},
"tcproute-default-tcp-app-1-gw-default-my-tcp-gateway-ep-footcp-0-e3b0c44298fc1c149afb-wrr@kubernetesgateway": {
"weighted": {