当前位置: 首页 > 工具软件 > YApi > 使用案例 >

k8s环境部署yapi以docker部署yapi

易英奕
2023-12-01

k8s环境部署yapi以docker部署yapi

使用Docker部署Yapi

https://www.jianshu.com/p/a97d2efb23c5

部署Yapi

运行mangodb

# 创建存储卷
docker volume create mongo-data

# 启动 MongoDB
docker run -d \
  --name mongo-yapi \
  -v mongo-data:/data/db \
  -e MONGO_INITDB_ROOT_USERNAME=anoyi \
  -e MONGO_INITDB_ROOT_PASSWORD=anoyi.com \
  mongo

获取Yapi镜像

docker pull registry.cn-hangzhou.aliyuncs.com/anoyi/yapi

自定义配置文件

{
  "port": "3000",
  "adminAccount": "admin@anoyi.com",
  "timeout":120000,
  "db": {
    "servername": "mongo",
    "DATABASE": "yapi",
    "port": 27017,
    "user": "anoyi",
    "pass": "anoyi.com",
    "authSource": "admin"
  }
}

初始化 YAPI 数据库索引及管理员账号

docker run -it --rm \
  --link mongo-yapi:mongo \
  --entrypoint npm \
  --workdir /yapi/vendors \
  -v $PWD/config.json:/yapi/config.json \
  registry.cn-hangzhou.aliyuncs.com/anoyi/yapi \
  run install-server

启动 Yapi 服务

docker run -d \
  --name yapi \
  --link mongo-yapi:mongo \
  --workdir /yapi/vendors \
  -p 3000:3000 \
  -v $PWD/config.json:/yapi/config.json \
  registry.cn-hangzhou.aliyuncs.com/anoyi/yapi \
  server/app.js

使用YAPI

访问页面

访问: http://localhost:3000
登录账号:admin@anoyi.com
密码:ymfe.org

手动构建YAPI的镜像

编辑docker file

FROM node:12-alpine as builder
WORKDIR /yapi
RUN apk add --no-cache wget python 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} vendors
RUN cd /yapi/vendors && cp config_example.json ../config.json && npm install --production --registry https://registry.npm.taobao.org

FROM node:12-alpine
MAINTAINER 545544032@qq.com
ENV TZ="Asia/Shanghai"
WORKDIR /yapi/vendors
COPY --from=builder /yapi/vendors /yapi/vendors
EXPOSE 3000
ENTRYPOINT ["node"]

注意将 Dockerfile 中的 VERSION 值修改为你需要构建的 YAPI 版本。

构建镜像

docker build -t yapi .

k8s环境部署yapi

yaml文件

存储根据自己需要自己配,这个配置是使用主机的文件路径,防止pod飘逸,固定pod跑在某个主机上

apiVersion: apps/v1
kind: Deployment
metadata:
  name: yapi
  namespace: devops-yapi
  labels:
    app: yapi
spec:
  replicas: 1
  selector:
    matchLabels:
      app: yapi
  template:
    metadata:
      labels:
        app: yapi
    spec:
      volumes:
        - name: config
          configMap:
            name: yapi-config
        - name: localtime
          hostPath:
            path: /etc/localtime
            type: ''
      initContainers:
        - name: init-data
          image: registry.cn-hangzhou.aliyuncs.com/anoyi/yapi:latest
          command:
          - /bin/sh
          - "-c"
          args:
          - "cp /yapi/config/config.json /yapi; node server/install.js"
          volumeMounts:
          - name: config
            mountPath: /yapi/config
          - name: localtime
            readOnly: true
            mountPath: /etc/localtime
      containers:
        - name: yapi
          image: registry.cn-hangzhou.aliyuncs.com/anoyi/yapi:latest
          ports:
            - name: yapiport
              containerPort: 3000
              protocol: TCP
          env:
            - name: MONGO_INITDB_ROOT_USERNAME
              value: yapi
            - name: MONGO_INITDB_ROOT_PASSWORD
              value: qingzhu
          command:
          - /bin/sh
          - "-c"
          args:
          - "cp /yapi/config/config.json /yapi; node server/app.js"
          resources:
            requests:
              cpu: 500m
              memory: 512Mi
          volumeMounts:
            - name: config
              mountPath: /yapi/config
            - name: localtime
              readOnly: true
              mountPath: /etc/localtime
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          imagePullPolicy: Always
      restartPolicy: Always
      terminationGracePeriodSeconds: 30
      dnsPolicy: ClusterFirst
      securityContext: {}
      schedulerName: default-scheduler
  strategy:
    type: Recreate
  revisionHistoryLimit: 10
  progressDeadlineSeconds: 600
