GX博客

分享个人 Full-Stack JavaScript 项目开发经验

在Kubernetes上部署YApi

YApi是一个有用户和项目管理功能的 mock 服务器平台。它支持与 swagger.json 自动同步,高级 mock 定义等功能,在前端开发中发挥着重要作用。但官方并没有提供镜像化部署的说明,本文将介绍如何在 kubernetes 下部署 YApi 服务。


YApi 以 mongodb 作为数据持久化存储的方式, 所以首先要部署 mongodb。

1、创建命名空间

apiVersion: v1
kind: Namespace
metadata:
    name: mock-server

添加镜像拉取 secret:

kubectl create secret docker-registry aliyun-docker-registry \
--docker-server=registry.cn-shenzhen.aliyuncs.com \
--docker-username=xxx \
--docker-password=xxx \
-n mock-server

2、为 mongodb 分配卷声明

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: mongo-nfs-pvc
  namespace: mock-server
spec:
  resources:
    requests:
      storage: 2Gi
  accessModes:
    - ReadWriteMany
  storageClassName: nfs-dynamic

上述使用的是名为 nfs-dynamic 的存储类。


3、部署 mongodb 及其服务

apiVersion: v1
kind: Service
metadata:
  name: mongo
  namespace: mock-server
spec:
  type: NodePort
  ports:
    - port: 27017
      targetPort: 27017
      nodePort: 27017
  selector:
    app: mongo

---

# https://docs.mongodb.com/manual/tutorial/backup-and-restore-tools/

# 备份
# mongodump --host=localhost --port=27017 --username=xxx --authenticationDatabase=admin --db=yapi --out=/data/backup/mongodump-2020-11-13

# 恢复
# mongorestore --host=localhost --port=27017 --username=xxx --authenticationDatabase=admin /data/backup/mongodump-2020-11-13

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mongo
  namespace: mock-server
spec:
  replicas: 1
  minReadySeconds: 30
  revisionHistoryLimit: 2
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
  progressDeadlineSeconds: 120
  selector:
    matchLabels:
      app: mongo
  template:
    metadata:
      name: mongo
      labels:
        app: mongo
    spec:
      imagePullSecrets:
        - name: aliyun-docker-registry
      volumes:
        - name: mongo-data
          persistentVolumeClaim:
            claimName: mongo-nfs-pvc
      containers:
        - image: registry.cn-shenzhen.aliyuncs.com/leeguangxing/mongo:4.0.20
          imagePullPolicy: IfNotPresent
          name: mongo
          ports:
            - containerPort: 27017
          env:
            - name: MONGO_INITDB_ROOT_USERNAME
              value: xxx
            - name: MONGO_INITDB_ROOT_PASSWORD
              value: xxx
          volumeMounts:
            - name: mongo-data
              mountPath: /data/db
              subPath: db
              readOnly: false
            - name: mongo-data
              mountPath: /data/configdb
              subPath: configdb
              readOnly: false
            - name: mongo-data
              mountPath: /data/backup
              subPath: backup
              readOnly: false
          # 设置 WiredTiger 缓存大小限制
          args: ["--wiredTigerCacheSizeGB", "0.5"]

要了解 mongodb 详细的备份和恢复,请查阅官方说明文档


4、构建 yapi 自定义镜像

yapi github 地址为https://github.com/YMFE/yapi。对项目稍作调整:

(1)在跟目录添加 Dockerfile 和 ecosystem.config.js。

Dockerfile

FROM node:12-slim
LABEL Description="This image is using PM2 as a layer between the container and node.js application."
COPY . /yapi
WORKDIR /yapi
RUN ["yarn", "global", "add", "pm2"]
RUN ["yarn", "install"]
EXPOSE 3100
CMD ["pm2-runtime", "start", "ecosystem.config.js"]

ecosystem.config.js

module.exports = {
  apps: [{
    name: 'web',
    script: 'server/app.js',
    instances: 2,
    autorestart: true,
    watch: false,
    max_memory_restart: '300M',
    exec_mode: 'cluster',
    listen_timeout: 3000, // wait ready timeout
    kill_timeout: 5000, // send SIGKILL timeout
  }]
};

(2)在根目录下创建 config 文件夹,并将根目录下的 config_example.json 复制到 config 文件夹下,命名为 config.json。

(3)修改 server/yapi.js 对 config.json 的引用路径。

(4)构建镜像。


5、部署 yapi 及其服务

添加配置 ConfigMap:

apiVersion: v1
kind: ConfigMap
metadata:
  name: yapi-config
  namespace: mock-server
data:
  config.json: |+
    {
      "port": "3000",
      "adminAccount": "admin@admin.com",
      "timeout":120000,
      "db": {
        "servername": "127.0.0.1",
        "DATABASE": "yapi",
        "port": 27017,
        "user": "test1",
        "pass": "test1",
        "authSource": ""
      },
      "mail": {
        "enable": true,
        "host": "smtp.163.com",
        "port": 465,
        "from": "***@163.com",
        "auth": {
          "user": "***@163.com",
          "pass": "*****"
        }
      }
    }

部署 yapi 及其服务:

apiVersion: v1
kind: Service
metadata:
  name: yapi
  namespace: mock-server
spec:
  type: NodePort
  ports:
    - port: 3100
      targetPort: 3100
      nodePort: 3100
  selector:
    app: yapi

---

apiVersion: apps/v1
kind: Deployment
metadata:
  name: yapi
  namespace: mock-server
spec:
  replicas: 1
  minReadySeconds: 30
  revisionHistoryLimit: 2
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
  progressDeadlineSeconds: 120
  selector:
    matchLabels:
      app: yapi
  template:
    metadata:
      name: yapi
      labels:
        app: yapi
    spec:
      imagePullSecrets:
        - name: aliyun-docker-registry
      volumes:
        - name: config
          configMap:
            name: yapi-config
      containers:
        - image: registry.cn-shenzhen.aliyuncs.com/leeguangxing/yapi:1.9.2
          imagePullPolicy: IfNotPresent
          name: yapi
          ports:
            - containerPort: 3100
          volumeMounts:
            - name: config
              mountPath: /yapi/config

6、进入 yapi 的 Pod 执行初始化数据库脚本

yarn install-server

必要时,可以重启 YApi:

pm2 restart ecosystem.config.js
文章关键字:YApimongodbkubernetes

版权声明:

本文为博主原创文章,若需转载,须注明出处,添加原文链接。

https://leeguangxing.cn/blog_post_92.html