跳至主要內容

Kubernetes 部署 ​

部署前準備 ​

  • 已有可用的 StorageClass,或預先準備了可與 PVC 配對的 PV。
  • 記錄 PostgreSQL 和 Redis 的連線位址與憑證。以下使用 PostgreSQL 17 和 Redis 7,資料庫帳號需要具備結構遷移權限。

建立命名空間和安裝權杖 ​

透過 Secret 注入安裝權杖,讓 Pod 在安裝期間重新啟動後仍使用相同權杖:

sh
kubectl create namespace sayrud
umask 077
openssl rand -hex 24 > sayrud-setup-token
kubectl -n sayrud create secret generic sayrud-setup --from-file=token=sayrud-setup-token

保留本機的 sayrud-setup-token 檔案,稍後在安裝頁填寫。設定 SAYRUD_SETUP_TOKEN 後,程式不會將權杖值輸出到日誌。

建立應用程式資源 ​

將以下內容儲存為 sayrud.yaml。此設定使用預設 StorageClass;若叢集沒有預設類別,請在兩個 PVC 的 spec 中設定實際的 storageClassName。

yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: sayrud-config
  namespace: sayrud
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: sayrud-data
  namespace: sayrud
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: sayrud
  namespace: sayrud
spec:
  replicas: 1
  strategy:
    type: Recreate
  selector:
    matchLabels:
      app: sayrud
  template:
    metadata:
      labels:
        app: sayrud
    spec:
      terminationGracePeriodSeconds: 45
      securityContext:
        runAsNonRoot: true
        runAsUser: 10001
        runAsGroup: 10001
        fsGroup: 10001
        fsGroupChangePolicy: OnRootMismatch
      containers:
        - name: sayrud
          image: ghcr.io/sayrud/sayrud:latest
          imagePullPolicy: IfNotPresent
          args: ["web", "--port", "2830", "--config", "/home/app/config/sayrud.yaml"]
          env:
            - name: SAYRUD_SETUP_TOKEN
              valueFrom:
                secretKeyRef:
                  name: sayrud-setup
                  key: token
          ports:
            - name: http
              containerPort: 2830
          volumeMounts:
            - name: config
              mountPath: /home/app/config
            - name: data
              mountPath: /home/app/data
          startupProbe:
            httpGet:
              path: /-/healthz
              port: http
            periodSeconds: 5
            timeoutSeconds: 3
            failureThreshold: 60
          readinessProbe:
            httpGet:
              path: /-/readyz
              port: http
            periodSeconds: 5
            timeoutSeconds: 5
          livenessProbe:
            httpGet:
              path: /-/healthz
              port: http
            periodSeconds: 10
            timeoutSeconds: 3
          resources:
            requests:
              cpu: 100m
              memory: 256Mi
            limits:
              cpu: "1"
              memory: 1Gi
      volumes:
        - name: config
          persistentVolumeClaim:
            claimName: sayrud-config
        - name: data
          persistentVolumeClaim:
            claimName: sayrud-data
---
apiVersion: v1
kind: Service
metadata:
  name: sayrud
  namespace: sayrud
spec:
  selector:
    app: sayrud
  ports:
    - name: http
      port: 2830
      targetPort: http
  type: ClusterIP

設定目錄掛載的是可寫入的 PVC,安裝精靈可以在其中建立檔案。

部署並查看狀態:

sh
kubectl apply -f sayrud.yaml
kubectl -n sayrud rollout status deployment/sayrud --timeout=300s
kubectl -n sayrud get pods,pvc
kubectl -n sayrud logs deployment/sayrud --tail=100

完成 Web 安裝 ​

在本機轉送連接埠,保持命令執行:

sh
kubectl -n sayrud port-forward service/sayrud 2830:2830

開啟 http://localhost:2830/setup,填入本機 sayrud-setup-token 檔案中的權杖,依照 Web 安裝流程完成連線設定與管理員建立。

安裝完成後,設定保存在 sayrud-config PVC,附件位於 sayrud-data PVC。

若要透過網域存取,請為 sayrud:2830 設定叢集現有的 Ingress 或閘道,並啟用 HTTPS 和 WebSocket 支援。

升級 ​

先備份資料庫、設定和上傳檔案,再修改清單中的映像檔版本並執行:

sh
kubectl apply -f sayrud.yaml
kubectl -n sayrud rollout status deployment/sayrud --timeout=300s
kubectl -n sayrud logs deployment/sayrud --tail=100

多副本部署 ​

完成一次安裝後,再準備擴充所需的共用資源:

  • 所有副本連線至同一個 PostgreSQL 資料庫和 Redis 執行個體。
  • 分發相同設定,尤其要保持 auth.secret_key 一致。已產生的設定可以改由 Kubernetes Secret 掛載,正常執行時可設為唯讀;變更設定後需要重新啟動 Pod。
  • 附件統一存放在 S3、ReadWriteMany 卷等共用儲存空間中,既有本機檔案需先遷移。