跳到內容
mofcloud

在 Kubernetes 上部署 MOF

最後更新於:

在一個 Pod 中執行 MOF、Dash 和 MCP。

Linux amd64 · kubectl · 外部 PostgreSQL · 1 Pod · 3 容器

可信任內網 · 需 CREATEDB 權限 · 暫不支援強制 TLS 的 PostgreSQL。

映像檔來源

下載 mof.yaml

阿里雲 ACR · MOF v2.0.15 · Dash v2.0.17 · MCP v2.0.3

下載 mof.yaml ↓
檢視檔案
mof.yaml
# MOF v2.0.15 + Dash v2.0.17 + MCP v2.0.3
# Edit database address, user and password before applying. Keep this file private.
# Keep the Service names mof, mof-mcp and dash, all in one namespace: the components find each
# other by them (mof:8080, mof-mcp:8001). Renamed Services can't connect.
# Private-network PostgreSQL without TLS. TLS-only databases are not supported by this release.
apiVersion: v1
kind: Namespace
metadata:
  name: mof
---
apiVersion: v1
kind: Secret
metadata:
  name: mof-database
  namespace: mof
type: Opaque
stringData:
  password: "REPLACE_WITH_YOUR_DATABASE_PASSWORD"
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: mof-config
  namespace: mof
data:
  RK_POSTGRES_0_ADDR: "db.example.com:5432"
  RK_POSTGRES_0_USER: "mof"
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mof
  namespace: mof
spec:
  replicas: 1
  # First-release template: stop the old Pod before starting the new version.
  strategy:
    type: Recreate
  selector:
    matchLabels:
      app: mof
  template:
    metadata:
      labels:
        app: mof
      # Prometheus discovery by annotation; see the monitoring guide for a PodMonitor
      annotations:
        prometheus.io/scrape: "true"
        prometheus.io/port: "8080"
        prometheus.io/path: /internal/v1/mof/metrics
    spec:
      enableServiceLinks: false
      nodeSelector:
        kubernetes.io/arch: amd64
      terminationGracePeriodSeconds: 120
      containers:
        - name: mof
          image: registry.cn-hangzhou.aliyuncs.com/mofcloud/mof:v2.0.15
          envFrom:
            - configMapRef:
                name: mof-config
          env:
            - name: RK_POSTGRES_0_PASS
              valueFrom:
                secretKeyRef:
                  name: mof-database
                  key: password
          ports:
            - name: api
              containerPort: 8080
          startupProbe:
            httpGet:
              path: /v1/mof/site-info
              port: api
            periodSeconds: 10
            failureThreshold: 60
          readinessProbe:
            httpGet:
              path: /v1/mof/site-info
              port: api
            periodSeconds: 10
            timeoutSeconds: 5
          resources:
            requests:
              cpu: "500m"
              memory: "1Gi"
          volumeMounts:
            - name: offer-cache
              mountPath: /usr/src/mof/offer-cache
      volumes:
        - name: offer-cache
          emptyDir: {}
---
apiVersion: v1
kind: Service
metadata:
  name: mof
  namespace: mof
spec:
  selector:
    app: mof
  ports:
    - name: api
      port: 8080
      targetPort: api
  type: ClusterIP
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mof-mcp
  namespace: mof
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mof-mcp
  template:
    metadata:
      labels:
        app: mof-mcp
    spec:
      enableServiceLinks: false
      nodeSelector:
        kubernetes.io/arch: amd64
      containers:
        - name: mof-mcp
          image: registry.cn-hangzhou.aliyuncs.com/mofcloud/mof-mcp:v2.0.3
          ports:
            - name: mcp
              containerPort: 8001
          readinessProbe:
            exec:
              command: ["node", "/app/build/healthcheck.js"]
            periodSeconds: 10
            timeoutSeconds: 5
          resources:
            requests:
              cpu: "100m"
              memory: "128Mi"
---
apiVersion: v1
kind: Service
metadata:
  name: mof-mcp
  namespace: mof
spec:
  selector:
    app: mof-mcp
  ports:
    - name: mcp
      port: 8001
      targetPort: mcp
  type: ClusterIP
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: dash
  namespace: mof
spec:
  replicas: 1
  selector:
    matchLabels:
      app: dash
  template:
    metadata:
      labels:
        app: dash
    spec:
      enableServiceLinks: false
      nodeSelector:
        kubernetes.io/arch: amd64
      containers:
        - name: dash
          image: registry.cn-hangzhou.aliyuncs.com/mofcloud/dash:v2.0.17
          ports:
            - name: web
              containerPort: 80
          readinessProbe:
            httpGet:
              path: /
              port: web
            periodSeconds: 10
          resources:
            requests:
              cpu: "100m"
              memory: "64Mi"
