选项

管理 Uncloud 集群——使用 `uc` CLI 部署服务、配置 Caddy Ingress、为非集群设备添加静态代理路由、发布端口、进行扩展、查看日志,以及管理机器和卷。

...展开全部
0
更新时间 2026-09-30

Uncloud 集群管理

CLI 参考指南 uc CLI 的参考指南——这是一个基于 Docker 容器、WireGuard 网格网络和 Caddy 反向代理的去中心化自托管平台。

何时启用

在处理Uncloud集群时使用此技能,特别是在以下情况:

  • 使用 uc machine
  • 通过Compose文件部署服务时 uc deploy
  • 通过Uncloud发布HTTP、HTTPS、TCP或UDP端口
  • 使用 x-caddy, x-ports,或 --caddyfile
  • 通过集群代理路由外部局域网设备
  • 检查日志、服务状态、卷、DNS 或机器部署位置

工作原理

Uncloud 在通过 WireGuard 网格连接的对等机器上运行 Docker 服务。每台机器都是平等的集群成员;服务在覆盖网络上进行通信,而 Caddy 在全局范围内运行以终止公共 HTTP/HTTPS 流量。Compose 文件可以使用 Uncloud 扩展来配置 ingress、部署和生成的 Caddy 配置,而 uc CLI 负责镜像分发、调度、扩展、日志和集群状态管理。

示例

uc machine init user@host --name machine-1
uc service run --name web -p app.example.com:8080/https nginx:latest
uc deploy

核心概念

  • 无中央控制平面——所有机器都是通过 WireGuard 连接的平等对等节点
  • Caddy 作为全局服务在每台机器上运行;从 Let's Encrypt 自动获取 TLS 证书
  • 覆盖网络——服务默认通过 10.210.0.0/16 ;DNS 由网状网络内部提供
  • Caddyfile 由系统自动生成——切勿直接编辑;请使用 x-caddy / --caddyfile 代替

CLI 快速参考

机器

命令 用途
uc machine init user@host 初始化第一台机器/新建集群
uc machine add user@host 将机器加入现有集群
uc machine ls 列出机器
uc machine update NAME --public-ip IP 更新入站公共 IP 地址
uc machine rm NAME 移除机器

键 init 标志: --name, --network 10.210.0.0/16, --no-caddy, --no-dns, --public-ip auto\|IP\|none

服务

命令 用途
uc service ls / uc ls 列出服务
uc service run IMAGE 运行单个容器服务
uc deploy 从...部署 compose.yaml
uc deploy --no-build 部署已推送的镜像(无需重新构建)
uc deploy --recreate 强制重新创建服务
uc scale SERVICE N 设置副本数量
uc service logs SERVICE 查看日志
uc service exec SERVICE 进入容器 shell
uc service inspect SERVICE 详细信息
uc service rm SERVICE 移除服务(保留命名卷)
uc ps 集群中的所有容器

镜像

uc image push myapp:latest                    # Push local image to all machines
uc image push myapp:latest -m machine1,machine2  # Push to specific machines
uc images                                     # List images in cluster

卷

uc volume ls                  # All volumes
uc volume ls -m machine1      # On specific machine
uc volume create NAME -m MACHINE
uc volume rm NAME

Caddy

uc caddy config    # Show current generated Caddyfile (read-only)
uc caddy deploy    # Deploy/upgrade Caddy across cluster

DNS 与上下文

uc dns show        # Show reserved *.uncld.dev domain
uc dns reserve     # Reserve a new domain
uc ctx ls          # List cluster contexts
uc ctx use prod    # Switch context

端口转发

HTTP/HTTPS(通过 Caddy 反向代理)

-p [hostname:]container_port[/protocol]
示例 含义
-p 8080/https 带自动配置的 HTTPS service-name.cluster-domain 主机名
-p app.example.com:8080/https 使用自定义主机名的 HTTPS
-p 8080/http 仅支持 HTTP,不支持 TLS

TCP/UDP(与主机绑定,绕过 Caddy)

-p [host_ip:]host_port:container_port[/protocol]@host
示例 含义
-p 5432:5432@host 所有接口上的 TCP 5432
-p 127.0.0.1:5432:5432@host 仅限 TCP 5432 回环
-p 53:5353/udp@host UDP

Compose 文件扩展名

Uncloud 在 Docker Compose 的基础上添加以下扩展:

x-ports — 使用域名发布端口

services:
  app:
    image: app:latest
    x-ports:
      - example.com:8000/https
      - www.example.com:8000/https
      - api.example.com:9000/https

x-caddy — 为服务提供自定义的 Caddy 配置

