创建命名空间
kubectl create namespace kube-yapi
创建sc
# cat data-sc.yaml
---
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: mongodb-data
namespace: kube-yapi
provisioner: fuseim.pri/ifs
parameters:
archiveOnDelete: "false"
---
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: yapi-data
namespace: kube-yapi
provisioner: fuseim.pri/ifs
parameters:
archiveOnDelete: "false"
kubectl create -f data-sc.yaml
创建pvc
# cat data-pvc.yaml
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: mongodb-pvc
namespace: kube-yapi
annotations:
volume.beta.kubernetes.io/storage-class: "mongodb-data"
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 50Gi
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: yapi-pvc
namespace: kube-yapi
annotations:
volume.beta.kubernetes.io/storage-class: "yapi-data"
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 30Gi
kubectl create -f data-pvc.yaml
创建MongoDB
# cat mongodb.yaml
apiVersion: v1
kind: Service
metadata:
namespace: kube-yapi
name: mongo
labels:
app: mongo
spec:
ports:
- port: 27017
targetPort: 27017
name: mongo
selector:
app: mongo
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
namespace: kube-yapi
name: mongo
labels:
app: mongo
spec:
serviceName: mongo
replicas: 1
selector:
matchLabels:
app: mongo
template:
metadata:
labels:
app: mongo
spec:
imagePullSecrets:
- name: registry-pull-secret
containers:
- name: mongo
image: mongo:latest
imagePullPolicy: IfNotPresent
resources:
limits:
cpu: 500m
memory: 1024Mi
requests:
cpu: 200m
memory: 512Mi
env:
- name: TZ
value: Asia/Shanghai
ports:
- name: mongo
containerPort: 27017
protocol: TCP
volumeMounts:
- name: datadb
mountPath: /data/db
volumes:
- name: datadb
persistentVolumeClaim:
claimName: mongodb-pvc
kubectl create -f mongodb.yaml
创建用户
kubectl exec -it mongo-0 /bin/bash -n kube-yapi
use admin
db.createUser({user:"admin",pwd:"password",roles:["root"]})
use yapi
db.createUser({user: "yapi", pwd: "yapi123", roles: [{ role: "dbOwner", db: "yapi" }]})
MongoDB连接地址
mongo.kube-yapi.svc.cluster.local:27017
# cat Dockerfile
FROM node:latest
ADD yapi-1.9.2.tar.gz /tmp
RUN mkdir -p /yapi/vendors
WORKDIR /yapi/vendors
COPY entrypoint.sh /usr/bin
ENTRYPOINT ["/usr/bin/entrypoint.sh"]
# cat entrypoint.sh
#!/bin/sh
# yapi初始化后会有一个init.lock文件
lockPath="/yapi/vendors/init.lock"
# 如果初始化文件文件存在,则直接运行,否则初始化
cd /yapi/vendors
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
docker build -t registry.loan.com/library/yapi:1.9.2 .
docker push registry.loan.com/library/yapi:1.9.2
挂载配置
# cat config.json
{
"port": "3000",
"adminAccount": "admin@admin.com",
"timeout":120000,
"db": {
"servername": "mongo.kube-yapi.svc.cluster.local",
"DATABASE": "yapi",
"port": 27017,
"user": "yapi",
"pass": "yapi123",
"authSource": ""
},
"mail": {
"enable": true,
"host": "smtp.163.com",
"port": 465,
"from": "***@163.com",
"auth": {
"user": "***@163.com",
"pass": "*****"
}
}
}
kubectl create cm yapi-config -n kube-yapi --from-file=config.json
创建yapi
# cat yapi.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: yapi
namespace: kube-yapi
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.loan.com/library/yapi:1.9.2
imagePullPolicy: IfNotPresent
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-pvc
---
apiVersion: v1
kind: Service
metadata:
name: yapi-svc
namespace: kube-yapi
spec:
ports:
- name: yapi
port: 80
targetPort: 3000
protocol: TCP
selector:
app: yapi
release: yapi
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: yapi-ingress
namespace: kube-yapi
annotations:
kubernetes.io/ingress.class: "nginx"
spec:
rules:
- host: yapi.loan.com
http:
paths:
- path: /
backend:
serviceName: yapi-svc
servicePort: 80
kubectl create -f yapi.yaml