---
apiVersion: v1
kind: Service
metadata:
  name: dash
  namespace: mof
spec:
  selector:
    app: dash
  ports:
    - name: web
      port: 80
      targetPort: web
  type: ClusterIP

不要修改 Service 名稱 mof、mof-mcp 和 dash,三者放在同一個命名空間。元件之間依這些名稱互相存取(mof:8080、mof-mcp:8001),因此不需其他設定;改了名稱就連不上。

資料庫連線

在 mof.yaml 裡填寫既有 PostgreSQL 的連線資訊:

RK_POSTGRES_0_ADDR
位址:連接埠
RK_POSTGRES_0_USER
使用者名稱
stringData.password
資料庫既有密碼

密碼保留引號,檔案不要公開。

部署

Terminal
kubectl apply -f mof.yaml
kubectl -n mof rollout status deployment/mof --timeout=10m
kubectl -n mof rollout status deployment/mof-mcp deployment/dash

三個都就緒後,查看 Admin 密碼:

Terminal
kubectl -n mof logs deployment/mof | grep "Admin password"

開啟 MOF

Terminal
kubectl -n mof port-forward service/dash 8000:80

保持這個終端機執行。

Adminhttp://127.0.0.1:8000/admin

設定授權並建立使用者。

MOF Webhttp://127.0.0.1:8000

使用 MOF 帳號登入。

進階設定

PostgreSQL TLS 相容性

MOF 的 TLS 設定僅涵蓋 Mof 和 AsyncJobMof,未涵蓋雲端廠商資料庫。此範本僅適用於可信任內網,無法連線強制 TLS 的 PostgreSQL。

網域、HTTPS 與選用 OAuth

使用既有的 Ingress 控制器並將網域指向入口,填寫 class、網域和 mof 命名空間中既有的 TLS Secret。

下載 ingress.yaml ↓
ingress.yaml
# Requires an installed Ingress controller and an existing TLS Secret.
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: mof
  namespace: mof
spec:
  ingressClassName: nginx
  tls:
    - hosts: [mof.example.com]
      secretName: mof-web-tls
  rules:
    - host: mof.example.com
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: dash
                port:
                  number: 80
Terminal
kubectl apply -f ingress.yaml
kubectl -n mof get ingress

在 Admin → 設定 → MCP 中啟用存取,路徑為 /v1/mof/remote-mcp。權杖不需要 OAuth;瀏覽器 OAuth 需要 HTTPS、MOF_PUBLIC_URL 和 MOF_MCP_OAUTH_SECRET。

健康檢查與疑難排解
Terminal
kubectl -n mof get pods
kubectl -n mof logs deployment/mof --tail=100
kubectl -n mof logs deployment/mof-mcp --tail=60
kubectl -n mof logs deployment/dash --tail=60
kubectl -n mof exec deployment/mof -- wget -q -O - http://mof-mcp:8001/health

預期:mof、mof-mcp、dash 都是 Running 且 Ready,MCP ready: true。啟動失敗時請檢查映像檔拉取、節點架構和資料庫連線。

範本參數說明
RK_POSTGRES_0_ADDR外部 PostgreSQL 位址:連接埠,需能從 Pod 存取。
RK_POSTGRES_0_USER既有的資料庫使用者,需能建立 MOF 及雲端廠商資料庫。
mof-database / password存放在 Secret 中的資料庫密碼,僅注入 MOF。
Services mof / mof-mcp / dash各一個 Deployment 和 Service,依 Service 名稱互相存取,不需設定。
replicas / strategy1 / Recreate。升級會短暫中斷,本範本不提供多副本高可用性。
resources.requests排程預留量,並非記憶體上限。請依帳單規模調整 MOF 資源並監控實際用量。
offer-cache / emptyDir可重建的公開價格快取,Pod 重建後重新載入;帳單資料存於 PostgreSQL。
ingressClassName / host / tls對應已安裝的控制器、存取網域和 TLS Secret;本機連接埠轉送驗證時不需要。
更新、資料與擴充

先備份 PostgreSQL,再修改映像檔版本並套用 mof.yaml。修改 ConfigMap 或 Secret 後需重新啟動;Recreate 升級會中斷服務。

Terminal
kubectl -n mof rollout restart deployment/mof
kubectl -n mof rollout status deployment/mof --timeout=10m

本範本僅支援單一副本;隨需 Kubernetes Job 尚未實作。應用程式資料保存在外部 PostgreSQL,價格快取可重建。