使用外部mongodb 及 k8s 部署 yapi
内网部署GitHub - YMFE/yapi: YApi 是一个可本地部署的、打通前后端及QA的、可视化的接口管理平台安装mongodb(这里用的是云mongodb,过程略),创建yapi库,用户yapi创建dockerfile,手动编译yapi镜像创建pvc,用于存放yapi数据文件创建deployment、svc、ingress、configmap部署及初始化初始化完成正常使用。
·
目录
1. 前言
参考文档&q链接:
yapi部署需要mongodb(2.6+)和nodejs(7.6+)的环境,初次安装需要进行初始数据库配置,因此运行在K8S环境上的时候,相对比较烦琐,具体流程如下:
- 安装mongodb(这里用的是云mongodb,过程略),创建yapi库,用户yapi
- 创建dockerfile,手动编译yapi镜像
- 创建pvc,用于存放yapi数据文件
- 创建deployment、svc、ingress、configmap
- 部署及初始化
- 初始化完成正常使用
2. 创建Dockerfile
这里部署的是1.9.2版本
Dockerfile
FROM node:12-alpine
ENV TZ="Asia/Shanghai"
RUN mkdir -p /yapi/vendors
WORKDIR /yapi/vendors
RUN apk add --no-cache wget python3 make
ENV VERSION=1.9.2
RUN wget https://github.com/YMFE/yapi/archive/v${VERSION}.zip
RUN unzip v${VERSION}.zip && mv yapi-${VERSION} /tmp
RUN npm install -g fs-extra
COPY entrypoint.sh /usr/bin/entrypoint.sh
RUN chmod +x /usr/bin/entrypoint.sh
EXPOSE 3000
ENTRYPOINT ["/usr/bin/entrypoint.sh"]
entrypoint.sh脚本内容如下
#!/bin/sh
# yapi初始化后会有一个init.lock文件
lockPath="/yapi/vendors/init.lock"
# 如果初始化文件文件存在,则直接运行,否则初始化
cd /yapi/vendors || exit
if [ ! -f "$lockPath" ]; then
cp -rf /tmp/yapi-1.9.2/* /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
3. 手动构建镜像
# 把dockerfile和entrypoint.sh文件放在同一个目录下
# 镜像名根据实际 harbor 地址进行替换
docker build -t harbor.test.cn/yapi:latest .
docker push harbor.test.cn/yapi:latest
4. k8s部署
4.1 创建pv和pvc
这里用于持久化yapi npm构建后生成的代码文件
这里用的是阿里云的ACK
pv
apiVersion: v1
kind: PersistentVolume
metadata:
annotations:
pv.kubernetes.io/bound-by-controller: 'yes'
labels:
alicloud-pvname: yapi-pv
csi.alibabacloud.com/nas-id: abddd48f28
name: yapi-pv
spec:
accessModes:
- ReadWriteMany
capacity:
storage: 50Gi
claimRef:
apiVersion: v1
kind: PersistentVolumeClaim
name: yapi-pvc
namespace: tools
csi:
driver: nasplugin.csi.alibabacloud.com
volumeAttributes:
path: /yapi
server: abddd48f28-suw54.cn-shenzhen.nas.aliyuncs.com
vers: '3'
volumeHandle: yapi-pv
persistentVolumeReclaimPolicy: Retain
storageClassName: nas
volumeMode: Filesystem
pvc
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
annotations:
pv.kubernetes.io/bind-completed: 'yes'
pv.kubernetes.io/bound-by-controller: 'yes'
name: yapi-pvc
namespace: tools
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 50Gi
selector:
matchLabels:
alicloud-pvname: yapi-pv
storageClassName: nas
volumeMode: Filesystem
volumeName: yapi-pv
结果如下
4.2 创建configmap
将yapi config.json配置文件放到configmap进行管理
创建config.json文件
{
"port": "3000", #服务端口
"adminAccount": "admin@admin.cn", #配置管理员帐号
"timeout":120000,
"db": {
"servername": "mongodb", # mongodb地址
"DATABASE": "yapi", # 指定库名
"port": 27017, # mongodb 端口
"user": "yapi", # mongodb用户名
"pass": "yapi@2022", # mongodb 密码
"authSource": ""
}
}
创建configmap
kubectl create configmap yapi-config --from-file=config.json
4.3 部署文件
deployment.yaml,包含deployment、service和ingress配置
apiVersion: v1
kind: Service
metadata:
name: yapi
namespace: tools
spec:
selector:
app: yapi
ports:
- name: http
protocol: TCP
port: 3000
targetPort: 3000
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: yapi
namespace: tools
labels:
app: yapi
spec:
replicas: 1
revisionHistoryLimit: 10
selector:
matchLabels:
app: yapi
template:
metadata:
labels:
app: yapi
spec:
restartPolicy: "Always"
containers:
- name: yapi
image: harbor.test.cn/yapi:latest
imagePullPolicy: IfNotPresent
ports:
- containerPort: 3000
name: http
protocol: TCP
volumeMounts:
- name: config
mountPath: /yapi/config.json
subPath: config.json
- name: datadir
mountPath: /yapi/vendors
resources:
requests:
cpu: 100m
memory: 400Mi
limits:
cpu: 800m
memory: 1024Mi
volumes:
- name: config
configMap:
name: yapi-config
- name: datadir
persistentVolumeClaim:
claimName: yapi-pvc
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: yapi
namespace: tools
spec:
ingressClassName: nginx
rules:
- host: yapi.test.cn
http:
paths:
- backend:
service:
name: yapi
port:
number: 3000
path: /
pathType: Prefix
部署
kubectl apply -f deployment.yaml
yapi部署启动成功的日志如下
用户名是config.json中配置的,密码是ymfe.org
更多推荐
已为社区贡献11条内容
所有评论(0)