在阿里云ack中部署Yapi

  1. 1. 构建Yapi镜像
  2. 2. 部署mongodb
    1. 2.1. 从helm仓库下载helm chart
    2. 2.2. 修改参数
    3. 2.3. 部署
    4. 2.4. 建立Yapi账号和数据库
  3. 3. 安装Yapi
    1. 3.1. kustomization.yaml
    2. 3.2. deployment.yaml
    3. 3.3. secret.yaml
    4. 3.4. service.yaml
    5. 3.5. ingress.yaml
    6. 3.6. 部署

昨天遇上的需求,需要在公网环境部署一个Yapi服务,用于接口测试。做个小记录。

参考了此处

构建Yapi镜像

搜索之后发现Yapi官方并没有提供镜像,于是参考上面的文档自己制作了一个镜像,过程参考上面的文档即可。

部署mongodb

这边为了方便,使用helm部署。

从helm仓库下载helm chart

1
helm pull --untar bitnami/mongodb

修改参数

这边修改了以下参数:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
persistence:
enabled: true
## A manually managed Persistent Volume and Claim
## Requires persistence.enabled: true
## If defined, PVC must be created manually before volume will be bound
## Ignored when mongodb.architecture=replicaset
##
# existingClaim:
## PV Storage Class
## If defined, storageClassName: <storageClass>
## If set to "-", storageClassName: "", which disables dynamic provisioning
## If undefined (the default) or set to null, no storageClassName spec is
## set, choosing the default provisioner.
##
storageClass: "alicloud-nas-subpath"
#使用阿里云提供的storageClass
## PV Access Mode
##
accessModes:
- ReadWriteOnce
## PVC size
##
size: 20Gi
#此处至少20Gi,由于阿里云的限制,如pvc过小会报错PVC defined storage should equal/greater than 20Gi
## PVC annotations
##
annotations:
volume.beta.kubernetes.io/storage-provisioner: nasplugin.csi.alibabacloud.com
# storageClass相关设置,没啥好说的
# 总共有两处storageClass需要修改,
1
2
rootPassword: "yapi-mongo"
#修改mongo的root密码

部署

1
2
kubectl create ns yapi # 创建单独的命名空间
helm install yapi-mongo mongodb -n yapi

建立Yapi账号和数据库

直接进pod执行,也没啥好说的

1
2
3
4
5
6
7
8
9
10
11
12
> use yapidb
switched to db yapidb
> db.createUser({user: "yapiuser",pwd: "yapipassword",roles: [ { role: "dbOwner", db: "yapidb" } ]} )
Successfully added user: {
"user" : "yapiuser",
"roles" : [
{
"role" : "dbOwner",
"db" : "yapidb"
}
]
}

安装Yapi

我们刚才已经制作了一个Yapi的镜像,为了方便使用,将其推送到公网的的自建harbor上。

使用kustomize来部署

1
2
3
4
5
6
7
tree yapi 
yapi
├── deployment.yaml
├── ingress.yaml
├── kustomization.yaml
├── secret.yaml
└── service.yaml

kustomization.yaml

1
2
3
4
5
6
7
8
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- deployment.yaml
- service.yaml
- secret.yaml
- ingress.yaml

deployment.yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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: #此处填写镜像地址
env:
- name: YAPI_DB_SERVERNAME
value: yapi-mongo-mongodb # 填自己的mongodbservice名称
- 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 #使用secret存储
- name: YAPI_DB_AUTHSOURCE
value: yapidb
imagePullPolicy: IfNotPresent
ports:
- containerPort: 3000
livenessProbe:
httpGet:
path: /
port: 3000
initialDelaySeconds: 5
periodSeconds: 5

secret.yaml

1
2
3
4
5
6
7
apiVersion: v1
kind: Secret
type: Opaque
metadata:
name: yapi-secret
stringData:
YAPI_DB_PASS: yapipassword #此处填写刚才建的mongo数据库用户的密码

service.yaml

1
2
3
4
5
6
7
8
9
10
11
apiVersion: v1
kind: Service
metadata:
name: yapi
spec:
selector:
app: yapi
ports:
- protocol: TCP
port: 3000
targetPort: 3000

ingress.yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: yapi-ingress
annotations:
kubernetes.io/ingress.class: traefik
traefik.ingress.kubernetes.io/router.entrypoints: web
#此处参考自己的ingress设置填写,这边使用了treafk作为ingress
#使用nginx请参考nginx的ingress文档
spec:
rules:
- host: #此处填写自己的域名
http:
paths:
- path: /
backend:
serviceName: yapi
servicePort: 3000

部署

1
kubectl apply -k yapi -n yapi