---
apiVersion: v1
kind: Service
metadata:
  name: yapi-service
  namespace: devops-yapi
  labels:
    app: yapi-service
spec:
  ports:
    - name: tcp
      protocol: TCP
      port: 3000
      targetPort: 3000
  selector:
    app: yapi
  type: NodePort
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: yapi-config
  namespace: devops-yapi
data:
  config.json: |
    {
      "port": "3000",
      "adminAccount": "qingzhu@dtstack.com",
      "timeout": 120000,
      "db": {
        "servername": "yapi-mangodb-service",
        "DATABASE": "yapi",
        "port": 27017,
        "user": "yapi",
        "pass": "qingzhu",
        "authSource": "admin"
      }
    }
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: yapi-mangodb
  namespace: devops-yapi
  labels:
    app: yapi-mangodb
spec:
  replicas: 1
  selector:
    matchLabels:
      app: yapi-mangodb
  template:
    metadata:
      labels:
        app: yapi-mangodb
    spec:
      volumes:
        - name: data
          persistentVolumeClaim:
            claimName: yapi-mangodb-pvc
        - name: localtime
          hostPath:
            path: /etc/localtime
            type: ''
      containers:
        - name: mangodb
          image: mongo:latest
          ports:
            - name: mangodbport
              containerPort: 27017
              protocol: TCP
          env:
            - name: MONGO_INITDB_ROOT_USERNAME
              value: yapi
            - name: MONGO_INITDB_ROOT_PASSWORD
              value: qingzhu
          resources:
            requests:
              cpu: 500m
              memory: 512Mi
          volumeMounts:
            - name: data
              mountPath: /data/db
            - name: localtime
              readOnly: true
              mountPath: /etc/localtime
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          imagePullPolicy: Always
      restartPolicy: Always
      terminationGracePeriodSeconds: 30
      dnsPolicy: ClusterFirst
      securityContext: {}
      schedulerName: default-scheduler
  strategy:
    type: Recreate
  revisionHistoryLimit: 10
  progressDeadlineSeconds: 600
---
apiVersion: v1
kind: Service
metadata:
  name: yapi-mangodb-service
  namespace: devops-yapi
  labels:
    app: yapi-mangodb-service
spec:
  ports:
    - name: tcp
      protocol: TCP
      port: 27017
      targetPort: 27017
  selector:
    app: yapi-mangodb
  type: NodePort
  sessionAffinity: None
  externalTrafficPolicy: Cluster
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: yapi-mangodb-pvc
  namespace: devops-yapi
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 20Gi
  volumeName: yapi-mangodb-pv
  storageClassName: yapi-mangodb
  volumeMode: Filesystem
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: yapi-mangodb-pv
spec:
  capacity:
    storage: 20Gi
  local:
    path: /var/lib/kubelet/yapi-mangodb
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  storageClassName: yapi-mangodb
  volumeMode: Filesystem
  nodeAffinity:
    required:
      nodeSelectorTerms:
        - matchExpressions:
            - key: kubernetes.io/hostname
              operator: In
              values:
                - 172-16-22-72

---
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: yapi-mangodb
provisioner: kubernetes.io/no-provisioner
reclaimPolicy: Delete
 类似资料: