feat: allow to bind to multiple host IP addresses specified as CIDR prefix in x-ports (#358)

Signed-off-by: Miek Gieben <miek@miek.nl>
Co-authored-by: Pasha Sviderski <me@psviderski.name>
This commit is contained in:
Miek Gieben
2026-06-04 20:07:58 +10:00
committed by GitHub
co-authored by Pasha Sviderski
parent 3586a32987
commit 1247f961c2
11 changed files with 312 additions and 76 deletions
@@ -45,20 +45,23 @@ extension:
network interface(s). This is useful for non-HTTP services that need direct port access (bypasses Caddy):
```
[host_ip:]host_port:container_port[/protocol]@host
[host_ip|host_prefix:]host_port:container_port[/protocol]@host
```
- `host_ip` (optional): The IP address on the host to bind to. If omitted, binds to all interfaces.
- `host_ip` / `host_prefix` (optional): The IP address on the host to bind to. Or an IP prefix in
[CIDR](https://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing) notation, which binds to every host IP address
that is contained in the prefix. If omitted, binds to all interfaces.
- `host_port`: The port number on the host to bind to.
- `container_port`: The port number within the container that's listening for traffic.
- `protocol` (optional): `tcp` or `udp` (default: `tcp`)
| Port value | Description |
|------------------------------|--------------------------------------------------------------------------------------|
| `8000/http` | Publish port 8000 as HTTP via Caddy using hostname `<service-name>.<cluster-domain>` |
| `app.example.com:8080/https` | Publish port 8080 as HTTPS via Caddy using hostname `app.example.com` |
| `127.0.0.1:5432:5432@host` | Bind TCP port 5432 to host port 5432 on loopback interface only |
| `53:5353/udp@host` | Bind UDP port 5353 to host port 53 on all network interfaces |
| Port value | Description |
|--------------------------------------|--------------------------------------------------------------------------------------|
| `8000/http` | Publish port 8000 as HTTP via Caddy using hostname `<service-name>.<cluster-domain>` |
| `app.example.com:8080/https` | Publish port 8080 as HTTPS via Caddy using hostname `app.example.com` |
| `127.0.0.1:5432:5432@host` | Bind TCP port 5432 to host port 5432 on loopback interface only |
| `53:5353/udp@host` | Bind UDP port 5353 to host port 53 on all network interfaces |
| `192.168.76.0/24:5432:5432/tcp@host` | Bind TCP port 5432 to host port 5432 on every host IP contained in 192.168.76.0/24 |
:::warning
@@ -138,7 +141,7 @@ custom global configuration.
The following functions and variables are available:
| Template | Description |
|---------------------------------------|-----------------------------------------------------------------------------------------------|
| ------------------------------------- | --------------------------------------------------------------------------------------------- |
| `{{upstreams [service-name] [port]}}` | A space-separated list of healthy container IPs for the current or specified service and port |
| `{{.Name}}` | The name of the service the config belongs to |
| `{{.Upstreams}}` | A map of all service names to their healthy container IPs |
@@ -149,38 +152,49 @@ changes.
**Examples:**
1. Current service upstreams, default port:
```caddyfile
reverse_proxy {{upstreams}}
```
```caddyfile
reverse_proxy 10.210.1.3 10.210.2.5
```
2. Current service upstreams, port 8000:
```caddyfile
reverse_proxy {{upstreams 8000}}
```
```caddyfile
reverse_proxy 10.210.1.3:8000 10.210.2.5:8000
```
3. Current service upstreams with `https` scheme:
```caddyfile
reverse_proxy {{- range $ip := index .Upstreams .Name}} https://{{$ip}}{{end}}
```
```caddyfile
reverse_proxy https://10.210.1.3 https://10.210.2.5
```
4. `api` service upstreams, port 9000:
```caddyfile
handle_path /api/* {
reverse_proxy {{upstreams "api" 9000}}
}
```
```caddyfile
+6 -4
View File
@@ -23,13 +23,15 @@ uc run IMAGE [COMMAND...] [flags]
-n, --name string Assign a name to the service. A random name is generated if not specified.
--privileged Give extended privileges to service containers. This is a security risk and should be used with caution.
-p, --publish strings Publish a service port to make it accessible outside the cluster. Can be specified multiple times.
Format: [hostname:]container_port[/protocol] or [host_ip:]host_port:container_port[/protocol]@host
Format: [hostname:]container_port[/protocol] or [host_ip|host_prefix:]host_port:container_port[/protocol]@host
Supported protocols: tcp, udp, http, https (default is tcp). If a hostname for http(s) port is not specified
and a cluster domain is reserved, service-name.cluster-domain will be used as the hostname.
Examples:
-p 8080/https Publish port 8080 as HTTPS via reverse proxy with default service-name.cluster-domain hostname
-p app.example.com:8080/https Publish port 8080 as HTTPS via reverse proxy with custom hostname
-p 53:5353/udp@host Bind UDP port 5353 to host port 53
-p 8080/https Publish port 8080 as HTTPS via reverse proxy with default service-name.cluster-domain hostname
-p app.example.com:8080/https Publish port 8080 as HTTPS via reverse proxy with custom hostname
-p 53:5353/udp@host Bind UDP port 5353 to host port 53
-p 192.168.76.0/24:53:5353/udp@host Bind UDP port 5353 to host port 53 on every host IP address
contained in the prefix 192.168.76.0/24
--pull string Pull image from the registry before running service containers ('always', 'missing', 'never'). (default "missing")
--replicas uint Number of containers to run for the service. Only valid for a replicated service. (default 1)
--shm-size bytes Maximum amount of shared memory (mounted at /dev/shm) a service container can use. Value is a positive integer
@@ -23,13 +23,15 @@ uc service run IMAGE [COMMAND...] [flags]
-n, --name string Assign a name to the service. A random name is generated if not specified.
--privileged Give extended privileges to service containers. This is a security risk and should be used with caution.
-p, --publish strings Publish a service port to make it accessible outside the cluster. Can be specified multiple times.
Format: [hostname:]container_port[/protocol] or [host_ip:]host_port:container_port[/protocol]@host
Format: [hostname:]container_port[/protocol] or [host_ip|host_prefix:]host_port:container_port[/protocol]@host
Supported protocols: tcp, udp, http, https (default is tcp). If a hostname for http(s) port is not specified
and a cluster domain is reserved, service-name.cluster-domain will be used as the hostname.
Examples:
-p 8080/https Publish port 8080 as HTTPS via reverse proxy with default service-name.cluster-domain hostname
-p app.example.com:8080/https Publish port 8080 as HTTPS via reverse proxy with custom hostname
-p 53:5353/udp@host Bind UDP port 5353 to host port 53
-p 8080/https Publish port 8080 as HTTPS via reverse proxy with default service-name.cluster-domain hostname
-p app.example.com:8080/https Publish port 8080 as HTTPS via reverse proxy with custom hostname
-p 53:5353/udp@host Bind UDP port 5353 to host port 53
-p 192.168.76.0/24:53:5353/udp@host Bind UDP port 5353 to host port 53 on every host IP address
contained in the prefix 192.168.76.0/24
--pull string Pull image from the registry before running service containers ('always', 'missing', 'never'). (default "missing")
--replicas uint Number of containers to run for the service. Only valid for a replicated service. (default 1)
--shm-size bytes Maximum amount of shared memory (mounted at /dev/shm) a service container can use. Value is a positive integer