services:
  app:
    image: app:latest
    x-caddy: |
      example.com {
        redir https://www.example.com{uri} permanent
      }
      www.example.com {
        reverse_proxy {{upstreams 8000}} {
          import common_proxy
        }
        basic_auth /admin/* {
          admin $2a$14$...
        }
      }

模板内可用的函数 x-caddy:

  • {{upstreams [service] [port]}} — 运行正常的容器 IP 地址
  • {{.Name}} — 服务名称
  • {{.Upstreams}} — 所有服务与 IP 的映射表

x-machines — 部署约束

services:
  db:
    image: postgres:18
    x-machines: db-machine          # Single machine name
  app:
    image: app:latest
    x-machines:
      - machine-1
      - machine-2

完整的多服务示例

services:
  api:
    build: ./api
    x-ports:
      - api.example.com:3000/https
    environment:
      DATABASE_URL: postgres://db:5432/mydb

  web:
    build: ./web
    x-ports:
      - example.com:8000/https
      - www.example.com:8000/https
    environment:
      API_URL: http://api:3000

  db:
    image: postgres:18
    environment:
      POSTGRES_PASSWORD: ${DB_PASSWORD}
    volumes:
      - db-data:/var/lib/postgresql/data
    x-machines: db-machine

volumes:
  db-data:

路由到外部(非集群)设备

若要通过 Caddy 公开外部设备(例如 BMC、NAS、路由器 UI),且无需运行真正的容器:

1. 创建一个 Caddyfile 代码片段(例如 ~/device.caddyfile):

https://device.example.com {
    reverse_proxy https://192.168.1.x {
        transport http {
            tls_insecure_skip_verify   # needed for self-signed BMC certs
        }
    }
    log
}

对于纯文本上游: reverse_proxy http://192.168.1.x:port

2. 将其注册为带空操作容器的命名服务:

uc service run \
  --name device-bmc \
  --caddyfile ~/device.caddyfile \
  registry.k8s.io/pause:3.9

pause 是一个最简的无操作容器——它不执行任何操作,但为 Uncloud 提供了一个服务条目,以便将 Caddyfile 挂载到该服务上。

3. 验证:

uc caddy config   # device.example.com block should appear

--caddyfile 不能与非@host 已发布端口结合使用。

DNS 提示:通配符记录 (*.yourdomain.com → cluster-public-ip) 意味着任何新子域都能立即生效——无需为每个服务单独修改 DNS。

服务 DNS(内部)

集群内的服务通过名称相互解析:

DNS名称 解析为
service-name 任何正常运行的容器
service-name.internal 相同
rr.service-name.internal 轮询
nearest.service-name.internal 优先使用本地机器

扩展与全局服务

uc scale web 5    # 5 replicas (spread across machines)
uc scale web 1    # Scale down
services:
  caddy:
    deploy:
      mode: global   # One container on every machine

镜像标签模板(在 compose.yaml 中)

image: myapp:{{gitdate "20060102"}}.{{gitsha 7}}
image: myapp:{{gitsha 7}}.${GITHUB_RUN_ID:-local}
函数 输出
{{gitsha N}} 提交 SHA 的前 N 个字符
{{gitdate "format"}} Git 提交日期(Go 格式)
{{date "format"}} 当前日期

常见工作流

从源代码部署:

uc deploy                          # Build + push + deploy
uc build --push && uc deploy --no-build   # Separate steps

检查服务:

uc inspect web
uc logs -f web
uc logs --since 1h web
uc exec web                        # Opens shell
uc exec web /bin/sh -c "env"       # Run specific command

零停机部署会自动进行;Uncloud 会在终止旧容器前等待健康检查结果。

强制重建:

uc deploy --recreate

常见错误

错误 解决方法
直接编辑 Caddyfile 请使用 x-caddy 在“撰写”中,或 --caddyfile 在 uc service run
使用自签名证书代理 HTTPS 上游服务器 添加 transport http { tls_insecure_skip_verify }
uc caddy config 显示没有用户定义的代码块 无法连接到 Caddy 管理套接字 — 请检查 uc inspect caddy 以及 uc logs caddy
服务无法从容器内访问外部局域网 IP 请确认 Caddy 容器的主机能否路由到目标网络
以下操作后卷丢失 uc service rm 命名卷会保留;只有匿名卷会被自动移除
在 GitHub 上查看
---
name: uncloud
description: Manage an Uncloud cluster — deploy services, configure Caddy ingress, add static proxy routes for non-cluster devices, publish ports, scale, inspect logs, and manage machines and volumes with the `uc` CLI.
---

# Uncloud Cluster Management

Reference for the `uc` CLI — a decentralised self-hosting platform using Docker containers, WireGuard mesh networking, and Caddy reverse proxy.

## When to Activate

Use this skill when working with Uncloud clusters, especially when:
- Bootstrapping or joining machines with `uc machine`
- Deploying services from Compose files with `uc deploy`
- Publishing HTTP, HTTPS, TCP, or UDP ports through Uncloud
- Configuring Caddy ingress with `x-caddy`, `x-ports`, or `--caddyfile`
- Routing external LAN devices through the cluster proxy
- Inspecting logs, service state, volumes, DNS, or machine placement

## How It Works

Uncloud runs Docker services across peer machines connected by a WireGuard mesh. Each machine is an equal cluster member; services communicate on the overlay network and Caddy runs globally to terminate public HTTP/HTTPS traffic. Compose files can use Uncloud extensions for ingress, placement, and generated Caddy configuration, while the `uc` CLI handles image distribution, scheduling, scaling, logs, and cluster state.

## Examples

```bash
uc machine init user@host --name machine-1
uc service run --name web -p app.example.com:8080/https nginx:latest
uc deploy
```

## Core Concepts

- **No central control plane** — all machines are equal peers connected by WireGuard
- **Caddy** runs as a global service on every machine; auto-obtains TLS from Let's Encrypt
- **Overlay network** — services communicate via `10.210.0.0/16` by default; DNS provided inside the mesh
- **Caddyfile is autogenerated** — never edit it directly; use `x-caddy` / `--caddyfile` instead

---

## CLI Quick Reference

### Machines

| Command | Purpose |
|---------|---------|
| `uc machine init user@host` | Bootstrap first machine / new cluster |
| `uc machine add user@host` | Join machine to existing cluster |
| `uc machine ls` | List machines |
| `uc machine update NAME --public-ip IP` | Update public IP for ingress |
| `uc machine rm NAME` | Remove machine |

Key `init` flags: `--name`, `--network 10.210.0.0/16`, `--no-caddy`, `--no-dns`, `--public-ip auto\|IP\|none`

### Services

| Command | Purpose |
|---------|---------|
| `uc service ls` / `uc ls` | List services |
| `uc service run IMAGE` | Run a single container service |
| `uc deploy` | Deploy from `compose.yaml` |
| `uc deploy --no-build` | Deploy already-pushed images without rebuilding |
| `uc deploy --recreate` | Force service recreation |
| `uc scale SERVICE N` | Set replica count |
| `uc service logs SERVICE` | View logs |
| `uc service exec SERVICE` | Shell into container |
| `uc service inspect SERVICE` | Detailed info |
| `uc service rm SERVICE` | Remove service (keeps named volumes) |
| `uc ps` | All containers across cluster |

### Images

```bash
uc image push myapp:latest                    # Push local image to all machines
uc image push myapp:latest -m machine1,machine2  # Push to specific machines
uc images                                     # List images in cluster
```

### Volumes

```bash
uc volume ls                  # All volumes
uc volume ls -m machine1      # On specific machine
uc volume create NAME -m MACHINE
uc volume rm NAME
```

### Caddy

```bash
uc caddy config    # Show current generated Caddyfile (read-only)
uc caddy deploy    # Deploy/upgrade Caddy across cluster
```

### DNS & Context

```bash
uc dns show        # Show reserved *.uncld.dev domain
uc dns reserve     # Reserve a new domain
uc ctx ls          # List cluster contexts
uc ctx use prod    # Switch context
```

---

## Port Publishing

### HTTP/HTTPS (via Caddy reverse proxy)

```
-p [hostname:]container_port[/protocol]
```

| Example | Meaning |
|---------|---------|
| `-p 8080/https` | HTTPS with auto `service-name.cluster-domain` hostname |
| `-p app.example.com:8080/https` | HTTPS with custom hostname |
| `-p 8080/http` | HTTP only, no TLS |

### TCP/UDP (host-bound, bypasses Caddy)

```
-p [host_ip:]host_port:container_port[/protocol]@host
```

| Example | Meaning |
|---------|---------|
| `-p 5432:5432@host` | TCP 5432 on all interfaces |
| `-p 127.0.0.1:5432:5432@host` | TCP 5432 loopback only |
| `-p 53:5353/udp@host` | UDP |

---

## Compose File Extensions

Uncloud adds these extensions on top of Docker Compose:

### `x-ports` — publish ports with domains

```yaml
services:
  app:
    image: app:latest
    x-ports:
      - example.com:8000/https
      - www.example.com:8000/https
      - api.example.com:9000/https
```

### `x-caddy` — custom Caddy config for service

```yaml
services:
  app:
    image: app:latest
    x-caddy: |
      example.com {
        redir https://www.example.com{uri} permanent
      }
      www.example.com {
        reverse_proxy {{upstreams 8000}} {
          import common_proxy
        }
        basic_auth /admin/* {
          admin $2a$14$...
        }
      }
```

Template functions available inside `x-caddy`:
- `{{upstreams [service] [port]}}` — healthy container IPs
- `{{.Name}}` — service name
- `{{.Upstreams}}` — map of all services → IPs

### `x-machines` — placement constraints

```yaml
services:
  db:
    image: postgres:18
    x-machines: db-machine          # Single machine name
  app:
    image: app:latest
    x-machines:
      - machine-1
      - machine-2
```

### Full multi-service example

```yaml
services:
  api:
    build: ./api
    x-ports:
      - api.example.com:3000/https
    environment:
      DATABASE_URL: postgres://db:5432/mydb

  web:
    build: ./web
    x-ports:
      - example.com:8000/https
      - www.example.com:8000/https
    environment:
      API_URL: http://api:3000

  db:
    image: postgres:18
    environment:
      POSTGRES_PASSWORD: ${DB_PASSWORD}
    volumes:
      - db-data:/var/lib/postgresql/data
    x-machines: db-machine

volumes:
  db-data:
```

---

## Routing to External (Non-Cluster) Devices

To expose an external device (e.g. BMC, NAS, router UI) via Caddy without running a real container:

**1. Create a Caddyfile snippet** (e.g. `~/device.caddyfile`):

```caddyfile
https://device.example.com {
    reverse_proxy https://192.168.1.x {
        transport http {
            tls_insecure_skip_verify   # needed for self-signed BMC certs
        }
    }
    log
}
```

For plaintext upstream: `reverse_proxy http://192.168.1.x:port`

**2. Register as a named service with no-op container:**

```bash
uc service run \
  --name device-bmc \
  --caddyfile ~/device.caddyfile \
  registry.k8s.io/pause:3.9
```

`pause` is a minimal no-op container — it does nothing, but gives Uncloud a service entry to attach the Caddyfile to.

**3. Verify:**

```bash
uc caddy config   # device.example.com block should appear
```

> `--caddyfile` cannot be combined with non-`@host` published ports.

**DNS tip:** A wildcard record (`*.yourdomain.com → cluster-public-ip`) means any new subdomain works immediately — no DNS change needed per service.

---

## Service DNS (Internal)

Services inside the cluster resolve each other by name:

| DNS name | Resolves to |
|----------|------------|
| `service-name` | Any healthy container |
| `service-name.internal` | Same |
| `rr.service-name.internal` | Round-robin |
| `nearest.service-name.internal` | Machine-local first |

---

## Scaling & Global Services

```bash
uc scale web 5    # 5 replicas (spread across machines)
uc scale web 1    # Scale down
```

```yaml
services:
  caddy:
    deploy:
      mode: global   # One container on every machine
```

---

## Image Tag Templates (in compose.yaml)

```yaml
image: myapp:{{gitdate "20060102"}}.{{gitsha 7}}
image: myapp:{{gitsha 7}}.${GITHUB_RUN_ID:-local}
```

| Function | Output |
|----------|--------|
| `{{gitsha N}}` | First N chars of commit SHA |
| `{{gitdate "format"}}` | Git commit date in Go format |
| `{{date "format"}}` | Current date |

---

## Common Workflows

**Deploy from source:**
```bash
uc deploy                          # Build + push + deploy
uc build --push && uc deploy --no-build   # Separate steps
```

**Inspect a service:**
```bash
uc inspect web
uc logs -f web
uc logs --since 1h web
uc exec web                        # Opens shell
uc exec web /bin/sh -c "env"       # Run specific command
```

**Zero-downtime deploys** happen automatically; Uncloud waits for health checks before terminating old containers.

**Force recreate:**
```bash
uc deploy --recreate
```

---

## Common Mistakes

| Mistake | Fix |
|---------|-----|
| Editing the Caddyfile directly | Use `x-caddy` in compose or `--caddyfile` on `uc service run` |
| Proxying an HTTPS upstream with self-signed cert | Add `transport http { tls_insecure_skip_verify }` |
| `uc caddy config` shows no user-defined blocks | Caddy admin socket unreachable — check `uc inspect caddy` and `uc logs caddy` |
| Service can't reach external LAN IP from container | Verify Caddy container's host can route to target network |
| Volumes lost after `uc service rm` | Named volumes persist; only anonymous volumes are auto-removed |

所有文件

1 个文件

安装 uncloud

下载技能文件并将其解压到 .claude/skills/ 目录中。

下载ZIP

克隆仓库并复制技能文件到您的项目中。

git clone https://github.com/affaan-m/ECC/tree/main/skills/uncloud # Copy SKILL.md to your .claude/skills/ directory

复制 复制
快速设置: 将技能文件夹复制到 .claude/skills/ Claude 将自动检测并使用该技能
仓库 affaan-m/ECC

相关技能

klingai-upgrade-migration
更新时间 2026-07-03
Verification & Quality Assurance
更新时间 2026-06-29
base44-cli
更新时间 2026-06-29
Railway CLI Management
更新时间 2026-07-02
OR