admin: default to port 12535, keep 23646 as a best-effort compat listener (#11816)

* admin: default to port 12535, keep 23646 as a best-effort compat listener

The default admin HTTP port 23646 derives a worker gRPC port of 33646
(port + GrpcPortOffset), inside the Linux default ephemeral range
(32768+). One of our own startup dials can claim 33646 before the admin
binds it, killing startup. This is the same failure #10209 fixed for
the test port allocator, still reachable via the mini default (#10838)
and via standalone `weed admin` defaults.

Move the default admin port to 12535 (gRPC 22535) everywhere, keeping
the http+10000 convention so worker -admin derivation, the Rust worker
and host:http.grpc addressing keep working:

- weed admin -port, weed mini -admin.port
- weed worker -admin, the plugin worker fallback, Rust worker and
  weed-lance-worker defaults
- help text, README, helm values, docker compose, tests

So old workers, bookmarks and scripts keep working, admin also serves
the same handler and the worker gRPC server on the previous default
(-port.legacy / -admin.port.legacy, default 23646, 0 disables). The
legacy gRPC port binds best-effort: it lives in the ephemeral range, so
a failed bind only warns instead of reintroducing the startup failure.
weed mini reserves it early like the primary port and keeps it out of
its own port relocation. grpc.Server tracks every listener handed to
Serve, so the compat listener stops with the primary.

Also warn when an explicitly configured admin port puts the derived
gRPC port at or above the ephemeral floor, the other fix suggested in
the issue.

Supersedes the pinned-grpc approach in #11814: pinning -admin.port.grpc
off the +10000 convention breaks external workers that derive the admin
gRPC port from the HTTP address. ClickHouse/ClickHouse#115512 pins
-admin.port on their side; this makes that pin unnecessary.

Fixes #10838

* admin: let services win the legacy ports, close compat listener on failure

Review findings on the compat listeners:

- Do not put the legacy ports in mini's reservedPorts: an explicit
  -s3.port=23646 or -master.port.grpc=33646 must be honored, not rejected
  or stolen by the early reservation. Mini now checks the finalized port
  set and skips only the conflicting compat listener, so HTTP and gRPC
  compat degrade independently and the service keeps its port.
- Also skip the legacy HTTP listener when a mini service owns the port:
  a wildcard-bound service can still leave loopback bindable, which would
  let the compat listener shadow it for loopback clients.
- Close legacyServer's raw listener in the deferred cleanup: Serve may
  not have registered it yet when startAdminServer returns early.
- Helm: add admin.legacyPort (default 23646) and expose the legacy
  http/grpc ports through the admin Service, container ports and
  NetworkPolicy so older workers resolved via the Service keep working.

* helm: only pass -port.legacy when it differs from the binary default

ct install deploys the released image with the new chart, and 4.48 does
not know -port.legacy, so the admin pod crash-loops. Omitting the flag at
the default value is behavior-identical on new binaries (their default
is also 23646) and keeps older releases installable; legacyPort: 0 or a
custom port still renders the flag.
This commit is contained in:
Chris Lu authored and GitHub committed 2026-10-18 21:21:05 -07:00
1 parent d3ee817b2e
commit 8ac5133abe
33 files changed
+316 -101

No files matched your search

+95 -7
View File
@@ -5,6 +5,7 @@ import (
"context"
"crypto/rand"
"crypto/tls"
"errors"
"fmt"
"log"
"net"
@@ -42,6 +43,7 @@ var (
type AdminOptions struct {
port *int
grpcPort *int
legacyPort *int
ip *string
master *string
masters *string // deprecated, for backward compatibility
@@ -68,6 +70,23 @@ type AdminOptions struct {
// binds it only after every other service is up.
workerGrpcListener net.Listener
// workerGrpcLegacyListener is the same reservation for the legacy
// compat gRPC port. Its bind failure is tolerated: the legacy port lives
// in the ephemeral range, so best-effort compat must not take startup down.
workerGrpcLegacyListener net.Listener
// workerGrpcLegacySkip disables just the legacy gRPC listener. `weed
// mini` sets it when another mini service already owns the legacy gRPC
// port, so the compat bind can neither steal the port nor warn about a
// failure that is expected.
workerGrpcLegacySkip bool
// adminHttpLegacySkip is the same for the legacy HTTP port. Skipping
// matters even when the bind would succeed: a service bound on a
// different address family or wildcard can leave loopback free, and the
// compat listener would then shadow it for loopback clients.
adminHttpLegacySkip bool
// workerGrpcBindIp, when non-empty, is the address the worker gRPC
// listener binds to. It is separate from ip because the worker gRPC has
// no password auth (its mTLS comes from grpc.admin, not https.admin), so
@@ -83,8 +102,9 @@ type AdminOptions struct {
func init() {
cmdAdmin.Run = runAdmin // break init cycle
a.port = cmdAdmin.Flag.Int("port", 23646, "admin server port")
a.port = cmdAdmin.Flag.Int("port", defaultAdminHTTPPort, "admin server port")
a.grpcPort = cmdAdmin.Flag.Int("port.grpc", 0, "gRPC server port for worker connections (default: http port + 10000)")
a.legacyPort = cmdAdmin.Flag.Int("port.legacy", defaultAdminHTTPLegacyPort, "also serve admin http and gRPC (port + 10000) on this port for backward compatibility (0 to disable)")
a.ip = cmdAdmin.Flag.String("ip", "127.0.0.1", "ip address to listen on. Defaults to loopback when auth is disabled, or 0.0.0.0 when -adminPassword or [https.admin] mTLS is configured. Set explicitly to override.")
a.master = cmdAdmin.Flag.String("master", "localhost:9333", "comma-separated master servers")
a.masters = cmdAdmin.Flag.String("masters", "", "comma-separated master servers (deprecated, use -master instead)")
@@ -108,7 +128,7 @@ func init() {
}
var cmdAdmin = &Command{
UsageLine: "admin -port=23646 -master=localhost:9333 [-filerGroup=group] [-port.grpc=33646] [-dataDir=/path/to/data]",
UsageLine: "admin -port=12535 -master=localhost:9333 [-filerGroup=group] [-port.grpc=22535] [-dataDir=/path/to/data]",
Short: "start SeaweedFS web admin interface",
Long: `Start a web admin interface for SeaweedFS cluster management.
@@ -124,12 +144,12 @@ var cmdAdmin = &Command{
A gRPC server for worker connections runs on the configured gRPC port (default: HTTP port + 10000).
Example Usage:
weed admin -port=23646 -master="master1:9333,master2:9333"
weed admin -port=23646 -master="localhost:9333" -filerGroup="tenant-a"
weed admin -port=23646 -master="localhost:9333" -dataDir="/var/lib/seaweedfs-admin"
weed admin -port=23646 -port.grpc=33646 -master="localhost:9333" -dataDir="~/seaweedfs-admin"
weed admin -port=12535 -master="master1:9333,master2:9333"
weed admin -port=12535 -master="localhost:9333" -filerGroup="tenant-a"
weed admin -port=12535 -master="localhost:9333" -dataDir="/var/lib/seaweedfs-admin"
weed admin -port=12535 -port.grpc=22535 -master="localhost:9333" -dataDir="~/seaweedfs-admin"
weed admin -port=9900 -port.grpc=19900 -master="localhost:9333"
weed admin -port=23646 -master="localhost:9333" -urlPrefix="/seaweedfs"
weed admin -port=12535 -master="localhost:9333" -urlPrefix="/seaweedfs"
Data Directory:
- If dataDir is specified, admin configuration and maintenance data is persisted
@@ -295,6 +315,9 @@ func runAdmin(cmd *Command, args []string) bool {
if *a.grpcPort == 0 {
*a.grpcPort = *a.port + 10000
}
if *a.grpcPort >= linuxEphemeralPortFloor {
glog.Warningf("Admin gRPC port %d is at or above the Linux ephemeral floor %d; an outbound connection may claim it before Admin binds it. Pick a lower -port.", *a.grpcPort, linuxEphemeralPortFloor)
}
hasMTLS := viper.GetString("https.admin.key") != "" && viper.GetString("https.admin.ca") != ""
@@ -504,6 +527,31 @@ func startAdminServer(ctx context.Context, options AdminOptions, enableUI bool,
}
warnInsecureWorkerGrpcBind(workerGrpcIp, *options.grpcPort, adminServer.WorkerGrpcMTLSEnabled())
// Legacy compat listeners keep the previous default admin port reachable
// for old workers, bookmarks and scripts while the deployment moves to the
// new default. Best-effort only: the legacy gRPC port sits inside the
// Linux ephemeral range, so it may already be taken and must never fail
// startup.
legacyPort := 0
if options.legacyPort != nil && *options.legacyPort > 0 && *options.legacyPort != *options.port {
legacyPort = *options.legacyPort
}
if legacyPort > 0 && !options.workerGrpcLegacySkip {
legacyGrpcPort := legacyPort + GrpcPortOffset
legacyGrpcListener := options.workerGrpcLegacyListener
if legacyGrpcListener == nil {
if l, listenErr := net.Listen("tcp", util.JoinHostPort(workerGrpcIp, legacyGrpcPort)); listenErr == nil {
legacyGrpcListener = l
} else {
glog.Warningf("Legacy admin gRPC port %d unavailable, skipping compat listener: %v", legacyGrpcPort, listenErr)
}
}
if legacyGrpcListener != nil {
adminServer.ServeWorkerGrpcOn(legacyGrpcListener)
glog.Infof("Worker gRPC compat listener on %s", legacyGrpcListener.Addr())
}
}
// Set up cleanup for gRPC server
defer func() {
if stopErr := adminServer.StopWorkerGrpcServer(); stopErr != nil {
@@ -578,6 +626,43 @@ func startAdminServer(ctx context.Context, options AdminOptions, enableUI bool,
server.TLSConfig.GetCertificate = getCert
}
// Serve the same handler on the legacy port so old clients keep working.
// Best-effort like the gRPC compat listener above.
var legacyServer *http.Server
if legacyPort > 0 && !options.adminHttpLegacySkip {
legacyAddr := util.JoinHostPort(*options.ip, legacyPort)
if legacyListener, listenErr := net.Listen("tcp", legacyAddr); listenErr != nil {
glog.Warningf("Legacy admin HTTP port %d unavailable, skipping compat listener: %v", legacyPort, listenErr)
} else {
legacyServer = &http.Server{
Addr: legacyAddr,
Handler: server.Handler,
TLSConfig: server.TLSConfig,
}
// Close on every exit path so a failed primary startup cannot
// leave the compat port held by a zombie admin. On the normal
// path the graceful Shutdown below runs first; this Close then
// no-ops. Close the raw listener too: Serve may not have
// registered it with legacyServer yet when this runs.
defer func() {
_ = legacyListener.Close()
_ = legacyServer.Close()
}()
go func() {
glog.Infof("Admin compat listener on %s", legacyAddr)
var serveErr error
if useTLS {
serveErr = legacyServer.ServeTLS(legacyListener, "", "")
} else {
serveErr = legacyServer.Serve(legacyListener)
}
if serveErr != nil && serveErr != http.ErrServerClosed && !errors.Is(serveErr, net.ErrClosed) {
glog.Warningf("Admin compat listener on %s stopped: %v", legacyAddr, serveErr)
}
}()
}
}
// Start server. Surface immediate failures (e.g. bind error) via
// serveErrCh so the caller doesn't block on ctx.Done() while no server
// is actually listening. http.ErrServerClosed after Shutdown is normal
@@ -612,6 +697,9 @@ func startAdminServer(ctx context.Context, options AdminOptions, enableUI bool,
if err := server.Shutdown(shutdownCtx); err != nil {
return fmt.Errorf("admin server forced to shutdown: %w", err)
}
if legacyServer != nil {
_ = legacyServer.Shutdown(shutdownCtx)
}
adminServer.Shutdown()