介绍
yapi是api 文档管理系统,基于nodejs和mongodb。集成ldap。
启用插件。本次启用的插件是把文档导出为word
编写entrypoint,sh
因为config.json这个配置,通过环境变量来配置比较方便,所以我们写一个entrypoint.sh文件,主要使用sed方法,用环境变量来替换json字段。具体如下,另外再加一个启动yapi的语句。
需要注意的是数字和布尔变量的sed语句和字符串sed语句有一点点不一样,需要多替换2个双引号。
#!/bin/sh
#update config file with env var
conf_file=/yapi/config.json
if [ $YAPI_SERVER_PORT ]; then
sed -i "s#\"YAPI_SERVER_PORT\"#$YAPI_SERVER_PORT#" $conf_file
fi
if [ $YAPI_ADMINACCOUNT ]; then
sed -i "s#YAPI_ADMINACCOUNT#$YAPI_ADMINACCOUNT#" $conf_file
fi
if [ $YAPI_TIMEOUT ]; then
sed -i "s#\"YAPI_TIMEOUT\"#$YAPI_TIMEOUT#" $conf_file
fi
#mongodb
if [ $YAPI_DB_SERVERNAME ]; then
sed -i "s#YAPI_DB_SERVERNAME#$YAPI_DB_SERVERNAME#" $conf_file
fi
if [ $YAPI_DB_DATABASE ]; then
sed -i "s#YAPI_DB_DATABASE#$YAPI_DB_DATABASE#" $conf_file
fi
if [ $YAPI_DB_PORT ]; then
sed -i "s#\"YAPI_DB_PORT\"#$YAPI_DB_PORT#" $conf_file
fi
if [ $YAPI_DB_USER ]; then
sed -i "s#YAPI_DB_USER#$YAPI_DB_USER#" $conf_file
fi
if [ $YAPI_DB_PASS ]; then
sed -i "s#YAPI_DB_PASS#$YAPI_DB_PASS#" $conf_file
fi
if [ $YAPI_DB_AUTHSOURCE ]; then
sed -i "s#YAPI_DB_AUTHSOURCE#$YAPI_DB_AUTHSOURCE#" $conf_file
fi
................
................
#start yapi
node server/app.js
编写yapi的dockerfile
基础镜像是node:11.15
最后打包镜像是node:11.15-alpine
把entrypoint.sh从本人github下载下来,加入到镜像中。
把config.json也从本人github下载下来,放入镜像中。
还需要下载input.docx
FROM node:11.15 as builder
WORKDIR /yapi
RUN npm install -g node-gyp yapi-cli ykit
RUN wget https://github.com/YMFE/yapi/archive/refs/tags/v1.9.2.tar.gz
RUN tar -zxvf v1.9.2.tar.gz
RUN mv yapi-1.9.2 vendors
RUN wget https://raw.githubusercontent.com/xie-shujian/yapi/main/ldap_docx/config.json
RUN yapi plugin --name yapi-plugin-export-docx-data
WORKDIR /yapi/vendors
RUN npm install --production
RUN wget https://raw.githubusercontent.com/xie-shujian/yapi/main/ldap_docx/entrypoint.sh
RUN wget https://raw.githubusercontent.com/inceptiongt/Yapi-plugin-export-docx-data/master/input.docx
FROM node:11.15-alpine
LABEL maintainer="[email protected]"
ENV TZ="Asia/Shanghai"
COPY --from=builder /yapi/vendors /yapi/vendors
COPY --from=builder /yapi/config.json /yapi/config.json
WORKDIR /yapi/vendors
RUN cat /yapi/config.json
EXPOSE 3000
ENTRYPOINT ["sh", "entrypoint.sh"]
这里使用了多重镜像,使用 copy --from 命令,第一个镜像作为builder镜像,把第一个镜像的builder结果,复制到第二个镜像里
制作成镜像
docker build -t xieshujian/yapi:1.9.2-ldap .
镜像大小大概是164m,还是很小的
k8s部署yaml文件
- 创建secret
- 创建部署
编写环境变量,包含mongodb的连接信息
编写探针 - 创建service
service端口是80,容器端口是3000
---
apiVersion: v1
kind: Secret
type: Opaque
metadata:
name: yapi-secret
stringData:
YAPI_DB_PASS: yapipassword
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: yapi
labels:
app: yapi
spec:
replicas: 1
selector:
matchLabels:
app: yapi
template:
metadata:
labels:
app: yapi
spec:
containers:
- name: yapi
image: xieshujian/yapi:1.9.2-ldap-docx
env:
- name: YAPI_SERVER_PORT
value: "3000"
- name: YAPI_ADMINACCOUNT
value: [email protected]
- name: YAPI_TIMEOUT
value: "120000"
- name: YAPI_DB_SERVERNAME
value: mongodb
- name: YAPI_DB_PORT
value: "27017"
- name: YAPI_DB_DATABASE
value: yapidb
- name: YAPI_DB_USER
value: yapiuser
- name: YAPI_DB_PASS
valueFrom:
secretKeyRef:
name: yapi-secret
key: YAPI_DB_PASS
- name: YAPI_DB_AUTHSOURCE
value: yapidb
- name: YAPI_MAIL_ENABLE
value: "false"
- name: YAPI_MAIL_PORT
value: "465"
- name: YAPI_LDAP_ENABLE
value: "true"
- name: YAPI_LDAP_EMAILPOSTFIX
value: "@163.com"
imagePullPolicy: IfNotPresent
ports:
- containerPort: 3000
livenessProbe:
httpGet:
path: /
port: 3000
initialDelaySeconds: 5
periodSeconds: 5
---
apiVersion: v1
kind: Service
metadata:
name: yapi
spec:
selector:
app: yapi
ports:
- protocol: TCP
port: 80
targetPort: 3000
config.json是修改过的
因为word插件会修改这个配置文件,所以这个json必须是合法的
{
"port": "YAPI_SERVER_PORT",
"adminAccount": "YAPI_ADMINACCOUNT",
"timeout": "YAPI_TIMEOUT",
"db": {
"servername": "YAPI_DB_SERVERNAME",
"DATABASE": "YAPI_DB_DATABASE",
"port": "YAPI_DB_PORT",
"user": "YAPI_DB_USER",
"pass": "YAPI_DB_PASS",
"authSource": "YAPI_DB_AUTHSOURCE"
},
"mail": {
"enable": "YAPI_MAIL_ENABLE",
"host": "YAPI_MAIL_HOST",
"port": "YAPI_MAIL_PORT",
"from": "YAPI_MAIL_FROM",
"auth": {
"user": "YAPI_MAIL_USER",
"pass": "YAPI_MAIL_PASS"
}
},
"ldapLogin": {
"enable": "YAPI_LDAP_ENABLE",
"server": "YAPI_LDAP_SERVER",
"baseDn": "YAPI_LDAP_BASEDN",
"bindPassword": "YAPI_LDAP_BINDPASSWORD",
"searchDn": "YAPI_LDAP_SEARCHDN",
"searchStandard": "YAPI_LDAP_SEARCHSTANDARD",
"emailPostfix": "YAPI_LDAP_EMAILPOSTFIX",
"emailKey": "YAPI_LDAP_EMAILKEY",
"usernameKey": "YAPI_LDAP_USERNAMEKEY"
}
}
我们会用mongodb,servername就是service name就叫mongodb
探针,这里使用http探针,5秒跑一次
建立service叫yapi
创建命名空间
kubectl create ns yapi
安装mongodb
把mongodb chart下载解压,找到values.yaml,打开,修改里面的rootPassword的值改为taihu123
另外把useStatefulSet设置成true,我们使用statefull
执行下面命令安装mongodb
helm repo add bitnami https://charts.bitnami.com/bitnami
helm install mongodb bitnami/mongodb -n yapi -f values.yaml
安装完毕之后进入容器,执行下面命令,新建普通账号,和数据库
mongo -u root -p taihu123
use yapidb
db.createUser({user: "yapiuser",pwd: "yapipassword",roles: [ { role: "dbOwner", db: "yapidb" } ]} )
安装yapi
kubectl apply -f yapi.yaml -n yapi
安装完毕之后,进入其中一个pod
执行下面命令
npm run install-server
初始化数据库
接下来就可以登录yapi了,账号是[email protected],密码是ymfe.org