跳转到正文

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 卷等共享存储中,已有本地文件需先迁移。