YApi 是高效、易用、功能强大的 api 管理平台,旨在为开发、产品、测试人员提供更优雅的接口管理服务…此处省略,具体介绍请自行查阅官方介绍

1.Dockerfile的编写

FROM nodejs:10.16.0 as builder
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories
RUN apk update && apk upgrade && apk add --no-cache git python make openssl tar gcc wget curl
RUN rm -rf /var/cache/apk/*
ADD v1.5.6.tar.gz /tmp
RUN mkdir /yapi
WORKDIR /yapi/vendors
COPY entrypoint.sh /usr/bin
ENTRYPOINT ["/usr/bin/entrypoint.sh"]

这里演示用的是v1.5.6的版本,大家可以下载自己想部署的版本并做相应修改即可

2.entrypoint.sh代码如下

#!/bin/sh
# yapi初始化后会有一个init.lock文件
lockPath="/yapi/vendors/init.lock"
# 如果初始化文件文件存在,则直接运行,否则初始化
cd /yapi/vendors
if [ ! -f "$lockPath" ]; then
    cp -rf /tmp/yapi-1.5.6/* /yapi/vendors
    npm install --production --registry https://registry.npm.taobao.org
    # 启动Yapi初始化
    npm run install-server
    cp -rf /yapi/init.lock /yapi/vendors/init.lock
    node server/app.js
else
    # 运行yapi管理系统
    node server/app.js
fi

大家可以看下注释,因为在初始化后会自动在/yapi目录下生成一个init.lock文件,我这里将这个文件往vendors目录下拷贝了一份,脚本中判断vendors目录下是否有该文件,如果没有,则认为是初次安装,则执行初始化安装命令,如果存在,则直接启动项目。
注意修改启动脚本中的版本号为你自己下载的版本

3.因为yapi项目在初始化的依赖配置文件,配置文件内容如下

{
  "port": "3000",
  "adminAccount": "admin@test.com",
  "db": {
    "connectString": "mongodb://mongodb-rs0-0.mongodb-rs0-svc.mongodb:27017,mongodb-rs0-1.mongodb-rs0-svc.mongodb:27017,mongodb-rs0-2.mongodb-rs0-svc.mongodb:27017/yapi?replicaSet=rs0&slaveOk=true",
    "user": "yapi",
    "pass": "123456"
  },
  "ldapLogin": {
    "enable": true,
    "server": "ldap://172.16.1.29",
    "baseDn": "CN=yapi,OU=ServiceAccount,OU=IT部,DC=test,DC=com",
    "bindPassword": "yapi123456",
    "searchDn": "OU=IT部,DC=test,DC=com",
    "searchStandard": "&(objectClass=user)(sAMAccountName=%s)",
    "usernameKey": "displayName",
    "emailKey": "mail"
  },
  "closeRegister":true
}

4.yapi在初始化时自行安装的node依赖需要持久保存,所以需要创建pv,并在容易中挂载
yapi-pvc.yaml如下

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: yapi-datadir
  namespace: omc
  annotations:
    volume.beta.kubernetes.io/storage-class: "nfs-storage"
spec:
  accessModes:
  - ReadWriteMany
  resources:
    requests:
      storage: 20Gi

kubectl create -f yapi-pvc.yaml
5. 部署文件如下
yapi.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: yapi
  namespace: omc
  labels:
    app: yapi
    release: yapi
spec:
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: yapi
      release: yapi
  template:
    metadata:
      labels:
        app: yapi
        release: yapi
    spec:
      containers:
      - name: yapi
        image: registry.cn-beijing.aliyuncs.com/niu-idc/yapi:v1.5.6
        imagePullPolicy: Always
        ports:
        - containerPort: 3000
          name: yapi
          protocol: TCP
        volumeMounts:
        - name: config
          mountPath: /yapi/config.json
          subPath: config.json
        - name: datadir
          mountPath: /yapi/vendors
        resources:
          requests:
            cpu: 500m
            memory: 512Mi
          limits:
            cpu: 1000m
            memory: 1024Mi
      volumes:
      - name: config
        configMap:
          name: yapi-config
      - name: datadir
        persistentVolumeClaim:
          claimName: yapi-datadir
      imagePullSecrets:
      - name: registry-aliyun

在该部署文件中,有两个volume,一个是配置文件,一个是持久存储
6.yapi-svc.yaml

apiVersion: v1
kind: Service
metadata:
  name: yapi-svc
  namespace: omc
spec:
  ports:
  - name: yapi
    port: 80
    targetPort: 3000
    protocol: TCP
  selector:
    app: yapi
    release: yapi

7.yapi-ingress.yaml

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: yapi-ingress
  namespace: omc
  annotations:
    kubernetes.io/ingress.class: "nginx"
spec:
  rules:
  - host: yapi-idc.niucache.com
    http:
      paths:
      - path: /
        backend:
          serviceName: yapi-svc
          servicePort: 80

执行如下命令完成部署:
1.打包镜像

docker build -t registry.cn-beijing.aliyuncs.com/niu-idc/yapi:v1.5.6 -t Dockerfile .

2.configmap yapi-config创建

kubectl create cm yapi-config -n omc --from-file=config.json

3.持久卷声明yapi-datadir创建

kubectl create -f yapi-pvc.yaml

4.yapi部署

kubectl create -f yapi.yaml

5.service创建

kubectl create -f yapi-svc.yaml

6.ingress创建

kubectl create -f yapi-ingress.yaml

稍等几分钟,当yapi初始化安装以后就可以访问到了。
在这里插入图片描述

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